35 lines
926 B
Go
35 lines
926 B
Go
|
|
package middleware
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"net/http"
|
|||
|
|
|
|||
|
|
"github.com/gin-gonic/gin"
|
|||
|
|
|
|||
|
|
"with_you/internal/pkg/auth"
|
|||
|
|
"with_you/internal/pkg/response"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// RequireActive 要求账户状态为 active。
|
|||
|
|
//
|
|||
|
|
// 业务策略:
|
|||
|
|
// - active:放行。
|
|||
|
|
// - pending_deletion:在 /users/me/* 路由允许(用户可取消注销),但在 /admin/* 与敏感操作拒绝。
|
|||
|
|
// - banned/inactive:已由 RequireAuth 在认证管道拦截,不会到达此处。
|
|||
|
|
//
|
|||
|
|
// 应挂在需要严格 active 状态的路由组上(如 /admin)。
|
|||
|
|
func RequireActive() gin.HandlerFunc {
|
|||
|
|
return func(c *gin.Context) {
|
|||
|
|
p, ok := auth.GetPrincipal(c)
|
|||
|
|
if !ok || p == nil {
|
|||
|
|
response.ErrorWithStringCode(c, http.StatusUnauthorized, "UNAUTHORIZED", "未授权")
|
|||
|
|
c.Abort()
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
if !p.IsActive() {
|
|||
|
|
response.ErrorWithStringCode(c, http.StatusForbidden, "ACCOUNT_INACTIVE", "账号未激活")
|
|||
|
|
c.Abort()
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
c.Next()
|
|||
|
|
}
|
|||
|
|
}
|