From 544a9fd071a5ecbf691074c31a0b2b1f2bf0acd7 Mon Sep 17 00:00:00 2001 From: Paul Pan Date: Tue, 20 Sep 2022 14:34:30 +0800 Subject: [PATCH] feat: add simple role support --- internal/api/user/create.go | 1 + internal/api/user/login.go | 1 + internal/global/jwt.go | 6 ++++-- internal/repo/model/Role.go | 9 +++++++++ internal/repo/model/User.go | 1 + internal/service/user/create.go | 1 + 6 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 internal/repo/model/Role.go diff --git a/internal/api/user/create.go b/internal/api/user/create.go index f00b5f2..8e2dc25 100644 --- a/internal/api/user/create.go +++ b/internal/api/user/create.go @@ -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) diff --git a/internal/api/user/login.go b/internal/api/user/login.go index 79ae4ed..78c6767 100644 --- a/internal/api/user/login.go +++ b/internal/api/user/login.go @@ -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) diff --git a/internal/global/jwt.go b/internal/global/jwt.go index 24cf814..18d2f70 100644 --- a/internal/global/jwt.go +++ b/internal/global/jwt.go @@ -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 } diff --git a/internal/repo/model/Role.go b/internal/repo/model/Role.go new file mode 100644 index 0000000..184c77f --- /dev/null +++ b/internal/repo/model/Role.go @@ -0,0 +1,9 @@ +package model + +type Role int + +const ( + RoleGuest Role = 10 + RoleGeneral Role = 20 + RoleAdmin Role = 30 +) diff --git a/internal/repo/model/User.go b/internal/repo/model/User.go index 07cc4f0..e2c7644 100644 --- a/internal/repo/model/User.go +++ b/internal/repo/model/User.go @@ -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"` } diff --git a/internal/service/user/create.go b/internal/service/user/create.go index 72d4854..cf8a8be 100644 --- a/internal/service/user/create.go +++ b/internal/service/user/create.go @@ -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, }