Skip to content

Commit

Permalink
Merge pull request #35 from ArtisanCloud/develop
Browse files Browse the repository at this point in the history
feature(rbac): add permission tree list
  • Loading branch information
Matrix-X authored Aug 10, 2022
2 parents b677e4a + 86ef4c2 commit c1cb6a2
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions authorization/rbac/models/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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
}

0 comments on commit c1cb6a2

Please sign in to comment.