diff --git a/app/classes/controllers/users_controller.py b/app/classes/controllers/users_controller.py index 667e01b4..99147a63 100644 --- a/app/classes/controllers/users_controller.py +++ b/app/classes/controllers/users_controller.py @@ -31,7 +31,7 @@ class UsersController: for permission in PermissionsCrafty.get_permissions_list() ], }, - "quantity": {"type": "number", "minimum": 0}, + "quantity": {"type": "number", "minimum": -1}, "enabled": {"type": "boolean"}, } self.user_jsonschema_props: t.Final = { @@ -46,7 +46,7 @@ class UsersController: "password": { "type": "string", "maxLength": 20, - "minLength": 4, + "minLength": 6, "examples": ["crafty"], "title": "Password", }, @@ -73,6 +73,8 @@ class UsersController: "examples": [False], "title": "Superuser", }, + "manager": {"type": ["integer", "null"]}, + "theme": {"type": "string"}, "permissions": { "type": "array", "items": { @@ -84,7 +86,7 @@ class UsersController: "roles": { "type": "array", "items": { - "type": "string", + "type": "integer", "minLength": 1, }, }, diff --git a/app/classes/web/routes/api/users/index.py b/app/classes/web/routes/api/users/index.py index a1f849ef..f7341d38 100644 --- a/app/classes/web/routes/api/users/index.py +++ b/app/classes/web/routes/api/users/index.py @@ -93,10 +93,17 @@ class ApiUsersIndexHandler(BaseApiHandler): "error_data": str(e), }, ) - username = data["username"] username = str(username).lower() - manager = int(user["user_id"]) + manager = data.get("manager", None) + if user["superuser"]: + if ( + manager == self.controller.users.get_id_by_name("SYSTEM") + or manager == 0 + ): + manager = None + else: + manager = int(user["user_id"]) password = data["password"] email = data.get("email", "default@example.com") enabled = data.get("enabled", True) diff --git a/app/classes/web/routes/api/users/user/index.py b/app/classes/web/routes/api/users/user/index.py index 47d8dd68..898a9fda 100644 --- a/app/classes/web/routes/api/users/user/index.py +++ b/app/classes/web/routes/api/users/user/index.py @@ -166,7 +166,13 @@ class ApiUsersUserIndexHandler(BaseApiHandler): return self.finish_json( 400, {"status": "error", "error": "INVALID_USERNAME"} ) - if self.controller.users.get_id_by_name(data["username"]) is not None: + if self.controller.users.get_id_by_name( + data["username"] + ) is not None and self.controller.users.get_id_by_name( + data["username"] + ) != int( + user_id + ): return self.finish_json( 400, {"status": "error", "error": "USER_EXISTS"} ) @@ -210,13 +216,13 @@ class ApiUsersUserIndexHandler(BaseApiHandler): 400, {"status": "error", "error": "INVALID_ROLES_MODIFY"} ) - if "password" in data and str(user["user_id"] == str(user_id)): - # TODO: edit your own password - return self.finish_json( - 400, {"status": "error", "error": "INVALID_PASSWORD_MODIFY"} - ) - user_obj = HelperUsers.get_user_model(user_id) + if "password" in data and str(user["user_id"]) != str(user_id): + if str(user["user_id"]) != str(user_obj.manager): + # TODO: edit your own password + return self.finish_json( + 400, {"status": "error", "error": "INVALID_PASSWORD_MODIFY"} + ) if "roles" in data: roles: t.Set[str] = set(data.pop("roles")) @@ -236,6 +242,13 @@ class ApiUsersUserIndexHandler(BaseApiHandler): user_id, removed_roles ) + if "manager" in data: + if ( + data["manager"] == self.controller.users.get_id_by_name("SYSTEM") + or data["manager"] == 0 + ): + data["manager"] = None + if "permissions" in data: permissions: t.List[UsersController.ApiPermissionDict] = data.pop( "permissions" @@ -246,7 +259,7 @@ class ApiUsersUserIndexHandler(BaseApiHandler): limit_role_creation = 0 for permission in permissions: - self.controller.crafty_perms.set_permission( + permissions_mask = self.controller.crafty_perms.set_permission( permissions_mask, EnumPermissionsCrafty.__members__[permission["name"]], "1" if permission["enabled"] else "0", diff --git a/app/frontend/templates/panel/panel_edit_role.html b/app/frontend/templates/panel/panel_edit_role.html index db760201..f614b7a3 100644 --- a/app/frontend/templates/panel/panel_edit_role.html +++ b/app/frontend/templates/panel/panel_edit_role.html @@ -49,7 +49,7 @@