From ec4d7c660aa381e875db33641e389dc5437bed13 Mon Sep 17 00:00:00 2001 From: June <55494127+notdu@users.noreply.github.com> Date: Tue, 3 Jan 2023 22:11:55 +0700 Subject: [PATCH] feat: add enforce, batch-enforce method (#52) --- casdoorsdk/base.go | 33 +++++++++++++------ casdoorsdk/enforce.go | 75 +++++++++++++++++++++++++++++++++++++++++++ casdoorsdk/util.go | 4 +++ 3 files changed, 103 insertions(+), 9 deletions(-) create mode 100644 casdoorsdk/enforce.go diff --git a/casdoorsdk/base.go b/casdoorsdk/base.go index 412fbce..0fbc072 100644 --- a/casdoorsdk/base.go +++ b/casdoorsdk/base.go @@ -94,10 +94,8 @@ func DoGetBytesRaw(url string) ([]byte, error) { } func DoPost(action string, queryMap map[string]string, postBytes []byte, isForm, isFile bool) (*Response, error) { - client := &http.Client{} url := GetUrl(action, queryMap) - var resp *http.Response var err error var contentType string var body io.Reader @@ -124,6 +122,29 @@ func DoPost(action string, queryMap map[string]string, postBytes []byte, isForm, body = bytes.NewReader(postBytes) } + respBytes, err := DoPostBytesRaw(url, contentType, body) + if err != nil { + return nil, err + } + + var response Response + err = json.Unmarshal(respBytes, &response) + if err != nil { + return nil, err + } + + return &response, nil +} + +// DoPostBytesRaw is a general function to post a request from url, body through HTTP Post method. +func DoPostBytesRaw(url string, contentType string, body io.Reader) ([]byte, error) { + if contentType == "" { + contentType = "text/plain;charset=UTF-8" + } + + client := &http.Client{} + var resp *http.Response + req, err := http.NewRequest("POST", url, body) if err != nil { return nil, err @@ -148,13 +169,7 @@ func DoPost(action string, queryMap map[string]string, postBytes []byte, isForm, return nil, err } - var response Response - err = json.Unmarshal(respByte, &response) - if err != nil { - return nil, err - } - - return &response, nil + return respByte, nil } // modifyUser is an encapsulation of user CUD(Create, Update, Delete) operations. diff --git a/casdoorsdk/enforce.go b/casdoorsdk/enforce.go new file mode 100644 index 0000000..ad26cf5 --- /dev/null +++ b/casdoorsdk/enforce.go @@ -0,0 +1,75 @@ +// 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 ( + "bytes" + "encoding/json" +) + +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"` +} + +func Enforce(permissionRule *PermissionRule) (bool, error) { + var allow bool + + postBytes, err := json.Marshal(permissionRule) + if err != nil { + return false, err + } + + url := GetUrl("enforce", nil) + bytes, err := DoPostBytesRaw(url, "", bytes.NewBuffer(postBytes)) + if err != nil { + return false, err + } + + err = json.Unmarshal(bytes, &allow) + if err != nil { + return false, err + } + + return allow, nil +} + +func BatchEnforce(permissionRules []PermissionRule) ([]bool, error) { + var allow []bool + + postBytes, err := json.Marshal(permissionRules) + if err != nil { + return nil, err + } + + url := GetUrl("batch-enforce", nil) + bytes, err := DoPostBytesRaw(url, "", bytes.NewBuffer(postBytes)) + if err != nil { + return nil, err + } + + err = json.Unmarshal(bytes, &allow) + if err != nil { + return nil, err + } + + return allow, nil +} diff --git a/casdoorsdk/util.go b/casdoorsdk/util.go index 1c11e3c..f981c70 100644 --- a/casdoorsdk/util.go +++ b/casdoorsdk/util.go @@ -33,6 +33,10 @@ func GetUrl(action string, queryMap map[string]string) string { return url } +func GetId(name string) string { + return authConfig.OrganizationName + "/" + name +} + func createFormFile(formData map[string][]byte) (string, io.Reader, error) { // https://tonybai.com/2021/01/16/upload-and-download-file-using-multipart-form-over-http/