From a30aadbc0bb5d15fc7405d5b6ad41b40a831013f Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Fri, 19 Aug 2022 23:34:49 +0800 Subject: [PATCH] feature(rbac): add GetRBACRuleName --- authorization/rbac/models/module.go | 11 ++++++++--- authorization/rbac/models/permission.go | 22 ++++++++++++++++++---- authorization/rbac/models/role.go | 10 ++++++++-- database/model.go | 4 ++-- 4 files changed, 36 insertions(+), 11 deletions(-) diff --git a/authorization/rbac/models/module.go b/authorization/rbac/models/module.go index b34c0e6..0850b41 100644 --- a/authorization/rbac/models/module.go +++ b/authorization/rbac/models/module.go @@ -3,7 +3,6 @@ package models import ( "errors" "github.com/ArtisanCloud/PowerLibs/v2/database" - fmt2 "github.com/ArtisanCloud/PowerLibs/v2/fmt" "github.com/ArtisanCloud/PowerLibs/v2/object" "github.com/ArtisanCloud/PowerLibs/v2/security" "gorm.io/gorm" @@ -70,12 +69,18 @@ func (mdl *PermissionModule) GetForeignValue() string { func (mdl *PermissionModule) GetComposedUniqueID() string { strKey := *mdl.ParentID + "-" + mdl.Name - fmt2.Dump(strKey) + //fmt2.Dump(strKey) hashKey := security.HashStringData(strKey) return hashKey } +func (mdl *PermissionModule) GetRBACRuleName() string { + + return mdl.Name + "-" + mdl.UniqueID[0:5] + +} + func (mdl *PermissionModule) GetGroupList(db *gorm.DB, conditions *map[string]interface{}, preloads []string) (permissionModules []*PermissionModule, err error) { permissionModules = []*PermissionModule{} @@ -90,7 +95,7 @@ func (mdl *PermissionModule) GetGroupList(db *gorm.DB, conditions *map[string]in (*conditions)["parent_id"] = "" } - db = db.Debug() + //db = db.Debug() err = database.GetAllList(db, conditions, &permissionModules, preloads) if err != nil { return nil, err diff --git a/authorization/rbac/models/permission.go b/authorization/rbac/models/permission.go index 2ae8eec..582d3b3 100644 --- a/authorization/rbac/models/permission.go +++ b/authorization/rbac/models/permission.go @@ -8,6 +8,14 @@ import ( "gorm.io/gorm" ) +const ( + RBAC_CONTROL_ALL = "all" + RBAC_CONTROL_WRITE = "write" + RBAC_CONTROL_READ = "read" + RBAC_CONTROL_DELETE = "read" + RBAC_CONTROL_NONE = "none" +) + // TableName overrides the table name used by Permission to `profiles` func (mdl *Permission) TableName() string { return mdl.GetTableName(true) @@ -20,10 +28,10 @@ type Permission struct { PermissionModule *PermissionModule `gorm:"ForeignKey:ModuleID;references:UniqueID" json:"permissionModule"` UniqueID string `gorm:"column:index_permission_id;index:,unique" json:"permissionID"` - ObjectAlias string `gorm:"column:object_alias" json:"objectAlias"` + 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"` + Description *string `gorm:"column:description" json:"description"` ModuleID *string `gorm:"column:module_id" json:"moduleID"` } @@ -42,10 +50,10 @@ func NewPermission(mapObject *object.Collection) *Permission { newPermission := &Permission{ PowerCompactModel: database.NewPowerCompactModel(), - ObjectAlias: mapObject.GetString("objectAlias", ""), + ObjectAlias: mapObject.GetStringPointer("objectAlias", ""), ObjectValue: mapObject.GetString("objectValue", ""), Action: mapObject.GetString("action", ""), - Description: mapObject.GetString("description", ""), + Description: mapObject.GetStringPointer("description", ""), ModuleID: mapObject.GetStringPointer("moduleID", ""), } newPermission.UniqueID = newPermission.GetComposedUniqueID() @@ -80,6 +88,12 @@ func (mdl *Permission) GetComposedUniqueID() string { return hashKey } +func (mdl *Permission) GetRBACRuleName() string { + + return *mdl.ObjectAlias + "-" + mdl.UniqueID[0:5] + +} + func (mdl *Permission) CheckPermissionNameAvailable(db *gorm.DB) (err error) { result := db. diff --git a/authorization/rbac/models/role.go b/authorization/rbac/models/role.go index fe89fa0..fb2c50f 100644 --- a/authorization/rbac/models/role.go +++ b/authorization/rbac/models/role.go @@ -34,7 +34,7 @@ const ROLE_TYPE_ALL int8 = 0 const ROLE_TYPE_SYSTEM int8 = 1 const ROLE_TYPE_NORMAL int8 = 2 -const ROLE_ROOT_NAME string = "超级管理员" +const ROLE_SUPER_ADMIN_NAME string = "超级管理员" const ROLE_ADMIN_NAME string = "管理员" const ROLE_EMPLOYEE_NAME string = "普通员工" @@ -82,7 +82,7 @@ func (mdl *Role) GetComposedUniqueID() string { } func (mdl *Role) GetRootComposedUniqueID() string { - strKey := "" + "-" + ROLE_ROOT_NAME + strKey := "" + "-" + ROLE_SUPER_ADMIN_NAME hashKey := security.HashStringData(strKey) return hashKey @@ -102,6 +102,12 @@ func (mdl *Role) GetEmployeeComposedUniqueID() string { return hashKey } +func (mdl *Role) GetRBACRuleName() string { + + return mdl.Name + "-" + mdl.UniqueID[0:5] + +} + func (mdl *Role) GetTreeList(db *gorm.DB, conditions *map[string]interface{}, preloads []string, roleType int8, parentID *string, needQueryChildren bool, ) (roles []*Role, err error) { diff --git a/database/model.go b/database/model.go index 87a8860..389f0b9 100644 --- a/database/model.go +++ b/database/model.go @@ -251,7 +251,7 @@ func GetAllList(db *gorm.DB, conditions *map[string]interface{}, func InsertModelsOnUniqueID(db *gorm.DB, mdl interface{}, uniqueName string, models interface{}) error { result := db.Model(mdl). - Debug(). + //Debug(). Clauses(clause.OnConflict{ Columns: []clause.Column{{Name: uniqueName}}, DoNothing: true, @@ -268,7 +268,7 @@ func UpsertModelsOnUniqueID(db *gorm.DB, mdl interface{}, uniqueName string, } result := db.Model(mdl). - Debug(). + //Debug(). Clauses(clause.OnConflict{ Columns: []clause.Column{{Name: uniqueName}}, DoUpdates: clause.AssignmentColumns(fieldsToUpdate),