tweak scope matcher

This commit is contained in:
tobi 2025-02-25 15:04:55 +01:00
commit c8edd5f142

View file

@ -93,6 +93,14 @@ const (
// scope permits the wanted scope. // scope permits the wanted scope.
func (has Scope) Permits(wanted Scope) bool { func (has Scope) Permits(wanted Scope) bool {
switch { switch {
// Exact match.
case has == wanted:
return true
// Check if we have a parent scope
// of what's wanted, eg., we have
// "admin", we want "admin:read".
case has == ScopeRead: case has == ScopeRead:
return strings.HasPrefix(string(wanted), string(ScopeRead)) return strings.HasPrefix(string(wanted), string(ScopeRead))
case has == ScopeWrite: case has == ScopeWrite:
@ -103,7 +111,9 @@ func (has Scope) Permits(wanted Scope) bool {
return strings.HasPrefix(string(wanted), string(ScopeAdminRead)) return strings.HasPrefix(string(wanted), string(ScopeAdminRead))
case has == ScopeAdminWrite: case has == ScopeAdminWrite:
return strings.HasPrefix(string(wanted), string(ScopeAdminWrite)) return strings.HasPrefix(string(wanted), string(ScopeAdminWrite))
// No match.
default: default:
return has == wanted return false
} }
} }