middleware

package
v0.0.27 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

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 GenerateToken(secret string, userID uint, role int, username, displayName, avatarURL string) (string, error)

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

type RequestScope struct {
	IsAdmin bool
	UserID  uint
}

RequestScope 封装当前请求的权限上下文。

func GetScope

func GetScope(c *gin.Context) *RequestScope

GetScope 从 gin context 读取 RequestScope,未设置时返回 nil。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL