feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2026-04-24 07:30:18 +08:00
|
|
|
|
"encoding/json"
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
"net/http"
|
|
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/user-management-system/internal/domain"
|
|
|
|
|
|
"github.com/user-management-system/internal/service"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// PermissionHandler handles permission management requests
|
|
|
|
|
|
type PermissionHandler struct {
|
|
|
|
|
|
permissionService *service.PermissionService
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewPermissionHandler creates a new PermissionHandler
|
|
|
|
|
|
func NewPermissionHandler(permissionService *service.PermissionService) *PermissionHandler {
|
|
|
|
|
|
return &PermissionHandler{permissionService: permissionService}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-11 21:23:52 +08:00
|
|
|
|
// CreatePermission 创建权限
|
|
|
|
|
|
// @Summary 创建权限
|
|
|
|
|
|
// @Description 创建新的权限定义(仅管理员)
|
|
|
|
|
|
// @Tags 权限管理
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Security BearerAuth
|
|
|
|
|
|
// @Param request body service.CreatePermissionRequest true "权限信息"
|
|
|
|
|
|
// @Success 201 {object} Response{data=domain.Permission} "创建成功"
|
|
|
|
|
|
// @Failure 400 {object} Response "请求参数错误"
|
|
|
|
|
|
// @Failure 403 {object} Response "无权限"
|
|
|
|
|
|
// @Router /api/v1/permissions [post]
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
func (h *PermissionHandler) CreatePermission(c *gin.Context) {
|
2026-04-24 07:30:18 +08:00
|
|
|
|
var req struct {
|
|
|
|
|
|
Name string `json:"name" binding:"required"`
|
|
|
|
|
|
Code string `json:"code" binding:"required"`
|
|
|
|
|
|
Type *int `json:"type" binding:"required"`
|
|
|
|
|
|
Description string `json:"description"`
|
|
|
|
|
|
ParentID *int64 `json:"parent_id"`
|
|
|
|
|
|
Path string `json:"path"`
|
|
|
|
|
|
Method string `json:"method"`
|
|
|
|
|
|
Sort int `json:"sort"`
|
|
|
|
|
|
Icon string `json:"icon"`
|
|
|
|
|
|
}
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
2026-04-11 13:12:27 +08:00
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-24 07:30:18 +08:00
|
|
|
|
if req.Type == nil || *req.Type < 0 || *req.Type > 2 {
|
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid permission type"})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
serviceReq := service.CreatePermissionRequest{
|
|
|
|
|
|
Name: req.Name,
|
|
|
|
|
|
Code: req.Code,
|
|
|
|
|
|
Type: *req.Type,
|
|
|
|
|
|
Description: req.Description,
|
|
|
|
|
|
ParentID: req.ParentID,
|
|
|
|
|
|
Path: req.Path,
|
|
|
|
|
|
Method: req.Method,
|
|
|
|
|
|
Sort: req.Sort,
|
|
|
|
|
|
Icon: req.Icon,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
perm, err := h.permissionService.CreatePermission(c.Request.Context(), &serviceReq)
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
handleError(c, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-08 20:06:54 +08:00
|
|
|
|
c.JSON(http.StatusCreated, gin.H{
|
|
|
|
|
|
"code": 0,
|
|
|
|
|
|
"message": "success",
|
|
|
|
|
|
"data": perm,
|
|
|
|
|
|
})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-11 21:23:52 +08:00
|
|
|
|
// ListPermissions 获取权限列表
|
|
|
|
|
|
// @Summary 获取权限列表
|
|
|
|
|
|
// @Description 获取系统权限列表
|
|
|
|
|
|
// @Tags 权限管理
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Security BearerAuth
|
|
|
|
|
|
// @Success 200 {object} Response{data=[]domain.Permission} "权限列表"
|
|
|
|
|
|
// @Router /api/v1/permissions [get]
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
func (h *PermissionHandler) ListPermissions(c *gin.Context) {
|
|
|
|
|
|
var req service.ListPermissionRequest
|
|
|
|
|
|
if err := c.ShouldBindQuery(&req); err != nil {
|
2026-04-11 13:12:27 +08:00
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-08 20:06:54 +08:00
|
|
|
|
perms, _, err := h.permissionService.ListPermissions(c.Request.Context(), &req)
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
handleError(c, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
2026-04-08 20:06:54 +08:00
|
|
|
|
"code": 0,
|
|
|
|
|
|
"message": "success",
|
|
|
|
|
|
"data": perms,
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-11 21:23:52 +08:00
|
|
|
|
// GetPermission 获取权限详情
|
|
|
|
|
|
// @Summary 获取权限详情
|
|
|
|
|
|
// @Description 根据ID获取权限详细信息
|
|
|
|
|
|
// @Tags 权限管理
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Security BearerAuth
|
|
|
|
|
|
// @Param id path int true "权限ID"
|
|
|
|
|
|
// @Success 200 {object} Response{data=domain.Permission} "权限信息"
|
|
|
|
|
|
// @Failure 404 {object} Response "权限不存在"
|
|
|
|
|
|
// @Router /api/v1/permissions/{id} [get]
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
func (h *PermissionHandler) GetPermission(c *gin.Context) {
|
|
|
|
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
|
|
|
|
if err != nil {
|
2026-04-11 13:12:27 +08:00
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid permission id"})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
perm, err := h.permissionService.GetPermission(c.Request.Context(), id)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
handleError(c, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-08 20:06:54 +08:00
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"code": 0,
|
|
|
|
|
|
"message": "success",
|
|
|
|
|
|
"data": perm,
|
|
|
|
|
|
})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-11 21:23:52 +08:00
|
|
|
|
// UpdatePermission 更新权限
|
|
|
|
|
|
// @Summary 更新权限
|
|
|
|
|
|
// @Description 更新权限信息(仅管理员)
|
|
|
|
|
|
// @Tags 权限管理
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Security BearerAuth
|
|
|
|
|
|
// @Param id path int true "权限ID"
|
|
|
|
|
|
// @Param request body service.UpdatePermissionRequest true "更新信息"
|
|
|
|
|
|
// @Success 200 {object} Response{data=domain.Permission} "更新成功"
|
|
|
|
|
|
// @Failure 400 {object} Response "请求参数错误"
|
|
|
|
|
|
// @Failure 403 {object} Response "无权限"
|
|
|
|
|
|
// @Failure 404 {object} Response "权限不存在"
|
|
|
|
|
|
// @Router /api/v1/permissions/{id} [put]
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
func (h *PermissionHandler) UpdatePermission(c *gin.Context) {
|
|
|
|
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
|
|
|
|
if err != nil {
|
2026-04-11 13:12:27 +08:00
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid permission id"})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var req service.UpdatePermissionRequest
|
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
2026-04-11 13:12:27 +08:00
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
perm, err := h.permissionService.UpdatePermission(c.Request.Context(), id, &req)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
handleError(c, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-08 20:06:54 +08:00
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"code": 0,
|
|
|
|
|
|
"message": "success",
|
|
|
|
|
|
"data": perm,
|
|
|
|
|
|
})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-11 21:23:52 +08:00
|
|
|
|
// DeletePermission 删除权限
|
|
|
|
|
|
// @Summary 删除权限
|
|
|
|
|
|
// @Description 删除权限定义(仅管理员)
|
|
|
|
|
|
// @Tags 权限管理
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Security BearerAuth
|
|
|
|
|
|
// @Param id path int true "权限ID"
|
|
|
|
|
|
// @Success 200 {object} Response "删除成功"
|
|
|
|
|
|
// @Failure 403 {object} Response "无权限"
|
|
|
|
|
|
// @Failure 404 {object} Response "权限不存在"
|
|
|
|
|
|
// @Router /api/v1/permissions/{id} [delete]
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
func (h *PermissionHandler) DeletePermission(c *gin.Context) {
|
|
|
|
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
|
|
|
|
if err != nil {
|
2026-04-11 13:12:27 +08:00
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid permission id"})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := h.permissionService.DeletePermission(c.Request.Context(), id); err != nil {
|
|
|
|
|
|
handleError(c, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-08 20:06:54 +08:00
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"code": 0,
|
|
|
|
|
|
"message": "permission deleted",
|
|
|
|
|
|
})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-11 21:23:52 +08:00
|
|
|
|
// UpdatePermissionStatus 更新权限状态
|
|
|
|
|
|
// @Summary 更新权限状态
|
|
|
|
|
|
// @Description 更新权限状态(enabled/disabled)(仅管理员)
|
|
|
|
|
|
// @Tags 权限管理
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Security BearerAuth
|
|
|
|
|
|
// @Param id path int true "权限ID"
|
|
|
|
|
|
// @Param request body UpdatePermissionStatusRequest true "状态信息"
|
|
|
|
|
|
// @Success 200 {object} Response "状态更新成功"
|
|
|
|
|
|
// @Failure 400 {object} Response "无效的状态值"
|
|
|
|
|
|
// @Failure 403 {object} Response "无权限"
|
|
|
|
|
|
// @Failure 404 {object} Response "权限不存在"
|
|
|
|
|
|
// @Router /api/v1/permissions/{id}/status [put]
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
func (h *PermissionHandler) UpdatePermissionStatus(c *gin.Context) {
|
|
|
|
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
|
|
|
|
if err != nil {
|
2026-04-11 13:12:27 +08:00
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid permission id"})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var req struct {
|
2026-04-24 07:30:18 +08:00
|
|
|
|
Status json.RawMessage `json:"status" binding:"required"`
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
2026-04-11 13:12:27 +08:00
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-24 07:30:18 +08:00
|
|
|
|
status, ok := parsePermissionStatus(req.Status)
|
|
|
|
|
|
if !ok {
|
2026-04-11 13:12:27 +08:00
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid status"})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := h.permissionService.UpdatePermissionStatus(c.Request.Context(), id, status); err != nil {
|
|
|
|
|
|
handleError(c, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-08 20:06:54 +08:00
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"code": 0,
|
|
|
|
|
|
"message": "status updated",
|
|
|
|
|
|
})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-11 21:23:52 +08:00
|
|
|
|
// GetPermissionTree 获取权限树
|
|
|
|
|
|
// @Summary 获取权限树
|
|
|
|
|
|
// @Description 获取系统权限的树形结构
|
|
|
|
|
|
// @Tags 权限管理
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Security BearerAuth
|
|
|
|
|
|
// @Success 200 {object} Response{data=[]domain.Permission} "权限树"
|
|
|
|
|
|
// @Router /api/v1/permissions/tree [get]
|
2026-04-24 07:30:18 +08:00
|
|
|
|
func parsePermissionStatus(raw json.RawMessage) (domain.PermissionStatus, bool) {
|
|
|
|
|
|
var statusText string
|
|
|
|
|
|
if err := json.Unmarshal(raw, &statusText); err == nil {
|
|
|
|
|
|
switch statusText {
|
|
|
|
|
|
case "enabled", "1":
|
|
|
|
|
|
return domain.PermissionStatusEnabled, true
|
|
|
|
|
|
case "disabled", "0":
|
|
|
|
|
|
return domain.PermissionStatusDisabled, true
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var statusNumber int
|
|
|
|
|
|
if err := json.Unmarshal(raw, &statusNumber); err == nil {
|
|
|
|
|
|
switch statusNumber {
|
|
|
|
|
|
case 1:
|
|
|
|
|
|
return domain.PermissionStatusEnabled, true
|
|
|
|
|
|
case 0:
|
|
|
|
|
|
return domain.PermissionStatusDisabled, true
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return domain.PermissionStatusDisabled, false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
func (h *PermissionHandler) GetPermissionTree(c *gin.Context) {
|
|
|
|
|
|
tree, err := h.permissionService.GetPermissionTree(c.Request.Context())
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
handleError(c, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-08 20:06:54 +08:00
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"code": 0,
|
|
|
|
|
|
"message": "success",
|
|
|
|
|
|
"data": tree,
|
|
|
|
|
|
})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
}
|