mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-05 05:50:19 +08:00
feat: add processArgsToTempFiles() to RunCasbinCommand()
This commit is contained in:
@ -17,9 +17,39 @@ package controllers
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func processArgsToTempFiles(args []string) ([]string, []string, error) {
|
||||||
|
tempFiles := []string{}
|
||||||
|
newArgs := []string{}
|
||||||
|
for i := 0; i < len(args); i++ {
|
||||||
|
if (args[i] == "-m" || args[i] == "-p") && i+1 < len(args) {
|
||||||
|
pattern := fmt.Sprintf("casbin_temp_%s_*.conf", args[i])
|
||||||
|
tempFile, err := os.CreateTemp("", pattern)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, fmt.Errorf("failed to create temp file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = tempFile.WriteString(args[i+1])
|
||||||
|
if err != nil {
|
||||||
|
tempFile.Close()
|
||||||
|
return nil, nil, fmt.Errorf("failed to write to temp file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tempFile.Close()
|
||||||
|
tempFiles = append(tempFiles, tempFile.Name())
|
||||||
|
newArgs = append(newArgs, args[i], tempFile.Name())
|
||||||
|
i++
|
||||||
|
} else {
|
||||||
|
newArgs = append(newArgs, args[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tempFiles, newArgs, nil
|
||||||
|
}
|
||||||
|
|
||||||
// RunCasbinCommand
|
// RunCasbinCommand
|
||||||
// @Title RunCasbinCommand
|
// @Title RunCasbinCommand
|
||||||
// @Tag Enforcer API
|
// @Tag Enforcer API
|
||||||
@ -43,9 +73,10 @@ func (c *ApiController) RunCasbinCommand() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// argString's example:
|
// RBAC model & policy example:
|
||||||
// ["enforce", "-m", "examples/rbac_model.conf", "-p", "examples/rbac_policy.csv", "alice", "data1", "read"]
|
// https://door.casdoor.com/api/run-casbin-command?language=go&args=["enforce", "-m", "[request_definition]\nr = sub, obj, act\n\n[policy_definition]\np = sub, obj, act\n\n[role_definition]\ng = _, _\n\n[policy_effect]\ne = some(where (p.eft == allow))\n\n[matchers]\nm = g(r.sub, p.sub) %26%26 r.obj == p.obj %26%26 r.act == p.act", "-p", "p, alice, data1, read\np, bob, data2, write\np, data2_admin, data2, read\np, data2_admin, data2, write\ng, alice, data2_admin", "alice", "data1", "read"]
|
||||||
// see: https://github.com/jcasbin/casbin-java-cli?tab=readme-ov-file#get-started
|
// Casbin CLI usage:
|
||||||
|
// https://github.com/jcasbin/casbin-java-cli?tab=readme-ov-file#get-started
|
||||||
var args []string
|
var args []string
|
||||||
err = json.Unmarshal([]byte(argString), &args)
|
err = json.Unmarshal([]byte(argString), &args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -53,19 +84,31 @@ func (c *ApiController) RunCasbinCommand() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
command := exec.Command(binaryName, args...)
|
tempFiles, processedArgs, err := processArgsToTempFiles(args)
|
||||||
outputBytes, err := command.CombinedOutput()
|
defer func() {
|
||||||
if outputBytes != nil {
|
for _, file := range tempFiles {
|
||||||
output := string(outputBytes)
|
os.Remove(file)
|
||||||
c.ResponseError(output)
|
}
|
||||||
return
|
}()
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.ResponseError(err.Error())
|
c.ResponseError(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
command := exec.Command(binaryName, processedArgs...)
|
||||||
|
outputBytes, err := command.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
errorString := err.Error()
|
||||||
|
if outputBytes != nil {
|
||||||
|
output := string(outputBytes)
|
||||||
|
errorString = fmt.Sprintf("%s, error: %s", output, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
c.ResponseError(errorString)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
output := string(outputBytes)
|
output := string(outputBytes)
|
||||||
|
output = strings.TrimSuffix(output, "\n")
|
||||||
c.ResponseOk(output)
|
c.ResponseOk(output)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user