Skip to content

Commit

Permalink
Merge pull request #37 from ArtisanCloud/develop
Browse files Browse the repository at this point in the history
feature(rbac): permission module
  • Loading branch information
Matrix-X authored Aug 13, 2022
2 parents 19dba49 + 79648be commit 7912637
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 59 deletions.
132 changes: 132 additions & 0 deletions authorization/rbac/models/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
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"
)

// TableName overrides the table name used by PermissionModule to `profiles`
func (mdl *PermissionModule) TableName() string {
return mdl.GetTableName(true)
}

// PermissionModule 数据表结构
type PermissionModule struct {
*database.PowerCompactModel

Parent *PermissionModule `gorm:"ForeignKey:ParentID;references:UniqueID" json:"parent"`
Children []*PermissionModule `gorm:"ForeignKey:ParentID;references:UniqueID" json:"children"`
Permissions []*Permission `gorm:"ForeignKey:ModuleID;references:UniqueID" json:"permissions"`

UniqueID string `gorm:"column:index_permission_module_id;index:,unique" json:"permissionModuleID"`
Name string `gorm:"column:name" json:"name"`
Description string `gorm:"column:description" json:"description"`
ParentID *string `gorm:"column:parent_id;index" json:"parentID"`
}

const TABLE_NAME_PERMISSION_MODULE = "rbac_permission_modules"

const PERMISSION_MODULE_UNIQUE_ID = "index_permission_module_id"

func NewPermissionModule(mapObject *object.Collection) *PermissionModule {

if mapObject == nil {
mapObject = object.NewCollection(&object.HashMap{})
}

newPermissionModule := &PermissionModule{
PowerCompactModel: database.NewPowerCompactModel(),
Name: mapObject.GetString("name", ""),
Description: mapObject.GetString("description", ""),
ParentID: mapObject.GetStringPointer("parentID", ""),
}
newPermissionModule.UniqueID = newPermissionModule.GetComposedUniqueID()

return newPermissionModule

}

// 获取当前 Model 的数据库表名称
func (mdl *PermissionModule) GetTableName(needFull bool) string {
tableName := TABLE_NAME_PERMISSION_MODULE
if needFull {
tableName = "public." + tableName
}
return tableName
}

func (mdl *PermissionModule) GetForeignKey() string {
return "index_permission_module_id"
}

func (mdl *PermissionModule) GetForeignValue() string {
return mdl.UniqueID
}

func (mdl *PermissionModule) GetComposedUniqueID() string {

strKey := *mdl.ParentID + "-" + mdl.Name
fmt2.Dump(strKey)
hashKey := security.HashStringData(strKey)

return hashKey
}

func (mdl *PermissionModule) GetGroupList(db *gorm.DB, conditions *map[string]interface{}, preloads []string) (permissionModules []*PermissionModule, err error) {
permissionModules = []*PermissionModule{}

if preloads == nil {
preloads = []string{"Permissions"}
}

if conditions == nil {
conditions = &map[string]interface{}{}
}
if _, ok := (*conditions)["parent_id"]; !ok {
(*conditions)["parent_id"] = ""
}

db = db.Debug()
err = database.GetAllList(db, conditions, &permissionModules, preloads)
if err != nil {
return nil, err
}

for _, module := range permissionModules {
(*conditions)["parent_id"] = &module.UniqueID
children, err := mdl.GetGroupList(db, conditions, preloads)
if err != nil {
return nil, err
}

module.Children = children
}

return permissionModules, err
}

func (mdl *PermissionModule) CheckPermissionModuleNameAvailable(db *gorm.DB) (err error) {

result := db.
//Debug().
Where("name", mdl.Name).
Where("index_permission_module_id != ?", mdl.UniqueID).
Where("parent_id = ?", mdl.ParentID).
First(&PermissionModule{})

if result.Error != nil && errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil
}

if result.Error != nil {
return result.Error
}

err = errors.New("permission module name is not available")

