Skip to content

Commit

Permalink
feat: improve enforce() and batchEnforce() API response
Browse files Browse the repository at this point in the history
  • Loading branch information
hsluoyz committed Dec 20, 2023
1 parent 46e0bc1 commit a425948
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 39 deletions.
87 changes: 50 additions & 37 deletions controllers/casbin_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package controllers

import (
"encoding/json"
"fmt"

"github.com/casdoor/casdoor/object"
"github.com/casdoor/casdoor/util"
Expand Down Expand Up @@ -56,13 +57,19 @@ func (c *ApiController) Enforce() {
return
}

res, err := enforcer.Enforce(request...)
res := []bool{}
keyRes := []string{}

enforceResult, err := enforcer.Enforce(request...)
if err != nil {
c.ResponseError(err.Error())
return
}

c.ResponseOk(res)
res = append(res, enforceResult)
keyRes = append(keyRes, enforcer.GetModelAndAdapter())

c.ResponseOk(res, keyRes)
return
}

Expand All @@ -72,22 +79,24 @@ func (c *ApiController) Enforce() {
c.ResponseError(err.Error())
return
}
if permission == nil {
c.ResponseError(fmt.Sprintf("permission: %s doesn't exist", permissionId))
return
}

res := []bool{}
keyRes := []string{}

if permission == nil {
res = append(res, false)
} else {
enforceResult, err := object.Enforce(permission, &request)
if err != nil {
c.ResponseError(err.Error())
return
}

res = append(res, enforceResult)
enforceResult, err := object.Enforce(permission, &request)
if err != nil {
c.ResponseError(err.Error())
return
}

c.ResponseOk(res)
res = append(res, enforceResult)
keyRes = append(keyRes, permission.GetModelAndAdapter())

c.ResponseOk(res, keyRes)
return
}

Expand All @@ -111,9 +120,9 @@ func (c *ApiController) Enforce() {
}

res := []bool{}

keyRes := []string{}
listPermissionIdMap := object.GroupPermissionsByModelAdapter(permissions)
for _, permissionIds := range listPermissionIdMap {
for key, permissionIds := range listPermissionIdMap {
firstPermission, err := object.GetPermission(permissionIds[0])
if err != nil {
c.ResponseError(err.Error())
Expand All @@ -127,9 +136,10 @@ func (c *ApiController) Enforce() {
}

res = append(res, enforceResult)
keyRes = append(keyRes, key)
}

c.ResponseOk(res)
c.ResponseOk(res, keyRes)
}

// BatchEnforce
Expand Down Expand Up @@ -160,13 +170,19 @@ func (c *ApiController) BatchEnforce() {
return
}

res, err := enforcer.BatchEnforce(requests)
res := [][]bool{}
keyRes := []string{}

enforceResult, err := enforcer.BatchEnforce(requests)
if err != nil {
c.ResponseError(err.Error())
return
}

c.ResponseOk(res)
res = append(res, enforceResult)
keyRes = append(keyRes, enforcer.GetModelAndAdapter())

c.ResponseOk(res, keyRes)
return
}

Expand All @@ -176,28 +192,24 @@ func (c *ApiController) BatchEnforce() {
c.ResponseError(err.Error())
return
}
if permission == nil {
c.ResponseError(fmt.Sprintf("permission: %s doesn't exist", permissionId))
return
}

res := [][]bool{}
keyRes := []string{}

if permission == nil {
l := len(requests)
resRequest := make([]bool, l)
for i := 0; i < l; i++ {
resRequest[i] = false
}

res = append(res, resRequest)
} else {
enforceResult, err := object.BatchEnforce(permission, &requests)
if err != nil {
c.ResponseError(err.Error())
return
}

res = append(res, enforceResult)
enforceResult, err := object.BatchEnforce(permission, &requests)
if err != nil {
c.ResponseError(err.Error())
return
}

c.ResponseOk(res)
res = append(res, enforceResult)
keyRes = append(keyRes, permission.GetModelAndAdapter())

c.ResponseOk(res, keyRes)
return
}

Expand All @@ -215,7 +227,7 @@ func (c *ApiController) BatchEnforce() {
}

res := [][]bool{}

keyRes := []string{}
listPermissionIdMap := object.GroupPermissionsByModelAdapter(permissions)
for _, permissionIds := range listPermissionIdMap {
firstPermission, err := object.GetPermission(permissionIds[0])
Expand All @@ -231,9 +243,10 @@ func (c *ApiController) BatchEnforce() {
}

res = append(res, enforceResult)
keyRes = append(keyRes, firstPermission.GetModelAndAdapter())
}

c.ResponseOk(res)
c.ResponseOk(res, keyRes)
}

func (c *ApiController) GetAllObjects() {
Expand Down
4 changes: 4 additions & 0 deletions object/enforcer.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ func (enforcer *Enforcer) GetId() string {
return fmt.Sprintf("%s/%s", enforcer.Owner, enforcer.Name)
}

func (enforcer *Enforcer) GetModelAndAdapter() string {
return util.GetId(enforcer.Model, enforcer.Adapter)
}

func (enforcer *Enforcer) InitEnforcer() error {
if enforcer.Enforcer != nil {
return nil
Expand Down
7 changes: 5 additions & 2 deletions object/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,8 @@ func GetMaskedPermissions(permissions []*Permission) []*Permission {
// as the policyFilter when the enforcer load policy).
func GroupPermissionsByModelAdapter(permissions []*Permission) map[string][]string {
m := make(map[string][]string)

for _, permission := range permissions {
key := permission.Model + permission.Adapter
key := permission.GetModelAndAdapter()
permissionIds, ok := m[key]
if !ok {
m[key] = []string{permission.GetId()}
Expand All @@ -464,6 +463,10 @@ func (p *Permission) GetId() string {
return util.GetId(p.Owner, p.Name)
}

func (p *Permission) GetModelAndAdapter() string {
return util.GetId(p.Model, p.Adapter)
}

func (p *Permission) isUserHit(name string) bool {
targetOrg, targetName := util.GetOwnerAndNameFromId(name)
for _, user := range p.Users {
Expand Down

0 comments on commit a425948

Please sign in to comment.