Skip to content

Commit b3d8b2a

Browse files
committed
Add superuser role, keep admin for zone admins
1 parent 5096a07 commit b3d8b2a

File tree

5 files changed

+37
-16
lines changed

5 files changed

+37
-16
lines changed

src/auth/auth.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,20 @@ func AuthenticationFunc(authHeader string) (string, string, error) {
4343
// Debug mode: override username and role
4444
if DEBUG_MODE == "true" {
4545
// make sure to create a debug user if it doesn't exist
46-
role := api.UserRequestRoleAdmin
46+
role := api.UserRequestRoleSuperuser
4747
debug_user := api.UserRequest{
48-
Username: "admin_debug",
49-
Password: "admin_debug",
48+
Username: "superuser_debug",
49+
Password: "superuser_debug",
5050
Role: &role,
51-
Email: "admin.debug@debug.com",
52-
Name: "Admin",
53-
Surname: "Debug",
51+
Email: "superuser.debug@debug.com",
52+
Name: "superuser",
53+
Surname: "debug",
5454
}
5555
_, err := dao.NewUserDao().AddUser(context.Background(), debug_user)
5656
if err != nil && !errors.Is(err, dao.ErrUserAlreadyExists) {
5757
return "", "", errors.New("failed to create debug user: " + err.Error())
5858
}
59-
return "admin_debug", "admin", nil
59+
return "superuser_debug", "superuser", nil
6060
}
6161

6262
if authHeader == "" {

src/db/postgres_schema_v1.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ CREATE TABLE IF NOT EXISTS users (
66
surname TEXT NOT NULL,
77
email TEXT NOT NULL UNIQUE,
88
password TEXT NOT NULL,
9-
role TEXT NOT NULL CHECK (role IN ('driver', 'controller', 'admin'))
9+
role TEXT NOT NULL CHECK (role IN ('driver', 'controller', 'admin', 'superuser'))
1010
);

src/handlers/session.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,27 @@ func (h *SessionHandlers) Register(c *gin.Context) {
7676
newUser.Role = &defaultRole
7777
}
7878

79-
// If registering as an admin or controller, verify permissions
80-
if *newUser.Role == api.UserRequestRoleAdmin || *newUser.Role == api.UserRequestRoleController {
79+
if *newUser.Role == api.UserRequestRoleSuperuser {
80+
c.JSON(http.StatusForbidden, gin.H{"error": "Superuser registration is not allowed"})
81+
return
82+
}
83+
// If registering as admin, you need to be a superuser
84+
if *newUser.Role == api.UserRequestRoleAdmin {
85+
// Check if the current user is authenticated with superuser privileges
86+
_, role, err := auth.AuthenticationFunc(c.GetHeader("Authorization"))
87+
if err != nil {
88+
c.JSON(http.StatusUnauthorized, gin.H{"error": "Authentication failed: " + err.Error()})
89+
return
90+
}
91+
// Check if the user has superuser privileges
92+
if role != "superuser" {
93+
c.JSON(http.StatusForbidden, gin.H{"error": "Superuser privileges required to register " + string(*newUser.Role) + " accounts"})
94+
return
95+
}
96+
}
97+
98+
// If registering as controller, you need to be at least an admin
99+
if *newUser.Role == api.UserRequestRoleController {
81100
// Check if the current user is authenticated with admin privileges
82101
_, role, err := auth.AuthenticationFunc(c.GetHeader("Authorization"))
83102
if err != nil {
@@ -86,7 +105,7 @@ func (h *SessionHandlers) Register(c *gin.Context) {
86105
}
87106

88107
// Check if the user has admin privileges
89-
if role != "admin" {
108+
if role != "admin" && role != "superuser" {
90109
c.JSON(http.StatusForbidden, gin.H{"error": "Admin privileges required to register " + string(*newUser.Role) + " accounts"})
91110
return
92111
}

src/handlers/user.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (uh *UserHandlers) GetUsers(c *gin.Context, params api.GetUsersParams) {
4242
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to get role"})
4343
return
4444
}
45-
if roleStr != "admin" {
45+
if roleStr != "superuser" {
4646
c.JSON(http.StatusForbidden, gin.H{"error": "forbidden"})
4747
return
4848
}
@@ -96,7 +96,7 @@ func (uh *UserHandlers) DeleteUsers(c *gin.Context) {
9696
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to get role"})
9797
return
9898
}
99-
if roleStr != "admin" {
99+
if roleStr != "superuser" {
100100
c.JSON(http.StatusForbidden, gin.H{"error": "forbidden"})
101101
return
102102
}
@@ -190,7 +190,7 @@ func (uh *UserHandlers) GetUserById(c *gin.Context, id int64) {
190190
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to get role"})
191191
return
192192
}
193-
if roleStr != "admin" {
193+
if roleStr != "superuser" {
194194
c.JSON(http.StatusForbidden, gin.H{"error": "forbidden"})
195195
return
196196
}
@@ -229,7 +229,7 @@ func (uh *UserHandlers) UpdateUserById(c *gin.Context, id int64) {
229229
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to get role"})
230230
return
231231
}
232-
if roleStr != "admin" {
232+
if roleStr != "superuser" {
233233
c.JSON(http.StatusForbidden, gin.H{"error": "forbidden"})
234234
return
235235
}
@@ -281,7 +281,7 @@ func (uh *UserHandlers) DeleteUserById(c *gin.Context, id int64) {
281281
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to get role"})
282282
return
283283
}
284-
if roleStr != "admin" {
284+
if roleStr != "superuser" {
285285
c.JSON(http.StatusForbidden, gin.H{"error": "forbidden"})
286286
return
287287
}

src/rbac/rbac.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
func SetupRBAC() *gorbac.RBAC {
88
rbac := gorbac.New()
99

10+
superuser := gorbac.NewStdRole("superuser")
1011
admin := gorbac.NewStdRole("admin")
1112
controller := gorbac.NewStdRole("controller")
1213
driver := gorbac.NewStdRole("driver")
@@ -18,6 +19,7 @@ func SetupRBAC() *gorbac.RBAC {
1819
// admin.Assign(readUsers)
1920
// admin.Assign(writeUsers)
2021

22+
rbac.Add(superuser)
2123
rbac.Add(admin)
2224
rbac.Add(controller)
2325
rbac.Add(driver)

0 commit comments

Comments
 (0)