From 7059235194b7f0e1408577547fa6fab61e8a2abf Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Tue, 16 Aug 2022 23:13:30 +0800 Subject: [PATCH] feature(http): add json options to replace body content --- authorization/rbac/models/permission.go | 20 ++++++++++---------- authorization/rbac/models/role.go | 16 +++++++++++----- http/drivers/gout/client.go | 7 +++++++ 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/authorization/rbac/models/permission.go b/authorization/rbac/models/permission.go index 031b9c3..2ae8eec 100644 --- a/authorization/rbac/models/permission.go +++ b/authorization/rbac/models/permission.go @@ -19,12 +19,12 @@ type Permission struct { PermissionModule *PermissionModule `gorm:"ForeignKey:ModuleID;references:UniqueID" json:"permissionModule"` - UniqueID string `gorm:"column:index_permission_id;index:,unique" json:"permissionID"` - SubjectAlias string `gorm:"column:subject_alias" json:"subjectAlias"` - SubjectValue string `gorm:"column:subject_value; not null;" json:"subjectValue"` - Action string `gorm:"column:action; not null;" json:"action"` - Description string `gorm:"column:description" json:"description"` - ModuleID *string `gorm:"column:module_id" json:"moduleID"` + UniqueID string `gorm:"column:index_permission_id;index:,unique" json:"permissionID"` + ObjectAlias string `gorm:"column:object_alias" json:"objectAlias"` + ObjectValue string `gorm:"column:object_value; not null;" json:"objectValue"` + Action string `gorm:"column:action; not null;" json:"action"` + Description string `gorm:"column:description" json:"description"` + ModuleID *string `gorm:"column:module_id" json:"moduleID"` } const TABLE_NAME_PERMISSION = "rbac_permissions" @@ -42,8 +42,8 @@ func NewPermission(mapObject *object.Collection) *Permission { newPermission := &Permission{ PowerCompactModel: database.NewPowerCompactModel(), - SubjectAlias: mapObject.GetString("subjectAlias", ""), - SubjectValue: mapObject.GetString("subjectValue", ""), + ObjectAlias: mapObject.GetString("objectAlias", ""), + ObjectValue: mapObject.GetString("objectValue", ""), Action: mapObject.GetString("action", ""), Description: mapObject.GetString("description", ""), ModuleID: mapObject.GetStringPointer("moduleID", ""), @@ -73,7 +73,7 @@ func (mdl *Permission) GetForeignValue() string { func (mdl *Permission) GetComposedUniqueID() string { - strKey := mdl.Action + "-" + mdl.SubjectValue + strKey := mdl.Action + "-" + mdl.ObjectValue //fmt2.Dump(strKey) hashKey := security.HashStringData(strKey) @@ -84,7 +84,7 @@ func (mdl *Permission) CheckPermissionNameAvailable(db *gorm.DB) (err error) { result := db. //Debug(). - Where("subject_alias", mdl.SubjectAlias). + Where("object_alias", mdl.ObjectAlias). Where("index_permission_id != ?", mdl.UniqueID). First(&Permission{}) diff --git a/authorization/rbac/models/role.go b/authorization/rbac/models/role.go index 4ff0157..fe89fa0 100644 --- a/authorization/rbac/models/role.go +++ b/authorization/rbac/models/role.go @@ -30,6 +30,7 @@ const TABLE_NAME_ROLE = "roles" const ROLE_UNIQUE_ID = "index_role_id" +const ROLE_TYPE_ALL int8 = 0 const ROLE_TYPE_SYSTEM int8 = 1 const ROLE_TYPE_NORMAL int8 = 2 @@ -102,15 +103,20 @@ func (mdl *Role) GetEmployeeComposedUniqueID() string { } func (mdl *Role) GetTreeList(db *gorm.DB, conditions *map[string]interface{}, preloads []string, - parentID *string, needQueryChildren bool, + roleType int8, parentID *string, needQueryChildren bool, ) (roles []*Role, err error) { roles = []*Role{} + + if conditions == nil { + conditions = &map[string]interface{}{} + } + if parentID != nil { - if conditions == nil { - conditions = &map[string]interface{}{} - } (*conditions)["parent_id"] = parentID } + if roleType != ROLE_TYPE_ALL { + (*conditions)["type"] = roleType + } err = database.GetAllList(db, conditions, &roles, preloads) if err != nil { @@ -119,7 +125,7 @@ func (mdl *Role) GetTreeList(db *gorm.DB, conditions *map[string]interface{}, pr if needQueryChildren { for _, role := range roles { - children, err := mdl.GetTreeList(db, conditions, preloads, &role.UniqueID, needQueryChildren) + children, err := mdl.GetTreeList(db, conditions, preloads, roleType, &role.UniqueID, needQueryChildren) if err != nil { return nil, err } diff --git a/http/drivers/gout/client.go b/http/drivers/gout/client.go index f09bdad..787cf20 100644 --- a/http/drivers/gout/client.go +++ b/http/drivers/gout/client.go @@ -67,6 +67,13 @@ func (client *Client) PrepareRequest(method string, uri string, options *object. } else { headers = &object.HashMap{} } + + if (*options)["json"] != nil { + (*options)["body"], err = object.JsonEncode((*options)["json"]) + if err != nil { + return nil, nil, nil, nil, "", false, err + } + } if (*options)["body"] != nil { body = (*options)["body"] } else {