Skip to content

Commit

Permalink
feature(rbac): add GetRBACRuleName
Browse files Browse the repository at this point in the history
  • Loading branch information
Matrix-X committed Aug 19, 2022
1 parent 60aea6c commit a30aadb
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
11 changes: 8 additions & 3 deletions authorization/rbac/models/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{}

Expand All @@ -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
Expand Down
22 changes: 18 additions & 4 deletions authorization/rbac/models/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"`
}

Expand All @@ -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()
Expand Down Expand Up @@ -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.
Expand Down
10 changes: 8 additions & 2 deletions authorization/rbac/models/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "普通员工"

Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions database/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand Down

0 comments on commit a30aadb

Please sign in to comment.