From ff247bcec336245f4f3ce7ca2934c76b432d5e4a Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 29 Dec 2021 19:29:35 +0100 Subject: [PATCH] [WIP] Added permission module --- cpl-workspace.json | 3 +- src/gismo/config/base.json | 6 --- src/gismo/config/permission.json | 10 +++++ src/gismo/startup.py | 9 +++- src/modules/__init__.py | 1 + src/modules/base/base.py | 1 + src/modules/base/base_settings.py | 16 ------- src/modules/permission/__init__.py | 1 + src/modules/permission/abc/__init__.py | 1 + .../permission/abc/permission_service_abc.py | 7 +++ .../configuration/permission_settings.py | 33 ++++++++++++++ src/modules/permission/permission.json | 43 +++++++++++++++++++ src/modules/permission/permission.py | 41 ++++++++++++++++++ src/modules/permission/service/__init__.py | 1 + .../permission/service/permission_service.py | 4 ++ 15 files changed, 153 insertions(+), 24 deletions(-) create mode 100644 src/gismo/config/permission.json create mode 100644 src/modules/permission/__init__.py create mode 100644 src/modules/permission/abc/__init__.py create mode 100644 src/modules/permission/abc/permission_service_abc.py create mode 100644 src/modules/permission/configuration/permission_settings.py create mode 100644 src/modules/permission/permission.json create mode 100644 src/modules/permission/permission.py create mode 100644 src/modules/permission/service/__init__.py create mode 100644 src/modules/permission/service/permission_service.py diff --git a/cpl-workspace.json b/cpl-workspace.json index 891044f..61764bb 100644 --- a/cpl-workspace.json +++ b/cpl-workspace.json @@ -11,7 +11,8 @@ "level-generator": "tools/level_generator/level-generator.json", "ontime-calculator": "tools/ontime_calculator/ontime-calculator.json", "database": "src/modules/database/database.json", - "base": "src/modules/base/base.json" + "base": "src/modules/base/base.json", + "permission": "src/modules/permission/permission.json" }, "Scripts": { "build-start": "cd src/gismo_cli; echo 'gismo-cli:'; cpl build; cd ../gismo; echo 'gismo:'; cpl build; cd ../../dist/gismo/build/gismo; bash gismo", diff --git a/src/gismo/config/base.json b/src/gismo/config/base.json index 41b1cd3..dbf85b8 100644 --- a/src/gismo/config/base.json +++ b/src/gismo/config/base.json @@ -9,12 +9,6 @@ "AFKChannelIds": [ 910199452915093593, 910199452915093594 - ], - "AdminRoleIds": [ - 925072155203477584 - ], - "ModeratorRoleIds": [ - 925072209884635167 ] } } \ No newline at end of file diff --git a/src/gismo/config/permission.json b/src/gismo/config/permission.json new file mode 100644 index 0000000..dac1484 --- /dev/null +++ b/src/gismo/config/permission.json @@ -0,0 +1,10 @@ +{ + "910199451145076828": { + "AdminRoleIds": [ + 925072155203477584 + ], + "ModeratorRoleIds": [ + 925072209884635167 + ] + } +} \ No newline at end of file diff --git a/src/gismo/startup.py b/src/gismo/startup.py index fbdd7cf..1a1f79e 100644 --- a/src/gismo/startup.py +++ b/src/gismo/startup.py @@ -37,6 +37,9 @@ from gismo_data.service.user_repository_service import UserRepositoryService from modules.base.base import Base from modules.boot_log.boot_log import BootLog from modules.database.database import Database +from modules.permission.abc.permission_service_abc import PermissionServiceABC +from modules.permission.permission import Permission +from modules.permission.service.permission_service import PermissionService from modules_core.abc.module_abc import ModuleABC from modules_core.abc.module_service_abc import ModuleServiceABC from modules_core.service.module_service import ModuleService @@ -73,7 +76,7 @@ class Startup(StartupABC): services.add_singleton(BotServiceABC, BotService) services.add_transient(MessageServiceABC, MessageService) - # services + # general services services.add_transient(MigrationService) # data services @@ -85,10 +88,14 @@ class Startup(StartupABC): services.add_transient(UserJoinedVoiceChannelRepositoryABC, UserJoinedVoiceChannelRepositoryService) # modules + services.add_transient(ModuleABC, Permission) services.add_transient(ModuleABC, Database) services.add_transient(ModuleABC, Base) services.add_transient(ModuleABC, BootLog) + # permission module services + services.add_transient(PermissionServiceABC, PermissionService) + # migrations services.add_transient(MigrationABC, InitialMigration) services.add_transient(MigrationABC, Migration_0_3) diff --git a/src/modules/__init__.py b/src/modules/__init__.py index e69de29..425ab6c 100644 --- a/src/modules/__init__.py +++ b/src/modules/__init__.py @@ -0,0 +1 @@ +# imports diff --git a/src/modules/base/base.py b/src/modules/base/base.py index d08363c..ca41fb6 100644 --- a/src/modules/base/base.py +++ b/src/modules/base/base.py @@ -100,6 +100,7 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS async def _add_if_not_exists_user(self, member: Union[discord.User, discord.Member]): self._logger.debug(__name__, f'Check if user exists {member}') + # todo: user permission service settings: BaseSettings = self._get_config() await self._messenger.send_dm_message(settings.welcome_message.format(member.guild.name), member) diff --git a/src/modules/base/base_settings.py b/src/modules/base/base_settings.py index 112e4e8..7afd1c0 100644 --- a/src/modules/base/base_settings.py +++ b/src/modules/base/base_settings.py @@ -16,8 +16,6 @@ class BaseSettings(ConfigurationModelABC): self._xp_per_message: int = 0 self._xp_per_ontime_hour: int = 0 self._afk_channel_ids: list[int] = [] - self._admin_roles: list[int] = [] - self._moderator_roles: list[int] = [] @property def welcome_message(self) -> str: @@ -47,14 +45,6 @@ class BaseSettings(ConfigurationModelABC): def afk_channel_ids(self) -> list[int]: return self._afk_channel_ids - @property - def admin_roles(self) -> list[int]: - return self._admin_roles - - @property - def moderator_roles(self) -> list[int]: - return self._moderator_roles - def from_dict(self, settings: dict): try: self._welcome_message = settings['WelcomeMessage'] @@ -65,12 +55,6 @@ class BaseSettings(ConfigurationModelABC): self._xp_per_ontime_hour = int(settings['XpPerOntimeHour']) for id in settings['AFKChannelIds']: self._afk_channel_ids.append(int(id)) - - for id in settings['AdminRoleIds']: - self._admin_roles.append(int(id)) - - for id in settings['ModeratorRoleIds']: - self._moderator_roles.append(int(id)) except Exception as e: Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in {self.__name__} settings') Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}') diff --git a/src/modules/permission/__init__.py b/src/modules/permission/__init__.py new file mode 100644 index 0000000..425ab6c --- /dev/null +++ b/src/modules/permission/__init__.py @@ -0,0 +1 @@ +# imports diff --git a/src/modules/permission/abc/__init__.py b/src/modules/permission/abc/__init__.py new file mode 100644 index 0000000..425ab6c --- /dev/null +++ b/src/modules/permission/abc/__init__.py @@ -0,0 +1 @@ +# imports diff --git a/src/modules/permission/abc/permission_service_abc.py b/src/modules/permission/abc/permission_service_abc.py new file mode 100644 index 0000000..d74070b --- /dev/null +++ b/src/modules/permission/abc/permission_service_abc.py @@ -0,0 +1,7 @@ +from abc import ABC, abstractmethod + + +class PermissionServiceABC(ABC): + + @abstractmethod + def __init__(self): pass diff --git a/src/modules/permission/configuration/permission_settings.py b/src/modules/permission/configuration/permission_settings.py new file mode 100644 index 0000000..95cf5ef --- /dev/null +++ b/src/modules/permission/configuration/permission_settings.py @@ -0,0 +1,33 @@ +import traceback + +from cpl_core.configuration.configuration_model_abc import ConfigurationModelABC +from cpl_core.console import Console + + +class PermissionSettings(ConfigurationModelABC): + + def __init__(self): + ConfigurationModelABC.__init__(self) + + self._admin_roles: list[int] = [] + self._moderator_roles: list[int] = [] + + + @property + def admin_roles(self) -> list[int]: + return self._admin_roles + + @property + def moderator_roles(self) -> list[int]: + return self._moderator_roles + + def from_dict(self, settings: dict): + try: + for id in settings['AdminRoleIds']: + self._admin_roles.append(int(id)) + + for id in settings['ModeratorRoleIds']: + self._moderator_roles.append(int(id)) + except Exception as e: + Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in {self.__name__} settings') + Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}') diff --git a/src/modules/permission/permission.json b/src/modules/permission/permission.json new file mode 100644 index 0000000..be0f7a5 --- /dev/null +++ b/src/modules/permission/permission.json @@ -0,0 +1,43 @@ +{ + "ProjectSettings": { + "Name": "modules/permission", + "Version": { + "Major": "0", + "Minor": "0", + "Micro": "0" + }, + "Author": "", + "AuthorEmail": "", + "Description": "", + "LongDescription": "", + "URL": "", + "CopyrightDate": "", + "CopyrightName": "", + "LicenseName": "", + "LicenseDescription": "", + "Dependencies": [ + "sh_cpl-core>=2021.11.0.post1" + ], + "PythonVersion": ">=3.9.2", + "PythonPath": { + "linux": "" + }, + "Classifiers": [] + }, + "BuildSettings": { + "ProjectType": "library", + "SourcePath": "", + "OutputPath": "../../dist", + "Main": "modules/permission.main", + "EntryPoint": "modules/permission", + "IncludePackageData": false, + "Included": [], + "Excluded": [ + "*/__pycache__", + "*/logs", + "*/tests" + ], + "PackageData": {}, + "ProjectReferences": [] + } +} \ No newline at end of file diff --git a/src/modules/permission/permission.py b/src/modules/permission/permission.py new file mode 100644 index 0000000..2b71dc8 --- /dev/null +++ b/src/modules/permission/permission.py @@ -0,0 +1,41 @@ +from ctypes import Union +from datetime import datetime + +import discord +from cpl_core.configuration import ConfigurationABC +from cpl_core.database.context import DatabaseContextABC +from cpl_core.logging import LoggerABC + +from gismo_core.abc.bot_service_abc import BotServiceABC +from gismo_core.configuration.server_settings import ServerSettings +from gismo_data.abc.client_repository_abc import ClientRepositoryABC +from gismo_data.abc.known_user_repository_abc import KnownUserRepositoryABC +from gismo_data.abc.user_joined_server_repository_abc import \ + UserJoinedServerRepositoryABC +from gismo_data.abc.user_joined_voice_channel_abc import UserJoinedVoiceChannelRepositoryABC +from gismo_data.abc.user_repository_abc import UserRepositoryABC +from gismo_data.model.client import Client +from gismo_data.model.known_user import KnownUser +from gismo_data.model.server import Server +from gismo_data.model.user import User +from gismo_data.model.user_joined_server import UserJoinedServer +from gismo_data.model.user_joined_voice_channel import UserJoinedVoiceChannel +from gismo_data.service.user_repository_service import ServerRepositoryABC +from modules.permission.configuration.permission_settings import PermissionSettings +from modules_core.abc.events.on_ready_abc import OnReadyABC +from modules_core.abc.module_abc import ModuleABC + + +class Permission(ModuleABC): + + def __init__( + self, + logger: LoggerABC, + ): + self._logger = logger + ModuleABC.__init__( + self, + { OnReadyABC: 0 }, + PermissionSettings + ) + self._logger.trace(__name__, f'Module {type(self)} loaded') \ No newline at end of file diff --git a/src/modules/permission/service/__init__.py b/src/modules/permission/service/__init__.py new file mode 100644 index 0000000..425ab6c --- /dev/null +++ b/src/modules/permission/service/__init__.py @@ -0,0 +1 @@ +# imports diff --git a/src/modules/permission/service/permission_service.py b/src/modules/permission/service/permission_service.py new file mode 100644 index 0000000..461fe98 --- /dev/null +++ b/src/modules/permission/service/permission_service.py @@ -0,0 +1,4 @@ +class PermissionService: + + def __init__(self): + pass