Skip to content

Commit

Permalink
feat: return error in getUserPermissions() related APIs (#1358)
Browse files Browse the repository at this point in the history
  • Loading branch information
dacongda committed Feb 7, 2024
1 parent c66aada commit 5acc404
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion enforcer_interface.go
Expand Up @@ -69,7 +69,7 @@ type IEnforcer interface {
AddPermissionsForUser(user string, permissions ...[]string) (bool, error)
DeletePermissionForUser(user string, permission ...string) (bool, error)
DeletePermissionsForUser(user string) (bool, error)
GetPermissionsForUser(user string, domain ...string) [][]string
GetPermissionsForUser(user string, domain ...string) ([][]string, error)
HasPermissionForUser(user string, permission ...string) bool
GetImplicitRolesForUser(name string, domain ...string) ([]string, error)
GetImplicitPermissionsForUser(user string, domain ...string) ([][]string, error)
Expand Down
8 changes: 4 additions & 4 deletions rbac_api.go
Expand Up @@ -166,12 +166,12 @@ func (e *Enforcer) DeletePermissionsForUser(user string) (bool, error) {
}

// GetPermissionsForUser gets permissions for a user or role.
func (e *Enforcer) GetPermissionsForUser(user string, domain ...string) [][]string {
func (e *Enforcer) GetPermissionsForUser(user string, domain ...string) ([][]string, error) {
return e.GetNamedPermissionsForUser("p", user, domain...)
}

// GetNamedPermissionsForUser gets permissions for a user or role by named policy.
func (e *Enforcer) GetNamedPermissionsForUser(ptype string, user string, domain ...string) [][]string {
func (e *Enforcer) GetNamedPermissionsForUser(ptype string, user string, domain ...string) ([][]string, error) {
permission := make([][]string, 0)
for pType, assertion := range e.model["p"] {
if pType != ptype {
Expand All @@ -187,14 +187,14 @@ func (e *Enforcer) GetNamedPermissionsForUser(ptype string, user string, domain
if len(domain) > 0 {
index, err := e.GetFieldIndex(ptype, constant.DomainIndex)
if err != nil {
return permission
return permission, err
}
args[index] = domain[0]
}
perm := e.GetFilteredNamedPolicy(ptype, 0, args...)
permission = append(permission, perm...)
}
return permission
return permission, nil
}

// HasPermissionForUser determines whether a user has a permission.
Expand Down
4 changes: 2 additions & 2 deletions rbac_api_synced.go
Expand Up @@ -124,14 +124,14 @@ func (e *SyncedEnforcer) DeletePermissionsForUser(user string) (bool, error) {
}

// GetPermissionsForUser gets permissions for a user or role.
func (e *SyncedEnforcer) GetPermissionsForUser(user string, domain ...string) [][]string {
func (e *SyncedEnforcer) GetPermissionsForUser(user string, domain ...string) ([][]string, error) {
e.m.RLock()
defer e.m.RUnlock()
return e.Enforcer.GetPermissionsForUser(user, domain...)
}

// GetNamedPermissionsForUser gets permissions for a user or role by named policy.
func (e *SyncedEnforcer) GetNamedPermissionsForUser(ptype string, user string, domain ...string) [][]string {
func (e *SyncedEnforcer) GetNamedPermissionsForUser(ptype string, user string, domain ...string) ([][]string, error) {
e.m.RLock()
defer e.m.RUnlock()
return e.Enforcer.GetNamedPermissionsForUser(ptype, user, domain...)
Expand Down
10 changes: 8 additions & 2 deletions rbac_api_test.go
Expand Up @@ -192,7 +192,10 @@ func TestEnforcer_AddRolesForUser(t *testing.T) {

func testGetPermissions(t *testing.T, e *Enforcer, name string, res [][]string, domain ...string) {
t.Helper()
myRes := e.GetPermissionsForUser(name, domain...)
myRes, err := e.GetPermissionsForUser(name, domain...)
if err != nil {
t.Error(err.Error())
}
t.Log("Permissions for ", name, ": ", myRes)

if !util.Array2DEquals(res, myRes) {
Expand All @@ -212,7 +215,10 @@ func testHasPermission(t *testing.T, e *Enforcer, name string, permission []stri

func testGetNamedPermissionsForUser(t *testing.T, e *Enforcer, ptype string, name string, res [][]string, domain ...string) {
t.Helper()
myRes := e.GetNamedPermissionsForUser(ptype, name, domain...)
myRes, err := e.GetNamedPermissionsForUser(ptype, name, domain...)
if err != nil {
t.Error(err.Error())
}
t.Log("Named permissions for ", name, ": ", myRes)

if !util.Array2DEquals(res, myRes) {
Expand Down

1 comment on commit 5acc404

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 5acc404 Previous: c66aada Ratio
BenchmarkCachedRaw 17.63 ns/op 0 B/op 0 allocs/op
BenchmarkCachedRaw - ns/op 17.63 ns/op 17.57 ns/op 1.00
BenchmarkCachedRaw - B/op 0 B/op 0 B/op 1
BenchmarkCachedRaw - allocs/op 0 allocs/op 0 allocs/op 1
BenchmarkCachedBasicModel 167 ns/op 104 B/op 4 allocs/op
BenchmarkCachedBasicModel - ns/op 167 ns/op 160.8 ns/op 1.04
BenchmarkCachedBasicModel - B/op 104 B/op 104 B/op 1
BenchmarkCachedBasicModel - allocs/op 4 allocs/op 4 allocs/op 1
BenchmarkCachedRBACModel 164.8 ns/op 104 B/op 4 allocs/op
BenchmarkCachedRBACModel - ns/op 164.8 ns/op 157.9 ns/op 1.04
BenchmarkCachedRBACModel - B/op 104 B/op 104 B/op 1
BenchmarkCachedRBACModel - allocs/op 4 allocs/op 4 allocs/op 1
BenchmarkCachedRBACModelSmall 174.1 ns/op 104 B/op 4 allocs/op
BenchmarkCachedRBACModelSmall - ns/op 174.1 ns/op 177.8 ns/op 0.98
BenchmarkCachedRBACModelSmall - B/op 104 B/op 104 B/op 1
BenchmarkCachedRBACModelSmall - allocs/op 4 allocs/op 4 allocs/op 1
BenchmarkCachedRBACModelMedium 176.3 ns/op 104 B/op 4 allocs/op
BenchmarkCachedRBACModelMedium - ns/op 176.3 ns/op 175.5 ns/op 1.00
BenchmarkCachedRBACModelMedium - B/op 104 B/op 104 B/op 1
BenchmarkCachedRBACModelMedium - allocs/op 4 allocs/op 4 allocs/op 1
BenchmarkCachedRBACModelLarge 164.5 ns/op 96 B/op 3 allocs/op
BenchmarkCachedRBACModelLarge - ns/op 164.5 ns/op 156.4 ns/op 1.05
BenchmarkCachedRBACModelLarge - B/op 96 B/op 96 B/op 1
BenchmarkCachedRBACModelLarge - allocs/op 3 allocs/op 3 allocs/op 1
BenchmarkCachedRBACModelWithResourceRoles 166.4 ns/op 104 B/op 4 allocs/op
BenchmarkCachedRBACModelWithResourceRoles - ns/op 166.4 ns/op 163.7 ns/op 1.02
BenchmarkCachedRBACModelWithResourceRoles - B/op 104 B/op 104 B/op 1
BenchmarkCachedRBACModelWithResourceRoles - allocs/op 4 allocs/op 4 allocs/op 1
BenchmarkCachedRBACModelWithDomains 173.6 ns/op 120 B/op 4 allocs/op
BenchmarkCachedRBACModelWithDomains - ns/op 173.6 ns/op 167.2 ns/op 1.04
BenchmarkCachedRBACModelWithDomains - B/op 120 B/op 120 B/op 1
BenchmarkCachedRBACModelWithDomains - allocs/op 4 allocs/op 4 allocs/op 1
BenchmarkCachedABACModel 2871 ns/op 1538 B/op 18 allocs/op
BenchmarkCachedABACModel - ns/op 2871 ns/op 2836 ns/op 1.01
BenchmarkCachedABACModel - B/op 1538 B/op 1535 B/op 1.00
BenchmarkCachedABACModel - allocs/op 18 allocs/op 18 allocs/op 1
BenchmarkCachedKeyMatchModel 180.4 ns/op 152 B/op 4 allocs/op
BenchmarkCachedKeyMatchModel - ns/op 180.4 ns/op 170.9 ns/op 1.06
BenchmarkCachedKeyMatchModel - B/op 152 B/op 152 B/op 1
BenchmarkCachedKeyMatchModel - allocs/op 4 allocs/op 4 allocs/op 1
BenchmarkCachedRBACModelWithDeny 164.6 ns/op 104 B/op 4 allocs/op
BenchmarkCachedRBACModelWithDeny - ns/op 164.6 ns/op 158.8 ns/op 1.04
BenchmarkCachedRBACModelWithDeny - B/op 104 B/op 104 B/op 1
BenchmarkCachedRBACModelWithDeny - allocs/op 4 allocs/op 4 allocs/op 1
BenchmarkCachedPriorityModel 170.5 ns/op 104 B/op 4 allocs/op
BenchmarkCachedPriorityModel - ns/op 170.5 ns/op 158.1 ns/op 1.08
BenchmarkCachedPriorityModel - B/op 104 B/op 104 B/op 1
BenchmarkCachedPriorityModel - allocs/op 4 allocs/op 4 allocs/op 1
BenchmarkCachedWithEnforceContext 295.5 ns/op 240 B/op 5 allocs/op
BenchmarkCachedWithEnforceContext - ns/op 295.5 ns/op 288.9 ns/op 1.02
BenchmarkCachedWithEnforceContext - B/op 240 B/op 240 B/op 1
BenchmarkCachedWithEnforceContext - allocs/op 5 allocs/op 5 allocs/op 1
BenchmarkCachedRBACModelMediumParallel 168.1 ns/op 106 B/op 4 allocs/op
BenchmarkCachedRBACModelMediumParallel - ns/op 168.1 ns/op 173.6 ns/op 0.97
BenchmarkCachedRBACModelMediumParallel - B/op 106 B/op 106 B/op 1
BenchmarkCachedRBACModelMediumParallel - allocs/op 4 allocs/op 4 allocs/op 1
BenchmarkHasPolicySmall 462.4 ns/op 150 B/op 6 allocs/op
BenchmarkHasPolicySmall - ns/op 462.4 ns/op 437.7 ns/op 1.06
BenchmarkHasPolicySmall - B/op 150 B/op 150 B/op 1
BenchmarkHasPolicySmall - allocs/op 6 allocs/op 6 allocs/op 1
BenchmarkHasPolicyMedium 486.9 ns/op 157 B/op 6 allocs/op
BenchmarkHasPolicyMedium - ns/op 486.9 ns/op 472.9 ns/op 1.03
BenchmarkHasPolicyMedium - B/op 157 B/op 157 B/op 1
BenchmarkHasPolicyMedium - allocs/op 6 allocs/op 6 allocs/op 1
BenchmarkHasPolicyLarge 522.1 ns/op 165 B/op 7 allocs/op
BenchmarkHasPolicyLarge - ns/op 522.1 ns/op 518 ns/op 1.01
BenchmarkHasPolicyLarge - B/op 165 B/op 165 B/op 1
BenchmarkHasPolicyLarge - allocs/op 7 allocs/op 7 allocs/op 1
BenchmarkAddPolicySmall 492.3 ns/op 152 B/op 6 allocs/op
BenchmarkAddPolicySmall - ns/op 492.3 ns/op 470 ns/op 1.05
BenchmarkAddPolicySmall - B/op 152 B/op 152 B/op 1
BenchmarkAddPolicySmall - allocs/op 6 allocs/op 6 allocs/op 1
BenchmarkAddPolicyMedium 593.4 ns/op 172 B/op 7 allocs/op
BenchmarkAddPolicyMedium - ns/op 593.4 ns/op 552 ns/op 1.07
BenchmarkAddPolicyMedium - B/op 172 B/op 172 B/op 1
BenchmarkAddPolicyMedium - allocs/op 7 allocs/op 7 allocs/op 1
BenchmarkAddPolicyLarge 1198 ns/op 473 B/op 9 allocs/op
BenchmarkAddPolicyLarge - ns/op 1198 ns/op 1153 ns/op 1.04
BenchmarkAddPolicyLarge - B/op 473 B/op 473 B/op 1
BenchmarkAddPolicyLarge - allocs/op 9 allocs/op 9 allocs/op 1
BenchmarkRemovePolicySmall 461.1 ns/op 166 B/op 7 allocs/op
BenchmarkRemovePolicySmall - ns/op 461.1 ns/op 483.8 ns/op 0.95
BenchmarkRemovePolicySmall - B/op 166 B/op 166 B/op 1
BenchmarkRemovePolicySmall - allocs/op 7 allocs/op 7 allocs/op 1
BenchmarkRemovePolicyMedium 539.9 ns/op 176 B/op 7 allocs/op
BenchmarkRemovePolicyMedium - ns/op 539.9 ns/op 547.2 ns/op 0.99
BenchmarkRemovePolicyMedium - B/op 176 B/op 176 B/op 1
BenchmarkRemovePolicyMedium - allocs/op 7 allocs/op 7 allocs/op 1
BenchmarkRemovePolicyLarge 1206 ns/op 286 B/op 13 allocs/op
BenchmarkRemovePolicyLarge - ns/op 1206 ns/op 1212 ns/op 1.00
BenchmarkRemovePolicyLarge - B/op 286 B/op 292 B/op 0.98
BenchmarkRemovePolicyLarge - allocs/op 13 allocs/op 13 allocs/op 1
BenchmarkRaw 17.58 ns/op 0 B/op 0 allocs/op
BenchmarkRaw - ns/op 17.58 ns/op 17.53 ns/op 1.00
BenchmarkRaw - B/op 0 B/op 0 B/op 1
BenchmarkRaw - allocs/op 0 allocs/op 0 allocs/op 1
BenchmarkBasicModel 3647 ns/op 1508 B/op 17 allocs/op
BenchmarkBasicModel - ns/op 3647 ns/op 3742 ns/op 0.97
BenchmarkBasicModel - B/op 1508 B/op 1504 B/op 1.00
BenchmarkBasicModel - allocs/op 17 allocs/op 17 allocs/op 1
BenchmarkRBACModel 5423 ns/op 2061 B/op 35 allocs/op
BenchmarkRBACModel - ns/op 5423 ns/op 5386 ns/op 1.01
BenchmarkRBACModel - B/op 2061 B/op 2057 B/op 1.00
BenchmarkRBACModel - allocs/op 35 allocs/op 35 allocs/op 1
BenchmarkRBACModelSizes/small 48230 ns/op 20261 B/op 480 allocs/op
BenchmarkRBACModelSizes/small - ns/op 48230 ns/op 48505 ns/op 0.99
BenchmarkRBACModelSizes/small - B/op 20261 B/op 20297 B/op 1.00
BenchmarkRBACModelSizes/small - allocs/op 480 allocs/op 480 allocs/op 1
BenchmarkRBACModelSizes/medium 516998 ns/op 191748 B/op 4827 allocs/op
BenchmarkRBACModelSizes/medium - ns/op 516998 ns/op 501473 ns/op 1.03
BenchmarkRBACModelSizes/medium - B/op 191748 B/op 191420 B/op 1.00
BenchmarkRBACModelSizes/medium - allocs/op 4827 allocs/op 4830 allocs/op 1.00
BenchmarkRBACModelSizes/large 5336522 ns/op 1906072 B/op 48353 allocs/op
BenchmarkRBACModelSizes/large - ns/op 5336522 ns/op 5190166 ns/op 1.03
BenchmarkRBACModelSizes/large - B/op 1906072 B/op 1901599 B/op 1.00
BenchmarkRBACModelSizes/large - allocs/op 48353 allocs/op 48224 allocs/op 1.00
BenchmarkRBACModelSmall 56481 ns/op 20362 B/op 615 allocs/op
BenchmarkRBACModelSmall - ns/op 56481 ns/op 58162 ns/op 0.97
BenchmarkRBACModelSmall - B/op 20362 B/op 20366 B/op 1.00
BenchmarkRBACModelSmall - allocs/op 615 allocs/op 615 allocs/op 1
BenchmarkRBACModelMedium 565580 ns/op 194598 B/op 6020 allocs/op
BenchmarkRBACModelMedium - ns/op 565580 ns/op 582623 ns/op 0.97
BenchmarkRBACModelMedium - B/op 194598 B/op 194743 B/op 1.00
BenchmarkRBACModelMedium - allocs/op 6020 allocs/op 6021 allocs/op 1.00
BenchmarkRBACModelLarge 5991834 ns/op 1940335 B/op 60599 allocs/op
BenchmarkRBACModelLarge - ns/op 5991834 ns/op 6170059 ns/op 0.97
BenchmarkRBACModelLarge - B/op 1940335 B/op 1941830 B/op 1.00
BenchmarkRBACModelLarge - allocs/op 60599 allocs/op 60649 allocs/op 1.00
BenchmarkRBACModelWithResourceRoles 4539 ns/op 1843 B/op 27 allocs/op
BenchmarkRBACModelWithResourceRoles - ns/op 4539 ns/op 4531 ns/op 1.00
BenchmarkRBACModelWithResourceRoles - B/op 1843 B/op 1840 B/op 1.00
BenchmarkRBACModelWithResourceRoles - allocs/op 27 allocs/op 27 allocs/op 1
BenchmarkRBACModelWithDomains 5093 ns/op 1827 B/op 25 allocs/op
BenchmarkRBACModelWithDomains - ns/op 5093 ns/op 5083 ns/op 1.00
BenchmarkRBACModelWithDomains - B/op 1827 B/op 1817 B/op 1.01
BenchmarkRBACModelWithDomains - allocs/op 25 allocs/op 25 allocs/op 1
BenchmarkABACModel 2893 ns/op 1534 B/op 17 allocs/op
BenchmarkABACModel - ns/op 2893 ns/op 2772 ns/op 1.04
BenchmarkABACModel - B/op 1534 B/op 1529 B/op 1.00
BenchmarkABACModel - allocs/op 17 allocs/op 17 allocs/op 1
BenchmarkABACRuleModel 4034367 ns/op 1326375 B/op 40092 allocs/op
BenchmarkABACRuleModel - ns/op 4034367 ns/op 3915413 ns/op 1.03
BenchmarkABACRuleModel - B/op 1326375 B/op 1324462 B/op 1.00
BenchmarkABACRuleModel - allocs/op 40092 allocs/op 40091 allocs/op 1.00
BenchmarkKeyMatchModel 6192 ns/op 3058 B/op 37 allocs/op
BenchmarkKeyMatchModel - ns/op 6192 ns/op 6076 ns/op 1.02
BenchmarkKeyMatchModel - B/op 3058 B/op 3055 B/op 1.00
BenchmarkKeyMatchModel - allocs/op 37 allocs/op 37 allocs/op 1
BenchmarkRBACModelWithDeny 6880 ns/op 2476 B/op 49 allocs/op
BenchmarkRBACModelWithDeny - ns/op 6880 ns/op 6911 ns/op 1.00
BenchmarkRBACModelWithDeny - B/op 2476 B/op 2469 B/op 1.00
BenchmarkRBACModelWithDeny - allocs/op 49 allocs/op 49 allocs/op 1
BenchmarkPriorityModel 4246 ns/op 1761 B/op 22 allocs/op
BenchmarkPriorityModel - ns/op 4246 ns/op 4208 ns/op 1.01
BenchmarkPriorityModel - B/op 1761 B/op 1757 B/op 1.00
BenchmarkPriorityModel - allocs/op 22 allocs/op 22 allocs/op 1
BenchmarkRBACModelWithDomainPatternLarge 24387 ns/op 16745 B/op 164 allocs/op
BenchmarkRBACModelWithDomainPatternLarge - ns/op 24387 ns/op 23776 ns/op 1.03
BenchmarkRBACModelWithDomainPatternLarge - B/op 16745 B/op 16737 B/op 1.00
BenchmarkRBACModelWithDomainPatternLarge - allocs/op 164 allocs/op 164 allocs/op 1
BenchmarkRoleManagerSmall 70383 ns/op 11955 B/op 797 allocs/op
BenchmarkRoleManagerSmall - ns/op 70383 ns/op 70117 ns/op 1.00
BenchmarkRoleManagerSmall - B/op 11955 B/op 11955 B/op 1
BenchmarkRoleManagerSmall - allocs/op 797 allocs/op 797 allocs/op 1
BenchmarkRoleManagerMedium 748653 ns/op 125915 B/op 8741 allocs/op
BenchmarkRoleManagerMedium - ns/op 748653 ns/op 739209 ns/op 1.01
BenchmarkRoleManagerMedium - B/op 125915 B/op 125914 B/op 1.00
BenchmarkRoleManagerMedium - allocs/op 8741 allocs/op 8741 allocs/op 1
BenchmarkRoleManagerLarge 8191233 ns/op 1349922 B/op 89741 allocs/op
BenchmarkRoleManagerLarge - ns/op 8191233 ns/op 8325910 ns/op 0.98
BenchmarkRoleManagerLarge - B/op 1349922 B/op 1349922 B/op 1
BenchmarkRoleManagerLarge - allocs/op 89741 allocs/op 89741 allocs/op 1
BenchmarkBuildRoleLinksWithPatternLarge 6188018487 ns/op 5346308392 B/op 60950166 allocs/op
BenchmarkBuildRoleLinksWithPatternLarge - ns/op 6188018487 ns/op 6208020408 ns/op 1.00
BenchmarkBuildRoleLinksWithPatternLarge - B/op 5346308392 B/op 5338626584 B/op 1.00
BenchmarkBuildRoleLinksWithPatternLarge - allocs/op 60950166 allocs/op 60948713 allocs/op 1.00
BenchmarkBuildRoleLinksWithDomainPatternLarge 167324166 ns/op 141336646 B/op 1676529 allocs/op
BenchmarkBuildRoleLinksWithDomainPatternLarge - ns/op 167324166 ns/op 173437397 ns/op 0.96
BenchmarkBuildRoleLinksWithDomainPatternLarge - B/op 141336646 B/op 141442892 B/op 1.00
BenchmarkBuildRoleLinksWithDomainPatternLarge - allocs/op 1676529 allocs/op 1676552 allocs/op 1.00
BenchmarkBuildRoleLinksWithPatternAndDomainPatternLarge 6322665690 ns/op 5485058616 B/op 62560942 allocs/op
BenchmarkBuildRoleLinksWithPatternAndDomainPatternLarge - ns/op 6322665690 ns/op 6578074404 ns/op 0.96
BenchmarkBuildRoleLinksWithPatternAndDomainPatternLarge - B/op 5485058616 B/op 5488807368 B/op 1.00
BenchmarkBuildRoleLinksWithPatternAndDomainPatternLarge - allocs/op 62560942 allocs/op 62562036 allocs/op 1.00
BenchmarkHasLinkWithPatternLarge 10698 ns/op 7609 B/op 111 allocs/op
BenchmarkHasLinkWithPatternLarge - ns/op 10698 ns/op 10664 ns/op 1.00
BenchmarkHasLinkWithPatternLarge - B/op 7609 B/op 7611 B/op 1.00
BenchmarkHasLinkWithPatternLarge - allocs/op 111 allocs/op 111 allocs/op 1
BenchmarkHasLinkWithDomainPatternLarge 497.3 ns/op 80 B/op 5 allocs/op
BenchmarkHasLinkWithDomainPatternLarge - ns/op 497.3 ns/op 496.2 ns/op 1.00
BenchmarkHasLinkWithDomainPatternLarge - B/op 80 B/op 80 B/op 1
BenchmarkHasLinkWithDomainPatternLarge - allocs/op 5 allocs/op 5 allocs/op 1
BenchmarkHasLinkWithPatternAndDomainPatternLarge 10858 ns/op 7607 B/op 111 allocs/op
BenchmarkHasLinkWithPatternAndDomainPatternLarge - ns/op 10858 ns/op 10684 ns/op 1.02
BenchmarkHasLinkWithPatternAndDomainPatternLarge - B/op 7607 B/op 7615 B/op 1.00
BenchmarkHasLinkWithPatternAndDomainPatternLarge - allocs/op 111 allocs/op 111 allocs/op 1

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.