diff --git a/bot/cpl-workspace.json b/bot/cpl-workspace.json index 74d016e7..8061d025 100644 --- a/bot/cpl-workspace.json +++ b/bot/cpl-workspace.json @@ -14,7 +14,6 @@ "config": "src/modules/config/config.json", "database": "src/modules/database/database.json", "level": "src/modules/level/level.json", - "permission": "src/modules/permission/permission.json", "technician": "src/modules/technician/technician.json", "short-role-name": "src/modules/short_role_name/short-role-name.json", "special-offers": "src/modules/special_offers/special-offers.json", diff --git a/bot/src/bot/bot.json b/bot/src/bot/bot.json index 3df77fe5..da730b2c 100644 --- a/bot/src/bot/bot.json +++ b/bot/src/bot/bot.json @@ -70,7 +70,6 @@ "../modules/config/config.json", "../modules/database/database.json", "../modules/level/level.json", - "../modules/permission/permission.json", "../modules/short_role_name/short-role-name.json", "../modules/special_offers/special-offers.json", "../modules/technician/technician.json" diff --git a/bot/src/bot/module_list.py b/bot/src/bot/module_list.py index 88e99721..8b0b72ad 100644 --- a/bot/src/bot/module_list.py +++ b/bot/src/bot/module_list.py @@ -12,7 +12,6 @@ from modules.boot_log.boot_log_module import BootLogModule from modules.config.config_module import ConfigModule from modules.database.database_module import DatabaseModule from modules.level.level_module import LevelModule -from modules.permission.permission_module import PermissionModule from modules.short_role_name.short_role_name_module import ShortRoleNameModule from modules.special_offers.special_offers_module import SteamSpecialOffersModule from modules.technician.technician_module import TechnicianModule @@ -30,7 +29,6 @@ class ModuleList: ConfigModule, # has to be before db check DatabaseModule, GraphQLModule, - PermissionModule, AutoRoleModule, BaseModule, LevelModule, diff --git a/bot/src/bot_api/transformer/auth_user_transformer.py b/bot/src/bot_api/transformer/auth_user_transformer.py index 9e52fec3..66506a3a 100644 --- a/bot/src/bot_api/transformer/auth_user_transformer.py +++ b/bot/src/bot_api/transformer/auth_user_transformer.py @@ -10,7 +10,7 @@ from bot_api.model.user_dto import UserDTO from bot_data.model.auth_role_enum import AuthRoleEnum from bot_data.model.auth_user import AuthUser from bot_data.model.user import User -from modules.permission.abc.permission_service_abc import PermissionServiceABC +from bot_core.abc.permission_service_abc import PermissionServiceABC class AuthUserTransformer(TransformerABC): diff --git a/bot/src/modules/permission/abc/permission_service_abc.py b/bot/src/bot_core/abc/permission_service_abc.py similarity index 100% rename from bot/src/modules/permission/abc/permission_service_abc.py rename to bot/src/bot_core/abc/permission_service_abc.py diff --git a/bot/src/bot_core/configuration/feature_flags_enum.py b/bot/src/bot_core/configuration/feature_flags_enum.py index 61a4652e..0a211505 100644 --- a/bot/src/bot_core/configuration/feature_flags_enum.py +++ b/bot/src/bot_core/configuration/feature_flags_enum.py @@ -15,7 +15,6 @@ class FeatureFlagsEnum(Enum): database_module = "DatabaseModule" level_module = "LevelModule" moderator_module = "ModeratorModule" - permission_module = "PermissionModule" short_role_name_module = "ShortRoleNameModule" steam_special_offers_module = "SteamSpecialOffersModule" # features diff --git a/bot/src/bot_core/configuration/feature_flags_settings.py b/bot/src/bot_core/configuration/feature_flags_settings.py index cbea9a97..104ff9e2 100644 --- a/bot/src/bot_core/configuration/feature_flags_settings.py +++ b/bot/src/bot_core/configuration/feature_flags_settings.py @@ -16,7 +16,6 @@ class FeatureFlagsSettings(ConfigurationModelABC): FeatureFlagsEnum.data_module.value: True, # 03.10.2022 #56 FeatureFlagsEnum.database_module.value: True, # 02.10.2022 #48 FeatureFlagsEnum.moderator_module.value: False, # 02.10.2022 #48 - FeatureFlagsEnum.permission_module.value: True, # 02.10.2022 #48 FeatureFlagsEnum.config_module.value: True, # 19.07.2023 #127 FeatureFlagsEnum.short_role_name_module.value: True, # 28.09.2023 #378 FeatureFlagsEnum.steam_special_offers_module.value: True, # 11.10.2023 #188 diff --git a/bot/src/bot_core/core_extension/core_extension.py b/bot/src/bot_core/core_extension/core_extension.py index de5a1781..02dc8171 100644 --- a/bot/src/bot_core/core_extension/core_extension.py +++ b/bot/src/bot_core/core_extension/core_extension.py @@ -9,7 +9,7 @@ from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings from bot_core.helper.command_checks import CommandChecks from bot_core.helper.event_checks import EventChecks -from modules.permission.abc.permission_service_abc import PermissionServiceABC +from bot_core.abc.permission_service_abc import PermissionServiceABC class CoreExtension(ApplicationExtensionABC): diff --git a/bot/src/bot_core/core_module.py b/bot/src/bot_core/core_module.py index 02611684..9323b43e 100644 --- a/bot/src/bot_core/core_module.py +++ b/bot/src/bot_core/core_module.py @@ -7,6 +7,7 @@ from cpl_discord.service.discord_collection_abc import DiscordCollectionABC from bot_core.abc.client_utils_abc import ClientUtilsABC from bot_core.abc.message_service_abc import MessageServiceABC from bot_core.abc.module_abc import ModuleABC +from bot_core.abc.permission_service_abc import PermissionServiceABC from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum from bot_core.events.core_on_ready_event import CoreOnReadyEvent from bot_core.pipes.date_time_offset_pipe import DateTimeOffsetPipe @@ -14,6 +15,7 @@ from bot_core.service.client_utils_service import ClientUtilsService from bot_core.service.config_service import ConfigService from bot_core.service.data_integrity_service import DataIntegrityService from bot_core.service.message_service import MessageService +from bot_core.service.permission_service import PermissionService class CoreModule(ModuleABC): @@ -28,6 +30,7 @@ class CoreModule(ModuleABC): services.add_transient(MessageServiceABC, MessageService) services.add_transient(ClientUtilsABC, ClientUtilsService) services.add_transient(DataIntegrityService) + services.add_singleton(PermissionServiceABC, PermissionService) # pipes services.add_transient(DateTimeOffsetPipe) diff --git a/bot/src/bot_core/helper/command_checks.py b/bot/src/bot_core/helper/command_checks.py index e2048409..99e57d5a 100644 --- a/bot/src/bot_core/helper/command_checks.py +++ b/bot/src/bot_core/helper/command_checks.py @@ -7,7 +7,7 @@ from discord.ext.commands import Context from bot_core.abc.client_utils_abc import ClientUtilsABC from bot_core.abc.message_service_abc import MessageServiceABC from bot_core.exception.check_error import CheckError -from modules.permission.abc.permission_service_abc import PermissionServiceABC +from bot_core.abc.permission_service_abc import PermissionServiceABC class CommandChecks: diff --git a/bot/src/bot_core/service/permission_service.py b/bot/src/bot_core/service/permission_service.py new file mode 100644 index 00000000..96dc92a5 --- /dev/null +++ b/bot/src/bot_core/service/permission_service.py @@ -0,0 +1,114 @@ +from typing import Optional + +import discord +from cpl_core.configuration import ConfigurationABC +from cpl_core.logging import LoggerABC +from cpl_discord.service import DiscordBotServiceABC + +from bot_data.abc.server_config_repository_abc import ServerConfigRepositoryABC +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.abc.technician_config_repository_abc import TechnicianConfigRepositoryABC +from bot_data.model.team_member_type_enum import TeamMemberTypeEnum +from bot_core.abc.permission_service_abc import PermissionServiceABC + + +class PermissionService(PermissionServiceABC): + def __init__( + self, + logger: LoggerABC, + bot: DiscordBotServiceABC, + config: ConfigurationABC, + servers: ServerRepositoryABC, + server_configs: ServerConfigRepositoryABC, + technician_configs: TechnicianConfigRepositoryABC, + ): + PermissionServiceABC.__init__(self) + self._logger = logger + self._bot = bot + self._config = config + self._servers = servers + self._server_configs = server_configs + self._technician_configs = technician_configs + + # member_id: {team_member_type: {guild_id: bool}} + self._cache: dict[int, dict[TeamMemberTypeEnum, dict[int, bool]]] = {} + + def reset_cache(self): + self._cache = {} + + def get_cached_permission( + self, member_id: int, team_member_type: TeamMemberTypeEnum, guild_id: int = None + ) -> Optional[bool]: + if member_id not in self._cache: + self._cache[member_id] = {} + + if team_member_type not in self._cache[member_id]: + self._cache[member_id][team_member_type] = {} + return None + + if guild_id not in self._cache[member_id][team_member_type]: + return None + + return self._cache[member_id][team_member_type][guild_id] + + def set_cached_permission( + self, value: bool, member_id: int, team_member_type: TeamMemberTypeEnum, guild_id: int = None + ): + if member_id not in self._cache: + self._cache[member_id] = {} + + if team_member_type not in self._cache[member_id]: + self._cache[member_id][team_member_type] = {} + + self._cache[member_id][team_member_type][guild_id] = value + + def _has_member_role(self, member: discord.Member, team_member_type: TeamMemberTypeEnum) -> bool: + if member is None or member.guild is None: + return False + + try: + has_permission_cached = self.get_cached_permission(member.id, team_member_type, member.guild.id) + if has_permission_cached is not None: + return has_permission_cached + + self._logger.debug(__name__, f"Checking is member {member.name} {team_member_type.value}") + + has_permission = True in [ + member.guild.get_role(x.role_id) not in member.roles + for x in self._server_configs.get_server_config_by_server( + self._servers.get_server_by_discord_id(member.guild.id).id + ).team_role_ids + if x.team_member_type == team_member_type + ] + self.set_cached_permission(has_permission, member.id, team_member_type, member.guild.id) + return has_permission + except Exception as e: + self._logger.error(__name__, "Permission check failed", e) + + return False + + def is_member_admin(self, member: discord.Member) -> bool: + return self._has_member_role(member, TeamMemberTypeEnum.admin) + + def is_member_moderator(self, member: discord.Member) -> bool: + return self._has_member_role(member, TeamMemberTypeEnum.moderator) or self._has_member_role( + member, TeamMemberTypeEnum.admin + ) + + def is_member_technician(self, member: discord.Member) -> bool: + if member is None or member.guild is None: + return False + + has_permission_cached = self.get_cached_permission(member.id, TeamMemberTypeEnum.technician) + if has_permission_cached is not None: + return has_permission_cached + + self._logger.debug(__name__, f"Checking is member {member.name} technician") + + try: + has_permission = member.id in self._technician_configs.get_technician_config().technician_ids + self.set_cached_permission(has_permission, member.id, TeamMemberTypeEnum.technician) + return has_permission + except Exception as e: + self._logger.error(__name__, "Permission check failed", e) + return False diff --git a/bot/src/bot_data/model/team_member_type_enum.py b/bot/src/bot_data/model/team_member_type_enum.py index 86b3b41d..84dc8fee 100644 --- a/bot/src/bot_data/model/team_member_type_enum.py +++ b/bot/src/bot_data/model/team_member_type_enum.py @@ -4,3 +4,4 @@ from enum import Enum class TeamMemberTypeEnum(Enum): moderator = "Moderator" admin = "Admin" + technician = "Technician" diff --git a/bot/src/bot_graphql/abc/query_abc.py b/bot/src/bot_graphql/abc/query_abc.py index 882a420d..d66fd516 100644 --- a/bot/src/bot_graphql/abc/query_abc.py +++ b/bot/src/bot_graphql/abc/query_abc.py @@ -12,6 +12,7 @@ from bot_api.exception.service_exception import ServiceException from bot_api.route.route import Route from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum from bot_core.environment_variables import MAINTENANCE +from bot_core.service.permission_service import PermissionService from bot_data.model.achievement import Achievement from bot_data.model.auth_role_enum import AuthRoleEnum from bot_data.model.auth_user import AuthUser @@ -32,7 +33,6 @@ from bot_data.model.user_role_enum import UserRoleEnum from bot_graphql.abc.filter_abc import FilterABC from bot_graphql.filter.page import Page from bot_graphql.filter.sort import Sort -from modules.permission.service.permission_service import PermissionService class QueryABC(ObjectType): diff --git a/bot/src/bot_graphql/mutations/achievement_mutation.py b/bot/src/bot_graphql/mutations/achievement_mutation.py index 40975a38..5cc939e0 100644 --- a/bot/src/bot_graphql/mutations/achievement_mutation.py +++ b/bot/src/bot_graphql/mutations/achievement_mutation.py @@ -6,7 +6,7 @@ from bot_data.abc.server_repository_abc import ServerRepositoryABC from bot_data.model.achievement import Achievement from bot_data.model.user_role_enum import UserRoleEnum from bot_graphql.abc.query_abc import QueryABC -from modules.permission.service.permission_service import PermissionService +from bot_core.service.permission_service import PermissionService class AchievementMutation(QueryABC): diff --git a/bot/src/bot_graphql/mutations/scheduled_event_mutation.py b/bot/src/bot_graphql/mutations/scheduled_event_mutation.py index 6544bb42..381c8685 100644 --- a/bot/src/bot_graphql/mutations/scheduled_event_mutation.py +++ b/bot/src/bot_graphql/mutations/scheduled_event_mutation.py @@ -10,7 +10,7 @@ from bot_data.model.scheduled_event import ScheduledEvent from bot_data.model.scheduled_event_interval_enum import ScheduledEventIntervalEnum from bot_data.model.user_role_enum import UserRoleEnum from bot_graphql.abc.query_abc import QueryABC -from modules.permission.service.permission_service import PermissionService +from bot_core.service.permission_service import PermissionService class ScheduledEventMutation(QueryABC): diff --git a/bot/src/bot_graphql/mutations/server_config_mutation.py b/bot/src/bot_graphql/mutations/server_config_mutation.py index 6b2ff5eb..88669d83 100644 --- a/bot/src/bot_graphql/mutations/server_config_mutation.py +++ b/bot/src/bot_graphql/mutations/server_config_mutation.py @@ -13,6 +13,7 @@ from bot_data.model.server_team_role_ids_config import ServerTeamRoleIdsConfig from bot_data.model.team_member_type_enum import TeamMemberTypeEnum from bot_data.model.user_role_enum import UserRoleEnum from bot_graphql.abc.query_abc import QueryABC +from bot_core.service.permission_service import PermissionService class ServerConfigMutation(QueryABC): @@ -24,6 +25,7 @@ class ServerConfigMutation(QueryABC): server_configs: ServerConfigRepositoryABC, db: DatabaseContextABC, config_service: ConfigService, + permissions: PermissionService, ): QueryABC.__init__(self, "ServerConfigMutation") @@ -33,6 +35,7 @@ class ServerConfigMutation(QueryABC): self._server_configs = server_configs self._db = db self._config_service = config_service + self._permissions = permissions self.set_field("updateServerConfig", self.resolve_update_server_config) @@ -175,11 +178,13 @@ class ServerConfigMutation(QueryABC): def _update_team_role_ids(self, new_config: ServerConfig): old_config = self._server_configs.get_server_config_by_server(new_config.server.id) + has_update = False for role_id in old_config.team_role_ids: if role_id.role_id in new_config.team_role_ids.select(lambda x: int(x.role_id)): continue self._server_configs.delete_server_team_role_id_config(role_id) + has_update = True for role_id in new_config.team_role_ids: guild = self._bot.get_guild(new_config.server.discord_id) @@ -192,3 +197,7 @@ class ServerConfigMutation(QueryABC): continue self._server_configs.add_server_team_role_id_config(role_id) + has_update = True + + if has_update: + self._permissions.reset_cache() diff --git a/bot/src/bot_graphql/mutations/short_role_name_mutation.py b/bot/src/bot_graphql/mutations/short_role_name_mutation.py index 3044af99..14ae37a0 100644 --- a/bot/src/bot_graphql/mutations/short_role_name_mutation.py +++ b/bot/src/bot_graphql/mutations/short_role_name_mutation.py @@ -6,7 +6,7 @@ from bot_data.abc.short_role_name_repository_abc import ShortRoleNameRepositoryA from bot_data.model.short_role_name import ShortRoleName from bot_data.model.user_role_enum import UserRoleEnum from bot_graphql.abc.query_abc import QueryABC -from modules.permission.service.permission_service import PermissionService +from bot_core.service.permission_service import PermissionService class ShortRoleNameMutation(QueryABC): diff --git a/bot/src/bot_graphql/mutations/technician_config_mutation.py b/bot/src/bot_graphql/mutations/technician_config_mutation.py index 67091955..8ca08e86 100644 --- a/bot/src/bot_graphql/mutations/technician_config_mutation.py +++ b/bot/src/bot_graphql/mutations/technician_config_mutation.py @@ -16,6 +16,7 @@ from bot_data.model.technician_ping_url_config import TechnicianPingUrlConfig from bot_data.model.user_role_enum import UserRoleEnum from bot_data.service.technician_config_seeder import TechnicianConfigSeeder from bot_graphql.abc.query_abc import QueryABC +from bot_core.service.permission_service import PermissionService class TechnicianConfigMutation(QueryABC): @@ -30,6 +31,7 @@ class TechnicianConfigMutation(QueryABC): config_service: ConfigService, tech_seeder: TechnicianConfigSeeder, client_utils: ClientUtilsABC, + permissions: PermissionService, ): QueryABC.__init__(self, "TechnicianConfigMutation") @@ -42,6 +44,7 @@ class TechnicianConfigMutation(QueryABC): self._config_service = config_service self._tech_seeder = tech_seeder self._client_utils = client_utils + self._permissions = permissions self.set_field("updateTechnicianConfig", self.resolve_update_technician_config) @@ -126,11 +129,13 @@ class TechnicianConfigMutation(QueryABC): def _update_technician_ids(self, new_config: TechnicianConfig): old_config = self._technician_configs.get_technician_config() + has_update = False for technician_id in old_config.technician_ids: if technician_id in new_config.technician_ids: continue self._technician_configs.delete_technician_id_config(TechnicianIdConfig(technician_id)) + has_update = True for technician_id in new_config.technician_ids: user = self._bot.get_user(technician_id) @@ -142,3 +147,7 @@ class TechnicianConfigMutation(QueryABC): continue self._technician_configs.add_technician_id_config(TechnicianIdConfig(technician_id)) + has_update = True + + if has_update: + self._permissions.reset_cache() diff --git a/bot/src/bot_graphql/mutations/user_joined_game_server_mutation.py b/bot/src/bot_graphql/mutations/user_joined_game_server_mutation.py index 4f53a741..c2a3a3ec 100644 --- a/bot/src/bot_graphql/mutations/user_joined_game_server_mutation.py +++ b/bot/src/bot_graphql/mutations/user_joined_game_server_mutation.py @@ -24,7 +24,7 @@ from bot_data.model.server_config import ServerConfig from bot_data.model.user_joined_game_server import UserJoinedGameServer from bot_data.model.user_role_enum import UserRoleEnum from bot_graphql.abc.query_abc import QueryABC -from modules.permission.service.permission_service import PermissionService +from bot_core.service.permission_service import PermissionService class UserJoinedGameServerMutation(QueryABC): diff --git a/bot/src/bot_graphql/mutations/user_mutation.py b/bot/src/bot_graphql/mutations/user_mutation.py index 9ec5f4cc..e203deb8 100644 --- a/bot/src/bot_graphql/mutations/user_mutation.py +++ b/bot/src/bot_graphql/mutations/user_mutation.py @@ -13,7 +13,7 @@ from bot_data.model.user_role_enum import UserRoleEnum from bot_graphql.abc.query_abc import QueryABC from modules.base.service.user_warnings_service import UserWarningsService from modules.level.service.level_service import LevelService -from modules.permission.service.permission_service import PermissionService +from bot_core.service.permission_service import PermissionService class UserMutation(QueryABC): diff --git a/bot/src/bot_graphql/queries/user_query.py b/bot/src/bot_graphql/queries/user_query.py index a6cffa64..d503448c 100644 --- a/bot/src/bot_graphql/queries/user_query.py +++ b/bot/src/bot_graphql/queries/user_query.py @@ -22,7 +22,7 @@ from bot_graphql.filter.user_joined_voice_channel_filter import ( ) from bot_graphql.filter.user_warning_filter import UserWarningFilter from modules.level.service.level_service import LevelService -from modules.permission.abc.permission_service_abc import PermissionServiceABC +from bot_core.abc.permission_service_abc import PermissionServiceABC class UserQuery(DataQueryWithHistoryABC): diff --git a/bot/src/modules/auto_role/command/auto_role_group.py b/bot/src/modules/auto_role/command/auto_role_group.py index f8d48d4e..dba39745 100644 --- a/bot/src/modules/auto_role/command/auto_role_group.py +++ b/bot/src/modules/auto_role/command/auto_role_group.py @@ -22,7 +22,7 @@ from bot_data.abc.server_repository_abc import ServerRepositoryABC from bot_data.model.auto_role import AutoRole from bot_data.model.auto_role_rule import AutoRoleRule from bot_data.model.server_config import ServerConfig -from modules.permission.abc.permission_service_abc import PermissionServiceABC +from bot_core.abc.permission_service_abc import PermissionServiceABC class AutoRoleGroup(DiscordCommandABC): diff --git a/bot/src/modules/base/command/game_server_group.py b/bot/src/modules/base/command/game_server_group.py index 55d235e3..a3d4805e 100644 --- a/bot/src/modules/base/command/game_server_group.py +++ b/bot/src/modules/base/command/game_server_group.py @@ -23,7 +23,7 @@ from bot_data.abc.user_game_ident_repository_abc import UserGameIdentRepositoryA from bot_data.abc.user_repository_abc import UserRepositoryABC from bot_data.model.game_server import GameServer from bot_data.model.server_config import ServerConfig -from modules.permission.abc.permission_service_abc import PermissionServiceABC +from bot_core.abc.permission_service_abc import PermissionServiceABC class GameServerGroup(DiscordCommandABC): diff --git a/bot/src/modules/base/command/ping_command.py b/bot/src/modules/base/command/ping_command.py index ca0a14ac..55cb48d4 100644 --- a/bot/src/modules/base/command/ping_command.py +++ b/bot/src/modules/base/command/ping_command.py @@ -11,7 +11,7 @@ from bot_core.helper.command_checks import CommandChecks from bot_core.logging.command_logger import CommandLogger from bot_data.abc.server_repository_abc import ServerRepositoryABC from bot_data.model.technician_config import TechnicianConfig -from modules.permission.abc.permission_service_abc import PermissionServiceABC +from bot_core.abc.permission_service_abc import PermissionServiceABC class PingCommand(DiscordCommandABC): diff --git a/bot/src/modules/base/command/purge_command.py b/bot/src/modules/base/command/purge_command.py index 9816e29f..67a2f44c 100644 --- a/bot/src/modules/base/command/purge_command.py +++ b/bot/src/modules/base/command/purge_command.py @@ -11,7 +11,7 @@ from bot_core.abc.message_service_abc import MessageServiceABC from bot_core.helper.command_checks import CommandChecks from bot_core.logging.command_logger import CommandLogger from bot_data.model.server_config import ServerConfig -from modules.permission.abc.permission_service_abc import PermissionServiceABC +from bot_core.abc.permission_service_abc import PermissionServiceABC class PurgeCommand(DiscordCommandABC): diff --git a/bot/src/modules/base/command/user_group.py b/bot/src/modules/base/command/user_group.py index 4f1df692..369cc4c6 100644 --- a/bot/src/modules/base/command/user_group.py +++ b/bot/src/modules/base/command/user_group.py @@ -27,7 +27,7 @@ from bot_data.abc.user_warnings_repository_abc import UserWarningsRepositoryABC from bot_data.model.server_config import ServerConfig from modules.base.service.user_warnings_service import UserWarningsService from modules.level.service.level_service import LevelService -from modules.permission.abc.permission_service_abc import PermissionServiceABC +from bot_core.abc.permission_service_abc import PermissionServiceABC class UserGroup(DiscordCommandABC): diff --git a/bot/src/modules/base/events/base_on_member_join_event.py b/bot/src/modules/base/events/base_on_member_join_event.py index 35a5cf05..63054277 100644 --- a/bot/src/modules/base/events/base_on_member_join_event.py +++ b/bot/src/modules/base/events/base_on_member_join_event.py @@ -20,7 +20,7 @@ from bot_data.model.known_user import KnownUser from bot_data.model.server_config import ServerConfig from bot_data.model.user import User from bot_data.model.user_joined_server import UserJoinedServer -from modules.permission.abc.permission_service_abc import PermissionServiceABC +from bot_core.abc.permission_service_abc import PermissionServiceABC class BaseOnMemberJoinEvent(OnMemberJoinABC): diff --git a/bot/src/modules/base/events/base_on_voice_state_update_event_help_channel.py b/bot/src/modules/base/events/base_on_voice_state_update_event_help_channel.py index 545620ac..8ae95da8 100644 --- a/bot/src/modules/base/events/base_on_voice_state_update_event_help_channel.py +++ b/bot/src/modules/base/events/base_on_voice_state_update_event_help_channel.py @@ -8,7 +8,7 @@ from bot_core.abc.message_service_abc import MessageServiceABC from bot_core.helper.event_checks import EventChecks from bot_data.abc.server_repository_abc import ServerRepositoryABC from bot_data.model.server_config import ServerConfig -from modules.permission.abc.permission_service_abc import PermissionServiceABC +from bot_core.abc.permission_service_abc import PermissionServiceABC class BaseOnVoiceStateUpdateEventHelpChannel(OnVoiceStateUpdateABC): diff --git a/bot/src/modules/base/service/user_warnings_service.py b/bot/src/modules/base/service/user_warnings_service.py index eea9129a..125b82a2 100644 --- a/bot/src/modules/base/service/user_warnings_service.py +++ b/bot/src/modules/base/service/user_warnings_service.py @@ -14,7 +14,7 @@ from bot_data.model.server_config import ServerConfig from bot_data.model.user import User from bot_data.model.user_warnings import UserWarnings from modules.level.service.level_service import LevelService -from modules.permission.abc.permission_service_abc import PermissionServiceABC +from bot_core.abc.permission_service_abc import PermissionServiceABC class UserWarningsService: diff --git a/bot/src/modules/level/command/level_group.py b/bot/src/modules/level/command/level_group.py index 50e03692..f7f1e626 100644 --- a/bot/src/modules/level/command/level_group.py +++ b/bot/src/modules/level/command/level_group.py @@ -24,7 +24,7 @@ from bot_data.model.level import Level from bot_data.model.server_config import ServerConfig from modules.level.level_seeder import LevelSeeder from modules.level.service.level_service import LevelService -from modules.permission.abc.permission_service_abc import PermissionServiceABC +from bot_core.abc.permission_service_abc import PermissionServiceABC class LevelGroup(DiscordCommandABC): diff --git a/bot/src/modules/level/service/level_service.py b/bot/src/modules/level/service/level_service.py index e85c1719..8f05d4d5 100644 --- a/bot/src/modules/level/service/level_service.py +++ b/bot/src/modules/level/service/level_service.py @@ -6,6 +6,8 @@ from cpl_discord.container import Guild, Role, Member from cpl_discord.service import DiscordBotServiceABC from cpl_translation import TranslatePipe +from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum +from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings from bot_core.service.message_service import MessageService from bot_data.model.level import Level from bot_data.model.server_config import ServerConfig @@ -38,7 +40,14 @@ class LevelService: self._message_service = message_service self._t = t + def _check_for_feature(self, user: User): + server_config: ServerConfig = self._config.get_configuration(f"ServerConfig_{user.server.discord_id}") + if not FeatureFlagsSettings.get_flag_from_dict(server_config.feature_flags, FeatureFlagsEnum.level_module): + raise Exception(f"Feature {FeatureFlagsEnum.level_module.value} disabled") + def get_level(self, user: User) -> Level: + self._check_for_feature(user) + levels_by_server = self._levels.get_levels_by_server_id(user.server.id) if user.xp < 0: return levels_by_server.order_by(lambda l: l.min_xp).first() @@ -51,6 +60,7 @@ class LevelService: return levels.last() async def set_level(self, user: User): + self._check_for_feature(user) level_names = self._levels.get_levels_by_server_id(user.server.id).select(lambda l: l.name) guild: Guild = self._bot.guilds.where(lambda g: g.id == user.server.discord_id).single() member: Member = guild.members.where(lambda m: m.id == user.discord_id).single() diff --git a/bot/src/modules/permission/__init__.py b/bot/src/modules/permission/__init__.py deleted file mode 100644 index 264460c3..00000000 --- a/bot/src/modules/permission/__init__.py +++ /dev/null @@ -1,26 +0,0 @@ -# -*- coding: utf-8 -*- - -""" -bot sh-edraft.de Discord bot -~~~~~~~~~~~~~~~~~~~ - -Discord bot for customers of sh-edraft.de - -:copyright: (c) 2022 - 2023 sh-edraft.de -:license: MIT, see LICENSE for more details. - -""" - -__title__ = "modules.permission" -__author__ = "Sven Heidemann" -__license__ = "MIT" -__copyright__ = "Copyright (c) 2022 - 2023 sh-edraft.de" -__version__ = "1.2.2" - -from collections import namedtuple - - -# imports: - -VersionInfo = namedtuple("VersionInfo", "major minor micro") -version_info = VersionInfo(major="1", minor="2", micro="2") diff --git a/bot/src/modules/permission/abc/__init__.py b/bot/src/modules/permission/abc/__init__.py deleted file mode 100644 index ff1f315c..00000000 --- a/bot/src/modules/permission/abc/__init__.py +++ /dev/null @@ -1,26 +0,0 @@ -# -*- coding: utf-8 -*- - -""" -bot sh-edraft.de Discord bot -~~~~~~~~~~~~~~~~~~~ - -Discord bot for customers of sh-edraft.de - -:copyright: (c) 2022 - 2023 sh-edraft.de -:license: MIT, see LICENSE for more details. - -""" - -__title__ = "modules.permission.abc" -__author__ = "Sven Heidemann" -__license__ = "MIT" -__copyright__ = "Copyright (c) 2022 - 2023 sh-edraft.de" -__version__ = "1.2.2" - -from collections import namedtuple - - -# imports - -VersionInfo = namedtuple("VersionInfo", "major minor micro") -version_info = VersionInfo(major="1", minor="2", micro="2") diff --git a/bot/src/modules/permission/permission.json b/bot/src/modules/permission/permission.json deleted file mode 100644 index 443c982f..00000000 --- a/bot/src/modules/permission/permission.json +++ /dev/null @@ -1,44 +0,0 @@ -{ - "ProjectSettings": { - "Name": "permission", - "Version": { - "Major": "1", - "Minor": "2", - "Micro": "2" - }, - "Author": "", - "AuthorEmail": "", - "Description": "", - "LongDescription": "", - "URL": "", - "CopyrightDate": "", - "CopyrightName": "", - "LicenseName": "", - "LicenseDescription": "", - "Dependencies": [ - "cpl-core==2022.12.0" - ], - "DevDependencies": [ - "cpl-cli==2022.12.0" - ], - "PythonVersion": ">=3.10.4", - "PythonPath": {}, - "Classifiers": [] - }, - "BuildSettings": { - "ProjectType": "library", - "SourcePath": "", - "OutputPath": "../../dist", - "Main": "permission.main", - "EntryPoint": "permission", - "IncludePackageData": false, - "Included": [], - "Excluded": [ - "*/__pycache__", - "*/logs", - "*/tests" - ], - "PackageData": {}, - "ProjectReferences": [] - } -} \ No newline at end of file diff --git a/bot/src/modules/permission/permission_module.py b/bot/src/modules/permission/permission_module.py deleted file mode 100644 index 75da783e..00000000 --- a/bot/src/modules/permission/permission_module.py +++ /dev/null @@ -1,22 +0,0 @@ -from cpl_core.configuration import ConfigurationABC -from cpl_core.dependency_injection import ServiceCollectionABC -from cpl_core.environment import ApplicationEnvironmentABC -from cpl_discord.service.discord_collection_abc import DiscordCollectionABC - -from bot_core.abc.module_abc import ModuleABC -from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum -from modules.permission.abc.permission_service_abc import PermissionServiceABC -from modules.permission.service.permission_service import PermissionService - - -class PermissionModule(ModuleABC): - def __init__(self, dc: DiscordCollectionABC): - ModuleABC.__init__(self, dc, FeatureFlagsEnum.permission_module) - - def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC): - pass - - def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC): - services.add_transient(PermissionServiceABC, PermissionService) - # commands - # events diff --git a/bot/src/modules/permission/service/__init__.py b/bot/src/modules/permission/service/__init__.py deleted file mode 100644 index 59ca5137..00000000 --- a/bot/src/modules/permission/service/__init__.py +++ /dev/null @@ -1,26 +0,0 @@ -# -*- coding: utf-8 -*- - -""" -bot sh-edraft.de Discord bot -~~~~~~~~~~~~~~~~~~~ - -Discord bot for customers of sh-edraft.de - -:copyright: (c) 2022 - 2023 sh-edraft.de -:license: MIT, see LICENSE for more details. - -""" - -__title__ = "modules.permission.service" -__author__ = "Sven Heidemann" -__license__ = "MIT" -__copyright__ = "Copyright (c) 2022 - 2023 sh-edraft.de" -__version__ = "1.2.2" - -from collections import namedtuple - - -# imports - -VersionInfo = namedtuple("VersionInfo", "major minor micro") -version_info = VersionInfo(major="1", minor="2", micro="2") diff --git a/bot/src/modules/permission/service/permission_service.py b/bot/src/modules/permission/service/permission_service.py deleted file mode 100644 index 9ac28c55..00000000 --- a/bot/src/modules/permission/service/permission_service.py +++ /dev/null @@ -1,74 +0,0 @@ -import discord -from cpl_core.configuration import ConfigurationABC -from cpl_core.logging import LoggerABC -from cpl_discord.service import DiscordBotServiceABC - -from bot_data.abc.server_config_repository_abc import ServerConfigRepositoryABC -from bot_data.abc.server_repository_abc import ServerRepositoryABC -from bot_data.abc.technician_config_repository_abc import TechnicianConfigRepositoryABC -from bot_data.model.team_member_type_enum import TeamMemberTypeEnum -from modules.permission.abc.permission_service_abc import PermissionServiceABC - - -class PermissionService(PermissionServiceABC): - def __init__( - self, - logger: LoggerABC, - bot: DiscordBotServiceABC, - config: ConfigurationABC, - servers: ServerRepositoryABC, - server_configs: ServerConfigRepositoryABC, - technician_configs: TechnicianConfigRepositoryABC, - ): - PermissionServiceABC.__init__(self) - self._logger = logger - self._bot = bot - self._config = config - self._servers = servers - self._server_configs = server_configs - self._technician_configs = technician_configs - - def _has_member_role(self, member: discord.Member, team_member_type: TeamMemberTypeEnum) -> bool: - if member is None or member.guild is None: - return False - - self._logger.debug(__name__, f"Checking is member {member.name} {team_member_type.value}") - - try: - server = self._servers.get_server_by_discord_id(member.guild.id) - config = self._server_configs.get_server_config_by_server(server.id) - roles = config.team_role_ids.where(lambda x: x.team_member_type == team_member_type).select( - lambda x: member.guild.get_role(x.role_id) - ) - for role in roles: - if role not in member.roles: - continue - - return True - except Exception as e: - self._logger.error(__name__, "Permission check failed", e) - - return False - - def is_member_admin(self, member: discord.Member) -> bool: - return self._has_member_role(member, TeamMemberTypeEnum.admin) - - def is_member_moderator(self, member: discord.Member) -> bool: - return self._has_member_role(member, TeamMemberTypeEnum.moderator) or self._has_member_role( - member, TeamMemberTypeEnum.admin - ) - - def is_member_technician(self, member: discord.Member) -> bool: - if member is None or member.guild is None: - return False - - self._logger.debug(__name__, f"Checking is member {member.name} technician") - - try: - tech_config = self._technician_configs.get_technician_config() - if member.id in tech_config.technician_ids: - return True - except Exception as e: - self._logger.error(__name__, "Permission check failed", e) - - return False diff --git a/bot/src/modules/short_role_name/short_role_name_check_command.py b/bot/src/modules/short_role_name/short_role_name_check_command.py index 663be0f1..4ff554c0 100644 --- a/bot/src/modules/short_role_name/short_role_name_check_command.py +++ b/bot/src/modules/short_role_name/short_role_name_check_command.py @@ -11,7 +11,7 @@ from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings from bot_core.helper.command_checks import CommandChecks from bot_core.logging.command_logger import CommandLogger from bot_data.model.server_config import ServerConfig -from modules.permission.abc.permission_service_abc import PermissionServiceABC +from bot_core.abc.permission_service_abc import PermissionServiceABC from modules.short_role_name.service.short_role_name_service import ShortRoleNameService diff --git a/bot/src/modules/technician/command/api_key_group.py b/bot/src/modules/technician/command/api_key_group.py index 11912385..48de1bbf 100644 --- a/bot/src/modules/technician/command/api_key_group.py +++ b/bot/src/modules/technician/command/api_key_group.py @@ -20,7 +20,7 @@ from bot_data.abc.api_key_repository_abc import ApiKeyRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC from bot_data.abc.user_repository_abc import UserRepositoryABC from bot_data.model.api_key import ApiKey -from modules.permission.abc.permission_service_abc import PermissionServiceABC +from bot_core.abc.permission_service_abc import PermissionServiceABC class ApiKeyGroup(DiscordCommandABC): diff --git a/bot/src/modules/technician/command/log_command.py b/bot/src/modules/technician/command/log_command.py index 2f799d61..5030cde5 100644 --- a/bot/src/modules/technician/command/log_command.py +++ b/bot/src/modules/technician/command/log_command.py @@ -1,5 +1,4 @@ import os -from string import Template from zipfile import ZipFile import discord @@ -18,7 +17,7 @@ from bot_core.abc.custom_file_logger_abc import CustomFileLoggerABC from bot_core.abc.message_service_abc import MessageServiceABC from bot_core.helper.command_checks import CommandChecks from bot_core.logging.command_logger import CommandLogger -from modules.permission.abc.permission_service_abc import PermissionServiceABC +from bot_core.abc.permission_service_abc import PermissionServiceABC class LogCommand(DiscordCommandABC): diff --git a/bot/src/modules/technician/command/restart_command.py b/bot/src/modules/technician/command/restart_command.py index 76fda82c..00461bba 100644 --- a/bot/src/modules/technician/command/restart_command.py +++ b/bot/src/modules/technician/command/restart_command.py @@ -13,7 +13,7 @@ from bot_core.helper.command_checks import CommandChecks from bot_core.logging.command_logger import CommandLogger from bot_core.service.data_integrity_service import DataIntegrityService from bot_data.model.technician_config import TechnicianConfig -from modules.permission.abc.permission_service_abc import PermissionServiceABC +from bot_core.abc.permission_service_abc import PermissionServiceABC class RestartCommand(DiscordCommandABC): diff --git a/bot/src/modules/technician/command/shutdown_command.py b/bot/src/modules/technician/command/shutdown_command.py index db2f807a..b6ea6011 100644 --- a/bot/src/modules/technician/command/shutdown_command.py +++ b/bot/src/modules/technician/command/shutdown_command.py @@ -13,7 +13,7 @@ from bot_core.helper.command_checks import CommandChecks from bot_core.logging.command_logger import CommandLogger from bot_core.service.data_integrity_service import DataIntegrityService from bot_data.model.technician_config import TechnicianConfig -from modules.permission.abc.permission_service_abc import PermissionServiceABC +from bot_core.abc.permission_service_abc import PermissionServiceABC class ShutdownCommand(DiscordCommandABC): diff --git a/bot/src/modules/technician/command/sync_xp_command.py b/bot/src/modules/technician/command/sync_xp_command.py index a14bd8ac..7dde7596 100644 --- a/bot/src/modules/technician/command/sync_xp_command.py +++ b/bot/src/modules/technician/command/sync_xp_command.py @@ -20,7 +20,7 @@ from bot_data.model.server_config import ServerConfig from bot_data.model.technician_config import TechnicianConfig from bot_data.model.user import User from modules.level.service.level_service import LevelService -from modules.permission.abc.permission_service_abc import PermissionServiceABC +from bot_core.abc.permission_service_abc import PermissionServiceABC class SyncXpGroup(DiscordCommandABC): diff --git a/web/src/app/modules/view/server/members/members.component.ts b/web/src/app/modules/view/server/members/members.component.ts index 32855b6e..65b43a9a 100644 --- a/web/src/app/modules/view/server/members/members.component.ts +++ b/web/src/app/modules/view/server/members/members.component.ts @@ -113,6 +113,7 @@ export class MembersComponent extends ComponentWithTable implements OnInit, OnDe { label: this.translate.instant("common.bool_as_string.true"), value: false }, { label: this.translate.instant("common.bool_as_string.false"), value: true } ]; + this.spinner.hideSpinner(); this.loadNextPage(); }); } @@ -123,7 +124,8 @@ export class MembersComponent extends ComponentWithTable implements OnInit, OnDe } loadNextPage() { - this.spinner.showSpinner(); + if (!this.server?.id) return; + this.loading = true; this.data.query(Queries.usersQuery, { serverId: this.server.id, filter: this.filter, page: this.page, sort: this.sort @@ -134,7 +136,6 @@ export class MembersComponent extends ComponentWithTable implements OnInit, OnDe ).subscribe(data => { this.totalRecords = data.userCount; this.members = data.users; - this.spinner.hideSpinner(); this.loading = false; }); }