From 513f4fefed89e12854f26d92b2dfb5de7d738670 Mon Sep 17 00:00:00 2001 From: tobi Date: Tue, 25 Feb 2025 15:08:03 +0100 Subject: [PATCH] simplify... --- internal/api/util/scopes.go | 28 ++++++---------------------- internal/api/util/scopes_test.go | 5 +++++ 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/internal/api/util/scopes.go b/internal/api/util/scopes.go index 189c4c6b4..d02d3cc0d 100644 --- a/internal/api/util/scopes.go +++ b/internal/api/util/scopes.go @@ -92,28 +92,12 @@ const ( // Permits returns true if the // scope permits the wanted scope. func (has Scope) Permits(wanted Scope) bool { - switch { - - // Exact match. - case has == wanted: + if has == wanted { + // Exact match. 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)) } diff --git a/internal/api/util/scopes_test.go b/internal/api/util/scopes_test.go index 7eed31af6..bd533585b 100644 --- a/internal/api/util/scopes_test.go +++ b/internal/api/util/scopes_test.go @@ -84,6 +84,11 @@ func TestScopes(t *testing.T) { WantsScope: util.ScopeWriteAccounts, Expect: false, }, + { + HasScope: util.ScopeWriteAccounts, + WantsScope: util.ScopeWrite, + Expect: false, + }, } { res := test.HasScope.Permits(test.WantsScope) if res != test.Expect {