feat: 添加用户的role信息
This commit is contained in:
4
go.mod
4
go.mod
@@ -4,10 +4,12 @@ go 1.24.0
|
||||
|
||||
require (
|
||||
github.com/gin-gonic/gin v1.10.0
|
||||
github.com/golang-jwt/jwt/v5 v5.3.1
|
||||
github.com/redis/go-redis/v9 v9.18.0
|
||||
github.com/spf13/viper v1.18.2
|
||||
github.com/stretchr/testify v1.9.0
|
||||
go.uber.org/zap v1.27.0
|
||||
golang.org/x/text v0.34.0
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1
|
||||
gorm.io/driver/mysql v1.6.0
|
||||
gorm.io/gorm v1.31.1
|
||||
@@ -30,7 +32,6 @@ require (
|
||||
github.com/go-playground/validator/v10 v10.20.0 // indirect
|
||||
github.com/go-sql-driver/mysql v1.9.3 // indirect
|
||||
github.com/goccy/go-json v0.10.2 // indirect
|
||||
github.com/golang-jwt/jwt/v5 v5.3.1 // indirect
|
||||
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
@@ -60,7 +61,6 @@ require (
|
||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
|
||||
golang.org/x/net v0.25.0 // indirect
|
||||
golang.org/x/sys v0.20.0 // indirect
|
||||
golang.org/x/text v0.34.0 // indirect
|
||||
google.golang.org/protobuf v1.34.1 // indirect
|
||||
gopkg.in/ini.v1 v1.67.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
|
||||
@@ -93,7 +93,7 @@ func VisitorMiddleware() gin.HandlerFunc {
|
||||
SecretKey: jwtConfig.Secret,
|
||||
})
|
||||
if claims, err := jwtUtil.ParseToken(token); err == nil {
|
||||
userInfo.WithUserID(claims.UserID).WithUserName(claims.UserName)
|
||||
userInfo.WithUserID(claims.UserID).WithUserName(claims.UserName).WithRole(claims.Role)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@ type JWT struct {
|
||||
type CustomClaims struct {
|
||||
UserID string `json:"user_id"`
|
||||
UserName string `json:"user_name"`
|
||||
Role int `json:"role"`
|
||||
Data map[string]interface{} `json:"data,omitempty"`
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
32
web/user.go
32
web/user.go
@@ -14,6 +14,7 @@ type UserInfo struct {
|
||||
FromIP string `json:"from_ip"`
|
||||
Province string `json:"province"`
|
||||
City string `json:"city"`
|
||||
Role int `json:"role"`
|
||||
}
|
||||
|
||||
// contextKey is the type for context keys to prevent collisions
|
||||
@@ -28,6 +29,7 @@ const (
|
||||
FromIPKey contextKey = "fromIP"
|
||||
ProvinceKey contextKey = "province"
|
||||
CityKey contextKey = "city"
|
||||
RoleKey contextKey = "role"
|
||||
)
|
||||
|
||||
// GetUserID returns user ID from context
|
||||
@@ -163,6 +165,25 @@ func SetCity(ctx context.Context, city string) context.Context {
|
||||
return context.WithValue(ctx, CityKey, city)
|
||||
}
|
||||
|
||||
// GetRole returns role from context
|
||||
func GetRole(ctx context.Context) int {
|
||||
if ctx == nil {
|
||||
return 0
|
||||
}
|
||||
if val, ok := ctx.Value(RoleKey).(int); ok {
|
||||
return val
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// SetRole sets role in context and returns new context
|
||||
func SetRole(ctx context.Context, role int) context.Context {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
return context.WithValue(ctx, RoleKey, role)
|
||||
}
|
||||
|
||||
// FromContext builds UserInfo from individual context fields
|
||||
// Returns nil if no fields are found
|
||||
func FromContext(ctx context.Context) *UserInfo {
|
||||
@@ -200,6 +221,10 @@ func FromContext(ctx context.Context) *UserInfo {
|
||||
info.City = city
|
||||
hasData = true
|
||||
}
|
||||
if role := GetRole(ctx); role != 0 {
|
||||
info.Role = role
|
||||
hasData = true
|
||||
}
|
||||
|
||||
if !hasData {
|
||||
return nil
|
||||
@@ -223,6 +248,7 @@ func ToContext(ctx context.Context, userInfo *UserInfo) context.Context {
|
||||
ctx = SetFromIP(ctx, userInfo.FromIP)
|
||||
ctx = SetProvince(ctx, userInfo.Province)
|
||||
ctx = SetCity(ctx, userInfo.City)
|
||||
ctx = SetRole(ctx, userInfo.Role)
|
||||
|
||||
return ctx
|
||||
}
|
||||
@@ -275,3 +301,9 @@ func (u *UserInfo) WithCity(city string) *UserInfo {
|
||||
u.City = city
|
||||
return u
|
||||
}
|
||||
|
||||
// WithRole sets the role
|
||||
func (u *UserInfo) WithRole(role int) *UserInfo {
|
||||
u.Role = role
|
||||
return u
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user