Skip to content

Commit

Permalink
added ActionsStrSlice
Browse files Browse the repository at this point in the history
  • Loading branch information
Erkan Durmus committed Jul 10, 2019
1 parent 3b06283 commit 919d6fa
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
12 changes: 12 additions & 0 deletions permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rbac
import (
"encoding/json"
"fmt"
"sort"
"sync"
)

Expand Down Expand Up @@ -49,6 +50,17 @@ func (p *Permission) Actions() []Action {
return res
}

// ActionsStrSlice returns list of Actions as sorted string slice
func (p *Permission) ActionsStrSlice() []string {
res := []string{}
p.Range(func(k, v interface{}) bool {
res = append(res, string(k.(Action)))
return true
})
sort.Strings(res)
return res
}

// MarshalJSON serializes a Permission to JSON
func (p *Permission) MarshalJSON() ([]byte, error) {
return json.Marshal(jsPermission{
Expand Down
10 changes: 10 additions & 0 deletions permission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package rbac

import (
"encoding/json"
"reflect"
"sort"
"testing"
)

Expand All @@ -28,4 +30,12 @@ func TestPermission(t *testing.T) {
t.Fatalf("test permission actions are not valid, expected %d items, got %d items", len(crudActions), len(testPerm.Actions()))
}

strActions := []string{}
for _, a := range crudActions {
strActions = append(strActions, string(a))
}
sort.Strings(strActions)
if !reflect.DeepEqual(testPerm.ActionsStrSlice(), strActions) {
t.Fatalf("test permission actions are not valid, expected %d items, got %d items", testPerm.ActionsStrSlice(), strActions)
}
}
8 changes: 4 additions & 4 deletions rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (r *RBAC) RemoveRole(roleID string) error {
return nil
}

// Roles reuturns all registered roles
// Roles returns all registered roles
func (r *RBAC) Roles() (res []*Role) {
r.Range(func(k, v interface{}) bool {
res = append(res, v.(*Role))
Expand Down Expand Up @@ -195,7 +195,7 @@ func (r *RBAC) IsGranted(roleID string, perm *Permission, actions ...Action) boo
return r.IsGrantedStr(roleID, perm.ID, actions...)
}

// IsGrantedStr checks if permID is greanted with target actions for role
// IsGrantedStr checks if permID is granted with target actions for role
func (r *RBAC) IsGrantedStr(roleID string, permID string, actions ...Action) bool {
if role, ok := r.Load(roleID); ok {
validActions := []Action{}
Expand All @@ -214,7 +214,7 @@ func (r *RBAC) IsGrantedStr(roleID string, permID string, actions ...Action) boo
return false
}

// IsGranted checks if a role with target permission and actions has a grant
// IsGrantInherited checks if a role with target permission and actions has a grant
func (r *RBAC) IsGrantInherited(roleID string, perm *Permission, actions ...Action) bool {
if perm == nil {
log.Errorf("Nil perm is sent for granted check for role %s", roleID)
Expand Down Expand Up @@ -333,7 +333,7 @@ func (r *RBAC) AllGrantInherited(roleIDs []string, perm *Permission, action ...A
return r.AllGrantInheritedStr(roleIDs, perm.ID, action...)
}

// AllGrantedStr checks if all roles have the permission.
// AllGrantInheritedStr checks if all roles have the permission.
func (r *RBAC) AllGrantInheritedStr(roleIDs []string, permName string, action ...Action) bool {
for _, roleID := range roleIDs {
if !r.IsGrantInheritedStr(roleID, permName, action...) {
Expand Down

0 comments on commit 919d6fa

Please sign in to comment.