return err
}
48 changes: 20 additions & 28 deletions authorization/rbac/models/permission.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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 All @@ -17,16 +17,14 @@ func (mdl *Permission) TableName() string {
type Permission struct {
*database.PowerCompactModel

Parent *Permission `gorm:"ForeignKey:ParentID;references:UniqueID" json:"parent"`
Children []*Permission `gorm:"ForeignKey:ParentID;references:UniqueID" json:"children"`
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"`
ParentID *string `gorm:"column:parent_id;index" json:"parentID"`
Type int8 `gorm:"column:type" json:"type"`
ModuleID *string `gorm:"column:module_id" json:"moduleID"`
}

const TABLE_NAME_PERMISSION = "rbac_permissions"
Expand All @@ -48,8 +46,7 @@ func NewPermission(mapObject *object.Collection) *Permission {
SubjectValue: mapObject.GetString("subjectValue", ""),
Action: mapObject.GetString("action", ""),
Description: mapObject.GetString("description", ""),
ParentID: mapObject.GetStringPointer("parentID", ""),
Type: mapObject.GetInt8("type", PERMISSION_TYPE_NORMAL),
ModuleID: mapObject.GetStringPointer("moduleID", ""),
}
newPermission.UniqueID = newPermission.GetComposedUniqueID()

Expand All @@ -76,35 +73,30 @@ func (mdl *Permission) GetForeignValue() string {

func (mdl *Permission) GetComposedUniqueID() string {

strKey := ""
if mdl.Type == PERMISSION_TYPE_MODULE {
strKey = *mdl.ParentID + "-" + mdl.Action + "-" +
mdl.SubjectAlias + mdl.SubjectValue
} else {
strKey = *mdl.ParentID + "-" + mdl.Action + "-" +
mdl.SubjectAlias + mdl.SubjectValue
}
fmt2.Dump(strKey)
strKey := *mdl.ModuleID + "-" + mdl.Action + "-" + mdl.SubjectValue
//fmt2.Dump(strKey)
hashKey := security.HashStringData(strKey)

return hashKey
}

func (mdl *Permission) GetGroupList(db *gorm.DB, conditions *map[string]interface{}, preloads []string) (groupedPermissions map[string]*Permission, err error) {
permissions := []*Permission{}
func (mdl *Permission) CheckPermissionModuleNameAvailable(db *gorm.DB) (err error) {

result := db.
//Debug().
Where("subject_alias", mdl.SubjectAlias).
Where("index_permission_id != ?", mdl.UniqueID).
First(&Permission{})

err = database.GetAllList(db, conditions, &permissions, preloads)
if err != nil {
return nil, err
if result.Error != nil && errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil
}

for _, permission := range permissions {
if permission.ParentID != nil {
groupedPermissions[*permission.ParentID] = permission
} else {
groupedPermissions["unGrouped"] = permission
}
if result.Error != nil {
return result.Error
}

return groupedPermissions, err
err = errors.New("permission name is not available")

return err
}
22 changes: 22 additions & 0 deletions authorization/rbac/models/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/ArtisanCloud/PowerLibs/v2/database"
"github.com/ArtisanCloud/PowerLibs/v2/object"
"github.com/ArtisanCloud/PowerLibs/v2/security"
"github.com/pkg/errors"
"gorm.io/gorm"
)

Expand Down Expand Up @@ -129,3 +130,24 @@ func (mdl *Role) GetTreeList(db *gorm.DB, conditions *map[string]interface{}, pr

return roles, err
}

func (mdl *Role) CheckRoleNameAvailable(db *gorm.DB) (err error) {

result := db.
//Debug().
Where("name", mdl.Name).
Where("index_role_id != ?", mdl.UniqueID).
First(&Role{})

if result.Error != nil && errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil
}

if result.Error != nil {
return result.Error
}

err = errors.New("role name is not available")

return err
}
22 changes: 22 additions & 0 deletions database/tag/tagGroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,28 @@ func (mdl *TagGroup) GetComposedUniqueID() string {
return hashKey
}

func (mdl *TagGroup) CheckTagGroupNameAvailable(db *gorm.DB) (err error) {

result := db.
//Debug().
Where("group_name", mdl.GroupName).
Where("owner_type", mdl.OwnerType).
Where("index_tag_group_id != ?", mdl.UniqueID).
First(&TagGroup{})

if result.Error != nil && errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil
}

if result.Error != nil {
return result.Error
}

err = errors.New("tag group name is not available")

return err
}

/**
* Relationships
*/
8 changes: 2 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@ require (
)

require (
github.com/getkin/kin-openapi v0.98.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/swag v0.22.0 // indirect
github.com/invopop/yaml v0.2.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/kr/text v0.2.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)

require (
Expand Down
Loading

0 comments on commit 7912637

Please sign in to comment.