From a2c65432ba2d46e543909fcc2aed6cb506d7d62a Mon Sep 17 00:00:00 2001 From: Alexei Date: Sat, 13 May 2023 13:37:24 +0300 Subject: [PATCH] SimpleMessages Access Interface --- bot_modules/mod_agregator.py | 13 +++ bot_modules/mod_interface.py | 25 ++++++ bot_sys/aiogram_bot.py | 41 ++++++++- bot_sys/bot_bd.py | 9 ++ bot_sys/interfaces.py | 20 +++++ bot_sys/user_access.py | 209 +++++++++++++++++++++++-------------------- template/simple_message.py | 63 +++++++------ 7 files changed, 255 insertions(+), 125 deletions(-) create mode 100644 bot_modules/mod_agregator.py create mode 100644 bot_modules/mod_interface.py diff --git a/bot_modules/mod_agregator.py b/bot_modules/mod_agregator.py new file mode 100644 index 0000000..1d7f8a2 --- /dev/null +++ b/bot_modules/mod_agregator.py @@ -0,0 +1,13 @@ +# -*- coding: utf8 -*- +# Общественное достояние, 2023, Алексей Безбородов (Alexei Bezborodov) + +class ModuleAgregator: + def __init__(self): + self.m_Modules = {} + + def GetModule(a_ModName): + return self.m_Modules[a_ModName] + + def AddModule(a_Module): + self.m_Modules[a_Module.GetName()] = a_Module + diff --git a/bot_modules/mod_interface.py b/bot_modules/mod_interface.py new file mode 100644 index 0000000..026deb3 --- /dev/null +++ b/bot_modules/mod_interface.py @@ -0,0 +1,25 @@ +# -*- coding: utf8 -*- +# Общественное достояние, 2023, Алексей Безбородов (Alexei Bezborodov) + +from abc import ABC, abstractmethod + +class IModule(ABC): + @abstractmethod + def GetName(): + pass + + @abstractmethod + def GetInitBDCommands(): + pass + + @abstractmethod + def GetAccess(): + pass + + @abstractmethod + def GetModuleButtons(): + pass + + @abstractmethod + def RegisterHandlers(): + pass diff --git a/bot_sys/aiogram_bot.py b/bot_sys/aiogram_bot.py index e134868..9c3927d 100644 --- a/bot_sys/aiogram_bot.py +++ b/bot_sys/aiogram_bot.py @@ -8,23 +8,58 @@ from aiogram import Bot from aiogram.dispatcher import Dispatcher from aiogram.contrib.fsm_storage.memory import MemoryStorage -class AiogramBot(IBot): - def __init__(self, a_TelegramBotApiToken, a_BDFileName): +class AiogramBot(interfaces.IBot): + def __init__(self, a_TelegramBotApiToken, a_BDFileName, a_RootIDs, a_Log): self.m_TelegramBotApiToken = a_TelegramBotApiToken self.m_BDFileName = a_BDFileName + self.m_RootIDs = a_RootIDs + self.m_Log = a_Log self.m_TBot = Bot(token=self.m_TelegramBotApiToken, parse_mode = types.ParseMode.HTML) self.m_Storage = MemoryStorage() self.m_Dispatcher = Dispatcher(self.m_TBot, storage = storage) + def GetRootIDs(): + return self.m_RootIDs + + def GetLog(): + return self.m_Log + def SQLRequest(self, a_Request : str, commit = False, return_error = False, param = None): return bot_bd.SQLRequest(self.m_BDFileName, a_Request, commit = commit, return_error = return_error, param = param) + def GetUserGroupData(a_UserID): + def GetGroupNamesForUser(a_UserID): + return SQLRequest('SELECT groupName FROM user_groups WHERE group_id=(SELECT group_id FROM user_in_groups WHERE user_id = ?)', param = [a_UserID]) + r = GetGroupNamesForUser(a_UserID) + groups = [] + for i in r: + if len(i) > 0: + groups += [i[0]] + return user_access.UserGroups(a_UserID, groups) + + def GetModulesAccessList(): + return bot_bd.RequestSelectTemplate(self.m_BDFileName, table_name)() + + def GetAccessForModule(a_ModuleName): + alist = GetModulesAccessList() + for i in alist: + if i[0] == a_ModuleName: + return i[1] + return '' + + def GetItemDefaultAccessForModule(a_ModuleName): + alist = GetModulesAccessList() + for i in alist: + if i[0] == a_ModuleName: + return i[2] + return '' + async def SendMessage(self, a_UserID, a_Message, a_PhotoIDs, a_InlineKeyboardButtons, a_KeyboardButtons): inline_keyboard = keyboard.MakeAiogramInlineKeyboard(a_InlineKeyboardButtons) keyboard = keyboard.MakeAiogramKeyboard(a_KeyboardButtons) if not keyboard: keyboard = inline_keyboard - if a_PhotoIDs: + if a_PhotoIDs and a_PhotoIDs != 0 or a_PhotoIDs != '0': self.m_TBot.send_photo( a_UserID, a_PhotoIDs, diff --git a/bot_sys/bot_bd.py b/bot_sys/bot_bd.py index 368ed57..ea9a266 100644 --- a/bot_sys/bot_bd.py +++ b/bot_sys/bot_bd.py @@ -31,6 +31,15 @@ def SelectBDTemplate(a_TableName): def SQLRequestToBD(a_Request : str, commit = False, return_error = False, param = None): return SQLRequest(GetBDFileName(), a_Request, commit = commit, return_error = return_error, param = param) +def RequestsExecute(a_BDFileName, a_Commands): + for cmd in a_Commands: + SQLRequest(a_BDFileName, cmd, commit = True) + +def RequestSelectTemplate(a_BDFileName, a_TableName): + def SelectBD(): + return SQLRequest(a_BDFileName, f'SELECT * FROM {a_TableName}') + return SelectBD + def SQLRequest(a_BDFileName, a_Request : str, commit = False, return_error = False, param = None): db = sqlite3.connect(a_BDFileName) cursor = db.cursor() diff --git a/bot_sys/interfaces.py b/bot_sys/interfaces.py index e3012f7..e39af40 100644 --- a/bot_sys/interfaces.py +++ b/bot_sys/interfaces.py @@ -5,10 +5,30 @@ from abc import ABC, abstractmethod class IBot(ABC): @abstractmethod + def GetRootIDs(): + pass + + @abstractmethod + def GetLog(): + pass + + @abstractmethod def SQLRequest(self, a_Request : str, commit = False, return_error = False, param = None): pass @abstractmethod + def GetUserGroupData(a_UserID): + pass + + @abstractmethod + def GetAccessForModule(a_ModuleName): + pass + + @abstractmethod + def GetItemDefaultAccessForModule(a_ModuleName): + pass + + @abstractmethod async def SendMessage(self, a_UserID, a_Message, a_PhotoIDs, a_InlineKeyboardButtons, a_KeyboardButtons): pass diff --git a/bot_sys/user_access.py b/bot_sys/user_access.py index 2823c7d..12e3c18 100644 --- a/bot_sys/user_access.py +++ b/bot_sys/user_access.py @@ -55,7 +55,11 @@ def CheckAccessItem(a_AccessItem : str, a_AccessMode : AccessMode): # Возвращает возможность доступа пользователю a_UserGroups в элемент с правами a_AccessValue по режиму доступа a_AccessMode def CheckAccessString(a_AccessValue : str, a_UserGroups : UserGroups, a_AccessMode : AccessMode): - if a_UserGroups.user_id in config.GetRootIDs(): + return CheckAccess(config.GetRootIDs(), a_AccessValue, a_UserGroups, a_AccessMode) + +# Возвращает возможность доступа пользователю a_UserGroups в элемент с правами a_AccessValue по режиму доступа a_AccessMode +def CheckAccess(a_RootIDs, a_AccessValue : str, a_UserGroups : UserGroups, a_AccessMode : AccessMode): + if a_UserGroups.user_id in a_RootIDs: return True for i in a_AccessValue.split(';'): d = i.split('=') @@ -72,99 +76,114 @@ def Test(): assert '1234' in ['123', '1234'] assert not '1234' in ['123', '12345'] + # root имеет полный доступ вне зависимости от a_AccessValue и a_UserGroups и a_AccessMode + roots = ['1234'] + for am in AccessMode.ADD, AccessMode.DELETE, AccessMode.EDIT, AccessMode.VIEW, AccessMode.ACCEES_EDIT: + assert CheckAccess(roots, '1234=+', UserGroups('1234', []), am) + assert CheckAccess(roots, '1234=-', UserGroups('1234', []), am) + assert CheckAccess(roots, '1234=+;gr1=+', UserGroups('1234', ['gr1']), am) + assert CheckAccess(roots, '1234=-;gr1=+', UserGroups('1234', ['gr1']), am) + assert CheckAccess(roots, '1234=+', UserGroups('123', []), am) + assert CheckAccess(roots, '1234=-', UserGroups('123', []), am) + assert CheckAccess(roots, '1234=+;gr1=+', UserGroups('123', ['gr']), am) + assert CheckAccess(roots, '1234=-;gr1=+', UserGroups('123', ['gr']), am) + assert CheckAccess(roots, '1234=+;gr=+', UserGroups('123', ['gr1']), am) + assert CheckAccess(roots, '1234=-;gr=+', UserGroups('123', ['gr1']), am) + + roots = ['12'] for am in AccessMode.ADD, AccessMode.DELETE, AccessMode.EDIT, AccessMode.VIEW, AccessMode.ACCEES_EDIT: - assert CheckAccessString('1234=+', UserGroups('1234', []), am) - assert not CheckAccessString('1234=-', UserGroups('1234', []), am) - assert CheckAccessString('1234=+;gr1=+', UserGroups('1234', ['gr1']), am) - assert CheckAccessString('1234=-;gr1=+', UserGroups('1234', ['gr1']), am) - assert not CheckAccessString('1234=+', UserGroups('123', []), am) - assert not CheckAccessString('1234=-', UserGroups('123', []), am) - assert not CheckAccessString('1234=+;gr1=+', UserGroups('123', ['gr']), am) - assert not CheckAccessString('1234=-;gr1=+', UserGroups('123', ['gr']), am) - assert not CheckAccessString('1234=+;gr=+', UserGroups('123', ['gr1']), am) - assert not CheckAccessString('1234=-;gr=+', UserGroups('123', ['gr1']), am) - - assert CheckAccessString('123=-;1234=a', UserGroups('1234', []), AccessMode.ADD) - assert CheckAccessString('123=-;1234=d', UserGroups('1234', []), AccessMode.DELETE) - assert CheckAccessString('123=-;1234=e', UserGroups('1234', []), AccessMode.EDIT) - assert CheckAccessString('123=-;1234=r', UserGroups('1234', []), AccessMode.ACCEES_EDIT) - assert CheckAccessString('123=-;1234=v', UserGroups('1234', []), AccessMode.VIEW) - assert CheckAccessString('123=-;gr1=a', UserGroups('1234', ['gr1']), AccessMode.ADD) - assert CheckAccessString('123=-;gr1=d', UserGroups('1234', ['gr1']), AccessMode.DELETE) - assert CheckAccessString('123=-;gr1=e', UserGroups('1234', ['gr1']), AccessMode.EDIT) - assert CheckAccessString('123=-;gr1=r', UserGroups('1234', ['gr1']), AccessMode.ACCEES_EDIT) - assert CheckAccessString('123=-;gr1=v', UserGroups('1234', ['gr1']), AccessMode.VIEW) - assert CheckAccessString('123=-;1234=daver', UserGroups('1234', []), AccessMode.ADD) - assert CheckAccessString('123=-;1234=dav', UserGroups('1234', []), AccessMode.DELETE) - assert CheckAccessString('123=-;1234=edv', UserGroups('1234', []), AccessMode.EDIT) - assert CheckAccessString('123=-;1234=rav', UserGroups('1234', []), AccessMode.ACCEES_EDIT) - assert CheckAccessString('123=-;1234=va', UserGroups('1234', []), AccessMode.VIEW) - assert CheckAccessString('123=-;gr1=avr', UserGroups('1234', ['gr1']), AccessMode.ADD) - assert CheckAccessString('123=-;gr1=daver', UserGroups('1234', ['gr1']), AccessMode.DELETE) - assert CheckAccessString('123=-;gr1=eva', UserGroups('1234', ['gr1']), AccessMode.EDIT) - assert CheckAccessString('123=-;gr1=re', UserGroups('1234', ['gr1']), AccessMode.ACCEES_EDIT) - assert CheckAccessString('123=-;gr1=vad', UserGroups('1234', ['gr1']), AccessMode.VIEW) - - assert not CheckAccessString('123=-;1234=d', UserGroups('1234', []), AccessMode.ADD) - assert not CheckAccessString('123=-;1234=a', UserGroups('1234', []), AccessMode.DELETE) - assert not CheckAccessString('123=-;1234=r', UserGroups('1234', []), AccessMode.EDIT) - assert not CheckAccessString('123=-;1234=v', UserGroups('1234', []), AccessMode.ACCEES_EDIT) - assert not CheckAccessString('123=-;1234=e', UserGroups('1234', []), AccessMode.VIEW) - assert not CheckAccessString('123=-;gr1=d', UserGroups('1234', ['gr']), AccessMode.ADD) - assert not CheckAccessString('123=-;gr1=a', UserGroups('1234', ['gr']), AccessMode.DELETE) - assert not CheckAccessString('123=-;gr1=v', UserGroups('1234', ['gr']), AccessMode.EDIT) - assert not CheckAccessString('123=-;gr1=e', UserGroups('1234', ['gr']), AccessMode.ACCEES_EDIT) - assert not CheckAccessString('123=-;gr1=r', UserGroups('1234', ['gr']), AccessMode.VIEW) - assert not CheckAccessString('123=-;1234=dver', UserGroups('1234', []), AccessMode.ADD) - assert not CheckAccessString('123=-;1234=av', UserGroups('1234', []), AccessMode.DELETE) - assert not CheckAccessString('123=-;1234=dv', UserGroups('1234', []), AccessMode.EDIT) - assert not CheckAccessString('123=-;1234=av', UserGroups('1234', []), AccessMode.ACCEES_EDIT) - assert not CheckAccessString('123=-;1234=a', UserGroups('1234', []), AccessMode.VIEW) - assert not CheckAccessString('123=-;gr1=vr', UserGroups('1234', ['gr']), AccessMode.ADD) - assert not CheckAccessString('123=-;gr1=aver', UserGroups('1234', ['gr']), AccessMode.DELETE) - assert not CheckAccessString('123=-;gr1=va', UserGroups('1234', ['gr']), AccessMode.EDIT) - assert not CheckAccessString('123=-;gr1=ea', UserGroups('1234', ['gr']), AccessMode.ACCEES_EDIT) - assert not CheckAccessString('123=-;gr1=ad', UserGroups('1234', ['gr']), AccessMode.VIEW) - - assert CheckAccessString(f'123=-;{user_access_group_all}=a', UserGroups('1234', []), AccessMode.ADD) - assert CheckAccessString(f'123=-;{user_access_group_all}=d', UserGroups('1234', []), AccessMode.DELETE) - assert CheckAccessString(f'123=-;{user_access_group_all}=e', UserGroups('1234', []), AccessMode.EDIT) - assert CheckAccessString(f'123=-;{user_access_group_all}=r', UserGroups('1234', []), AccessMode.ACCEES_EDIT) - assert CheckAccessString(f'123=-;{user_access_group_all}=v', UserGroups('1234', []), AccessMode.VIEW) - assert CheckAccessString(f'123=-;{user_access_group_all}=a', UserGroups('1234', ['gr1']), AccessMode.ADD) - assert CheckAccessString(f'123=-;{user_access_group_all}=d', UserGroups('1234', ['gr1']), AccessMode.DELETE) - assert CheckAccessString(f'123=-;{user_access_group_all}=e', UserGroups('1234', ['gr1']), AccessMode.EDIT) - assert CheckAccessString(f'123=-;{user_access_group_all}=r', UserGroups('1234', ['gr1']), AccessMode.ACCEES_EDIT) - assert CheckAccessString(f'123=-;{user_access_group_all}=v', UserGroups('1234', ['gr1']), AccessMode.VIEW) - assert CheckAccessString(f'123=-;{user_access_group_all}=daver', UserGroups('1234', []), AccessMode.ADD) - assert CheckAccessString(f'123=-;{user_access_group_all}=dav', UserGroups('1234', []), AccessMode.DELETE) - assert CheckAccessString(f'123=-;{user_access_group_all}=edv', UserGroups('1234', []), AccessMode.EDIT) - assert CheckAccessString(f'123=-;{user_access_group_all}=rav', UserGroups('1234', []), AccessMode.ACCEES_EDIT) - assert CheckAccessString(f'123=-;{user_access_group_all}=va', UserGroups('1234', []), AccessMode.VIEW) - assert CheckAccessString(f'123=-;{user_access_group_all}=avr', UserGroups('1234', ['gr1']), AccessMode.ADD) - assert CheckAccessString(f'123=-;{user_access_group_all}=daver', UserGroups('1234', ['gr1']), AccessMode.DELETE) - assert CheckAccessString(f'123=-;{user_access_group_all}=eva', UserGroups('1234', ['gr1']), AccessMode.EDIT) - assert CheckAccessString(f'123=-;{user_access_group_all}=re', UserGroups('1234', ['gr1']), AccessMode.ACCEES_EDIT) - assert CheckAccessString(f'123=-;{user_access_group_all}=vad', UserGroups('1234', ['gr1']), AccessMode.VIEW) - - assert not CheckAccessString(f'123=-;{user_access_group_all}=d', UserGroups('1234', []), AccessMode.ADD) - assert not CheckAccessString(f'123=-;{user_access_group_all}=a', UserGroups('1234', []), AccessMode.DELETE) - assert not CheckAccessString(f'123=-;{user_access_group_all}=r', UserGroups('1234', []), AccessMode.EDIT) - assert not CheckAccessString(f'123=-;{user_access_group_all}=v', UserGroups('1234', []), AccessMode.ACCEES_EDIT) - assert not CheckAccessString(f'123=-;{user_access_group_all}=e', UserGroups('1234', []), AccessMode.VIEW) - assert not CheckAccessString(f'123=-;gr1=d;{user_access_group_all}=-', UserGroups('1234', ['gr']), AccessMode.ADD) - assert not CheckAccessString(f'123=-;gr1=a;{user_access_group_all}=-', UserGroups('1234', ['gr']), AccessMode.DELETE) - assert not CheckAccessString(f'123=-;gr1=v;{user_access_group_all}=-', UserGroups('1234', ['gr']), AccessMode.EDIT) - assert not CheckAccessString(f'123=-;gr1=e;{user_access_group_all}=-', UserGroups('1234', ['gr']), AccessMode.ACCEES_EDIT) - assert not CheckAccessString(f'123=-;gr1=r;{user_access_group_all}=-', UserGroups('1234', ['gr']), AccessMode.VIEW) - assert not CheckAccessString(f'123=-;{user_access_group_all}=dver', UserGroups('1234', []), AccessMode.ADD) - assert not CheckAccessString(f'123=-;{user_access_group_all}=av', UserGroups('1234', []), AccessMode.DELETE) - assert not CheckAccessString(f'123=-;{user_access_group_all}=dv', UserGroups('1234', []), AccessMode.EDIT) - assert not CheckAccessString(f'123=-;{user_access_group_all}=av', UserGroups('1234', []), AccessMode.ACCEES_EDIT) - assert not CheckAccessString(f'123=-;{user_access_group_all}=a', UserGroups('1234', []), AccessMode.VIEW) - assert not CheckAccessString(f'123=-;{user_access_group_all}=vr', UserGroups('1234', ['gr']), AccessMode.ADD) - assert not CheckAccessString(f'123=-;{user_access_group_all}=aver', UserGroups('1234', ['gr']), AccessMode.DELETE) - assert not CheckAccessString(f'123=-;{user_access_group_all}=va', UserGroups('1234', ['gr']), AccessMode.EDIT) - assert not CheckAccessString(f'123=-;{user_access_group_all}=ea', UserGroups('1234', ['gr']), AccessMode.ACCEES_EDIT) - assert not CheckAccessString(f'123=-;{user_access_group_all}=ad', UserGroups('1234', ['gr']), AccessMode.VIEW) + assert CheckAccess(roots, '1234=+', UserGroups('1234', []), am) + assert not CheckAccess(roots, '1234=-', UserGroups('1234', []), am) + assert CheckAccess(roots, '1234=+;gr1=+', UserGroups('1234', ['gr1']), am) + assert CheckAccess(roots, '1234=-;gr1=+', UserGroups('1234', ['gr1']), am) + assert not CheckAccess(roots, '1234=+', UserGroups('123', []), am) + assert not CheckAccess(roots, '1234=-', UserGroups('123', []), am) + assert not CheckAccess(roots, '1234=+;gr1=+', UserGroups('123', ['gr']), am) + assert not CheckAccess(roots, '1234=-;gr1=+', UserGroups('123', ['gr']), am) + assert not CheckAccess(roots, '1234=+;gr=+', UserGroups('123', ['gr1']), am) + assert not CheckAccess(roots, '1234=-;gr=+', UserGroups('123', ['gr1']), am) + + assert CheckAccess(roots, '123=-;1234=a', UserGroups('1234', []), AccessMode.ADD) + assert CheckAccess(roots, '123=-;1234=d', UserGroups('1234', []), AccessMode.DELETE) + assert CheckAccess(roots, '123=-;1234=e', UserGroups('1234', []), AccessMode.EDIT) + assert CheckAccess(roots, '123=-;1234=r', UserGroups('1234', []), AccessMode.ACCEES_EDIT) + assert CheckAccess(roots, '123=-;1234=v', UserGroups('1234', []), AccessMode.VIEW) + assert CheckAccess(roots, '123=-;gr1=a', UserGroups('1234', ['gr1']), AccessMode.ADD) + assert CheckAccess(roots, '123=-;gr1=d', UserGroups('1234', ['gr1']), AccessMode.DELETE) + assert CheckAccess(roots, '123=-;gr1=e', UserGroups('1234', ['gr1']), AccessMode.EDIT) + assert CheckAccess(roots, '123=-;gr1=r', UserGroups('1234', ['gr1']), AccessMode.ACCEES_EDIT) + assert CheckAccess(roots, '123=-;gr1=v', UserGroups('1234', ['gr1']), AccessMode.VIEW) + assert CheckAccess(roots, '123=-;1234=daver', UserGroups('1234', []), AccessMode.ADD) + assert CheckAccess(roots, '123=-;1234=dav', UserGroups('1234', []), AccessMode.DELETE) + assert CheckAccess(roots, '123=-;1234=edv', UserGroups('1234', []), AccessMode.EDIT) + assert CheckAccess(roots, '123=-;1234=rav', UserGroups('1234', []), AccessMode.ACCEES_EDIT) + assert CheckAccess(roots, '123=-;1234=va', UserGroups('1234', []), AccessMode.VIEW) + assert CheckAccess(roots, '123=-;gr1=avr', UserGroups('1234', ['gr1']), AccessMode.ADD) + assert CheckAccess(roots, '123=-;gr1=daver', UserGroups('1234', ['gr1']), AccessMode.DELETE) + assert CheckAccess(roots, '123=-;gr1=eva', UserGroups('1234', ['gr1']), AccessMode.EDIT) + assert CheckAccess(roots, '123=-;gr1=re', UserGroups('1234', ['gr1']), AccessMode.ACCEES_EDIT) + assert CheckAccess(roots, '123=-;gr1=vad', UserGroups('1234', ['gr1']), AccessMode.VIEW) + + assert not CheckAccess(roots, '123=-;1234=d', UserGroups('1234', []), AccessMode.ADD) + assert not CheckAccess(roots, '123=-;1234=a', UserGroups('1234', []), AccessMode.DELETE) + assert not CheckAccess(roots, '123=-;1234=r', UserGroups('1234', []), AccessMode.EDIT) + assert not CheckAccess(roots, '123=-;1234=v', UserGroups('1234', []), AccessMode.ACCEES_EDIT) + assert not CheckAccess(roots, '123=-;1234=e', UserGroups('1234', []), AccessMode.VIEW) + assert not CheckAccess(roots, '123=-;gr1=d', UserGroups('1234', ['gr']), AccessMode.ADD) + assert not CheckAccess(roots, '123=-;gr1=a', UserGroups('1234', ['gr']), AccessMode.DELETE) + assert not CheckAccess(roots, '123=-;gr1=v', UserGroups('1234', ['gr']), AccessMode.EDIT) + assert not CheckAccess(roots, '123=-;gr1=e', UserGroups('1234', ['gr']), AccessMode.ACCEES_EDIT) + assert not CheckAccess(roots, '123=-;gr1=r', UserGroups('1234', ['gr']), AccessMode.VIEW) + assert not CheckAccess(roots, '123=-;1234=dver', UserGroups('1234', []), AccessMode.ADD) + assert not CheckAccess(roots, '123=-;1234=av', UserGroups('1234', []), AccessMode.DELETE) + assert not CheckAccess(roots, '123=-;1234=dv', UserGroups('1234', []), AccessMode.EDIT) + assert not CheckAccess(roots, '123=-;1234=av', UserGroups('1234', []), AccessMode.ACCEES_EDIT) + assert not CheckAccess(roots, '123=-;1234=a', UserGroups('1234', []), AccessMode.VIEW) + assert not CheckAccess(roots, '123=-;gr1=vr', UserGroups('1234', ['gr']), AccessMode.ADD) + assert not CheckAccess(roots, '123=-;gr1=aver', UserGroups('1234', ['gr']), AccessMode.DELETE) + assert not CheckAccess(roots, '123=-;gr1=va', UserGroups('1234', ['gr']), AccessMode.EDIT) + assert not CheckAccess(roots, '123=-;gr1=ea', UserGroups('1234', ['gr']), AccessMode.ACCEES_EDIT) + assert not CheckAccess(roots, '123=-;gr1=ad', UserGroups('1234', ['gr']), AccessMode.VIEW) + + assert CheckAccess(roots, f'123=-;{user_access_group_all}=a', UserGroups('1234', []), AccessMode.ADD) + assert CheckAccess(roots, f'123=-;{user_access_group_all}=d', UserGroups('1234', []), AccessMode.DELETE) + assert CheckAccess(roots, f'123=-;{user_access_group_all}=e', UserGroups('1234', []), AccessMode.EDIT) + assert CheckAccess(roots, f'123=-;{user_access_group_all}=r', UserGroups('1234', []), AccessMode.ACCEES_EDIT) + assert CheckAccess(roots, f'123=-;{user_access_group_all}=v', UserGroups('1234', []), AccessMode.VIEW) + assert CheckAccess(roots, f'123=-;{user_access_group_all}=a', UserGroups('1234', ['gr1']), AccessMode.ADD) + assert CheckAccess(roots, f'123=-;{user_access_group_all}=d', UserGroups('1234', ['gr1']), AccessMode.DELETE) + assert CheckAccess(roots, f'123=-;{user_access_group_all}=e', UserGroups('1234', ['gr1']), AccessMode.EDIT) + assert CheckAccess(roots, f'123=-;{user_access_group_all}=r', UserGroups('1234', ['gr1']), AccessMode.ACCEES_EDIT) + assert CheckAccess(roots, f'123=-;{user_access_group_all}=v', UserGroups('1234', ['gr1']), AccessMode.VIEW) + assert CheckAccess(roots, f'123=-;{user_access_group_all}=daver', UserGroups('1234', []), AccessMode.ADD) + assert CheckAccess(roots, f'123=-;{user_access_group_all}=dav', UserGroups('1234', []), AccessMode.DELETE) + assert CheckAccess(roots, f'123=-;{user_access_group_all}=edv', UserGroups('1234', []), AccessMode.EDIT) + assert CheckAccess(roots, f'123=-;{user_access_group_all}=rav', UserGroups('1234', []), AccessMode.ACCEES_EDIT) + assert CheckAccess(roots, f'123=-;{user_access_group_all}=va', UserGroups('1234', []), AccessMode.VIEW) + assert CheckAccess(roots, f'123=-;{user_access_group_all}=avr', UserGroups('1234', ['gr1']), AccessMode.ADD) + assert CheckAccess(roots, f'123=-;{user_access_group_all}=daver', UserGroups('1234', ['gr1']), AccessMode.DELETE) + assert CheckAccess(roots, f'123=-;{user_access_group_all}=eva', UserGroups('1234', ['gr1']), AccessMode.EDIT) + assert CheckAccess(roots, f'123=-;{user_access_group_all}=re', UserGroups('1234', ['gr1']), AccessMode.ACCEES_EDIT) + assert CheckAccess(roots, f'123=-;{user_access_group_all}=vad', UserGroups('1234', ['gr1']), AccessMode.VIEW) + + assert not CheckAccess(roots, f'123=-;{user_access_group_all}=d', UserGroups('1234', []), AccessMode.ADD) + assert not CheckAccess(roots, f'123=-;{user_access_group_all}=a', UserGroups('1234', []), AccessMode.DELETE) + assert not CheckAccess(roots, f'123=-;{user_access_group_all}=r', UserGroups('1234', []), AccessMode.EDIT) + assert not CheckAccess(roots, f'123=-;{user_access_group_all}=v', UserGroups('1234', []), AccessMode.ACCEES_EDIT) + assert not CheckAccess(roots, f'123=-;{user_access_group_all}=e', UserGroups('1234', []), AccessMode.VIEW) + assert not CheckAccess(roots, f'123=-;gr1=d;{user_access_group_all}=-', UserGroups('1234', ['gr']), AccessMode.ADD) + assert not CheckAccess(roots, f'123=-;gr1=a;{user_access_group_all}=-', UserGroups('1234', ['gr']), AccessMode.DELETE) + assert not CheckAccess(roots, f'123=-;gr1=v;{user_access_group_all}=-', UserGroups('1234', ['gr']), AccessMode.EDIT) + assert not CheckAccess(roots, f'123=-;gr1=e;{user_access_group_all}=-', UserGroups('1234', ['gr']), AccessMode.ACCEES_EDIT) + assert not CheckAccess(roots, f'123=-;gr1=r;{user_access_group_all}=-', UserGroups('1234', ['gr']), AccessMode.VIEW) + assert not CheckAccess(roots, f'123=-;{user_access_group_all}=dver', UserGroups('1234', []), AccessMode.ADD) + assert not CheckAccess(roots, f'123=-;{user_access_group_all}=av', UserGroups('1234', []), AccessMode.DELETE) + assert not CheckAccess(roots, f'123=-;{user_access_group_all}=dv', UserGroups('1234', []), AccessMode.EDIT) + assert not CheckAccess(roots, f'123=-;{user_access_group_all}=av', UserGroups('1234', []), AccessMode.ACCEES_EDIT) + assert not CheckAccess(roots, f'123=-;{user_access_group_all}=a', UserGroups('1234', []), AccessMode.VIEW) + assert not CheckAccess(roots, f'123=-;{user_access_group_all}=vr', UserGroups('1234', ['gr']), AccessMode.ADD) + assert not CheckAccess(roots, f'123=-;{user_access_group_all}=aver', UserGroups('1234', ['gr']), AccessMode.DELETE) + assert not CheckAccess(roots, f'123=-;{user_access_group_all}=va', UserGroups('1234', ['gr']), AccessMode.EDIT) + assert not CheckAccess(roots, f'123=-;{user_access_group_all}=ea', UserGroups('1234', ['gr']), AccessMode.ACCEES_EDIT) + assert not CheckAccess(roots, f'123=-;{user_access_group_all}=ad', UserGroups('1234', ['gr']), AccessMode.VIEW) diff --git a/template/simple_message.py b/template/simple_message.py index a400720..5f848b8 100644 --- a/template/simple_message.py +++ b/template/simple_message.py @@ -3,53 +3,62 @@ # Простые информационные сообщения -from bot_sys import user_access, config -from bot_modules import access, groups +from bot_sys import user_access +from bot_modules import access from aiogram import types -from aiogram import Bot -bot = Bot(token=config.GetTelegramBotApiToken(), parse_mode = types.ParseMode.HTML) - class WorkFuncResult(): - def __init__(self, a_StringMessage : str, photo_id = None, item_access = None, keyboard_func = None): + def __init__(self, a_StringMessage : str, photo_id = None, item_access = None): self.string_message = a_StringMessage self.photo_id = photo_id self.item_access = item_access - self.keyboard_func = keyboard_func - -def InfoMessageTemplateLegacy(a_HelpMessage, a_GetButtonsFunc, a_AccessFunc, access_mode = user_access.AccessMode.VIEW): +def InfoMessageTemplate(a_Bot, a_HelpMessage, a_GetButtonsFunc, a_GetInlineButtonsFunc, a_AccessFunc, access_mode = user_access.AccessMode.VIEW): async def GetMessage(a_Message : types.message, state = None): return WorkFuncResult(a_HelpMessage) - return SimpleMessageTemplateLegacy(GetMessage, a_GetButtonsFunc, a_AccessFunc, access_mode) + return SimpleMessageTemplate(a_Bot, GetMessage, a_GetButtonsFunc, a_GetInlineButtonsFunc, a_AccessFunc, access_mode = access_mode) + +def SimpleMessageTemplate(a_Bot, a_WorkFunc, a_GetButtonsFunc, a_GetInlineButtonsFunc, a_AccessFunc, access_mode = user_access.AccessMode.VIEW): + def ProxyGetButtonsTemplate(a_GetButtonsFunc): + def ReturnNone(): + return None + if a_GetButtonsFunc: + return a_GetButtonsFunc + else: + return ReturnNone + + async def AccessDeniedMessage(a_UserID, a_Message, user_groups): + return a_Bot.SendMessage( + a_UserID, + access.access_denied_message, + None, + None, + ProxyGetButtonsTemplate(a_GetButtonsFunc)(a_Message, user_groups) + ) -def SimpleMessageTemplateLegacy(a_WorkFunc, a_GetButtonsFunc, a_AccessFunc, access_mode = user_access.AccessMode.VIEW): async def SimpleMessage(a_Message : types.message, state = None): user_id = str(a_Message.from_user.id) - user_groups = groups.GetUserGroupData(user_id) - if not user_access.CheckAccessString(a_AccessFunc(), user_groups, access_mode): - return await bot.send_message(a_Message.from_user.id, access.access_denied_message, reply_markup = a_GetButtonsFunc(a_Message, user_groups)) + user_groups = a_Bot.GetUserGroupData(user_id) + if not user_access.CheckAccess(a_Bot.GetRootIDs(), a_AccessFunc(), user_groups, access_mode): + return await AccessDeniedMessage(user_id, a_Message, user_groups) res = await a_WorkFunc(a_Message, state = state) if res is None: return - - keyboard_func = a_GetButtonsFunc - if res.keyboard_func: - keyboard_func = res.keyboard_func - + msg = res.string_message if msg is None: return - photo_id = res.photo_id - - if not res.item_access is None and not user_access.CheckAccessString(res.item_access, user_groups, access_mode): - return await bot.send_message(a_Message.from_user.id, access.access_denied_message, reply_markup = keyboard_func(a_Message, user_groups)) - - if photo_id is None or photo_id == 0 or photo_id == '0': - return await bot.send_message(a_Message.from_user.id, msg, reply_markup = keyboard_func(a_Message, user_groups)) + if not res.item_access is None and not user_access.CheckAccess(a_Bot.GetRootIDs(), res.item_access, user_groups, access_mode): + return await AccessDeniedMessage(user_id, a_Message, user_groups) - await bot.send_photo(user_id, photo_id, msg, reply_markup = keyboard_func(a_Message, user_groups)) + await a_Bot.SendMessage( + user_id, + msg, + res.photo_id, + ProxyGetButtonsTemplate(a_GetInlineButtonsFunc)(a_Message, user_groups), + ProxyGetButtonsTemplate(a_GetButtonsFunc)(a_Message, user_groups) + ) return SimpleMessage