feat: 帖子分级置顶(板块/全站)与板块管理员置顶权限、消息管理权限调整
This commit is contained in:
@@ -164,7 +164,7 @@ func (h *Handlers) Register(c *gin.Context) {
|
||||
})
|
||||
return
|
||||
}
|
||||
setAuthCookies(c, accessToken, refreshToken, !h.Cfg.DevMode)
|
||||
setAuthCookies(c, accessToken, refreshToken, h.Cfg.IsSecureOrigin())
|
||||
h.AdminUser.RecordLogin(loginUser.ID, req.Username, c.ClientIP(), c.Request.UserAgent(), true)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"user": gin.H{
|
||||
@@ -212,7 +212,7 @@ func (h *Handlers) Login(c *gin.Context) {
|
||||
h.AdminUser.RecordLogin(user.ID, req.Username, c.ClientIP(), c.Request.UserAgent(), true)
|
||||
// dev 模式不设 Secure,生产环境需 HTTPS
|
||||
h.Ops.ClearFailure(req.Username)
|
||||
setAuthCookies(c, accessToken, refreshToken, !h.Cfg.DevMode)
|
||||
setAuthCookies(c, accessToken, refreshToken, h.Cfg.IsSecureOrigin())
|
||||
// 多会话并存:新登录不踢旧设备;仅当本次登录超出会话上限、淘汰了最旧
|
||||
// 会话时广播告知(被淘汰设备 WS 在线则当场弹"登录已失效",无需等
|
||||
// access token 过期或硬刷新才发现)
|
||||
@@ -244,13 +244,13 @@ func (h *Handlers) Refresh(c *gin.Context) {
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrAccountBanned) {
|
||||
// 账号已封禁:清 cookie 的同时给出可识别 code,前端弹封禁告知并强制下线
|
||||
clearAuthCookies(c, !h.Cfg.DevMode)
|
||||
clearAuthCookies(c, h.Cfg.IsSecureOrigin())
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "账号已被封禁", "code": "account_banned"})
|
||||
return
|
||||
}
|
||||
if errors.Is(err, service.ErrRefreshInvalid) || errors.Is(err, service.ErrRefreshExpired) {
|
||||
// 登录态确实失效:清 cookie 要求重新登录(前端弹"登录已失效")
|
||||
clearAuthCookies(c, !h.Cfg.DevMode)
|
||||
clearAuthCookies(c, h.Cfg.IsSecureOrigin())
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "登录已过期,请重新登录"})
|
||||
return
|
||||
}
|
||||
@@ -259,7 +259,7 @@ func (h *Handlers) Refresh(c *gin.Context) {
|
||||
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "登录刷新暂不可用,请稍后重试"})
|
||||
return
|
||||
}
|
||||
setAuthCookies(c, accessToken, newRefresh, !h.Cfg.DevMode)
|
||||
setAuthCookies(c, accessToken, newRefresh, h.Cfg.IsSecureOrigin())
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"user": gin.H{
|
||||
"id": user.ID,
|
||||
@@ -279,7 +279,7 @@ func (h *Handlers) Logout(c *gin.Context) {
|
||||
if refreshToken, err := c.Cookie(service.RefreshCookieName); err == nil && refreshToken != "" {
|
||||
_ = h.Auth.RevokeRefreshToken(refreshToken)
|
||||
}
|
||||
clearAuthCookies(c, !h.Cfg.DevMode)
|
||||
clearAuthCookies(c, h.Cfg.IsSecureOrigin())
|
||||
c.JSON(http.StatusOK, gin.H{"message": "已登出"})
|
||||
}
|
||||
|
||||
@@ -302,7 +302,7 @@ func (h *Handlers) ChangePassword(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
// 密码已改,token_version 已递增,旧 token 全部失效,清除 cookie 要求重新登录
|
||||
clearAuthCookies(c, !h.Cfg.DevMode)
|
||||
clearAuthCookies(c, h.Cfg.IsSecureOrigin())
|
||||
c.JSON(http.StatusOK, gin.H{"message": "密码修改成功,请重新登录"})
|
||||
}
|
||||
|
||||
|
||||
@@ -69,7 +69,7 @@ func (h *Handlers) chatOversee(userID uint) bool {
|
||||
if err != nil || actor == nil {
|
||||
return false
|
||||
}
|
||||
return service.CanOverseeChat(actor.Role)
|
||||
return actor.CanOverseeChat()
|
||||
}
|
||||
|
||||
// RequireMessagesOpen 前台群聊/私聊闸门;关闭后一律 403。管理端 /api/admin/chat 不受影响。
|
||||
@@ -673,7 +673,7 @@ func (h *Handlers) AdminChatMessages(c *gin.Context) {
|
||||
"messages": msgs,
|
||||
"total": total,
|
||||
"page": page,
|
||||
"can_oversee": service.CanOverseeChat(actor.Role),
|
||||
"can_oversee": actor.CanOverseeChat(),
|
||||
"can_recall": true,
|
||||
"site_messages": actor.HasSiteMessagePerm(),
|
||||
})
|
||||
@@ -697,7 +697,7 @@ func (h *Handlers) AdminRecallChatMessage(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "消息 ID 无效"})
|
||||
return
|
||||
}
|
||||
oversee := service.CanOverseeChat(actor.Role)
|
||||
oversee := actor.CanOverseeChat()
|
||||
msg, newly, err := h.Chat.RecallMessage(claims.ID, uint(roomID), uint(mid), oversee)
|
||||
if err != nil {
|
||||
c.JSON(chatErrToStatus(err), gin.H{"error": err.Error()})
|
||||
|
||||
@@ -246,3 +246,55 @@ func (h *Handlers) execModeration(c *gin.Context, fn func(*service.Actor, uint)
|
||||
h.broadcastModerationChanged()
|
||||
c.JSON(http.StatusOK, gin.H{"message": "操作成功"})
|
||||
}
|
||||
|
||||
// ===== 置顶顺序配置 =====
|
||||
|
||||
// AdminListPinnedPosts 列出指定层级(level=1 板块 / 2 全站)的置顶帖,供后台排序
|
||||
func (h *Handlers) AdminListPinnedPosts(c *gin.Context) {
|
||||
level, _ := strconv.Atoi(c.Query("level"))
|
||||
boardID, _ := strconv.ParseUint(c.Query("board_id"), 10, 64)
|
||||
if level != model.PinBoard && level != model.PinGlobal {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的置顶层级"})
|
||||
return
|
||||
}
|
||||
items, err := h.Moderation.ListPinnedPosts(middleware.CurrentActor(c), level, uint(boardID))
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrPinOrderForbidden) {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "获取置顶列表失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"posts": nonNilSlice(items)})
|
||||
}
|
||||
|
||||
// AdminSavePinOrderRequest 置顶顺序保存请求
|
||||
type AdminSavePinOrderRequest struct {
|
||||
Level int `json:"level" binding:"required"`
|
||||
BoardID uint `json:"board_id"`
|
||||
IDs []uint `json:"ids" binding:"required"`
|
||||
}
|
||||
|
||||
// AdminSavePinOrder 保存置顶帖显示顺序(ids 数组顺序即展示顺序)
|
||||
func (h *Handlers) AdminSavePinOrder(c *gin.Context) {
|
||||
var req AdminSavePinOrderRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "参数错误"})
|
||||
return
|
||||
}
|
||||
if req.Level != model.PinBoard && req.Level != model.PinGlobal {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的置顶层级"})
|
||||
return
|
||||
}
|
||||
if err := h.Moderation.SavePinOrder(middleware.CurrentActor(c), req.Level, req.BoardID, req.IDs); err != nil {
|
||||
if errors.Is(err, service.ErrPinOrderForbidden) {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
h.broadcastFeedChanged("post", 0, req.BoardID, middleware.CurrentActor(c).ID)
|
||||
c.JSON(http.StatusOK, gin.H{"message": "已保存"})
|
||||
}
|
||||
|
||||
@@ -550,9 +550,12 @@ func (h *Handlers) DeletePost(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"message": "已删除"})
|
||||
}
|
||||
|
||||
// TogglePin 切换帖子置顶(管理员及以上,板块管理员无此权限)
|
||||
// TogglePin 切换帖子置顶:管理员及以上切全站置顶,板块管理员切本板板块置顶
|
||||
func (h *Handlers) TogglePin(c *gin.Context) {
|
||||
if !h.requireAdminOrAbove(c) {
|
||||
claims := middleware.CurrentUser(c)
|
||||
actor, err := h.Auth.LoadActor(claims.ID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"})
|
||||
return
|
||||
}
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
@@ -560,9 +563,17 @@ func (h *Handlers) TogglePin(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的帖子 ID"})
|
||||
return
|
||||
}
|
||||
pinned, err := h.Post.TogglePin(uint(id))
|
||||
level, _ := strconv.Atoi(c.Query("level"))
|
||||
pinned, err := h.Post.TogglePin(uint(id), actor, level)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
switch {
|
||||
case errors.Is(err, service.ErrPostNotFound):
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
||||
case errors.Is(err, service.ErrPostForbidden), errors.Is(err, service.ErrPinGlobalOnly):
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
|
||||
default:
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
}
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"pinned": pinned})
|
||||
|
||||
@@ -61,7 +61,7 @@ func (h *Handlers) RealtimeWS(c *gin.Context) {
|
||||
canOverseeChat := false
|
||||
if actor, loadErr := h.Auth.LoadActor(claims.ID); loadErr == nil {
|
||||
isStaff = actor.IsStaff()
|
||||
canOverseeChat = service.CanOverseeChat(actor.Role)
|
||||
canOverseeChat = actor.CanOverseeChat()
|
||||
}
|
||||
|
||||
client := realtime.NewClient(
|
||||
@@ -74,7 +74,7 @@ func (h *Handlers) RealtimeWS(c *gin.Context) {
|
||||
return verr == nil
|
||||
},
|
||||
func() { h.Auth.TouchLastSeen(claims.ID) },
|
||||
// 动态房间订阅鉴权:成员可订阅;站长/超管旁路可订阅任意群
|
||||
// 动态房间订阅鉴权:成员可订阅;站长/已授权超管旁路可订阅任意群
|
||||
func(room string) bool {
|
||||
roomID, ok := parseWSChatRoom(room)
|
||||
if !ok {
|
||||
|
||||
Reference in New Issue
Block a user