simplify...

This commit is contained in:
tobi 2025-02-25 15:08:03 +01:00
commit 513f4fefed
2 changed files with 11 additions and 22 deletions

View file

@ -92,28 +92,12 @@ const (
// Permits returns true if the // Permits returns true if the
// 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 { if has == wanted {
// Exact match.
// Exact match.
case has == wanted:
return true return true
// Check if we have a parent scope
// of what's wanted, eg., we have
// "admin", we want "admin:read".
case has == ScopeRead:
return strings.HasPrefix(string(wanted), string(ScopeRead))
case has == ScopeWrite:
return strings.HasPrefix(string(wanted), string(ScopeWrite))
case has == ScopeAdmin:
return strings.HasPrefix(string(wanted), string(ScopeAdmin))
case has == ScopeAdminRead:
return strings.HasPrefix(string(wanted), string(ScopeAdminRead))
case has == ScopeAdminWrite:
return strings.HasPrefix(string(wanted), string(ScopeAdminWrite))
// No match.
default:
return false
} }
// Check if we have a parent scope of what's wanted,
// eg., we have scope "admin", we want "admin:read".
return strings.HasPrefix(string(wanted), string(has))
} }

View file

@ -84,6 +84,11 @@ func TestScopes(t *testing.T) {
WantsScope: util.ScopeWriteAccounts, WantsScope: util.ScopeWriteAccounts,
Expect: false, Expect: false,
}, },
{
HasScope: util.ScopeWriteAccounts,
WantsScope: util.ScopeWrite,
Expect: false,
},
} { } {
res := test.HasScope.Permits(test.WantsScope) res := test.HasScope.Permits(test.WantsScope)
if res != test.Expect { if res != test.Expect {