async def apiAdminRolesCreate(cls: "WebIndex", WebRequest: Request) -> Response: Data: WebRequestContent = WebRequestContent(WebRequest) await Data.load() # get required stuff name: str = Data.getStr("name", "") description: str = Data.getStr("description", "") can_be_removed: bool = Data.getBool("can_be_removed", True) # checks if not name: return await apiMissingData(cls, WebRequest, msg="missing or invalid 'name'") res: list = cls.Web.BASE.PhaazeDB.selectQuery( "SELECT COUNT(*) AS `i` FROM `role` WHERE LOWER(`role`.`name`) = %s", (name, )) if res[0]['i'] != 0: return await apiWrongData(cls, WebRequest, msg=f"role '{name}' already exists") cls.Web.BASE.PhaazeDB.insertQuery( table="role", content=dict(name=name, description=description, can_be_removed=validateDBInput(bool, can_be_removed))) return cls.response(text=json.dumps( dict(msg="role successfull created", role=name, status=200)), content_type="application/json", status=200)
async def apiDiscordCommandsEdit(cls: "WebIndex", WebRequest: Request) -> Response: """ Default url: /api/discord/commands/edit """ Data: WebRequestContent = WebRequestContent(WebRequest) await Data.load() # get required stuff guild_id: str = Data.getStr("guild_id", "", must_be_digit=True) command_id: str = Data.getStr("command_id", "", must_be_digit=True) # checks if not guild_id: return await apiMissingData(cls, WebRequest, msg="missing or invalid 'guild_id'") if not command_id: return await apiMissingData(cls, WebRequest, msg="missing or invalid 'command_id'") # get command res_commands: list = await getDiscordServerCommands(cls.Web.BASE.Discord, guild_id, command_id=command_id, show_nonactive=True) if not res_commands: return await apiDiscordCommandNotExists(cls, WebRequest, command_id=command_id) CurrentEditCommand: DiscordCommand = res_commands.pop(0) # after this point we have a existing command # check all possible values if it should be edited # db_update is for the database # update is the return for the user db_update: dict = dict() update: dict = dict() # active value: bool = Data.getBool("active", UNDEFINED) if value != UNDEFINED: db_update["active"] = validateDBInput(bool, value) update["active"] = value # trigger value: str = Data.getStr("trigger", "").lower().split(" ")[0] if value: # try to get command with this trigger check_double_trigger: list = await getDiscordServerCommands( cls.Web.BASE.Discord, guild_id, trigger=value) if check_double_trigger: CommandToCheck: DiscordCommand = check_double_trigger.pop(0) # tryed to set a trigger twice if str(CommandToCheck.command_id) != str( CurrentEditCommand.command_id): return await apiDiscordCommandExists(cls, WebRequest, command=value) db_update["trigger"] = validateDBInput(str, value) update["trigger"] = value # cooldown value: int = Data.getInt("cooldown", UNDEFINED, min_x=0) if value != UNDEFINED: # wants a invalid cooldown if not (cls.Web.BASE.Limit.DISCORD_COMMANDS_COOLDOWN_MIN <= value <= cls.Web.BASE.Limit.DISCORD_COMMANDS_COOLDOWN_MAX): return await apiWrongData(cls, WebRequest, msg="'cooldown' is wrong") db_update["cooldown"] = validateDBInput(int, value) update["cooldown"] = value # currency value: int = Data.getInt("required_currency", UNDEFINED, min_x=0) if value != UNDEFINED: db_update["required_currency"] = validateDBInput(int, value) update["required_currency"] = value # require value: int = Data.getInt("require", UNDEFINED, min_x=0) if value != UNDEFINED: db_update["require"] = validateDBInput(int, value) update["require"] = value # content value: str = Data.getStr("content", UNDEFINED) if value != UNDEFINED: db_update["content"] = validateDBInput(str, value) update["content"] = value # hidden value: bool = Data.getBool("hidden", UNDEFINED) if value != UNDEFINED: db_update["hidden"] = validateDBInput(bool, value) update["hidden"] = value # function (and type) complex_: bool = Data.getBool("complex", False) function: str = Data.getStr("function", UNDEFINED) if (complex_ == False) and (function != UNDEFINED): # it its not complex, we need a function if not function in [ cmd["function"].__name__ for cmd in command_register ]: return await apiWrongData( cls, WebRequest, msg=f"'{function}' is not a valid value for field 'function'") db_update["complex"] = validateDBInput(bool, complex_) db_update["function"] = validateDBInput(str, function) update["complex"] = complex_ update["function"] = function if (complex_ == True) and (not function): # it is complex, we need a content if not update.get("content", False): return await apiWrongData( cls, WebRequest, msg=f"'complex' is true, but missing 'content'") db_update["complex"] = validateDBInput(bool, complex_) db_update["function"] = "" update["complex"] = complex_ # get/check discord PhaazeDiscord: "PhaazebotDiscord" = cls.Web.BASE.Discord Guild: discord.Guild = discord.utils.get(PhaazeDiscord.guilds, id=int(guild_id)) if not Guild: return await apiDiscordGuildUnknown(cls, WebRequest) # get user info DiscordUser: DiscordWebUserInfo = await cls.getDiscordUserInfo(WebRequest) if not DiscordUser.found: return await apiMissingAuthorisation(cls, WebRequest) # get member CheckMember: discord.Member = Guild.get_member(int(DiscordUser.user_id)) if not CheckMember: return await apiDiscordMemberNotFound(cls, WebRequest, guild_id=guild_id, user_id=DiscordUser.user_id) # check permissions if not (CheckMember.guild_permissions.administrator or CheckMember.guild_permissions.manage_guild): return await apiDiscordMissingPermission(cls, WebRequest, guild_id=guild_id, user_id=DiscordUser.user_id) if not update: return await apiWrongData(cls, WebRequest, msg=f"No changes, please add at least one") cls.Web.BASE.PhaazeDB.updateQuery( table="discord_command", content=db_update, where= "`discord_command`.`guild_id` = %s AND `discord_command`.`id` = %s", where_values=(guild_id, command_id)) cls.Web.BASE.Logger.debug( f"(API/Discord) Edited command: S:{guild_id} C:{command_id} U:{str(db_update)}", require="discord:commands") return cls.response(text=json.dumps( dict(msg="new command successfull edited", command=CurrentEditCommand.trigger, changes=update, status=200)), content_type="application/json", status=200)
async def apiAdminUsersEdit(cls: "WebIndex", WebRequest: Request) -> Response: """ Default url: /api/admin/users/edit """ Data: WebRequestContent = WebRequestContent(WebRequest) await Data.load() # get required stuff user_id: str = Data.getStr("user_id", "", must_be_digit=True) # checks if not user_id: return await apiMissingData(cls, WebRequest, msg="missing or invalid 'user_id'") # single actions action: str = Data.getStr("userrole_action", UNDEFINED) if action: return await singleActionUserRole(cls, WebRequest, action, Data) # get user that should be edited check_user: list = await getWebUsers(cls, where="user.id = %s", values=(user_id, )) if not check_user: return await apiUserNotFound(cls, WebRequest, msg=f"no user found with id: {user_id}") # check if this is a (super)admin, if so, is the current user a superadmin? # and not himself UserToEdit: WebUserInfo = check_user.pop(0) CurrentUser: WebUserInfo = await cls.getWebUserInfo(WebRequest) if UserToEdit.checkRoles(["superadmin", "admin"]): if UserToEdit.user_id != CurrentUser.user_id: if not CurrentUser.checkRoles(["superadmin"]): return await apiNotAllowed( cls, WebRequest, msg=f"Only Superadmin's can edit other (Super)admin user") # check all update values update: dict = dict() db_update: dict = dict() # username value: str = Data.getStr("username", UNDEFINED) if value != UNDEFINED: db_update["username"] = validateDBInput(str, value) update["username"] = value # email value: str = Data.getStr("email", UNDEFINED) if value != UNDEFINED: db_update["email"] = validateDBInput(str, value) update["email"] = value # password value: str = Data.getStr("password", UNDEFINED) if value: # aka non empty string and not UNDEFINED value = password_function(value) db_update["password"] = validateDBInput(str, value) update["password"] = value # verified value: bool = Data.getBool("verified", UNDEFINED) if value != UNDEFINED: db_update["verified"] = validateDBInput(bool, value) update["verified"] = value if not db_update: return await apiMissingData(cls, WebRequest, msg="No changes, please add at least one") cls.Web.BASE.Logger.debug( f"(API) Config User U:{user_id} {str(db_update)}", require="api:user") cls.Web.BASE.PhaazeDB.updateQuery(table="user", content=db_update, where="`user`.`id` = %s", where_values=(user_id, )) return cls.response(text=json.dumps( dict(msg="user successfull updated", update=update, status=200)), content_type="application/json", status=200)
async def apiDiscordAssignrolesEdit(cls: "WebIndex", WebRequest: Request) -> Response: """ Default url: /api/discord/assignroles/edit """ Data: WebRequestContent = WebRequestContent(WebRequest) await Data.load() # get required stuff guild_id: str = Data.getStr("guild_id", "", must_be_digit=True) assignrole_id: str = Data.getStr("assignrole_id", "", must_be_digit=True) # checks if not guild_id: return await apiMissingData(cls, WebRequest, msg="missing or invalid 'guild_id'") if not assignrole_id: return await apiMissingData(cls, WebRequest, msg="missing or invalid 'assignrole_id'") PhaazeDiscord: "PhaazebotDiscord" = cls.Web.BASE.Discord Guild: discord.Guild = discord.utils.get(PhaazeDiscord.guilds, id=int(guild_id)) if not Guild: return await apiDiscordGuildUnknown(cls, WebRequest) # check if exists res_assignroles: list = await getDiscordServerAssignRoles( PhaazeDiscord, guild_id, assignrole_id=assignrole_id) if not res_assignroles: return await apiDiscordAssignRoleNotExists(cls, WebRequest) AssignRoleToEdit: DiscordAssignRole = res_assignroles.pop(0) # check all update values db_update: dict = dict() update: dict = dict() value: str = Data.getStr("role_id", UNDEFINED, must_be_digit=True) if value != UNDEFINED: AssignRole: discord.Role = getDiscordRoleFromString(cls, Guild, value) if not AssignRole: return await apiMissingData( cls, WebRequest, msg=f"Could not find any role matching '{value}'") if AssignRole > Guild.me.top_role: return await apiWrongData( cls, WebRequest, msg=f"The Role `{AssignRole.name}` is to high") db_update["role_id"] = validateDBInput(str, value) update["role_id"] = value value: str = Data.getStr("trigger", "").lower().split(" ")[0] if value: # try to get command with this trigger check_double_trigger: list = await getDiscordServerAssignRoles( cls.Web.BASE.Discord, guild_id, trigger=value) if check_double_trigger: AssignRoleToCheck: DiscordAssignRole = check_double_trigger.pop(0) # tryed to set a trigger twice if str(AssignRoleToEdit.assignrole_id) != str( AssignRoleToCheck.assignrole_id): return await apiDiscordAssignRoleExists(cls, WebRequest, trigger=value) db_update["trigger"] = validateDBInput(str, value) update["trigger"] = value # get user info DiscordUser: DiscordWebUserInfo = await cls.getDiscordUserInfo(WebRequest) if not DiscordUser.found: return await apiMissingAuthorisation(cls, WebRequest) # get member CheckMember: discord.Member = Guild.get_member(int(DiscordUser.user_id)) if not CheckMember: return await apiDiscordMemberNotFound(cls, WebRequest, guild_id=guild_id, user_id=DiscordUser.user_id) # check permissions if not (CheckMember.guild_permissions.administrator or CheckMember.guild_permissions.manage_guild): return await apiDiscordMissingPermission(cls, WebRequest, guild_id=guild_id, user_id=DiscordUser.user_id) if not update: return await apiWrongData(cls, WebRequest, msg=f"No changes, please add at least one") cls.Web.BASE.PhaazeDB.updateQuery( table="discord_assignrole", content=db_update, where= "`discord_assignrole`.`guild_id` = %s AND `discord_assignrole`.`id` = %s", where_values=(AssignRoleToEdit.guild_id, AssignRoleToEdit.assignrole_id)) cls.Web.BASE.Logger.debug( f"(API/Discord) Created new assign role: S:{guild_id} T:{assignrole_id} N:{str(update)}", require="discord:role") return cls.response(text=json.dumps( dict(msg="assign role successfull updated", changes=update, status=200)), content_type="application/json", status=200)
async def apiDiscordLevelsEdit(cls: "WebIndex", WebRequest: Request) -> Response: """ Default url: /api/discord/levels/edit """ Data: WebRequestContent = WebRequestContent(WebRequest) await Data.load() # get required stuff guild_id: str = Data.getStr("guild_id", "", must_be_digit=True) member_id: str = Data.getStr("member_id", "", must_be_digit=True) # checks if not guild_id: return await apiMissingData(cls, WebRequest, msg="missing or invalid 'guild_id'") if not member_id: return await apiMissingData(cls, WebRequest, msg="missing or invalid 'member_id'") PhaazeDiscord: "PhaazebotDiscord" = cls.Web.BASE.Discord Guild: discord.Guild = discord.utils.get(PhaazeDiscord.guilds, id=int(guild_id)) if not Guild: return await apiDiscordGuildUnknown(cls, WebRequest) # get user info DiscordUser: DiscordWebUserInfo = await cls.getDiscordUserInfo(WebRequest) if not DiscordUser.found: return await apiMissingAuthorisation(cls, WebRequest) # get member CheckMember: discord.Member = Guild.get_member(int(DiscordUser.user_id)) if not CheckMember: return await apiDiscordMemberNotFound(cls, WebRequest, guild_id=guild_id, user_id=DiscordUser.user_id) # check permissions # to edit configs, at least moderator rights are needed, (there can be options that require server only duh) if not (CheckMember.guild_permissions.administrator or CheckMember.guild_permissions.manage_guild): return await apiDiscordMissingPermission(cls, WebRequest, guild_id=guild_id, user_id=DiscordUser.user_id) # get level user res_level: list = await getDiscordServerUsers(PhaazeDiscord, guild_id=guild_id, member_id=member_id) if not res_level: return await apiDiscordGuildUnknown( cls, WebRequest, msg="Could not find a level for this user") CurrentLevelUser: DiscordUserStats = res_level.pop(0) # single actions action: str = Data.getStr("medal_action", UNDEFINED) if action: return await singleActionMedal(cls, WebRequest, action, Data, CurrentLevelUser) changes: dict = dict() db_changes: dict = dict() # get all changed stuff and add changes to list # changes is for the user, so return with types, db_changes is with, well... db values # e.g.: # changes["x"] = true # db_changes["x"] = "1" # currency value: int = Data.getInt("currency", UNDEFINED, min_x=0) if value != UNDEFINED: db_changes["currency"] = validateDBInput(int, value) changes["currency"] = value # exp value: int = Data.getInt("exp", UNDEFINED, min_x=0) if value != UNDEFINED: db_changes["exp"] = validateDBInput(int, value) changes["exp"] = value if value != 0: db_changes["edited"] = validateDBInput(bool, True) changes["edited"] = True else: db_changes["edited"] = validateDBInput(bool, False) changes["edited"] = False # on_server value: bool = Data.getBool("on_server", UNDEFINED) if value != UNDEFINED: if not Guild.owner == CheckMember: return await apiDiscordMissingPermission( cls, WebRequest, guild_id=guild_id, user_id=DiscordUser.user_id, msg="changing 'on_server' require server owner") db_changes["on_server"] = validateDBInput(bool, value) changes["on_server"] = value if not db_changes: return await apiMissingData(cls, WebRequest, msg="No changes, please add at least one") cls.Web.BASE.Logger.debug( f"(API/Discord) Level Update: S:{guild_id} M:{member_id} {str(db_changes)}", require="discord:levels") cls.Web.BASE.PhaazeDB.updateQuery( table="discord_user", content=db_changes, where="discord_user.guild_id = %s AND discord_user.member_id = %s", where_values=(guild_id, member_id)) return cls.response(text=json.dumps( dict(msg="level successfull updated", changes=changes, status=200)), content_type="application/json", status=200)
async def apiDiscordConfigsEdit(cls:"WebIndex", WebRequest:Request) -> Response: """ Default url: /api/discord/configs/edit """ Data:WebRequestContent = WebRequestContent(WebRequest) await Data.load() # get required stuff guild_id:str = Data.getStr("guild_id", "", must_be_digit=True) # checks if not guild_id: return await apiMissingData(cls, WebRequest, msg="missing or invalid 'guild_id'") PhaazeDiscord:"PhaazebotDiscord" = cls.Web.BASE.Discord Guild:discord.Guild = discord.utils.get(PhaazeDiscord.guilds, id=int(guild_id)) if not Guild: return await apiDiscordGuildUnknown(cls, WebRequest) # get user info DiscordUser:DiscordWebUserInfo = await cls.getDiscordUserInfo(WebRequest) if not DiscordUser.found: return await apiMissingAuthorisation(cls, WebRequest) # get member CheckMember:discord.Member = Guild.get_member(int(DiscordUser.user_id)) if not CheckMember: return await apiDiscordMemberNotFound(cls, WebRequest, guild_id=guild_id, user_id=DiscordUser.user_id) # check permissions # to edit configs, at least moderator rights are needed, (there can be options that require server only duh) if not (CheckMember.guild_permissions.administrator or CheckMember.guild_permissions.manage_guild): return await apiDiscordMissingPermission(cls, WebRequest, guild_id=guild_id, user_id=DiscordUser.user_id) Configs:DiscordServerSettings = await getDiscordSeverSettings(PhaazeDiscord, origin=guild_id, prevent_new=True) if not Configs: return await apiDiscordGuildUnknown(cls, WebRequest, msg="Could not find configs for this guild") # single actions action:str = Data.getStr("wordblacklist_action", UNDEFINED) if action: return await singleActionWordBlacklist(cls, WebRequest, action, Data, Configs) action:str = Data.getStr("linkwhitelist_action", UNDEFINED) if action: return await singleActionLinkWhitelist(cls, WebRequest, action, Data, Configs) action:str = Data.getStr("exceptionrole_action", UNDEFINED) if action: return await singleActionExceptionRole(cls, WebRequest, action, Data, Configs, Guild) action:str = Data.getStr("disabled_levelchan_action", UNDEFINED) if action: return await singleActionDisableLevelChannel(cls, WebRequest, action, Data, Configs, Guild) action:str = Data.getStr("disabled_quotechan_action", UNDEFINED) if action: return await singleActionDisableQuoteChannel(cls, WebRequest, action, Data, Configs, Guild) action:str = Data.getStr("disabled_normalchan_action", UNDEFINED) if action: return await singleActionDisableNormalChannel(cls, WebRequest, action, Data, Configs, Guild) action:str = Data.getStr("disabled_regularchan_action", UNDEFINED) if action: return await singleActionDisableRegularChannel(cls, WebRequest, action, Data, Configs, Guild) action:str = Data.getStr("enabled_gamechan_action", UNDEFINED) if action: return await singleActionEnableGameChannel(cls, WebRequest, action, Data, Configs, Guild) action:str = Data.getStr("enabled_nsfwchan_action", UNDEFINED) if action: return await singleActionEnableNSFWChannel(cls, WebRequest, action, Data, Configs, Guild) update:dict = dict() db_update:dict = dict() # get all changed stuff and add changes to list # changes is for the user, so return with types, db_changes is with, well... db values # e.g.: # update["x"] = true # db_update["x"] = "1" # blacklist_ban_links value:bool = Data.getBool("blacklist_ban_links", UNDEFINED) if value != UNDEFINED: db_update["blacklist_ban_links"] = validateDBInput(bool, value) update["blacklist_ban_links"] = value # blacklist_punishment value:str = Data.getStr("blacklist_punishment", UNDEFINED) if value != UNDEFINED: value = checkBlacklistPunishmentString(value) db_update["blacklist_punishment"] = validateDBInput(str, value) update["blacklist_punishment"] = value # leave_chan value:str = Data.getStr("leave_chan", UNDEFINED) if value != UNDEFINED: error:bool = False if not value: value = None elif value.isdigit(): Chan:discord.abc.Messageable = discord.utils.get(Guild.channels, id=int(value)) if type(Chan) != discord.TextChannel: error = True else: value = str(Chan.id) else: error = True if error: return await apiWrongData(cls, WebRequest, msg=f"'{value}' could not be resolved as a valid discord text channel id") db_update["leave_chan"] = validateDBInput(str, value, allow_null=True) update["leave_chan"] = value # leave_msg value:str = Data.getStr("leave_msg", UNDEFINED) if value != UNDEFINED: if not value: value = None db_update["leave_msg"] = validateDBInput(str, value, allow_null=True) update["leave_msg"] = value # level_custom_msg value:str = Data.getStr("level_custom_msg", UNDEFINED) if value != UNDEFINED: if not value: value = None db_update["level_custom_msg"] = validateDBInput(str, value, allow_null=True) update["level_custom_msg"] = value # level_announce_chan value:str = Data.getStr("level_announce_chan", UNDEFINED) if value != UNDEFINED: error:bool = False if not value: value = None elif value.isdigit(): Chan:discord.abc.Messageable = discord.utils.get(Guild.channels, id=int(value)) if type(Chan) != discord.TextChannel: error = True else: value = str(Chan.id) else: error = True if error: return await apiWrongData(cls, WebRequest, msg=f"'{value}' could not be resolved as a valid discord text channel id") db_update["level_announce_chan"] = validateDBInput(str, value, allow_null=True) update["level_announce_chan"] = value # owner_disable_level value:bool = Data.getBool("owner_disable_level", UNDEFINED) if value != UNDEFINED: if not Guild.owner == CheckMember: return await apiDiscordMissingPermission(cls, WebRequest, guild_id=guild_id, user_id=DiscordUser.user_id, msg="changing 'owner_disable_level' require server owner") db_update["owner_disable_level"] = validateDBInput(bool, value) update["owner_disable_level"] = value # owner_disable_normal value:bool = Data.getBool("owner_disable_normal", UNDEFINED) if value != UNDEFINED: if not Guild.owner == CheckMember: return await apiDiscordMissingPermission(cls, WebRequest, guild_id=guild_id, user_id=DiscordUser.user_id, msg="changing 'owner_disable_normal' require server owner") db_update["owner_disable_normal"] = validateDBInput(bool, value) update["owner_disable_normal"] = value # owner_disable_regular value:bool = Data.getBool("owner_disable_regular", UNDEFINED) if value != UNDEFINED: if not Guild.owner == CheckMember: return await apiDiscordMissingPermission(cls, WebRequest, guild_id=guild_id, user_id=DiscordUser.user_id, msg="changing 'owner_disable_regular' require server owner") db_update["owner_disable_regular"] = validateDBInput(bool, value) update["owner_disable_regular"] = value # owner_disable_mod value:bool = Data.getBool("owner_disable_mod", UNDEFINED) if value != UNDEFINED: if not Guild.owner == CheckMember: return await apiDiscordMissingPermission(cls, WebRequest, guild_id=guild_id, user_id=DiscordUser.user_id, msg="changing 'owner_disable_mod' require server owner") db_update["owner_disable_mod"] = validateDBInput(bool, value) update["owner_disable_mod"] = value # welcome_chan value:str = Data.getStr("welcome_chan", UNDEFINED) if value != UNDEFINED: error:bool = False if not value: value = None elif value.isdigit(): Chan:discord.abc.Messageable = discord.utils.get(Guild.channels, id=int(value)) if type(Chan) != discord.TextChannel: error = True else: value = str(Chan.id) else: error = True if error: return await apiWrongData(cls, WebRequest, msg=f"'{value}' could not be resolved as a valid discord text channel id") db_update["welcome_chan"] = validateDBInput(str, value, allow_null=True) update["welcome_chan"] = value # welcome_msg value:str = Data.getStr("welcome_msg", UNDEFINED) if value != UNDEFINED: if not value: value = None db_update["welcome_msg"] = validateDBInput(str, value, allow_null=True) update["welcome_msg"] = value # welcome_msg_priv value:str = Data.getStr("welcome_msg_priv", UNDEFINED) if value != UNDEFINED: if not value: value = None db_update["welcome_msg_priv"] = validateDBInput(str, value, allow_null=True) update["welcome_msg_priv"] = value if not db_update: return await apiMissingData(cls, WebRequest, msg="No changes, please add at least one") cls.Web.BASE.Logger.debug(f"(API/Discord) Config Update: S:{guild_id} {str(db_update)}", require="discord:configs") cls.Web.BASE.PhaazeDB.updateQuery( table = "discord_setting", content = db_update, where = "discord_setting.guild_id = %s", where_values = (guild_id,) ) return cls.response( text=json.dumps( dict(msg="configs successfull updated", update=update, status=200) ), content_type="application/json", status=200 )
async def apiAdminRolesEdit(cls: "WebIndex", WebRequest: Request) -> Response: Data: WebRequestContent = WebRequestContent(WebRequest) await Data.load() # get required stuff role_id: int = Data.getInt("role_id", UNDEFINED, min_x=1) # checks if not role_id: return await apiMissingData(cls, WebRequest, msg="missing or invalid 'role_id'") res: list = cls.Web.BASE.PhaazeDB.selectQuery( "SELECT * FROM `role` WHERE `role`.`id` = %s", (role_id, )) if not res: return await apiWrongData(cls, WebRequest, msg=f"could not find role") CurrentRole: WebRole = WebRole(res.pop(0)) db_changes: dict = dict() changes: dict = dict() # name value: str = Data.getStr("name", UNDEFINED) if value != UNDEFINED: # only allow role name change as long the role is removable, # because based on name... a rename whould be a delete... got it? if CurrentRole.can_be_removed: db_changes["name"] = validateDBInput(str, value) changes["name"] = value # description value: str = Data.getStr("description", UNDEFINED) if value != UNDEFINED: db_changes["description"] = validateDBInput(str, value) changes["description"] = value # can_be_removed value: bool = Data.getBool("can_be_removed", UNDEFINED) if value != UNDEFINED: # this value can only be set to 0 # if the user gives a 1, meaning to make it removeable... we just ignore it if not value: db_changes["can_be_removed"] = validateDBInput(bool, value) changes["can_be_removed"] = value if not db_changes: return await apiMissingData(cls, WebRequest, msg="No changes, please add at least one") cls.Web.BASE.Logger.debug( f"(API) Role update: R:{role_id} {str(db_changes)}", require="api:admin") cls.Web.BASE.PhaazeDB.updateQuery(table="role", content=db_changes, where="role.id = %s", where_values=(role_id, )) return cls.response(text=json.dumps( dict(msg="role successfull updated", changes=changes, status=200)), content_type="application/json", status=200)