mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-12-23 14:06:15 -06:00
simplify...
This commit is contained in:
parent
c8edd5f142
commit
513f4fefed
2 changed files with 11 additions and 22 deletions
|
|
@ -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))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue