Skip to content

Commit

Permalink
added errors in rbac_api.go
Browse files Browse the repository at this point in the history
added vendor to .gitignore

error package

re-added spaces for clarity
  • Loading branch information
L04DB4L4NC3R committed Jun 15, 2019
1 parent ccd536f commit c64f6f9
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 27 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Expand Up @@ -24,4 +24,7 @@ _testmain.go
*.prof

.idea/
*.iml
*.iml

# vendor files
vendor
24 changes: 24 additions & 0 deletions errors/rbac_errors.go
@@ -0,0 +1,24 @@
// Copyright 2018 The casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package errors

import "errors"

// Global errors for rbac defined here
var (
ERR_NAME_NOT_FOUND = errors.New("error: name does not exist")
ERR_DOMAIN_PARAMETER = errors.New("error: domain should be 1 parameter")
ERR_NAMES12_NOT_FOUND = errors.New("error: name1 or name2 does not exist")
)
1 change: 0 additions & 1 deletion examples/rbac_policy.csv
Expand Up @@ -2,5 +2,4 @@ p, alice, data1, read
p, bob, data2, write
p, data2_admin, data2, read
p, data2_admin, data2, write

g, alice, data2_admin
1 change: 0 additions & 1 deletion examples/rbac_with_domains_policy.csv
Expand Up @@ -2,6 +2,5 @@ p, admin, domain1, data1, read
p, admin, domain1, data1, write
p, admin, domain2, data2, read
p, admin, domain2, data2, write

g, alice, admin, domain1
g, bob, admin, domain2
16 changes: 8 additions & 8 deletions rbac/default-role-manager/role_manager.go
Expand Up @@ -15,9 +15,9 @@
package defaultrolemanager

import (
"errors"
"sync"

"github.com/casbin/casbin/errors"
"github.com/casbin/casbin/log"
"github.com/casbin/casbin/rbac"
)
Expand Down Expand Up @@ -91,7 +91,7 @@ func (rm *RoleManager) AddLink(name1 string, name2 string, domain ...string) err
name1 = domain[0] + "::" + name1
name2 = domain[0] + "::" + name2
} else if len(domain) > 1 {
return errors.New("error: domain should be 1 parameter")
return errors.ERR_DOMAIN_PARAMETER
}

role1 := rm.createRole(name1)
Expand All @@ -108,11 +108,11 @@ func (rm *RoleManager) DeleteLink(name1 string, name2 string, domain ...string)
name1 = domain[0] + "::" + name1
name2 = domain[0] + "::" + name2
} else if len(domain) > 1 {
return errors.New("error: domain should be 1 parameter")
return errors.ERR_DOMAIN_PARAMETER
}

if !rm.hasRole(name1) || !rm.hasRole(name2) {
return errors.New("error: name1 or name2 does not exist")
return errors.ERR_NAMES12_NOT_FOUND
}

role1 := rm.createRole(name1)
Expand All @@ -128,7 +128,7 @@ func (rm *RoleManager) HasLink(name1 string, name2 string, domain ...string) (bo
name1 = domain[0] + "::" + name1
name2 = domain[0] + "::" + name2
} else if len(domain) > 1 {
return false, errors.New("error: domain should be 1 parameter")
return false, errors.ERR_DOMAIN_PARAMETER
}

