feat: add simple role support

This commit is contained in:
Paul Pan 2022-09-20 14:34:30 +08:00
parent 8603315a5d
commit 544a9fd071
6 changed files with 17 additions and 2 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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
}

View File

@ -0,0 +1,9 @@
package model
type Role int
const (
RoleGuest Role = 10
RoleGeneral Role = 20
RoleAdmin Role = 30
)

View File

@ -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"`
}

View File

@ -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,
}