Skip to content

Commit

Permalink
Remove old user role design
Browse files Browse the repository at this point in the history
  • Loading branch information
slhmy committed Jul 24, 2024
1 parent 17e265a commit 38b5411
Show file tree
Hide file tree
Showing 7 changed files with 6 additions and 56 deletions.
1 change: 0 additions & 1 deletion cmd/init/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ func initDB() {
db := gorm_agent.GetDefaultDB()
err := db.AutoMigrate(
&user_model.User{},
&user_model.Role{},
&problem_model.Problem{},
&judge_model.Judge{},
&judge_model.JudgeResult{},
Expand Down
7 changes: 0 additions & 7 deletions cmd/web_server/middleware/login_session.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package middleware

import (
"fmt"
"time"

"github.com/gin-gonic/gin"
Expand All @@ -27,12 +26,6 @@ func BuildHandleRequireLoginWithRoles(roles []string) gin.HandlerFunc {
}
ginCtx.Set(loginSessionGinCtxKey, ls)

if !ls.Data.HasRoles(roles) {
modules.NewUnauthorizedError(fmt.Sprintf("require roles: %v", roles)).AppendToGin(ginCtx)
ginCtx.Abort()
return
}

ginCtx.Next()
}
}
Expand Down
15 changes: 0 additions & 15 deletions models/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,8 @@ type User struct {
Name string `json:"name"`
Password *string `json:"password,omitempty" gorm:"-:all"`
HashedPassword string `json:"-" gorm:"not null"`
Roles []*Role `json:"roles,omitempty" gorm:"many2many:user_roles;"`
Email *string `json:"email,omitempty" gorm:"unique"`
Mobile *string `json:"mobile,omitempty" gorm:"unique"`
}

var PublicUserSelection = append([]string{"account", "name"}, models.MetaFieldsSelection...)

type Role struct {
models.MetaFields
Name string `json:"name" gorm:"primaryKey"`
Users []*User `json:"users,omitempty" gorm:"many2many:user_roles"`
}

func (user User) GetRolesStringSet() map[string]struct{} {
roleSet := map[string]struct{}{}
for _, role := range user.Roles {
roleSet[role.Name] = struct{}{}
}
return roleSet
}
8 changes: 2 additions & 6 deletions models/user/user_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ func CreateUser(tx *gorm.DB, user User) error {
Name: user.Name,
Account: user.Account,
HashedPassword: hashedPassword,
Roles: user.Roles,
}

return tx.Create(&User).Error
}

func GetUser(tx *gorm.DB, account string) (*User, error) {
db_user := User{}
err := tx.Model(&User{}).Preload("Roles").Where("account = ?", account).First(&db_user).Error
err := tx.Model(&User{}).Where("account = ?", account).First(&db_user).Error
if err != nil {
return nil, err
}
Expand All @@ -37,7 +36,7 @@ func GetUser(tx *gorm.DB, account string) (*User, error) {

func GetPublicUser(tx *gorm.DB, account string) (*User, error) {
db_user := User{}
err := tx.Model(&User{}).Preload("Roles").Select(PublicUserSelection).Where("account = ?", account).First(&db_user).Error
err := tx.Model(&User{}).Select(PublicUserSelection).Where("account = ?", account).First(&db_user).Error
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -68,9 +67,6 @@ func UpdateUser(tx *gorm.DB, update User) error {
if update.Password != nil {
new.HashedPassword = hashedPassword
}
if update.Roles != nil {
new.Roles = update.Roles
}

return tx.Model(&User{Account: new.Account}).Updates(new).Error
}
Expand Down
14 changes: 1 addition & 13 deletions modules/auth/login_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,13 @@ type LoginSessionKey struct {
}

type LoginSessionData struct {
RoleSet map[string]struct{}
}

type LoginSession struct {
Key LoginSessionKey
Data LoginSessionData
}

func (data LoginSessionData) HasRoles(roles []string) bool {
for _, role := range roles {
if _, ok := data.RoleSet[role]; !ok {
return false
}
}
return true
}

func (data LoginSessionData) GetJsonString() (string, error) {
bytes, err := json.Marshal(data)
if err != nil {
Expand All @@ -55,9 +45,7 @@ func NewLoginSession(account string, data LoginSessionData) *LoginSession {
Account: account,
Id: uuid.New(),
},
LoginSessionData{
RoleSet: data.RoleSet,
},
LoginSessionData{},
}
}

Expand Down
16 changes: 3 additions & 13 deletions services/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ func UpdateUser(ctx context.Context, user user_model.User) error {

return auth_module.UpdateLoginSessionByAccount(ctx,
user.Account,
auth_module.LoginSessionData{
RoleSet: user.GetRolesStringSet(),
})
auth_module.LoginSessionData{})
}

func CheckUserExist(ctx context.Context, account string) (bool, error) {
Expand All @@ -66,16 +64,8 @@ func CheckUserExist(ctx context.Context, account string) (bool, error) {
}

func StartLoginSession(ctx context.Context, account, password string) (*auth_module.LoginSession, error) {
db := gorm_agent.GetDefaultDB()
user, err := user_model.GetUserByAccountPassword(db, account, password)
if err != nil {
return nil, err
}

ls := auth_module.NewLoginSession(account, auth_module.LoginSessionData{
RoleSet: user.GetRolesStringSet(),
})
err = ls.SaveToRedis(ctx)
ls := auth_module.NewLoginSession(account, auth_module.LoginSessionData{})
err := ls.SaveToRedis(ctx)
if err != nil {
return nil, err
}
Expand Down
1 change: 0 additions & 1 deletion tests/models/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ func TestUserDB(t *testing.T) {
user := user_model.User{
Account: "test",
Password: func() *string { s := "test"; return &s }(),
Roles: []*user_model.Role{{Name: "test"}},
}
err := user_model.CreateUser(db, user)
if err != nil {
Expand Down

0 comments on commit 38b5411

Please sign in to comment.