Files
jiang13-bbs/backend/handler/admin_user.go
freefire a7b6421840 feat: 实现完整的板块级RBAC内容审核系统
- 新增站长/超级管理员/管理员/板块管理员四级角色体系
- 实现实时权限快照加载与细粒度权限校验
- 新增内容审核队列与前后端页面
- 重构用户权限管理与站点管理逻辑
- 新增评论/帖子审核状态与通知推送
- 优化前端权限控制与角色徽章展示
- 修正数据库迁移与默认值问题
2026-09-15 04:47:34 +08:00

136 lines
3.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package handler
import (
"errors"
"net/http"
"strconv"
"strings"
"github.com/freefire/jiang13-bbs/middleware"
"github.com/freefire/jiang13-bbs/model"
"github.com/freefire/jiang13-bbs/service"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// ===== 用户管理(RequireStaff + PermUsers 兜底,前端不做权限判定) =====
// AdminListUsers 用户分页列表(搜索/角色/状态筛选 + 全站汇总)
func (h *Handlers) AdminListUsers(c *gin.Context) {
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
size, _ := strconv.Atoi(c.DefaultQuery("size", "20"))
res, err := h.AdminUser.List(service.AdminUserListQuery{
Page: page,
Size: size,
Keyword: strings.TrimSpace(c.Query("q")),
Role: c.Query("role"),
Status: c.Query("status"),
})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "获取用户列表失败"})
return
}
c.JSON(http.StatusOK, gin.H{
"users": res.Users,
"total": res.Total,
"page": res.Page,
"size": res.Size,
"summary": res.Summary,
})
}
// adminStaffBody 角色与板块授权变更请求体
type adminStaffBody struct {
Role string `json:"role"`
BoardIDs []uint `json:"board_ids"`
}
type adminUserBanBody struct {
Banned bool `json:"banned"`
}
// AdminUpdateUserRole 设置管理角色与板块授权
func (h *Handlers) AdminUpdateUserRole(c *gin.Context) {
id, ok := parseAdminUserID(c)
if !ok {
return
}
var body adminStaffBody
if err := c.ShouldBindJSON(&body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数无效"})
return
}
user, err := h.AdminUser.SetStaff(
middleware.CurrentActor(c), id, model.Role(body.Role), body.BoardIDs,
)
if err != nil {
respondAdminUserError(c, err)
return
}
c.JSON(http.StatusOK, gin.H{"user": user})
}
// AdminSetUserBan 封禁 / 解封用户
func (h *Handlers) AdminSetUserBan(c *gin.Context) {
id, ok := parseAdminUserID(c)
if !ok {
return
}
var body adminUserBanBody
if err := c.ShouldBindJSON(&body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数无效"})
return
}
user, err := h.AdminUser.SetBanned(middleware.CurrentActor(c), id, body.Banned)
if err != nil {
respondAdminUserError(c, err)
return
}
c.JSON(http.StatusOK, gin.H{"user": user})
}
// AdminUserLoginLogs 某用户的登录历史(IP/UA/成败)
func (h *Handlers) AdminUserLoginLogs(c *gin.Context) {
id, ok := parseAdminUserID(c)
if !ok {
return
}
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
logs, total, err := h.AdminUser.ListLoginLogs(id, page, 15)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "获取登录历史失败"})
return
}
c.JSON(http.StatusOK, gin.H{"logs": logs, "total": total, "page": page})
}
// parseAdminUserID 解析路径中的用户 ID,失败时直接写出 400
func parseAdminUserID(c *gin.Context) (uint, bool) {
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil || id == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的用户 ID"})
return 0, false
}
return uint(id), true
}
// respondAdminUserError 统一映射用户管理业务错误到 HTTP 状态码
func respondAdminUserError(c *gin.Context, err error) {
switch {
case errors.Is(err, gorm.ErrRecordNotFound):
c.JSON(http.StatusNotFound, gin.H{"error": "用户不存在"})
case errors.Is(err, service.ErrProtectedOwner):
// 站长保护属于权限边界而非参数错误
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
case errors.Is(err, service.ErrAdminSelfAction),
errors.Is(err, service.ErrInvalidRole),
errors.Is(err, service.ErrCannotAssignRole),
errors.Is(err, service.ErrBoardRequired),
errors.Is(err, service.ErrBoardNotFound):
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
default:
c.JSON(http.StatusInternalServerError, gin.H{"error": "操作失败"})
}
}