feat: add simple role support
This commit is contained in:
parent
8603315a5d
commit
544a9fd071
@ -50,6 +50,7 @@ func (h *handler) Create(c *gin.Context) {
|
||||
}
|
||||
claim := &global.Claim{
|
||||
UID: u.ID,
|
||||
Role: u.Role,
|
||||
Version: version,
|
||||
}
|
||||
token, status := h.jwtService.SignClaim(claim)
|
||||
|
@ -48,6 +48,7 @@ func (h *handler) Login(c *gin.Context) {
|
||||
}
|
||||
claim := &global.Claim{
|
||||
UID: user.ID,
|
||||
Role: user.Role,
|
||||
Version: version,
|
||||
}
|
||||
token, status := h.jwtService.SignClaim(claim)
|
||||
|
@ -2,13 +2,15 @@ package global
|
||||
|
||||
import (
|
||||
"github.com/WHUPRJ/woj-server/internal/e"
|
||||
"github.com/WHUPRJ/woj-server/internal/repo/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
)
|
||||
|
||||
type Claim struct {
|
||||
UID uint `json:"id"`
|
||||
Version int64 `json:"version"`
|
||||
UID uint `json:"id"`
|
||||
Role model.Role `json:"role"`
|
||||
Version int64 `json:"version"`
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
|
9
internal/repo/model/Role.go
Normal file
9
internal/repo/model/Role.go
Normal file
@ -0,0 +1,9 @@
|
||||
package model
|
||||
|
||||
type Role int
|
||||
|
||||
const (
|
||||
RoleGuest Role = 10
|
||||
RoleGeneral Role = 20
|
||||
RoleAdmin Role = 30
|
||||
)
|
@ -8,6 +8,7 @@ type User struct {
|
||||
gorm.Model `json:"-"`
|
||||
UserName string `json:"user_name" gorm:"not null;uniqueIndex"`
|
||||
NickName string `json:"nick_name" gorm:"not null"`
|
||||
Role Role `json:"role" gorm:"not null"`
|
||||
Password []byte `json:"-" gorm:"not null"`
|
||||
IsEnabled bool `json:"is_enabled" gorm:"not null;index"`
|
||||
}
|
||||
|
@ -25,6 +25,7 @@ func (s *service) Create(data *CreateData) (*model.User, e.Status) {
|
||||
UserName: data.Username,
|
||||
Password: hashed,
|
||||
NickName: data.Nickname,
|
||||
Role: model.RoleGeneral,
|
||||
IsEnabled: true,
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user