Skip to content

Commit

Permalink
feat: add enforce, batch-enforce method (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
notdu authored Jan 3, 2023
1 parent 58598e1 commit ec4d7c6
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 9 deletions.
33 changes: 24 additions & 9 deletions casdoorsdk/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand Down
75 changes: 75 additions & 0 deletions casdoorsdk/enforce.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 4 additions & 0 deletions casdoorsdk/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/

Expand Down

0 comments on commit ec4d7c6

Please sign in to comment.