Merge pull request #1611 from micro/auth-has-role

Auth account.HasRole
This commit is contained in:
ben-toogood 2020-05-11 11:40:20 +01:00 committed by GitHub
commit 22de001a80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 0 deletions

View File

@ -72,6 +72,21 @@ type Account struct {
Secret string `json:"secret"`
}
// HasRole returns a boolean indicating if the account has the given role
func (a *Account) HasRole(role string) bool {
if a.Roles == nil {
return false
}
for _, r := range a.Roles {
if r == role {
return true
}
}
return false
}
// Token can be short or long lived
type Token struct {
// The token to be used for accessing resources

17
auth/auth_test.go Normal file
View File

@ -0,0 +1,17 @@
package auth
import "testing"
func TestHasRole(t *testing.T) {
if new(Account).HasRole("foo") {
t.Errorf("Expected the blank account to not have a role")
}
acc := Account{Roles: []string{"foo"}}
if !acc.HasRole("foo") {
t.Errorf("Expected the account to have the foo role")
}
if acc.HasRole("bar") {
t.Errorf("Expected the account to not have the bar role")
}
}