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{ claim := &global.Claim{
UID: u.ID, UID: u.ID,
Role: u.Role,
Version: version, Version: version,
} }
token, status := h.jwtService.SignClaim(claim) token, status := h.jwtService.SignClaim(claim)

View File

@ -48,6 +48,7 @@ func (h *handler) Login(c *gin.Context) {
} }
claim := &global.Claim{ claim := &global.Claim{
UID: user.ID, UID: user.ID,
Role: user.Role,
Version: version, Version: version,
} }
token, status := h.jwtService.SignClaim(claim) token, status := h.jwtService.SignClaim(claim)

View File

@ -2,13 +2,15 @@ package global
import ( import (
"github.com/WHUPRJ/woj-server/internal/e" "github.com/WHUPRJ/woj-server/internal/e"
"github.com/WHUPRJ/woj-server/internal/repo/model"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v4" "github.com/golang-jwt/jwt/v4"
) )
type Claim struct { type Claim struct {
UID uint `json:"id"` UID uint `json:"id"`
Version int64 `json:"version"` Role model.Role `json:"role"`
Version int64 `json:"version"`
jwt.RegisteredClaims 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:"-"` gorm.Model `json:"-"`
UserName string `json:"user_name" gorm:"not null;uniqueIndex"` UserName string `json:"user_name" gorm:"not null;uniqueIndex"`
NickName string `json:"nick_name" gorm:"not null"` NickName string `json:"nick_name" gorm:"not null"`
Role Role `json:"role" gorm:"not null"`
Password []byte `json:"-" gorm:"not null"` Password []byte `json:"-" gorm:"not null"`
IsEnabled bool `json:"is_enabled" gorm:"not null;index"` 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, UserName: data.Username,
Password: hashed, Password: hashed,
NickName: data.Nickname, NickName: data.Nickname,
Role: model.RoleGeneral,
IsEnabled: true, IsEnabled: true,
} }