feat: get all roles endpoint
This commit is contained in:
61
internal/admin/roles.go
Normal file
61
internal/admin/roles.go
Normal file
@ -0,0 +1,61 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"gitea.local/admin/hspguard/internal/repository"
|
||||
"gitea.local/admin/hspguard/internal/web"
|
||||
)
|
||||
|
||||
func (h *AdminHandler) GetAllRoles(w http.ResponseWriter, r *http.Request) {
|
||||
rows, err := h.repo.GetRolesGroupedWithPermissions(r.Context())
|
||||
if err != nil {
|
||||
log.Println("ERR: Failed to list roles from db:", err)
|
||||
web.Error(w, "failed to get all roles", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
type RolePermissions struct {
|
||||
Permissions []repository.Permission `json:"permissions"`
|
||||
repository.Role
|
||||
}
|
||||
|
||||
type RowDTO struct {
|
||||
Scope string `json:"scope"`
|
||||
Roles []RolePermissions `json:"roles"`
|
||||
}
|
||||
|
||||
if len(rows) == 0 {
|
||||
rows = make([]repository.GetRolesGroupedWithPermissionsRow, 0)
|
||||
}
|
||||
|
||||
var mapped []RowDTO
|
||||
|
||||
for _, row := range rows {
|
||||
var mappedRow RowDTO
|
||||
|
||||
mappedRow.Scope = row.Scope
|
||||
|
||||
if err := json.Unmarshal(row.Roles, &mappedRow.Roles); err != nil {
|
||||
log.Println("ERR: Failed to extract roles from byte array:", err)
|
||||
web.Error(w, "failed to get roles", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
mapped = append(mapped, mappedRow)
|
||||
}
|
||||
|
||||
if len(mapped) == 0 {
|
||||
mapped = make([]RowDTO, 0)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
@ -44,6 +44,8 @@ func (h *AdminHandler) RegisterRoutes(router chi.Router) {
|
||||
|
||||
r.Get("/permissions", h.GetAllPermissions)
|
||||
r.Get("/permissions/{user_id}", h.GetUserPermissions)
|
||||
|
||||
r.Get("/roles", h.GetAllRoles)
|
||||
})
|
||||
|
||||
router.Get("/api-services/client/{client_id}", h.GetApiServiceCID)
|
||||
|
Reference in New Issue
Block a user