From 14ffd0e9fb903f80a77b948ef73b2e695f5bbce5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Tue, 23 Aug 2022 22:39:42 +0800 Subject: [PATCH] =?UTF-8?q?refactor(model):=E5=9B=BA=E5=8C=96=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E7=B1=BB=E5=9E=8B=E4=B8=BA=E6=9E=9A=E4=B8=BE=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/user.go | 4 ++-- model/user.go | 6 ++++++ security/security.go | 6 +++--- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/controller/user.go b/controller/user.go index f17af0a..1cd8e80 100644 --- a/controller/user.go +++ b/controller/user.go @@ -48,7 +48,7 @@ func login(c *gin.Context) { var ( session *model.Session ) - if loginData.Type == 0 { + if loginData.Type == model.USER_TYPE_ENT { session, err = service.UserService.ProcessEnterpriseUserLogin(loginData.Username, loginData.Password) } else { session, err = service.UserService.ProcessManagementUserLogin(loginData.Username, loginData.Password) @@ -276,7 +276,7 @@ func createEnterpriseAccount(c *gin.Context) { } newUser := new(model.User) newUser.Username = creationForm.Username - newUser.Type = 0 + newUser.Type = model.USER_TYPE_ENT newUser.Enabled = true newUserDetail := new(model.UserDetail) newUserDetail.Name = &creationForm.Name diff --git a/model/user.go b/model/user.go index 9a25efd..4e6944f 100644 --- a/model/user.go +++ b/model/user.go @@ -1,5 +1,11 @@ package model +const ( + USER_TYPE_ENT int8 = iota + USER_TYPE_SUP + USER_TYPE_OPS +) + type User struct { Created `xorm:"extends"` Id string `xorm:"varchar(120) pk not null" json:"id"` diff --git a/security/security.go b/security/security.go index 9179ea7..7682c3f 100644 --- a/security/security.go +++ b/security/security.go @@ -45,7 +45,7 @@ func EnterpriseAuthorize(c *gin.Context) { if !exists || session == nil { c.AbortWithStatus(http.StatusForbidden) } - if sess, ok := session.(*model.Session); !ok || sess.Type != 0 { + if sess, ok := session.(*model.Session); !ok || sess.Type != model.USER_TYPE_ENT { c.AbortWithStatus(http.StatusForbidden) } c.Next() @@ -58,7 +58,7 @@ func ManagementAuthorize(c *gin.Context) { if !exists || session == nil { c.AbortWithStatus(http.StatusForbidden) } - if sess, ok := session.(*model.Session); !ok || (sess.Type != 1 && sess.Type != 2) { + if sess, ok := session.(*model.Session); !ok || (sess.Type != model.USER_TYPE_SUP && sess.Type != model.USER_TYPE_OPS) { c.AbortWithStatus(http.StatusForbidden) } c.Next() @@ -71,7 +71,7 @@ func OPSAuthorize(c *gin.Context) { if !exists { c.AbortWithStatus(http.StatusForbidden) } - if sess, ok := session.(*model.Session); !ok || sess.Type != 2 { + if sess, ok := session.(*model.Session); !ok || sess.Type != model.USER_TYPE_OPS { c.AbortWithStatus(http.StatusForbidden) } c.Next()