Skip to content

Commit 3a2961e

Browse files
committed
fix: refactor admin permission checks into util package (#2060)
1 parent 43a0bcf commit 3a2961e

6 files changed

Lines changed: 52 additions & 16 deletions

File tree

controllers/account.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (c *ApiController) Signin() {
8686
}
8787

8888
if strings.Count(claims.Type, "-") <= 1 {
89-
if !claims.IsAdmin && claims.Type != "chat-admin" {
89+
if !util.IsAdminOrChatAdmin(&claims.User) {
9090
claims.Type = "chat-user"
9191
}
9292
}

controllers/util.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,7 @@ func (c *ApiController) RequireAdmin() bool {
133133

134134
func (c *ApiController) IsAdmin() bool {
135135
user := c.GetSessionUser()
136-
if user == nil {
137-
return false
138-
}
139-
140-
res := user.IsAdmin || user.Type == "chat-admin"
141-
return res
136+
return util.IsAdminOrChatAdmin(user)
142137
}
143138

144139
func DenyRequest(ctx *context.Context) {

controllers/video.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func (c *ApiController) UpdateVideo() {
145145
return
146146
}
147147

148-
if user.Type == "video-normal-user" {
148+
if util.IsVideoNormalUser(user) {
149149
if len(video.Remarks) > 0 || len(video.Remarks2) > 0 || video.State != "Draft" {
150150
c.ResponseError(c.T("video:The video can only be updated when there are no remarks and the state is \"Draft\""))
151151
return

object/util.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,7 @@ func GetDbSession(owner string, offset, limit int, field, value, sortField, sort
6161
}
6262

6363
func isAdmin(user *casdoorsdk.User) bool {
64-
if user == nil {
65-
return false
66-
}
67-
68-
res := user.IsAdmin || user.Type == "chat-admin"
69-
return res
64+
return util.IsAdminOrChatAdmin(user)
7065
}
7166

7267
func IsAnonymousUserByUsername(username string) bool {

routers/authz_filter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/beego/beego/context"
2222
"github.com/casibase/casibase/conf"
2323
"github.com/casibase/casibase/controllers"
24+
"github.com/casibase/casibase/util"
2425
)
2526

2627
func AuthzFilter(ctx *context.Context) {
@@ -88,8 +89,7 @@ func permissionFilter(ctx *context.Context) {
8889

8990
user := GetSessionUser(ctx)
9091

91-
isAdmin := user != nil && (user.IsAdmin || user.Type == "chat-admin")
92-
if !isAdmin {
92+
if !util.IsAdminOrChatAdmin(user) {
9393
responseError(ctx, "auth:this operation requires admin privilege")
9494
return
9595
}

util/permission.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2025 The Casibase Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package util
16+
17+
import "github.com/casdoor/casdoor-go-sdk/casdoorsdk"
18+
19+
const (
20+
UserTypeChatAdmin = "chat-admin"
21+
UserTypeVideoNormalUser = "video-normal-user"
22+
)
23+
24+
// IsChatAdmin checks if the user has the chat-admin role
25+
func IsChatAdmin(user *casdoorsdk.User) bool {
26+
if user == nil {
27+
return false
28+
}
29+
return user.Type == UserTypeChatAdmin
30+
}
31+
32+
// IsAdminOrChatAdmin checks if the user is either a system admin or a chat-admin
33+
func IsAdminOrChatAdmin(user *casdoorsdk.User) bool {
34+
if user == nil {
35+
return false
36+
}
37+
return user.IsAdmin || user.Type == UserTypeChatAdmin
38+
}
39+
40+
// IsVideoNormalUser checks if the user has the video-normal-user role
41+
func IsVideoNormalUser(user *casdoorsdk.User) bool {
42+
if user == nil {
43+
return false
44+
}
45+
return user.Type == UserTypeVideoNormalUser
46+
}

0 commit comments

Comments
 (0)