mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-30 14:26:36 +08:00
528240026d
- make format 现在与 code-check 使用同一格式化器(golangci-lint fmt),消除 goimports -local 与 gofumpt 的格式拉锯 - .golangci.yml 关闭默认 50/3 截断,完整上报所有问题(只增强不弱化) - 全库 gofumpt 规范化(203 files, 纯格式无行为变更)
46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package admin
|
|
|
|
import (
|
|
"Wavelet/core/contracts"
|
|
"Wavelet/pkg/logger"
|
|
"Wavelet/pkg/response"
|
|
"Wavelet/pkg/trace"
|
|
"Wavelet/pkg/util"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// LoginAdminRequired 返回管理员权限校验中间件
|
|
func LoginAdminRequired() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
ctx, span := trace.Start(c.Request.Context(), "LoginAdminRequired")
|
|
defer span.End()
|
|
|
|
user, _ := util.GetFromContext[*contracts.UserDTO](c, contracts.AuthUserObjKey)
|
|
if user == nil {
|
|
response.AbortNotFound(c, AdminRequired)
|
|
return
|
|
}
|
|
|
|
// 如果是通过 Access Token 鉴权,需要检查令牌本身是否具有管理员权限
|
|
if tokenAuth, _ := util.GetFromContext[bool](c, contracts.AuthTokenAuthKey); tokenAuth {
|
|
tokenAdmin, _ := util.GetFromContext[bool](c, contracts.AuthTokenAdminKey)
|
|
if !tokenAdmin {
|
|
response.AbortNotFound(c, TokenAdminRequired)
|
|
return
|
|
}
|
|
}
|
|
|
|
if !user.IsAdmin {
|
|
response.AbortNotFound(c, AdminRequired)
|
|
return
|
|
}
|
|
|
|
logger.InfoF(ctx, "[LoginAdminRequired] %d %s", user.ID, user.Username)
|
|
c.Next()
|
|
}
|
|
}
|