From 86ef4c28b86bb201040bfb67760340f2ba6e59d4 Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Wed, 10 Aug 2022 22:34:51 +0800 Subject: [PATCH] feature(rbac): add permission tree list --- authorization/rbac/models/permission.go | 31 +++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/authorization/rbac/models/permission.go b/authorization/rbac/models/permission.go index c8454f7..a0b1a5a 100644 --- a/authorization/rbac/models/permission.go +++ b/authorization/rbac/models/permission.go @@ -5,6 +5,7 @@ import ( "github.com/ArtisanCloud/PowerLibs/v2/database" "github.com/ArtisanCloud/PowerLibs/v2/object" "github.com/ArtisanCloud/PowerLibs/v2/security" + "gorm.io/gorm" ) // TableName overrides the table name used by Permission to `profiles` @@ -79,3 +80,33 @@ func (mdl *Permission) GetComposedUniqueID() string { return hashKey } + +func (mdl *Permission) GetTreeList(db *gorm.DB, conditions *map[string]interface{}, preloads []string, + parentID *string, needQueryChildren bool, +) (permissions []*Permission, err error) { + permissions = []*Permission{} + if parentID != nil { + if conditions == nil { + conditions = &map[string]interface{}{} + } + (*conditions)["parent_id"] = parentID + } + + err = database.GetAllList(db, conditions, &permissions, preloads) + if err != nil { + return nil, err + } + + if needQueryChildren { + for _, permission := range permissions { + children, err := mdl.GetTreeList(db, conditions, preloads, &permission.UniqueID, needQueryChildren) + if err != nil { + return nil, err + } + + permission.Children = children + } + } + + return permissions, err +}