Skip to content

Commit

Permalink
feat: support new Enforce() and BatchEnforce() APIs (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
hound672 authored Jun 16, 2023
1 parent 1ecd904 commit 7523c28
Showing 1 changed file with 37 additions and 35 deletions.
72 changes: 37 additions & 35 deletions casdoorsdk/enforce.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package casdoorsdk

import (
"bytes"
"encoding/json"
"errors"
)
Expand All @@ -31,69 +30,72 @@ type PermissionRule struct {
Id string `xorm:"varchar(100) index not null default ''" json:"id"`
}

func Enforce(permissionRule *PermissionRule) (bool, error) {
postBytes, err := json.Marshal(permissionRule)
type CasbinRequest = []interface{}

func Enforce(permissionId, modelId, resourceId string, casbinRequest CasbinRequest) (bool, error) {
postBytes, err := json.Marshal(casbinRequest)
if err != nil {
return false, err
}

bytes, err := doEnforce("enforce", postBytes)
res, err := doEnforce("enforce", permissionId, modelId, resourceId, postBytes)
if err != nil {
return false, err
}

var allow bool
data, ok := res.Data.([]interface{})
if !ok {
return false, errors.New("invalid data")
}

err = json.Unmarshal(bytes, &allow)
if err != nil {
return false, err
allow, ok := data[0].(bool)
if !ok {
return false, errors.New("invalid data")
}

return allow, nil
}

func BatchEnforce(permissionRules []PermissionRule) ([]bool, error) {
postBytes, err := json.Marshal(permissionRules)
func BatchEnforce(permissionId, modelId, resourceId string, casbinRequests []CasbinRequest) ([]bool, error) {
postBytes, err := json.Marshal(casbinRequests)
if err != nil {
return nil, err
}

bytes, err := doEnforce("batch-enforce", postBytes)
res, err := doEnforce("batch-enforce", permissionId, modelId, resourceId, postBytes)
if err != nil {
return nil, err
}

var allow []bool
var allows []bool
data, ok := res.Data.([]interface{})
if !ok {
return nil, errors.New("invalid data")
}

err = json.Unmarshal(bytes, &allow)
if err != nil {
return nil, err
for _, d := range data {
el, ok := d.([]interface{})
if !ok {
return nil, errors.New("invalid data")
}
allows = append(allows, el[0].(bool))
}

return allow, nil
return allows, nil
}

func doEnforce(action string, postBytes []byte) ([]byte, error) {
url := GetUrl(action, nil)
bytes, err := DoPostBytesRaw(url, "", bytes.NewBuffer(postBytes))
if err != nil {
return nil, err
func doEnforce(action string, permissionId, modelId, resourceId string, postBytes []byte) (*Response, error) {
queryMap := map[string]string{
"permissionId": permissionId,
"modelId": modelId,
"resourceId": resourceId,
}

if len(bytes) == 0 {
return nil, errors.New("response is empty")
}

if bytes[0] == '{' {
var res Response

err = json.Unmarshal(bytes, &res)
if err != nil {
return nil, err
}

return nil, errors.New(res.Msg)
//bytes, err := DoPostBytesRaw(url, "", bytes.NewBuffer(postBytes))
resp, err := DoPost(action, queryMap, postBytes, false, false)
if err != nil {
return nil, err
}

return bytes, nil
return resp, nil
}

0 comments on commit 7523c28

Please sign in to comment.