Skip to content

Commit

Permalink
Fix "All" roles bug in permission edit page
Browse files Browse the repository at this point in the history
  • Loading branch information
hsluoyz committed Dec 2, 2023
1 parent 113c27d commit 947dcf6
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 17 deletions.
24 changes: 21 additions & 3 deletions controllers/casbin_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,13 @@ func (c *ApiController) GetAllObjects() {
return
}

c.ResponseOk(object.GetAllObjects(userId))
objects, err := object.GetAllObjects(userId)
if err != nil {
c.ResponseError(err.Error())
return
}

c.ResponseOk(objects)
}

func (c *ApiController) GetAllActions() {
Expand All @@ -253,7 +259,13 @@ func (c *ApiController) GetAllActions() {
return
}

c.ResponseOk(object.GetAllActions(userId))
actions, err := object.GetAllActions(userId)
if err != nil {
c.ResponseError(err.Error())
return
}

c.ResponseOk(actions)
}

func (c *ApiController) GetAllRoles() {
Expand All @@ -263,5 +275,11 @@ func (c *ApiController) GetAllRoles() {
return
}

c.ResponseOk(object.GetAllRoles(userId))
roles, err := object.GetAllRoles(userId)
if err != nil {
c.ResponseError(err.Error())
return
}

c.ResponseOk(roles)
}
6 changes: 5 additions & 1 deletion object/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ func checkPermissionValid(permission *Permission) error {
return nil
}

groupingPolicies := getGroupingPolicies(permission)
groupingPolicies, err := getGroupingPolicies(permission)
if err != nil {
return err
}

if len(groupingPolicies) > 0 {
_, err = enforcer.AddGroupingPolicies(groupingPolicies)
if err != nil {
Expand Down
50 changes: 39 additions & 11 deletions object/permission_enforcer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/casbin/casbin/v2/log"
"github.com/casbin/casbin/v2/model"
"github.com/casdoor/casdoor/conf"
"github.com/casdoor/casdoor/util"
xormadapter "github.com/casdoor/xorm-adapter/v3"
)

Expand Down Expand Up @@ -137,6 +138,16 @@ func getPolicies(permission *Permission) [][]string {
}

func getRolesInRole(roleId string, visited map[string]struct{}) ([]*Role, error) {
roleOwner, roleName := util.GetOwnerAndNameFromId(roleId)
if roleName == "*" {
roles, err := GetRoles(roleOwner)
if err != nil {
return []*Role{}, err
}

return roles, nil
}

role, err := GetRole(roleId)
if err != nil {
return []*Role{}, err
Expand All @@ -162,20 +173,26 @@ func getRolesInRole(roleId string, visited map[string]struct{}) ([]*Role, error)
return roles, nil
}

func getGroupingPolicies(permission *Permission) [][]string {
func getGroupingPolicies(permission *Permission) ([][]string, error) {
var groupingPolicies [][]string

domainExist := len(permission.Domains) > 0
permissionId := permission.GetId()

for _, roleId := range permission.Roles {
visited := map[string]struct{}{}

if roleId == "*" {
roleId = util.GetId(permission.Owner, "*")
}

rolesInRole, err := getRolesInRole(roleId, visited)
if err != nil {
panic(err)
return nil, err
}

for _, role := range rolesInRole {
roleId := role.GetId()
roleId = role.GetId()
for _, subUser := range role.Users {
if domainExist {
for _, domain := range permission.Domains {
Expand All @@ -198,7 +215,7 @@ func getGroupingPolicies(permission *Permission) [][]string {
}
}

return groupingPolicies
return groupingPolicies, nil
}

func addPolicies(permission *Permission) error {
Expand Down Expand Up @@ -231,7 +248,10 @@ func addGroupingPolicies(permission *Permission) error {
return err
}

groupingPolicies := getGroupingPolicies(permission)
groupingPolicies, err := getGroupingPolicies(permission)
if err != nil {
return err
}

if len(groupingPolicies) > 0 {
_, err = enforcer.AddGroupingPolicies(groupingPolicies)
Expand All @@ -249,7 +269,10 @@ func removeGroupingPolicies(permission *Permission) error {
return err
}

groupingPolicies := getGroupingPolicies(permission)
groupingPolicies, err := getGroupingPolicies(permission)
if err != nil {
return err
}

if len(groupingPolicies) > 0 {
_, err = enforcer.RemoveGroupingPolicies(groupingPolicies)
Expand Down Expand Up @@ -287,7 +310,12 @@ func getAllValues(userId string, fn func(enforcer *casbin.Enforcer) []string) ([
return nil, err
}

for _, role := range GetAllRoles(userId) {
allRoles, err := GetAllRoles(userId)
if err != nil {
return nil, err
}

for _, role := range allRoles {
permissionsByRole, err := GetPermissionsByRole(role)
if err != nil {
return nil, err
Expand Down Expand Up @@ -321,17 +349,17 @@ func GetAllActions(userId string) ([]string, error) {
})
}

func GetAllRoles(userId string) []string {
func GetAllRoles(userId string) ([]string, error) {
roles, err := getRolesByUser(userId)
if err != nil {
panic(err)
return nil, err
}

var res []string
res := []string{}
for _, role := range roles {
res = append(res, role.Name)
}
return res
return res, nil
}

func GetBuiltInModel(modelText string) (model.Model, error) {
Expand Down
4 changes: 2 additions & 2 deletions web/src/PermissionEditPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ class PermissionEditPage extends React.Component {
{Setting.getLabel(i18next.t("role:Sub roles"), i18next.t("role:Sub roles - Tooltip"))} :
</Col>
<Col span={22} >
<Select disabled={!this.hasRoleDefinition(this.state.model)} virtual={false} mode="multiple" style={{width: "100%"}} value={this.state.permission.roles}
<Select disabled={!this.hasRoleDefinition(this.state.model)} placeholder={this.hasRoleDefinition(this.state.model) ? "" : "This field is disabled because the model is empty or it doesn't support RBAC (in another word, doesn't contain [role_definition])"} virtual={false} mode="multiple" style={{width: "100%"}} value={this.state.permission.roles}
onChange={(value => {this.updatePermissionField("roles", value);})}
options={[
Setting.getOption(i18next.t("organization:All"), "*"),
Expand All @@ -323,7 +323,7 @@ class PermissionEditPage extends React.Component {
})}
options={[
Setting.getOption(i18next.t("organization:All"), "*"),
...this.state.permission.domains.map((domain) => Setting.getOption(domain, domain)),
...this.state.permission.domains.filter(domain => domain !== "*").map((domain) => Setting.getOption(domain, domain)),
]}
/>
</Col>
Expand Down

0 comments on commit 947dcf6

Please sign in to comment.