代码拉取完成,页面将自动刷新
// Copyright 2022 The Casdoor 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 records and
// limitations under the License.
package casdoorsdk
import (
"encoding/json"
"errors"
)
type PermissionRule struct {
Ptype string `xorm:"varchar(100) index not null default ''" json:"ptype"`
V0 string `xorm:"varchar(100) index not null default ''" json:"v0"`
V1 string `xorm:"varchar(100) index not null default ''" json:"v1"`
V2 string `xorm:"varchar(100) index not null default ''" json:"v2"`
V3 string `xorm:"varchar(100) index not null default ''" json:"v3"`
V4 string `xorm:"varchar(100) index not null default ''" json:"v4"`
V5 string `xorm:"varchar(100) index not null default ''" json:"v5"`
Id string `xorm:"varchar(100) index not null default ''" json:"id"`
}
type CasbinRequest = []interface{}
func (c *Client) Enforce(permissionId, modelId, resourceId string, casbinRequest CasbinRequest) (bool, error) {
postBytes, err := json.Marshal(casbinRequest)
if err != nil {
return false, err
}
res, err := c.doEnforce("enforce", permissionId, modelId, resourceId, postBytes)
if err != nil {
return false, err
}
results, ok := res.Data.([]interface{})
if !ok {
return false, errors.New("invalid data")
}
for _, result := range results {
isAllow, ok := result.(bool)
if !ok {
return false, errors.New("invalid data")
}
if isAllow {
return isAllow, nil
}
}
return false, nil
}
func Enforce(permissionId, modelId, resourceId string, casbinRequest CasbinRequest) (bool, error) {
return globalClient.Enforce(permissionId, modelId, resourceId, casbinRequest)
}
func (c *Client) BatchEnforce(permissionId, modelId, resourceId string, casbinRequests []CasbinRequest) ([][]bool, error) {
postBytes, err := json.Marshal(casbinRequests)
if err != nil {
return nil, err
}
res, err := c.doEnforce("batch-enforce", permissionId, modelId, resourceId, postBytes)
if err != nil {
return nil, err
}
var allows [][]bool
data, ok := res.Data.([]interface{})
if !ok {
return nil, errors.New("invalid data")
}
for _, d := range data {
elems, ok := d.([]interface{})
if !ok {
return nil, errors.New("invalid data")
}
var permRes []bool
for _, el := range elems {
r, ok := el.(bool)
if !ok {
return nil, errors.New("invalid data")
}
permRes = append(permRes, r)
}
allows = append(allows, permRes)
}
return allows, nil
}
func BatchEnforce(permissionId, modelId, resourceId string, casbinRequests []CasbinRequest) ([][]bool, error) {
return globalClient.BatchEnforce(permissionId, modelId, resourceId, casbinRequests)
}
func (c *Client) doEnforce(action string, permissionId, modelId, resourceId string, postBytes []byte) (*Response, error) {
queryMap := map[string]string{
"permissionId": permissionId,
"modelId": modelId,
"resourceId": resourceId,
}
// bytes, err := DoPostBytesRaw(url, "", bytes.NewBuffer(postBytes))
resp, err := c.DoPost(action, queryMap, postBytes, false, false)
if err != nil {
return nil, err
}
return resp, nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。