mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-01 18:40:18 +08:00
feat: ABAC support for /api/enforce endpoint (#2660)
This commit is contained in:
32
util/json.go
32
util/json.go
@ -14,7 +14,10 @@
|
||||
|
||||
package util
|
||||
|
||||
import "encoding/json"
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func StructToJson(v interface{}) string {
|
||||
data, err := json.Marshal(v)
|
||||
@ -37,3 +40,30 @@ func StructToJsonFormatted(v interface{}) string {
|
||||
func JsonToStruct(data string, v interface{}) error {
|
||||
return json.Unmarshal([]byte(data), v)
|
||||
}
|
||||
|
||||
func TryJsonToAnonymousStruct(j string) (interface{}, error) {
|
||||
var data map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(j), &data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Create a slice of StructFields
|
||||
fields := make([]reflect.StructField, 0, len(data))
|
||||
for k, v := range data {
|
||||
fields = append(fields, reflect.StructField{
|
||||
Name: k,
|
||||
Type: reflect.TypeOf(v),
|
||||
})
|
||||
}
|
||||
|
||||
// Create the struct type
|
||||
t := reflect.StructOf(fields)
|
||||
|
||||
// Unmarshal again, this time to the new struct type
|
||||
val := reflect.New(t)
|
||||
i := val.Interface()
|
||||
if err := json.Unmarshal([]byte(j), &i); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return i, nil
|
||||
}
|
||||
|
@ -324,9 +324,16 @@ func GetUsernameFromEmail(email string) string {
|
||||
}
|
||||
|
||||
func StringToInterfaceArray(array []string) []interface{} {
|
||||
var interfaceArray []interface{}
|
||||
for _, v := range array {
|
||||
interfaceArray = append(interfaceArray, v)
|
||||
var (
|
||||
interfaceArray []interface{}
|
||||
elem interface{}
|
||||
)
|
||||
for _, elem = range array {
|
||||
jStruct, err := TryJsonToAnonymousStruct(elem.(string))
|
||||
if err == nil {
|
||||
elem = jStruct
|
||||
}
|
||||
interfaceArray = append(interfaceArray, elem)
|
||||
}
|
||||
return interfaceArray
|
||||
}
|
||||
|
Reference in New Issue
Block a user