Spaces:
Sleeping
Sleeping
File size: 9,866 Bytes
f2b94bf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | package controllers
import (
"strconv"
"abdanhafidz.com/go-boilerplate/models/dto"
entity "abdanhafidz.com/go-boilerplate/models/entity"
http_error "abdanhafidz.com/go-boilerplate/models/error"
"abdanhafidz.com/go-boilerplate/services"
"github.com/gin-gonic/gin"
)
type SuperAdminRoleController interface {
ListRoles(ctx *gin.Context)
CreateRole(ctx *gin.Context)
UpdateRole(ctx *gin.Context)
DeleteRole(ctx *gin.Context)
ListAuditLogs(ctx *gin.Context)
}
type superAdminRoleController struct {
accountService services.AccountService
}
func NewSuperAdminRoleController(accountService services.AccountService) SuperAdminRoleController {
return &superAdminRoleController{accountService: accountService}
}
// ListRoles godoc
// @Summary List All Roles
// @Description Get all system and named custom roles with their privilege flags
// @Tags Super Admin Roles
// @Accept json
// @Produce json
// @Security BearerAuth
// @Success 200 {object} dto.SuccessResponse[[]dto.RoleResponse]
// @Failure 401 {object} dto.ErrorResponse
// @Failure 403 {object} dto.ErrorResponse
// @Router /api/v1/super-admin/roles [get]
func (c *superAdminRoleController) ListRoles(ctx *gin.Context) {
roles, err := c.accountService.GetAllRoles(ctx.Request.Context())
if err != nil {
ResponseJSON(ctx, gin.H{}, []dto.RoleResponse{}, err)
return
}
var res []dto.RoleResponse
for _, r := range roles {
res = append(res, dto.RoleResponse{
Id: r.Id,
Name: r.Name,
IsSystem: r.IsSystem,
EventManagement: r.EventManagement,
ExamManagement: r.ExamManagement,
AcademyManagement: r.AcademyManagement,
ContentManagement: r.ContentManagement,
})
}
ResponseJSON(ctx, gin.H{}, res, nil)
}
// CreateRole godoc
// @Summary Create Named Role
// @Description Create a new named custom role with privilege flags. Superadmin only.
// @Tags Super Admin Roles
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param request body dto.CreateRoleRequest true "Create Role Request"
// @Success 200 {object} dto.SuccessResponse[dto.RoleResponse]
// @Failure 400 {object} dto.ErrorResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 403 {object} dto.ErrorResponse
// @Failure 409 {object} dto.ErrorResponse
// @Router /api/v1/super-admin/roles [post]
func (c *superAdminRoleController) CreateRole(ctx *gin.Context) {
req := RequestJSON[dto.CreateRoleRequest](ctx)
role := entity.Role{
Name: req.Name,
IsSystem: false,
EventManagement: req.EventManagement,
ExamManagement: req.ExamManagement,
AcademyManagement: req.AcademyManagement,
ContentManagement: req.ContentManagement,
}
created, err := c.accountService.CreateRole(ctx.Request.Context(), role)
if err != nil {
ResponseJSON(ctx, req, dto.RoleResponse{}, err)
return
}
callerID := ParseAccountId(ctx)
roleIDStr := created.Id.String()
detailStr := `{"role_name":"` + created.Name + `"}`
_ = c.accountService.WriteAuditLog(ctx.Request.Context(), callerID, "role_created", &roleIDStr, &detailStr)
res := dto.RoleResponse{
Id: created.Id,
Name: created.Name,
IsSystem: created.IsSystem,
EventManagement: created.EventManagement,
ExamManagement: created.ExamManagement,
AcademyManagement: created.AcademyManagement,
ContentManagement: created.ContentManagement,
}
ResponseJSON(ctx, req, res, nil)
}
// UpdateRole godoc
// @Summary Update Named Role
// @Description Update a named custom role's privilege flags. System roles are immutable.
// @Tags Super Admin Roles
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param role_id path string true "Role ID"
// @Param request body dto.UpdateRoleRequest true "Update Role Request"
// @Success 200 {object} dto.SuccessResponse[dto.RoleResponse]
// @Failure 400 {object} dto.ErrorResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 403 {object} dto.ErrorResponse
// @Router /api/v1/super-admin/roles/{role_id} [put]
func (c *superAdminRoleController) UpdateRole(ctx *gin.Context) {
roleID := ParseUUID(ctx, "role_id")
req := RequestJSON[dto.UpdateRoleRequest](ctx)
existing, err := c.accountService.GetRoleByID(ctx.Request.Context(), roleID)
if err != nil {
ResponseJSON(ctx, req, dto.RoleResponse{}, err)
return
}
if existing.IsSystem {
ResponseJSON(ctx, req, dto.RoleResponse{}, http_error.ROLE_IMMUTABLE)
return
}
if req.Name != nil {
existing.Name = *req.Name
}
if req.EventManagement != nil {
existing.EventManagement = *req.EventManagement
}
if req.ExamManagement != nil {
existing.ExamManagement = *req.ExamManagement
}
if req.AcademyManagement != nil {
existing.AcademyManagement = *req.AcademyManagement
}
if req.ContentManagement != nil {
existing.ContentManagement = *req.ContentManagement
}
updated, err := c.accountService.UpdateRole(ctx.Request.Context(), existing)
if err != nil {
ResponseJSON(ctx, req, dto.RoleResponse{}, err)
return
}
callerID := ParseAccountId(ctx)
roleIDStr := updated.Id.String()
detailStr := `{"role_name":"` + updated.Name + `"}`
_ = c.accountService.WriteAuditLog(ctx.Request.Context(), callerID, "role_updated", &roleIDStr, &detailStr)
res := dto.RoleResponse{
Id: updated.Id,
Name: updated.Name,
IsSystem: updated.IsSystem,
EventManagement: updated.EventManagement,
ExamManagement: updated.ExamManagement,
AcademyManagement: updated.AcademyManagement,
ContentManagement: updated.ContentManagement,
}
ResponseJSON(ctx, req, res, nil)
}
// DeleteRole godoc
// @Summary Delete Named Role
// @Description Delete a named custom role. System roles are immutable. Cannot delete if assigned to any account.
// @Tags Super Admin Roles
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param role_id path string true "Role ID"
// @Success 200 {object} dto.SuccessResponse[any]
// @Failure 401 {object} dto.ErrorResponse
// @Failure 403 {object} dto.ErrorResponse
// @Failure 409 {object} dto.ErrorResponse
// @Router /api/v1/super-admin/roles/{role_id} [delete]
func (c *superAdminRoleController) DeleteRole(ctx *gin.Context) {
roleID := ParseUUID(ctx, "role_id")
existing, err := c.accountService.GetRoleByID(ctx.Request.Context(), roleID)
if err != nil {
ResponseJSON[any](ctx, gin.H{}, nil, err)
return
}
if existing.IsSystem {
ResponseJSON[any](ctx, gin.H{}, nil, http_error.ROLE_IMMUTABLE)
return
}
inUse, err := c.accountService.CountRoleAssignments(ctx.Request.Context(), roleID)
if err != nil {
ResponseJSON[any](ctx, gin.H{}, nil, err)
return
}
if inUse > 0 {
ResponseJSON[any](ctx, gin.H{}, nil, http_error.ROLE_IN_USE)
return
}
if err := c.accountService.DeleteRole(ctx.Request.Context(), roleID); err != nil {
ResponseJSON[any](ctx, gin.H{}, nil, err)
return
}
callerID := ParseAccountId(ctx)
roleIDStr := roleID.String()
detailStr := `{"role_name":"` + existing.Name + `"}`
_ = c.accountService.WriteAuditLog(ctx.Request.Context(), callerID, "role_deleted", &roleIDStr, &detailStr)
ResponseJSON(ctx, gin.H{}, gin.H{"message": "Role deleted successfully"}, nil)
}
// ListAuditLogs godoc
// @Summary List Audit Logs
// @Description Get paginated audit logs with optional filters. Superadmin only.
// @Tags Super Admin Audit
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param page query int false "Page number (default 1)"
// @Param limit query int false "Items per page (default 10, max 50)"
// @Param action query string false "Filter by action"
// @Param actor_account_id query string false "Filter by actor account ID"
// @Param from query string false "Start time (RFC3339)"
// @Param to query string false "End time (RFC3339)"
// @Success 200 {object} dto.SuccessResponse[[]dto.AuditLogResponse]
// @Failure 401 {object} dto.ErrorResponse
// @Failure 403 {object} dto.ErrorResponse
// @Router /api/v1/super-admin/logs [get]
func (c *superAdminRoleController) ListAuditLogs(ctx *gin.Context) {
limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10"))
page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
action := ctx.DefaultQuery("action", "")
actorID := ctx.DefaultQuery("actor_account_id", "")
from := ctx.DefaultQuery("from", "")
to := ctx.DefaultQuery("to", "")
if limit < 1 {
limit = 10
} else if limit > 50 {
limit = 50
}
if page < 1 {
page = 1
}
logs, total, err := c.accountService.GetAuditLogs(ctx.Request.Context(), page, limit, action, actorID, from, to)
if err != nil {
ResponseJSON(ctx, gin.H{}, []dto.AuditLogResponse{}, err)
return
}
var res []dto.AuditLogResponse
for _, l := range logs {
res = append(res, dto.AuditLogResponse{
Id: l.Id,
ActorAccountID: l.ActorAccountID,
Action: l.Action,
TargetID: l.TargetID,
Detail: l.Detail,
CreatedAt: l.CreatedAt.Format("2006-01-02T15:04:05Z07:00"),
})
}
var totalPages int
if total == 0 {
totalPages = 1
} else {
totalPages = int((total + int64(limit) - 1) / int64(limit))
}
if page > totalPages {
page = totalPages
}
meta := gin.H{
"totalItems": total,
"totalPages": totalPages,
"currentPage": page,
"limit": limit,
}
ResponseJSON(ctx, meta, res, nil)
}
|