mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-02 23:06:36 +08:00
c9b702d234
Architecture: - Move GetFromContext/SetToContext from plugins/domain/auth to pkg/util - Move auth context key constants to core/contracts (AuthUserObjKey, AuthTokenAuthKey, etc.) - Add AuthUserIDKey, AuthUserNameKey, GetCurrentUserID, RevokeToken to contracts.AuthService - All 4 domain plugin Apply() methods now resolve AuthService via core.Using IoC - Plugin route middleware uses authSvc.RequireAuthMiddleware() cast to gin.HandlerFunc - DisallowTokenAuth added to AuthService contract Migration: - Replace cmd/app.go SetMigrationRunner bridge with gooseEngine implementing core.MigrationEngine - Remove cmd/root.go PreRun migration hooks and runMigrations() function - Migrations now run via core.App.Start() → RunMigrations() Events: - Add complete domain event topic catalog and payload DTOs to core/contracts/events.go - 15 event topics across auth, user, admin, upload, message_gateway, risk_control Downstream: - Create downstream/ directory with README and custom_example plugin scaffold CI: - Update Makefile code-check architecture guards for Cordis layering - Enforce: core no gin/gorm/asynq, contracts no plugins/, pkg no plugins/, domain no cross-domain
45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package admin
|
|
|
|
import (
|
|
"github.com/Rain-kl/Wavelet/core/contracts"
|
|
"github.com/Rain-kl/Wavelet/pkg/logger"
|
|
"github.com/Rain-kl/Wavelet/pkg/response"
|
|
"github.com/Rain-kl/Wavelet/pkg/trace"
|
|
"github.com/Rain-kl/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()
|
|
}
|
|
}
|