feat: roles system backend
This commit is contained in:
1 parent
44cca46d5e
commit
8c6c6456da
50 files changed
+2578
-22
No files matched your search
@@ -71,6 +71,7 @@ func (g *CreateArticleUseCase) CreateArticle(ctx context.Context, createArticle
|
||||
Body: createArticle.Body,
|
||||
IsOnline: false,
|
||||
SiteID: siteID,
|
||||
CreatedBy: createArticle.CreatedBy,
|
||||
Authors: creditedAuthors,
|
||||
SEODismissed: []string{},
|
||||
})
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
package useCases
|
||||
|
||||
import (
|
||||
"RenewCMS/internal/domain/role"
|
||||
"RenewCMS/internal/domain/user"
|
||||
)
|
||||
|
||||
type Access struct {
|
||||
Role role.Role
|
||||
Permissions []role.Grant
|
||||
}
|
||||
|
||||
type GetMyAccessUseCase struct {
|
||||
roles role.Repository
|
||||
}
|
||||
|
||||
func NewGetMyAccessUseCase(roles role.Repository) *GetMyAccessUseCase {
|
||||
return &GetMyAccessUseCase{roles: roles}
|
||||
}
|
||||
|
||||
var legacyBuiltInNames = map[user.Role]string{
|
||||
user.RoleOwner: role.OwnerName,
|
||||
user.RoleAdmin: role.AdminName,
|
||||
}
|
||||
|
||||
func (u *GetMyAccessUseCase) resolveRole(account user.User) (role.Role, error) {
|
||||
if account.RoleID != 0 {
|
||||
return u.roles.Get(account.RoleID)
|
||||
}
|
||||
name, known := legacyBuiltInNames[account.Role]
|
||||
if !known {
|
||||
return role.Role{}, role.ErrRoleNotFound
|
||||
}
|
||||
found, err := u.roles.GetByName(name)
|
||||
if err != nil {
|
||||
return role.Role{}, err
|
||||
}
|
||||
if !found.BuiltIn {
|
||||
return role.Role{}, role.ErrRoleNotFound
|
||||
}
|
||||
return found, nil
|
||||
}
|
||||
|
||||
func (u *GetMyAccessUseCase) Get(account user.User) (Access, error) {
|
||||
resolved, err := u.resolveRole(account)
|
||||
if err != nil {
|
||||
return Access{}, err
|
||||
}
|
||||
granted, err := u.roles.ListGrants(resolved.ID)
|
||||
if err != nil {
|
||||
return Access{}, err
|
||||
}
|
||||
return Access{Role: resolved, Permissions: orderedGrants(granted)}, nil
|
||||
}
|
||||
|
||||
func orderedGrants(granted []role.Grant) []role.Grant {
|
||||
scopes := make(map[string]role.Scope, len(granted))
|
||||
for _, grant := range granted {
|
||||
scopes[grant.Key] = grant.Scope
|
||||
}
|
||||
ordered := []role.Grant{}
|
||||
for _, permission := range role.Catalogue() {
|
||||
scope, present := scopes[permission.Key]
|
||||
if present && scope != role.ScopeNone {
|
||||
ordered = append(ordered, role.Grant{Key: permission.Key, Scope: scope})
|
||||
}
|
||||
}
|
||||
return ordered
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package useCases
|
||||
|
||||
import "RenewCMS/internal/domain/role"
|
||||
|
||||
type PermissionGroup struct {
|
||||
Key string
|
||||
Items []role.Permission
|
||||
}
|
||||
|
||||
type ListPermissionsUseCase struct{}
|
||||
|
||||
func NewListPermissionsUseCase() *ListPermissionsUseCase {
|
||||
return &ListPermissionsUseCase{}
|
||||
}
|
||||
|
||||
func (u *ListPermissionsUseCase) List() []PermissionGroup {
|
||||
groups := []PermissionGroup{}
|
||||
for _, permission := range role.Catalogue() {
|
||||
last := len(groups) - 1
|
||||
if last < 0 || groups[last].Key != permission.Group {
|
||||
groups = append(groups, PermissionGroup{Key: permission.Group})
|
||||
last++
|
||||
}
|
||||
groups[last].Items = append(groups[last].Items, permission)
|
||||
}
|
||||
return groups
|
||||
}
|
||||
Reference in new issue
Block a user