if name1 == name2 {
Expand All @@ -149,7 +149,7 @@ func (rm *RoleManager) GetRoles(name string, domain ...string) ([]string, error)
if len(domain) == 1 {
name = domain[0] + "::" + name
} else if len(domain) > 1 {
return nil, errors.New("error: domain should be 1 parameter")
return nil, errors.ERR_DOMAIN_PARAMETER
}

if !rm.hasRole(name) {
Expand All @@ -171,11 +171,11 @@ func (rm *RoleManager) GetUsers(name string, domain ...string) ([]string, error)
if len(domain) == 1 {
name = domain[0] + "::" + name
} else if len(domain) > 1 {
return nil, errors.New("error: domain should be 1 parameter")
return nil, errors.ERR_DOMAIN_PARAMETER
}

if !rm.hasRole(name) {
return nil, errors.New("error: name does not exist")
return nil, errors.ERR_NAME_NOT_FOUND
}

names := []string{}
Expand Down
22 changes: 12 additions & 10 deletions rbac_api.go
Expand Up @@ -15,21 +15,23 @@
package casbin

// GetRolesForUser gets the roles that a user has.
func (e *Enforcer) GetRolesForUser(name string) []string {
res, _ := e.model["g"]["g"].RM.GetRoles(name)
return res
func (e *Enforcer) GetRolesForUser(name string) ([]string, error) {
res, err := e.model["g"]["g"].RM.GetRoles(name)
return res, err
}

// GetUsersForRole gets the users that has a role.
func (e *Enforcer) GetUsersForRole(name string) []string {
res, _ := e.model["g"]["g"].RM.GetUsers(name)
return res
func (e *Enforcer) GetUsersForRole(name string) ([]string, error) {
res, err := e.model["g"]["g"].RM.GetUsers(name)
return res, err
}

// HasRoleForUser determines whether a user has a role.
func (e *Enforcer) HasRoleForUser(name string, role string) bool {
roles := e.GetRolesForUser(name)

func (e *Enforcer) HasRoleForUser(name string, role string) (bool, error) {
roles, err := e.GetRolesForUser(name)
if err != nil {
return false, err
}
hasRole := false
for _, r := range roles {
if r == role {
Expand All @@ -38,7 +40,7 @@ func (e *Enforcer) HasRoleForUser(name string, role string) bool {
}
}

return hasRole
return hasRole, nil
}

// AddRoleForUser adds a role for a user.
Expand Down
6 changes: 3 additions & 3 deletions rbac_api_synced.go
Expand Up @@ -15,21 +15,21 @@
package casbin

// GetRolesForUser gets the roles that a user has.
func (e *SyncedEnforcer) GetRolesForUser(name string) []string {
func (e *SyncedEnforcer) GetRolesForUser(name string) ([]string, error) {
e.m.Lock()
defer e.m.Unlock()
return e.Enforcer.GetRolesForUser(name)
}

// GetUsersForRole gets the users that has a role.
func (e *SyncedEnforcer) GetUsersForRole(name string) []string {
func (e *SyncedEnforcer) GetUsersForRole(name string) ([]string, error) {
e.m.Lock()
defer e.m.Unlock()
return e.Enforcer.GetUsersForRole(name)
}

// HasRoleForUser determines whether a user has a role.
func (e *SyncedEnforcer) HasRoleForUser(name string, role string) bool {
func (e *SyncedEnforcer) HasRoleForUser(name string, role string) (bool, error) {
e.m.Lock()
defer e.m.Unlock()
return e.Enforcer.HasRoleForUser(name, role)
Expand Down
21 changes: 18 additions & 3 deletions rbac_api_test.go
Expand Up @@ -17,12 +17,16 @@ package casbin
import (
"testing"

"github.com/casbin/casbin/errors"
"github.com/casbin/casbin/util"
)

func testGetRoles(t *testing.T, e *Enforcer, name string, res []string) {
t.Helper()
myRes := e.GetRolesForUser(name)
myRes, err := e.GetRolesForUser(name)
if err != nil {
t.Error("Roles for ", name, " could not be fetched: ", err.Error())
}
t.Log("Roles for ", name, ": ", myRes)

if !util.SetEquals(res, myRes) {
Expand All @@ -32,7 +36,15 @@ func testGetRoles(t *testing.T, e *Enforcer, name string, res []string) {

func testGetUsers(t *testing.T, e *Enforcer, name string, res []string) {
t.Helper()
myRes := e.GetUsersForRole(name)
myRes, err := e.GetUsersForRole(name)
switch err {
case nil:
break
case errors.ERR_NAME_NOT_FOUND:
t.Log("No name found")
default:
t.Error("Users for ", name, " could not be fetched: ", err.Error())
}
t.Log("Users for ", name, ": ", myRes)

if !util.SetEquals(res, myRes) {
Expand All @@ -42,7 +54,10 @@ func testGetUsers(t *testing.T, e *Enforcer, name string, res []string) {

func testHasRole(t *testing.T, e *Enforcer, name string, role string, res bool) {
t.Helper()
myRes := e.HasRoleForUser(name, role)
myRes, err := e.HasRoleForUser(name, role)
if err != nil {
t.Error("HasRoleForUser returned an error: ", err.Error())
}
t.Log(name, " has role ", role, ": ", myRes)

if res != myRes {
Expand Down

0 comments on commit c64f6f9

Please sign in to comment.