feat: admin fetch all permissions
This commit is contained in:
@ -11,7 +11,55 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func (h *AdminHandler) GetPermissions(w http.ResponseWriter, r *http.Request) {
|
||||
func (h *AdminHandler) GetAllPermissions(w http.ResponseWriter, r *http.Request) {
|
||||
rows, err := h.repo.GetGroupedPermissions(r.Context())
|
||||
if err != nil {
|
||||
log.Println("ERR: Failed to list permissions from db:", err)
|
||||
web.Error(w, "failed to get all permissions", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
type RowDTO struct {
|
||||
Scope string `json:"scope"`
|
||||
Permissions []repository.Permission `json:"permissions"`
|
||||
}
|
||||
|
||||
if len(rows) == 0 {
|
||||
rows = make([]repository.GetGroupedPermissionsRow, 0)
|
||||
}
|
||||
|
||||
var mapped []RowDTO
|
||||
|
||||
for _, row := range rows {
|
||||
var permissions []repository.Permission
|
||||
|
||||
if err := json.Unmarshal(row.Permissions, &permissions); err != nil {
|
||||
log.Println("ERR: Failed to extract permissions from byte array:", err)
|
||||
web.Error(w, "failed to get permissions", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
log.Println("DEBUG: Appending row dto:", RowDTO{
|
||||
Scope: row.Scope,
|
||||
Permissions: permissions,
|
||||
})
|
||||
|
||||
mapped = append(mapped, RowDTO{
|
||||
Scope: row.Scope,
|
||||
Permissions: permissions,
|
||||
})
|
||||
}
|
||||
|
||||
encoder := json.NewEncoder(w)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
if err := encoder.Encode(mapped); err != nil {
|
||||
web.Error(w, "failed to encode response", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *AdminHandler) GetUserPermissions(w http.ResponseWriter, r *http.Request) {
|
||||
userId := chi.URLParam(r, "user_id")
|
||||
|
||||
permissions, err := h.repo.GetUserPermissions(r.Context(), uuid.MustParse(userId))
|
||||
|
@ -42,7 +42,8 @@ func (h *AdminHandler) RegisterRoutes(router chi.Router) {
|
||||
r.Get("/service-sessions", h.GetServiceSessions)
|
||||
r.Patch("/service-sessions/revoke/{id}", h.RevokeUserSession)
|
||||
|
||||
r.Get("/permissions/{user_id}", h.GetPermissions)
|
||||
r.Get("/permissions", h.GetAllPermissions)
|
||||
r.Get("/permissions/{user_id}", h.GetUserPermissions)
|
||||
})
|
||||
|
||||
router.Get("/api-services/client/{client_id}", h.GetApiServiceCID)
|
||||
|
Reference in New Issue
Block a user