Skip to content

Commit

Permalink
Merge pull request YJU-OKURA#252 from Regulus0811/refactor/user-roles
Browse files Browse the repository at this point in the history
  • Loading branch information
yuminn-k authored May 2, 2024
2 parents f3a4fcc + 249e816 commit 76903ce
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
16 changes: 8 additions & 8 deletions middlewares/authentication_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
)

const (
AdminRoleID = 2
AssistantRoleID = 3
AdminRole = "ADMIN"
AssistantRole = "ASSISTANT"
)

// getUserInfoFromPath はクエリパラメータからユーザー情報を取得します。
Expand All @@ -28,21 +28,21 @@ func getUserInfoFromPath(ctx *gin.Context) (uid uint, cid uint, err error) {
}

// ClassUserRoleMiddleware は指定された権限を持っているかどうかを確認するミドルウェアです。
func ClassUserRoleMiddleware(roleService services.ClassUserService, requiredRoleID int) gin.HandlerFunc {
func ClassUserRoleMiddleware(roleService services.ClassUserService, requiredRoleName string) gin.HandlerFunc {
return func(ctx *gin.Context) {
uid, cid, err := getUserInfoFromPath(ctx)
if err != nil {
ctx.AbortWithStatusJSON(constants.StatusUnauthorized, gin.H{"error": "Unauthorized: invalid user or class ID"})
return
}

roleID, err := roleService.GetRole(uid, cid)
roleName, err := roleService.GetRole(uid, cid)
if err != nil {
ctx.AbortWithStatusJSON(constants.StatusUnauthorized, gin.H{"error": "Unauthorized: role ID check failed"})
ctx.AbortWithStatusJSON(constants.StatusUnauthorized, gin.H{"error": "Unauthorized: role check failed"})
return
}

if roleID != requiredRoleID {
if roleName != requiredRoleName {
ctx.AbortWithStatusJSON(constants.StatusForbidden, gin.H{"error": "Forbidden: insufficient privileges"})
return
}
Expand All @@ -53,12 +53,12 @@ func ClassUserRoleMiddleware(roleService services.ClassUserService, requiredRole

// AdminMiddleware は管理者権限を持っているかどうかを確認するミドルウェアです。
func AdminMiddleware(roleService services.ClassUserService) gin.HandlerFunc {
return ClassUserRoleMiddleware(roleService, AdminRoleID)
return ClassUserRoleMiddleware(roleService, AdminRole)
}

// AssistantMiddleware はアシスタント権限を持っているかどうかを確認するミドルウェアです。
func AssistantMiddleware(roleService services.ClassUserService) gin.HandlerFunc {
return ClassUserRoleMiddleware(roleService, AssistantRoleID)
return ClassUserRoleMiddleware(roleService, AssistantRole)
}

func AuthMiddleware(authenticate func(token string) bool) gin.HandlerFunc {
Expand Down
1 change: 0 additions & 1 deletion migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ func Migrate(db *gorm.DB) {
&models.ClassBoard{},
&models.ClassCode{},
&models.ClassSchedule{},
&models.Role{},
&models.Attendance{},
)
if err != nil {
Expand Down

0 comments on commit 76903ce

Please sign in to comment.