diff --git a/app/classes/controllers/users_controller.py b/app/classes/controllers/users_controller.py index c3c90b2f..c62d209d 100644 --- a/app/classes/controllers/users_controller.py +++ b/app/classes/controllers/users_controller.py @@ -132,6 +132,10 @@ class UsersController: def set_support_path(user_id, support_path): HelperUsers.set_support_path(user_id, support_path) + @staticmethod + def get_managed_users(exec_user_id): + return HelperUsers.get_managed_users(exec_user_id) + def update_user(self, user_id: str, user_data=None, user_crafty_data=None): if user_crafty_data is None: user_crafty_data = {} @@ -206,6 +210,7 @@ class UsersController: def add_user( self, username, + manager, password, email="default@example.com", enabled: bool = True, @@ -213,6 +218,7 @@ class UsersController: ): return self.users_helper.add_user( username, + manager, password=password, email=email, enabled=enabled, @@ -236,6 +242,8 @@ class UsersController: ) def remove_user(self, user_id): + for user in self.get_managed_users(user_id): + self.update_user(user.user_id, {"manager": None}) return self.users_helper.remove_user(user_id) @staticmethod diff --git a/app/classes/models/users.py b/app/classes/models/users.py index ac204e3c..c3d2a3a8 100644 --- a/app/classes/models/users.py +++ b/app/classes/models/users.py @@ -6,6 +6,7 @@ from peewee import ( ForeignKeyField, CharField, AutoField, + IntegerField, DateTimeField, BooleanField, CompositeKey, @@ -40,6 +41,7 @@ class Users(BaseModel): server_order = CharField(default="") preparing = BooleanField(default=False) hints = BooleanField(default=True) + manager = IntegerField(default=None, null=True) class Meta: table_name = "users" @@ -138,6 +140,11 @@ class HelperUsers: user_query = Users.select().where(Users.user_id == user_id) return user_query + @staticmethod + def get_managed_users(exec_user_id): + user_query = Users.select().where(Users.manager == exec_user_id) + return user_query + @staticmethod def get_user(user_id): if user_id == 0: @@ -192,6 +199,7 @@ class HelperUsers: def add_user( self, username: str, + manager: str, password: str = None, email: t.Optional[str] = None, enabled: bool = True, @@ -209,6 +217,7 @@ class HelperUsers: Users.enabled: enabled, Users.superuser: superuser, Users.created: Helpers.get_time_as_string(), + Users.manager: manager, } ).execute() return user_id @@ -216,6 +225,7 @@ class HelperUsers: @staticmethod def add_rawpass_user( username: str, + manager: str, password: str = "", email: t.Optional[str] = "default@example.com", enabled: bool = True, @@ -229,6 +239,7 @@ class HelperUsers: Users.enabled: enabled, Users.superuser: superuser, Users.created: Helpers.get_time_as_string(), + Users.manager: manager, } ).execute() return user_id diff --git a/app/classes/web/api_handler.py b/app/classes/web/api_handler.py index 43af4ae8..34b09ee8 100644 --- a/app/classes/web/api_handler.py +++ b/app/classes/web/api_handler.py @@ -340,10 +340,11 @@ class CreateUser(ApiHandler): new_username = self.get_argument("username").lower() new_pass = self.get_argument("password") + manager = int(user_obj["user_id"]) if new_username: self.controller.users.add_user( - new_username, new_pass, "default@example.com", True, False + new_username, manager, new_pass, "default@example.com", True, False ) self.return_response( diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index a5e56029..624450c5 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -860,6 +860,11 @@ class PanelHandler(BaseHandler): page_data["users"] = self.controller.users.get_all_users() page_data["roles"] = self.controller.roles.get_all_roles() page_data["auth-servers"][user.user_id] = super_auth_servers + page_data["managed_users"] = [] + else: + page_data["managed_users"] = self.controller.users.get_managed_users( + exec_user["user_id"] + ) template = "panel/panel_config.html" @@ -904,8 +909,14 @@ class PanelHandler(BaseHandler): ) if superuser: page_data["super-disabled"] = "" + page_data["users"] = self.controller.users.get_all_users() else: page_data["super-disabled"] = "disabled" + + page_data["manager"] = { + "user_id": -100, + "username": "None", + } for file in sorted( os.listdir(os.path.join(self.helper.root_dir, "app", "translations")) ): @@ -1077,6 +1088,17 @@ class PanelHandler(BaseHandler): page_data["roles_all"] = self.controller.roles.get_all_roles() page_data["servers_all"] = self.controller.servers.get_all_defined_servers() page_data["superuser"] = superuser + if page_data["user"]["manager"] is not None: + page_data["manager"] = self.controller.users.get_user_by_id( + page_data["user"]["manager"] + ) + else: + page_data["manager"] = { + "user_id": -100, + "username": "None", + } + if exec_user["superuser"]: + page_data["users"] = self.controller.users.get_all_users() page_data[ "permissions_all" ] = self.controller.crafty_perms.list_defined_crafty_permissions() @@ -1115,6 +1137,13 @@ class PanelHandler(BaseHandler): "/panel/error?error=Unauthorized access: not a user editor" ) return + if ( + self.controller.users.get_user_by_id(user_id)["manager"] + != exec_user["user_id"] + ) and not exec_user["superuser"]: + self.redirect( + "/panel/error?error=Unauthorized access: you cannot edit this user" + ) page_data["servers"] = [] page_data["role-servers"] = [] @@ -1962,6 +1991,13 @@ class PanelHandler(BaseHandler): else: superuser = 0 + if exec_user["superuser"]: + manager = self.get_argument("manager") + if manager == "": + manager = None + else: + manager = int(manager) + if not exec_user["superuser"]: if username is None or username == "": self.redirect("/panel/error?error=Invalid username") @@ -2013,6 +2049,7 @@ class PanelHandler(BaseHandler): user_data = { "username": username, + "manager": manager, "password": password0, "email": email, "enabled": enabled, @@ -2158,6 +2195,15 @@ class PanelHandler(BaseHandler): if username is None or username == "": self.redirect("/panel/error?error=Invalid username") return + + if exec_user["superuser"]: + manager = self.get_argument("manager") + if manager == "": + manager = None + else: + manager = int(manager) + else: + manager = int(exec_user["user_id"]) # does this user id exist? if self.controller.users.get_id_by_name(username) is not None: self.redirect("/panel/error?error=User exists") @@ -2172,6 +2218,7 @@ class PanelHandler(BaseHandler): user_id = self.controller.users.add_user( username, + manager=manager, password=password0, email=email, enabled=enabled, diff --git a/app/classes/web/routes/api/users/index.py b/app/classes/web/routes/api/users/index.py index 3e4cfdab..6f46740e 100644 --- a/app/classes/web/routes/api/users/index.py +++ b/app/classes/web/routes/api/users/index.py @@ -96,6 +96,7 @@ class ApiUsersIndexHandler(BaseApiHandler): username = data["username"] username = str(username).lower() + manager = int(user["user_id"]) password = data["password"] email = data.get("email", "default@example.com") enabled = data.get("enabled", True) @@ -149,6 +150,7 @@ class ApiUsersIndexHandler(BaseApiHandler): # TODO: do this in the most efficient way user_id = self.controller.users.add_user( username, + manager, password, email, enabled, diff --git a/app/frontend/templates/panel/panel_config.html b/app/frontend/templates/panel/panel_config.html index 94d5c0f1..0c89a596 100644 --- a/app/frontend/templates/panel/panel_config.html +++ b/app/frontend/templates/panel/panel_config.html @@ -87,6 +87,38 @@