From 1d4e99de46fedbe9add9a1862c83277d0f09241f Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 5 Feb 2024 22:55:52 +0100 Subject: [PATCH] Update permission service cache when roles for member changed --- bot/src/bot_core/core_module.py | 4 ++-- ...ce.py => permission_service_with_cache.py} | 2 +- bot/src/bot_graphql/abc/query_abc.py | 6 ++--- .../mutations/achievement_mutation.py | 4 ++-- .../mutations/scheduled_event_mutation.py | 4 ++-- .../mutations/server_config_mutation.py | 4 ++-- .../mutations/short_role_name_mutation.py | 4 ++-- .../mutations/technician_config_mutation.py | 4 ++-- .../user_joined_game_server_mutation.py | 4 ++-- .../bot_graphql/mutations/user_mutation.py | 4 ++-- bot/src/modules/base/base_module.py | 2 ++ .../events/base_on_member_update_event.py | 24 +++++++++++++++++++ 12 files changed, 46 insertions(+), 20 deletions(-) rename bot/src/bot_core/service/{permission_service.py => permission_service_with_cache.py} (98%) create mode 100644 bot/src/modules/base/events/base_on_member_update_event.py diff --git a/bot/src/bot_core/core_module.py b/bot/src/bot_core/core_module.py index 9323b43e..f44c0312 100644 --- a/bot/src/bot_core/core_module.py +++ b/bot/src/bot_core/core_module.py @@ -15,7 +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 +from bot_core.service.permission_service_with_cache import PermissionServiceWithCache class CoreModule(ModuleABC): @@ -30,7 +30,7 @@ class CoreModule(ModuleABC): services.add_transient(MessageServiceABC, MessageService) services.add_transient(ClientUtilsABC, ClientUtilsService) services.add_transient(DataIntegrityService) - services.add_singleton(PermissionServiceABC, PermissionService) + services.add_singleton(PermissionServiceABC, PermissionServiceWithCache) # pipes services.add_transient(DateTimeOffsetPipe) diff --git a/bot/src/bot_core/service/permission_service.py b/bot/src/bot_core/service/permission_service_with_cache.py similarity index 98% rename from bot/src/bot_core/service/permission_service.py rename to bot/src/bot_core/service/permission_service_with_cache.py index 0ca7e0f3..06005387 100644 --- a/bot/src/bot_core/service/permission_service.py +++ b/bot/src/bot_core/service/permission_service_with_cache.py @@ -12,7 +12,7 @@ from bot_data.abc.technician_config_repository_abc import TechnicianConfigReposi from bot_data.model.team_member_type_enum import TeamMemberTypeEnum -class PermissionService(PermissionServiceABC): +class PermissionServiceWithCache(PermissionServiceABC): def __init__( self, logger: LoggerABC, diff --git a/bot/src/bot_graphql/abc/query_abc.py b/bot/src/bot_graphql/abc/query_abc.py index 9dae04ba..561adbfc 100644 --- a/bot/src/bot_graphql/abc/query_abc.py +++ b/bot/src/bot_graphql/abc/query_abc.py @@ -12,7 +12,7 @@ from bot_core.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_core.service.permission_service_with_cache import PermissionServiceWithCache from bot_data.model.achievement import Achievement from bot_data.model.auth_role_enum import AuthRoleEnum from bot_data.model.auth_user import AuthUser @@ -82,7 +82,7 @@ class QueryABC(ObjectType): return config services = get_services() - permissions: PermissionService = services.get_service(PermissionService) + permissions: PermissionServiceWithCache = services.get_service(PermissionServiceWithCache) bot: DiscordBotServiceABC = services.get_service(DiscordBotServiceABC) config = get_config() @@ -269,7 +269,7 @@ class QueryABC(ObjectType): def _can_user_mutate_data( self, server: Server, permission: UserRoleEnum, services: ServiceProviderABC, config: ConfigurationABC ): - permissions: PermissionService = services.get_service(PermissionService) + permissions: PermissionServiceWithCache = services.get_service(PermissionServiceWithCache) bot: DiscordBotServiceABC = services.get_service(DiscordBotServiceABC) auth_user = Route.get_user() diff --git a/bot/src/bot_graphql/mutations/achievement_mutation.py b/bot/src/bot_graphql/mutations/achievement_mutation.py index 5cc939e0..7f8123d9 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 bot_core.service.permission_service import PermissionService +from bot_core.service.permission_service_with_cache import PermissionServiceWithCache class AchievementMutation(QueryABC): @@ -16,7 +16,7 @@ class AchievementMutation(QueryABC): achievements: AchievementRepositoryABC, bot: DiscordBotServiceABC, db: DatabaseContextABC, - permissions: PermissionService, + permissions: PermissionServiceWithCache, ): QueryABC.__init__(self, "AchievementMutation") diff --git a/bot/src/bot_graphql/mutations/scheduled_event_mutation.py b/bot/src/bot_graphql/mutations/scheduled_event_mutation.py index 381c8685..a5b057b0 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 bot_core.service.permission_service import PermissionService +from bot_core.service.permission_service_with_cache import PermissionServiceWithCache class ScheduledEventMutation(QueryABC): @@ -20,7 +20,7 @@ class ScheduledEventMutation(QueryABC): scheduled_events: ScheduledEventRepositoryABC, bot: DiscordBotServiceABC, db: DatabaseContextABC, - permissions: PermissionService, + permissions: PermissionServiceWithCache, ): QueryABC.__init__(self, "ScheduledEventMutation") diff --git a/bot/src/bot_graphql/mutations/server_config_mutation.py b/bot/src/bot_graphql/mutations/server_config_mutation.py index b3434ee0..c9a93819 100644 --- a/bot/src/bot_graphql/mutations/server_config_mutation.py +++ b/bot/src/bot_graphql/mutations/server_config_mutation.py @@ -13,7 +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 +from bot_core.service.permission_service_with_cache import PermissionServiceWithCache class ServerConfigMutation(QueryABC): @@ -25,7 +25,7 @@ class ServerConfigMutation(QueryABC): server_configs: ServerConfigRepositoryABC, db: DatabaseContextABC, config_service: ConfigService, - permissions: PermissionService, + permissions: PermissionServiceWithCache, ): QueryABC.__init__(self, "ServerConfigMutation") 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 ec5ee9b2..c3b4d2f4 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 bot_core.service.permission_service import PermissionService +from bot_core.service.permission_service_with_cache import PermissionServiceWithCache from modules.short_role_name.service.short_role_name_service import ShortRoleNameService @@ -17,7 +17,7 @@ class ShortRoleNameMutation(QueryABC): short_role_names: ShortRoleNameRepositoryABC, bot: DiscordBotServiceABC, db: DatabaseContextABC, - permissions: PermissionService, + permissions: PermissionServiceWithCache, short_role_name_service: ShortRoleNameService, ): QueryABC.__init__(self, "ShortRoleNameMutation") diff --git a/bot/src/bot_graphql/mutations/technician_config_mutation.py b/bot/src/bot_graphql/mutations/technician_config_mutation.py index 8ca08e86..06d15a53 100644 --- a/bot/src/bot_graphql/mutations/technician_config_mutation.py +++ b/bot/src/bot_graphql/mutations/technician_config_mutation.py @@ -16,7 +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 +from bot_core.service.permission_service_with_cache import PermissionServiceWithCache class TechnicianConfigMutation(QueryABC): @@ -31,7 +31,7 @@ class TechnicianConfigMutation(QueryABC): config_service: ConfigService, tech_seeder: TechnicianConfigSeeder, client_utils: ClientUtilsABC, - permissions: PermissionService, + permissions: PermissionServiceWithCache, ): QueryABC.__init__(self, "TechnicianConfigMutation") 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 4c4987fc..d25a7b78 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 @@ -11,7 +11,7 @@ from flask import request from bot_api.configuration.authentication_settings import AuthenticationSettings from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings -from bot_core.service.permission_service import PermissionService +from bot_core.service.permission_service_with_cache import PermissionServiceWithCache from bot_data.abc.api_key_repository_abc import ApiKeyRepositoryABC from bot_data.abc.game_server_repository_abc import GameServerRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC @@ -40,7 +40,7 @@ class UserJoinedGameServerMutation(QueryABC): user_game_idents: UserGameIdentRepositoryABC, bot: DiscordBotServiceABC, db: DatabaseContextABC, - permissions: PermissionService, + permissions: PermissionServiceWithCache, auth_settings: AuthenticationSettings, ): QueryABC.__init__(self, "UserJoinedGameServerMutation") diff --git a/bot/src/bot_graphql/mutations/user_mutation.py b/bot/src/bot_graphql/mutations/user_mutation.py index e2494c52..61953431 100644 --- a/bot/src/bot_graphql/mutations/user_mutation.py +++ b/bot/src/bot_graphql/mutations/user_mutation.py @@ -4,7 +4,7 @@ from cpl_core.database.context import DatabaseContextABC from cpl_discord.service import DiscordBotServiceABC from bot_api.route.route import Route -from bot_core.service.permission_service import PermissionService +from bot_core.service.permission_service_with_cache import PermissionServiceWithCache from bot_data.abc.level_repository_abc import LevelRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC from bot_data.abc.user_repository_abc import UserRepositoryABC @@ -23,7 +23,7 @@ class UserMutation(QueryABC): users: UserRepositoryABC, bot: DiscordBotServiceABC, db: DatabaseContextABC, - permissions: PermissionService, + permissions: PermissionServiceWithCache, levels: LevelRepositoryABC, level_service: LevelService, user_warnings: UserWarningsRepositoryABC, diff --git a/bot/src/modules/base/base_module.py b/bot/src/modules/base/base_module.py index 53ed3c71..72361ffb 100644 --- a/bot/src/modules/base/base_module.py +++ b/bot/src/modules/base/base_module.py @@ -8,6 +8,7 @@ from bot_core.abc.module_abc import ModuleABC from bot_core.abc.task_abc import TaskABC from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum from modules.base.command.scheduled_events_group import ScheduledEventsCommand +from modules.base.events.base_on_member_update_event import BaseOnMemberUpdateEvent from modules.base.tasks.birthday_watcher import BirthdayWatcher from modules.base.command.afk_command import AFKCommand from modules.base.command.game_server_group import GameServerGroup @@ -117,3 +118,4 @@ class BaseModule(ModuleABC): DiscordEventTypesEnum.on_guild_join.value, BaseOnGuildJoinEvent, ) + services.add_transient(DiscordEventTypesEnum.on_member_update.value, BaseOnMemberUpdateEvent) diff --git a/bot/src/modules/base/events/base_on_member_update_event.py b/bot/src/modules/base/events/base_on_member_update_event.py new file mode 100644 index 00000000..d2701026 --- /dev/null +++ b/bot/src/modules/base/events/base_on_member_update_event.py @@ -0,0 +1,24 @@ +import discord +from cpl_core.logging import LoggerABC +from cpl_discord.events import OnMemberUpdateABC +from cpl_discord.service import DiscordBotServiceABC + +from bot_core.service.permission_service_with_cache import PermissionServiceWithCache + + +class BaseOnMemberUpdateEvent(OnMemberUpdateABC): + def __init__( + self, + logger: LoggerABC, + bot: DiscordBotServiceABC, + permissions: PermissionServiceWithCache, + ): + OnMemberUpdateABC.__init__(self) + + self._logger = logger + self._bot = bot + self._permissions = permissions + + async def on_member_update(self, before: discord.member.Member, after: discord.member.Member): + if before.roles != after.roles: + self._permissions.reset_cache()