Skip to content

πŸ“š The open-source, offline-first Notion, Obsidian & Confluence alternative. Advanced Markdown, multi-tenant teams, OIDC/SSO, and local S3 backups. Deploy in one command.

Test permissions system

Smaug6739913b0605a605
diff --git a/backend/controllers/users.controller.go b/backend/controllers/users.controller.go
index 10b89b4f..38cd4ec0 100644
--- a/backend/controllers/users.controller.go
+++ b/backend/controllers/users.controller.go
@@ -198,10 +198,18 @@ func (ctr *UserControllerImpl) UpdateUser(c *gin.Context) (int, any) {
// @Failure 400 {object} Error
// @Failure 401 {object} Error
func (ctr *UserControllerImpl) UpdatePassword(c *gin.Context) (int, any) {
- id, err := strconv.ParseInt(c.Param("id"), 10, 64)
+
+ id, _ := utils.GetUserIdParam(c.Param("id"), c)
+
+ userId, err := utils.GetUserIdCtx(c)
if err != nil {
- return http.StatusBadRequest, errors.New("invalid user ID")
+ return http.StatusBadRequest, err
+ }
+ role, _ := c.Get("user_role")
+ if role != utils.ADMINISTRATOR && userId != id {
+ return http.StatusForbidden, errors.New("not authorized to update this user")
}
+
var payload struct {
Password string `form:"password" json:"password"`
}
diff --git a/backend/utils/permissions.go b/backend/utils/permissions.go
new file mode 100644
index 00000000..eb0e5527
--- /dev/null
+++ b/backend/utils/permissions.go
@@ -0,0 +1,49 @@
+package utils
+
+import (
+ "errors"
+ "strconv"
+
+ "github.com/gin-gonic/gin"
+)
+
+// We define user roles as bit flags to allow for easy combination and checking of permissions.
+
+const (
+ ADMINISTRATOR = 1 << 1
+ MANAGER = 1 << 2
+ MODERATOR = 1 << 3
+)
+
+// CheckPermission checks if the user has the required permission.
+func CheckPermission(userRole, requiredRole int) bool {
+ return userRole&requiredRole == requiredRole
+}
+
+func GetUserIdParam(param string, ctx *gin.Context) (int64, error) {
+ if param == "" {
+ return 0, errors.New("user ID parameter is empty")
+ }
+ if param != "@me" {
+ id_param, err := strconv.ParseInt(ctx.Param("id"), 10, 64)
+ if err != nil {
+ return 0, err
+ }
+ return id_param, nil
+ }
+
+ return GetUserIdCtx(ctx)
+}
+
+func GetUserIdCtx(ctx *gin.Context) (int64, error) {
+ userId, exists := ctx.Get("user_id")
+ if !exists {
+ return 0, errors.New("user ID not found in context")
+ }
+ id, ok := userId.(int64)
+ if !ok {
+ return 0, errors.ErrUnsupported
+ }
+ return id, nil
+
+}
diff --git a/backend/utils/permissions_test.go b/backend/utils/permissions_test.go
new file mode 100644
index 00000000..1f40901b
--- /dev/null
+++ b/backend/utils/permissions_test.go
@@ -0,0 +1,50 @@
+package utils_test
+
+import (
+ "Smaug6739/Alexandrie/utils"
+ "testing"
+)
+
+func TestCheckPermission(t *testing.T) {
+ tests := []struct {
+ userRole int
+ requiredRole int
+ expected bool
+ }{
+ {utils.ADMINISTRATOR, utils.ADMINISTRATOR, true},
+ {utils.ADMINISTRATOR | utils.MANAGER, utils.MANAGER, true},
+ {utils.MANAGER, utils.ADMINISTRATOR, false},
+ {0, utils.ADMINISTRATOR, false},
+ {1, utils.ADMINISTRATOR, false},
+ {1, utils.MANAGER, false},
+ {1, utils.MODERATOR, false},
+ {2, utils.ADMINISTRATOR, true},
+ {2, utils.MANAGER, false},
+ {2, utils.MODERATOR, false},
+ }
+
+ for _, test := range tests {
+ result := utils.CheckPermission(test.userRole, test.requiredRole)
+ if result != test.expected {
+ t.Errorf("CheckPermission(%d, %d) = %v; expected %v", test.userRole, test.requiredRole, result, test.expected)
+ }
+ }
+}
+func TestCheckPermissionWithMultipleRoles(t *testing.T) {
+ tests := []struct {
+ userRole int
+ requiredRole int
+ expected bool
+ }{
+ {utils.ADMINISTRATOR | utils.MANAGER, utils.MANAGER, true},
+ {utils.MANAGER | utils.MODERATOR, utils.MODERATOR, true},
+ {utils.MODERATOR, utils.MANAGER, false},
+ }
+
+ for _, test := range tests {
+ result := utils.CheckPermission(test.userRole, test.requiredRole)
+ if result != test.expected {
+ t.Errorf("CheckPermission(%d, %d) = %v; expected %v", test.userRole, test.requiredRole, result, test.expected)
+ }
+ }
+}