feat: roles system backend
This commit is contained in:
1 parent
44cca46d5e
commit
8c6c6456da
50 files changed
+2578
-22
No files matched your search
@@ -5,6 +5,7 @@ import (
|
||||
"RenewCMS/api/pkg/claims"
|
||||
"RenewCMS/api/pkg/publicuser"
|
||||
"RenewCMS/internal/domain/author"
|
||||
"RenewCMS/internal/domain/role"
|
||||
"RenewCMS/internal/domain/user"
|
||||
"RenewCMS/internal/infrastructure/useCases"
|
||||
"encoding/json"
|
||||
@@ -15,12 +16,14 @@ import (
|
||||
type MeHandler struct {
|
||||
GetUseCase *useCases.GetUserUseCase
|
||||
GetAuthorUseCase *useCases.GetMyAuthorUseCase
|
||||
GetAccessUseCase *useCases.GetMyAccessUseCase
|
||||
}
|
||||
|
||||
func NewMeHandler(getUser *useCases.GetUserUseCase, getAuthor *useCases.GetMyAuthorUseCase) *MeHandler {
|
||||
func NewMeHandler(getUser *useCases.GetUserUseCase, getAuthor *useCases.GetMyAuthorUseCase, getAccess *useCases.GetMyAccessUseCase) *MeHandler {
|
||||
return &MeHandler{
|
||||
GetUseCase: getUser,
|
||||
GetAuthorUseCase: getAuthor,
|
||||
GetAccessUseCase: getAccess,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,9 +32,29 @@ type meAuthor struct {
|
||||
ArticleCount int `json:"article_count"`
|
||||
}
|
||||
|
||||
type meRole struct {
|
||||
ID uint32 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
BuiltIn bool `json:"built_in"`
|
||||
Locked bool `json:"locked"`
|
||||
}
|
||||
|
||||
type meAccess struct {
|
||||
Role meRole `json:"role"`
|
||||
Permissions []role.Grant `json:"permissions"`
|
||||
}
|
||||
|
||||
type meResponse struct {
|
||||
publicuser.PublicUser
|
||||
Author *meAuthor `json:"author"`
|
||||
Access meAccess `json:"access"`
|
||||
}
|
||||
|
||||
func toMeAccess(access useCases.Access) meAccess {
|
||||
return meAccess{
|
||||
Role: meRole{ID: access.Role.ID, Name: access.Role.Name, BuiltIn: access.Role.BuiltIn, Locked: access.Role.Locked},
|
||||
Permissions: access.Permissions,
|
||||
}
|
||||
}
|
||||
|
||||
func toMeAuthor(found *author.Author) *meAuthor {
|
||||
@@ -64,7 +87,13 @@ func (h *MeHandler) Me(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
body, err := json.Marshal(meResponse{PublicUser: publicuser.New(loadedUser), Author: toMeAuthor(linked)})
|
||||
access, err := h.GetAccessUseCase.Get(loadedUser)
|
||||
if err != nil {
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
body, err := json.Marshal(meResponse{PublicUser: publicuser.New(loadedUser), Author: toMeAuthor(linked), Access: toMeAccess(access)})
|
||||
if err != nil {
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package permission
|
||||
|
||||
import (
|
||||
"RenewCMS/internal/domain/role"
|
||||
"RenewCMS/internal/infrastructure/useCases"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
ListUseCase *useCases.ListPermissionsUseCase
|
||||
}
|
||||
|
||||
func NewHandler(list *useCases.ListPermissionsUseCase) *Handler {
|
||||
return &Handler{ListUseCase: list}
|
||||
}
|
||||
|
||||
type permissionItem struct {
|
||||
Key string `json:"key"`
|
||||
ScopeKind role.Kind `json:"scope_kind"`
|
||||
OwnerOnly bool `json:"owner_only"`
|
||||
PerType bool `json:"per_type"`
|
||||
}
|
||||
|
||||
type permissionGroup struct {
|
||||
Key string `json:"key"`
|
||||
Items []permissionItem `json:"items"`
|
||||
}
|
||||
|
||||
type permissionResponse struct {
|
||||
Groups []permissionGroup `json:"groups"`
|
||||
}
|
||||
|
||||
func toResponse(groups []useCases.PermissionGroup) permissionResponse {
|
||||
response := permissionResponse{Groups: make([]permissionGroup, 0, len(groups))}
|
||||
for _, group := range groups {
|
||||
items := make([]permissionItem, 0, len(group.Items))
|
||||
for _, permission := range group.Items {
|
||||
items = append(items, permissionItem{Key: permission.Key, ScopeKind: permission.Kind, OwnerOnly: permission.OwnerOnly, PerType: permission.PerType()})
|
||||
}
|
||||
response.Groups = append(response.Groups, permissionGroup{Key: group.Key, Items: items})
|
||||
}
|
||||
return response
|
||||
}
|
||||
|
||||
func (h *Handler) List(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := json.Marshal(toResponse(h.ListUseCase.List()))
|
||||
if err != nil {
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
_, _ = w.Write(body)
|
||||
}
|
||||
Reference in new issue
Block a user