Documentation
¶
Index ¶
- func AdminOnly() gin.HandlerFunc
- func AuthMiddleware(secret string) gin.HandlerFunc
- func EnsureOwnership(c *gin.Context, scope *RequestScope, resourceUserID uint) bool
- func GenerateToken(secret string, userID uint, role int, username, displayName, avatarURL string) (string, error)
- func OptionalAuthMiddleware(secret string) gin.HandlerFunc
- func OptionalScopeMiddleware() gin.HandlerFunc
- func ScopeMiddleware() gin.HandlerFunc
- func ScopedUserID(scope *RequestScope, reqUserID *uint) *uint
- type RequestScope
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AdminOnly ¶
func AdminOnly() gin.HandlerFunc
func AuthMiddleware ¶
func AuthMiddleware(secret string) gin.HandlerFunc
func EnsureOwnership ¶
func EnsureOwnership(c *gin.Context, scope *RequestScope, resourceUserID uint) bool
EnsureOwnership 检查当前用户是否有权访问指定资源。 管理员始终通过,普通用户仅当资源属于自己时通过,否则返回 404。
func GenerateToken ¶
func OptionalAuthMiddleware ¶ added in v0.0.25
func OptionalAuthMiddleware(secret string) gin.HandlerFunc
OptionalAuthMiddleware permits an absent header, never invalid credentials.
func OptionalScopeMiddleware ¶ added in v0.0.25
func OptionalScopeMiddleware() gin.HandlerFunc
OptionalScopeMiddleware keeps the normal authenticated scope derivation while allowing explicitly public handlers to accept an unauthenticated request.
func ScopeMiddleware ¶
func ScopeMiddleware() gin.HandlerFunc
ScopeMiddleware 从 UserInfo 派生 RequestScope 并写入 gin context。
func ScopedUserID ¶
func ScopedUserID(scope *RequestScope, reqUserID *uint) *uint
ScopedUserID 根据权限返回应使用的 user_id 过滤值。 管理员使用请求参数中的 user_id(可为 nil 表示不过滤),普通用户始终使用自身 ID。
Types ¶
type RequestScope ¶
RequestScope 封装当前请求的权限上下文。
func GetScope ¶
func GetScope(c *gin.Context) *RequestScope
GetScope 从 gin context 读取 RequestScope,未设置时返回 nil。
Click to show internal directories.
Click to hide internal directories.