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.

Fix permissions system

Smaug67399532b07199c8
diff --git a/backend/controllers/users.controller.go b/backend/controllers/users.controller.go
index b8f7759b..b3738aad 100644
--- a/backend/controllers/users.controller.go
+++ b/backend/controllers/users.controller.go
@@ -153,9 +153,9 @@ func (ctr *UserControllerImpl) CreateUser(c *gin.Context) (int, any) {
// @Failure 400 {object} Error
// @Failure 401 {object} Error
func (ctr *UserControllerImpl) UpdateUser(c *gin.Context) (int, any) {
- id, err := utils.GetUserIdParam(c.Param("id"), c)
+ id, err := utils.SelfOrPermission(c, utils.ADMINISTRATOR)
if err != nil {
- return http.StatusBadRequest, errors.New("invalid user ID")
+ return http.StatusBadRequest, err
}
var user models.User
if err := c.ShouldBind(&user); err != nil {
@@ -197,19 +197,10 @@ 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 := utils.GetUserIdParam(c.Param("id"), c)
+ id, err := utils.SelfOrPermission(c, utils.ADMINISTRATOR)
if err != nil {
return http.StatusBadRequest, err
}
- userId, err := utils.GetUserIdCtx(c)
- if err != nil {
- 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
index d2f9f739..642a71d6 100644
--- a/backend/utils/permissions.go
+++ b/backend/utils/permissions.go
@@ -31,7 +31,6 @@ func GetUserIdParam(param string, ctx *gin.Context) (int64, error) {
}
return id_param, nil
}
-
return GetUserIdCtx(ctx)
}
@@ -45,5 +44,30 @@ func GetUserIdCtx(ctx *gin.Context) (int64, error) {
return 0, errors.ErrUnsupported
}
return id, nil
+}
+func SelfOrPermission(ctx *gin.Context, allowed int) (int64, error) {
+ // Check if the c.Param("id") is "@me" or c.Param("id") is the same as the user ID in the context
+ // If not, check if the user has the required permission
+ // If one of the above is true, return the user ID from the context
+ // Otherwise, return an error
+ id_param, err := GetUserIdParam(ctx.Param("id"), ctx)
+ if err != nil {
+ return 0, err
+ }
+ userId, err := GetUserIdCtx(ctx)
+ if err != nil {
+ return 0, err
+ }
+ if id_param == userId {
+ return id_param, nil
+ }
+ role, exists := ctx.Get("user_role")
+ if !exists {
+ return 0, errors.New("user role not found in context")
+ }
+ if id_param != userId && CheckPermission(role.(int), allowed) {
+ return id_param, nil
+ }
+ return 0, errors.New("unauthorized access")
}