From 86a7ce2c88438dd777b008e90d8ee5f27ec7c398 Mon Sep 17 00:00:00 2001 From: Alexei Date: Mon, 24 Apr 2023 23:17:12 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D1=81=D1=82=D1=83=D0=BF=20=D0=BA?= =?UTF-8?q?=D0=BE=20=D0=B2=D1=81=D0=B5=D0=BC=20=D0=BF=D1=80=D0=BE=D0=B5?= =?UTF-8?q?=D0=BA=D1=82=D0=B0=D0=BC=20=D0=BA=D1=80=D0=BE=D0=BC=D0=B5=20?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot_modules/access.py | 24 ++++++++++++---- bot_modules/groups.py | 74 ++++++++++++++++++++++++++++++------------------- bot_modules/profile.py | 7 +++-- bot_modules/projects.py | 70 +++++++++++++++++++++++----------------------- bot_modules/start.py | 7 +++-- bot_sys/keyboard.py | 22 ++++++++++----- bot_sys/user_access.py | 5 +++- 7 files changed, 128 insertions(+), 81 deletions(-) diff --git a/bot_modules/access.py b/bot_modules/access.py index b37c30c..75c8085 100644 --- a/bot_modules/access.py +++ b/bot_modules/access.py @@ -6,6 +6,8 @@ from bot_sys import bot_bd, log, config, keyboard, user_access from bot_modules import start, groups from aiogram import Bot, types +from aiogram.dispatcher import FSMContext +from aiogram.dispatcher.filters.state import State, StatesGroup import sqlite3 @@ -13,6 +15,9 @@ from aiogram.dispatcher import Dispatcher bot = Bot(token=config.GetTelegramBotApiToken(), parse_mode=types.ParseMode.HTML) +class FSMRequestToBD(StatesGroup): + sqlRequest = State() + # --------------------------------------------------------- # БД table_name = 'module_access' @@ -51,6 +56,10 @@ help_message = ''' modAccess - строка ''' + user_access.user_access_readme +access_denied_message = ''' +Доступ запрещён! +''' + access_button_name = "📰 Доступ пользователей" sql_request_button_name = "📰 Запрос к БД для редактирования доступа" help_button_name = "📰 Информация по редактированию доступа" @@ -64,7 +73,7 @@ def GetEditAccessKeyboardButtons(a_UserGroups): keyboard.ButtonWithAccess(help_button_name, user_access.AccessMode.VIEW, GetAccess()) ] mods = [start] - return keyboard.MakeKeyboard(keyboard.GetButtons(mods) + cur_buttons) + return keyboard.MakeKeyboard(keyboard.GetButtons(mods) + cur_buttons, a_UserGroups) # --------------------------------------------------------- # Обработка сообщений @@ -73,6 +82,8 @@ def GetEditAccessKeyboardButtons(a_UserGroups): async def AccessStart(a_Message): user_id = str(a_Message.from_user.id) user_groups = groups.GetUserGroupData(user_id) + if not user_access.CheckAccessString(GetAccess(), user_groups, user_access.AccessMode.VIEW): + return await bot.send_message(user_id, access.access_denied_message, reply_markup = GetEditAccessKeyboardButtons(user_groups)) await bot.send_message(user_id, access_start_message, reply_markup = GetEditAccessKeyboardButtons(user_groups)) # --------------------------------------------------------- # Работа с базой данных @@ -90,9 +101,6 @@ def GetAccessForModule(a_ModuleName): # --------------------------------------------------------- # API -def GetUserAccess(a_UserID): - return None - # Инициализация БД def GetInitBDCommands(): return init_bd_cmds @@ -107,5 +115,9 @@ def GetModuleButtons(): # Обработка кнопок def RegisterHandlers(dp : Dispatcher): dp.register_message_handler(AccessStart, text = access_button_name) - dp.register_message_handler(groups.RequestToBDTemplate(request_start_message), text = sql_request_button_name) - dp.register_message_handler(groups.HelpTemplate(help_message, GetEditAccessKeyboardButtons), text = help_button_name) + + dp.register_message_handler(groups.RequestToBDTemplate(request_start_message, GetAccess, FSMRequestToBD), text = sql_request_button_name) + dp.register_message_handler(groups.RequestToBDCancelTemplate(GetEditAccessKeyboardButtons, GetAccess), text = groups.canсel_button_name, state = FSMRequestToBD.sqlRequest) + dp.register_message_handler(groups.RequestToBDFinishTemplate(GetEditAccessKeyboardButtons, GetAccess), state = FSMRequestToBD.sqlRequest) + + dp.register_message_handler(groups.HelpTemplate(help_message, GetEditAccessKeyboardButtons, GetAccess), text = help_button_name) diff --git a/bot_modules/groups.py b/bot_modules/groups.py index 671209c..609cd48 100644 --- a/bot_modules/groups.py +++ b/bot_modules/groups.py @@ -82,9 +82,9 @@ def GetEditGroupKeyboardButtons(a_UserGroups): mods = [start] return keyboard.MakeKeyboard(keyboard.GetButtons(mods) + cur_buttons, a_UserGroups) -def GetCancelKeyboardButtons(a_UserGroups): +def GetCancelKeyboardButtons(a_UserGroups, a_AccessFunc): cur_buttons = [ - keyboard.ButtonWithAccess(canсel_button_name, user_access.AccessMode.VIEW, GetAccess()) + keyboard.ButtonWithAccess(canсel_button_name, user_access.AccessMode.VIEW, a_AccessFunc()) ] return keyboard.MakeKeyboard(cur_buttons, a_UserGroups) @@ -95,40 +95,55 @@ def GetCancelKeyboardButtons(a_UserGroups): async def GroupStart(a_Message): user_id = str(a_Message.from_user.id) user_groups = GetUserGroupData(user_id) + if not user_access.CheckAccessString(GetAccess(), user_groups, user_access.AccessMode.VIEW): + return await bot.send_message(user_id, access.access_denied_message, reply_markup = GetEditGroupKeyboardButtons(user_groups)) + await bot.send_message(user_id, group_start_message, reply_markup = GetEditGroupKeyboardButtons(user_groups)) -async def RequestToBDCancel(a_Message : types.message, state : FSMContext): - user_id = str(a_Message.from_user.id) - user_groups = GetUserGroupData(user_id) - await state.finish() - await a_Message.answer(request_cancel_message, reply_markup = GetEditGroupKeyboardButtons(user_groups)) +def RequestToBDCancelTemplate(a_GetButtonsFunc, a_AccessFunc): + async def RequestToBDCancel(a_Message : types.message, state : FSMContext): + user_id = str(a_Message.from_user.id) + user_groups = GetUserGroupData(user_id) + if not user_access.CheckAccessString(a_AccessFunc(), user_groups, user_access.AccessMode.VIEW): + return await bot.send_message(user_id, access.access_denied_message, reply_markup = a_GetButtonsFunc(user_groups)) + await state.finish() + await a_Message.answer(request_cancel_message, reply_markup = a_GetButtonsFunc(user_groups)) + return RequestToBDCancel -def HelpTemplate(a_HelpMessage, a_GetButtonsFunc): +def HelpTemplate(a_HelpMessage, a_GetButtonsFunc, a_AccessFunc): async def Help(a_Message : types.message): user_id = str(a_Message.from_user.id) user_groups = GetUserGroupData(user_id) + if not user_access.CheckAccessString(a_AccessFunc(), user_groups, user_access.AccessMode.VIEW): + return await bot.send_message(user_id, access.access_denied_message, reply_markup = a_GetButtonsFunc(user_groups)) await a_Message.answer(a_HelpMessage, reply_markup = a_GetButtonsFunc(user_groups)) #, parse_mode='Markdown') return Help -def RequestToBDTemplate(a_StartMessage): +def RequestToBDTemplate(a_StartMessage, a_AccessFunc, a_FSM): async def RequestToBDStart(a_Message : types.message): user_id = str(a_Message.from_user.id) user_groups = GetUserGroupData(user_id) - await FSMRequestToBD.sqlRequest.set() - await a_Message.answer(a_StartMessage, reply_markup = GetCancelKeyboardButtons(user_groups), parse_mode='Markdown') + if not user_access.CheckAccessString(a_AccessFunc(), user_groups, user_access.AccessMode.EDIT): + return await bot.send_message(user_id, access.access_denied_message, reply_markup = GetCancelKeyboardButtons(user_groups, a_AccessFunc)) + await a_FSM.sqlRequest.set() + await a_Message.answer(a_StartMessage, reply_markup = GetCancelKeyboardButtons(user_groups, a_AccessFunc), parse_mode='Markdown') return RequestToBDStart -async def RequestToBD(a_Message : types.message, state : FSMContext): - user_id = str(a_Message.from_user.id) - user_groups = GetUserGroupData(user_id) - result = '' - async with state.proxy() as prjData: - sql_request = a_Message.text - log.Success(f'Сделан запрос [{sql_request}] пользователем {a_Message.from_user.id}.') - result = bot_bd.SQLRequestToBDCommit(sql_request) - log.Success(f'Результат запроса [{sql_request}] от пользователя {a_Message.from_user.id} следующий [{result}].') - await state.finish() - await a_Message.answer(str(result), reply_markup = GetEditGroupKeyboardButtons(user_groups)) +def RequestToBDFinishTemplate(a_GetButtonsFunc, a_AccessFunc): + async def RequestToBDFinish(a_Message : types.message, state : FSMContext): + user_id = str(a_Message.from_user.id) + user_groups = GetUserGroupData(user_id) + if not user_access.CheckAccessString(a_AccessFunc(), user_groups, user_access.AccessMode.EDIT): + return await bot.send_message(user_id, access.access_denied_message, reply_markup = GetEditGroupKeyboardButtons(user_groups)) + result = '' + async with state.proxy() as prjData: + sql_request = a_Message.text + log.Success(f'Сделан запрос [{sql_request}] пользователем {a_Message.from_user.id}.') + result = bot_bd.SQLRequestToBDCommit(sql_request) + log.Success(f'Результат запроса [{sql_request}] от пользователя {a_Message.from_user.id} следующий [{result}].') + await state.finish() + await a_Message.answer(str(result), reply_markup = a_GetButtonsFunc(user_groups)) + return RequestToBDFinish # --------------------------------------------------------- # Работа с базой данных групп @@ -141,8 +156,10 @@ def GetGroupNamesForUser(a_UserID): def GetUserGroupData(a_UserID): r = GetGroupNamesForUser(a_UserID) - print(r) - return user_access.UserGroups(a_UserID, r) + groups = [] + for i in r: + groups += [i[0]] + return user_access.UserGroups(a_UserID, groups) # --------------------------------------------------------- # API @@ -161,7 +178,8 @@ def GetModuleButtons(): # Обработка кнопок def RegisterHandlers(dp : Dispatcher): dp.register_message_handler(GroupStart, text = user_group_button_name) - dp.register_message_handler(RequestToBDTemplate(request_start_message), text = sql_request_button_name) - dp.register_message_handler(HelpTemplate(help_message, GetEditGroupKeyboardButtons), text = help_button_name) - dp.register_message_handler(RequestToBDCancel, text = canсel_button_name, state = FSMRequestToBD.sqlRequest) - dp.register_message_handler(RequestToBD, state = FSMRequestToBD.sqlRequest) + dp.register_message_handler(HelpTemplate(help_message, GetEditGroupKeyboardButtons, GetAccess), text = help_button_name) + + dp.register_message_handler(RequestToBDTemplate(request_start_message, GetAccess, FSMRequestToBD), text = sql_request_button_name) + dp.register_message_handler(RequestToBDCancelTemplate(GetEditGroupKeyboardButtons, GetAccess), text = canсel_button_name, state = FSMRequestToBD.sqlRequest) + dp.register_message_handler(RequestToBDFinishTemplate(GetEditGroupKeyboardButtons, GetAccess), state = FSMRequestToBD.sqlRequest) diff --git a/bot_modules/profile.py b/bot_modules/profile.py index c94ddca..a0f7731 100644 --- a/bot_modules/profile.py +++ b/bot_modules/profile.py @@ -50,12 +50,15 @@ def GetStartKeyboardButtons(a_UserGroups): # Отображение профиля пользователя async def ProfileOpen(a_Message): user_id = str(a_Message.from_user.id) - user_group = groups.GetUserGroupData(user_id) + user_groups= groups.GetUserGroupData(user_id) + if not user_access.CheckAccessString(GetAccess(), user_groups, user_access.AccessMode.VIEW): + return await bot.send_message(user_id, access.access_denied_message, reply_markup = GetStartKeyboardButtons(user_groups)) + user_info = GetUserInfo(user_id) msg = profile_message if not user_info is None: msg = msg.replace('@user_id', str(user_info[0])).replace('@user_name', str(user_info[1])) - await bot.send_message(user_id, msg, reply_markup = GetStartKeyboardButtons(user_group)) + await bot.send_message(user_id, msg, reply_markup = GetStartKeyboardButtons(user_groups)) # --------------------------------------------------------- # Работа с базой данных пользователей diff --git a/bot_modules/projects.py b/bot_modules/projects.py index 39820cf..0647595 100644 --- a/bot_modules/projects.py +++ b/bot_modules/projects.py @@ -181,9 +181,9 @@ def GetProjectsListKeyboardButtons(a_UserGroups, a_Prefix): # Отображение всех проектов async def ProjectsOpen(a_Message : types.message): user_id = str(a_Message.from_user.id) - user_group = groups.GetUserGroupData(user_id) - await bot.send_message(a_Message.from_user.id, base_project_message, reply_markup = GetEditProjectKeyboardButtons(user_group)) - await bot.send_message(a_Message.from_user.id, select_project_message, reply_markup = GetProjectsListKeyboardButtons(user_group, select_project_callback_prefix)) + user_groups = groups.GetUserGroupData(user_id) + await bot.send_message(a_Message.from_user.id, base_project_message, reply_markup = GetEditProjectKeyboardButtons(user_groups)) + await bot.send_message(a_Message.from_user.id, select_project_message, reply_markup = GetProjectsListKeyboardButtons(user_groups, select_project_callback_prefix)) def GetProjectData(a_ProjectID): project = GetProject(a_ProjectID) @@ -198,39 +198,39 @@ def GetProjectData(a_ProjectID): async def ShowProject(a_CallbackQuery : types.CallbackQuery): project_id = str(a_CallbackQuery.data).replace(select_project_callback_prefix, '') user_id = str(a_CallbackQuery.from_user.id) - user_group = groups.GetUserGroupData(user_id) + user_groups = groups.GetUserGroupData(user_id) msg, photo_id, name, desc = GetProjectData(project_id) if msg != '': - await bot.send_message(a_CallbackQuery.from_user.id, msg, reply_markup = GetEditProjectKeyboardButtons(user_group)) + await bot.send_message(a_CallbackQuery.from_user.id, msg, reply_markup = GetEditProjectKeyboardButtons(user_groups)) return msg = project_open_message.replace('@proj_name', name).replace('@proj_desk', desc) if photo_id == '0': - await bot.send_message(a_CallbackQuery.from_user.id, msg, reply_markup = GetEditProjectKeyboardButtons(user_group)) + await bot.send_message(a_CallbackQuery.from_user.id, msg, reply_markup = GetEditProjectKeyboardButtons(user_groups)) else: - await bot.send_photo(a_CallbackQuery.from_user.id, photo_id, msg, reply_markup = GetEditProjectKeyboardButtons(user_group)) + await bot.send_photo(a_CallbackQuery.from_user.id, photo_id, msg, reply_markup = GetEditProjectKeyboardButtons(user_groups)) # Создание нового проекта async def ProjectCreateCancel(a_Message : types.message, state : FSMContext): user_id = str(a_Message.from_user.id) - user_group = groups.GetUserGroupData(user_id) + user_groups = groups.GetUserGroupData(user_id) await state.finish() - await a_Message.answer(project_cancel_create_message, reply_markup = GetEditProjectKeyboardButtons(user_group)) + await a_Message.answer(project_cancel_create_message, reply_markup = GetEditProjectKeyboardButtons(user_groups)) async def ProjectCreate(a_Message : types.message): user_id = str(a_Message.from_user.id) - user_group = groups.GetUserGroupData(user_id) + user_groups = groups.GetUserGroupData(user_id) await FSMCreateProject.prjPhoto.set() - await a_Message.answer(project_create_message_1, reply_markup = GetSkipAndCancelKeyboardButtons(user_group)) + await a_Message.answer(project_create_message_1, reply_markup = GetSkipAndCancelKeyboardButtons(user_groups)) async def PhotoLoad(a_Message : types.message, state : FSMContext, a_FileID): user_id = str(a_Message.from_user.id) - user_group = groups.GetUserGroupData(user_id) + user_groups = groups.GetUserGroupData(user_id) async with state.proxy() as prjData: prjData['photo'] = a_FileID await FSMCreateProject.next() - await a_Message.answer(project_create_message_2, reply_markup = GetCancelKeyboardButtons(user_group)) + await a_Message.answer(project_create_message_2, reply_markup = GetCancelKeyboardButtons(user_groups)) async def ProjectPhotoLoad(a_Message : types.message, state : FSMContext): await PhotoLoad(a_Message, state, a_Message.photo[0].file_id) @@ -240,15 +240,15 @@ async def ProjectPhotoSkip(a_Message : types.message, state : FSMContext): async def ProjectNameLoad(a_Message : types.message, state : FSMContext): user_id = str(a_Message.from_user.id) - user_group = groups.GetUserGroupData(user_id) + user_groups = groups.GetUserGroupData(user_id) async with state.proxy() as prjData: prjData['name'] = a_Message.text await FSMCreateProject.next() - await a_Message.answer(project_create_message_3, reply_markup = GetCancelKeyboardButtons(user_group)) + await a_Message.answer(project_create_message_3, reply_markup = GetCancelKeyboardButtons(user_groups)) async def ProjectDescLoad(a_Message : types.message, state : FSMContext): user_id = str(a_Message.from_user.id) - user_group = groups.GetUserGroupData(user_id) + user_groups = groups.GetUserGroupData(user_id) async with state.proxy() as prjData: prjData['desc'] = a_Message.text prjPhoto = prjData['photo'] @@ -257,34 +257,34 @@ async def ProjectDescLoad(a_Message : types.message, state : FSMContext): AddProject(prjPhoto, prjName, prjDesc) log.Success(f'Добавлен проект {prjName} пользователем {a_Message.from_user.id}.') await state.finish() - await a_Message.answer(project_success_create_message, reply_markup = GetEditProjectKeyboardButtons(user_group)) + await a_Message.answer(project_success_create_message, reply_markup = GetEditProjectKeyboardButtons(user_groups)) # Редактирование проекта async def ProjectSelectForEdit(a_Message : types.message): user_id = str(a_Message.from_user.id) - user_group = groups.GetUserGroupData(user_id) - await bot.send_message(a_Message.from_user.id, project_select_to_edit_message, reply_markup = GetProjectsListKeyboardButtons(user_group, select_to_edit_project_callback_prefix)) + user_groups = groups.GetUserGroupData(user_id) + await bot.send_message(a_Message.from_user.id, project_select_to_edit_message, reply_markup = GetProjectsListKeyboardButtons(user_groups, select_to_edit_project_callback_prefix)) async def ProjectEditCancel(a_Message : types.message, state : FSMContext): user_id = str(a_Message.from_user.id) - user_group = groups.GetUserGroupData(user_id) + user_groups = groups.GetUserGroupData(user_id) await state.finish() - await a_Message.answer(project_cancel_edit_message, reply_markup = GetEditProjectKeyboardButtons(user_group)) + await a_Message.answer(project_cancel_edit_message, reply_markup = GetEditProjectKeyboardButtons(user_groups)) async def ProjectEdit(a_CallbackQuery : types.CallbackQuery, state : FSMContext): user_id = str(a_CallbackQuery.from_user.id) - user_group = groups.GetUserGroupData(user_id) + user_groups = groups.GetUserGroupData(user_id) await FSMEditProject.prjID.set() prjID = str(a_CallbackQuery.data).replace(select_to_edit_project_callback_prefix, '') async with state.proxy() as prjData: prjData['prjID'] = prjID await FSMEditProject.next() - await bot.send_message(a_CallbackQuery.from_user.id, project_edit_message_1, reply_markup = GetSkipAndCancelKeyboardButtons(user_group)) + await bot.send_message(a_CallbackQuery.from_user.id, project_edit_message_1, reply_markup = GetSkipAndCancelKeyboardButtons(user_groups)) async def PhotoEditLoad(a_Message : types.message, state : FSMContext, a_FileID): user_id = str(a_Message.from_user.id) - user_group = groups.GetUserGroupData(user_id) + user_groups = groups.GetUserGroupData(user_id) project_id = 0 async with state.proxy() as prjData: prjData['photo'] = a_FileID @@ -292,9 +292,9 @@ async def PhotoEditLoad(a_Message : types.message, state : FSMContext, a_FileID) await FSMEditProject.next() msg, photo_id, name, desc = GetProjectData(project_id) if msg != '': - await bot.send_message(a_Message.from_user.id, msg, reply_markup = GetEditProjectKeyboardButtons(user_group)) + await bot.send_message(a_Message.from_user.id, msg, reply_markup = GetEditProjectKeyboardButtons(user_groups)) return - await a_Message.answer(project_edit_message_2.replace('@proj_name', name), reply_markup = GetSkipAndCancelKeyboardButtons(user_group)) + await a_Message.answer(project_edit_message_2.replace('@proj_name', name), reply_markup = GetSkipAndCancelKeyboardButtons(user_groups)) async def ProjectEditPhotoLoad(a_Message : types.message, state : FSMContext): await PhotoEditLoad(a_Message, state, a_Message.photo[0].file_id) @@ -305,24 +305,24 @@ async def ProjectEditPhotoSkip(a_Message : types.message, state : FSMContext): project_id = prjData['prjID'] msg, photo_id, name, desc = GetProjectData(project_id) if msg != '': - await bot.send_message(a_Message.from_user.id, msg, reply_markup = GetEditProjectKeyboardButtons(user_group)) + await bot.send_message(a_Message.from_user.id, msg, reply_markup = GetEditProjectKeyboardButtons(user_groups)) return await PhotoEditLoad(a_Message, state, photo_id) async def EditNameSkip(a_Message : types.message, state : FSMContext, a_Name): user_id = str(a_Message.from_user.id) - user_group = groups.GetUserGroupData(user_id) + user_groups = groups.GetUserGroupData(user_id) async with state.proxy() as prjData: prjData['name'] = a_Name await FSMEditProject.next() - await a_Message.answer(project_edit_message_3, reply_markup = GetSkipAndCancelKeyboardButtons(user_group)) + await a_Message.answer(project_edit_message_3, reply_markup = GetSkipAndCancelKeyboardButtons(user_groups)) async def ProjectEditNameLoad(a_Message : types.message, state : FSMContext): await EditNameSkip(a_Message, state, a_Message.text) async def ProjectEditDescLoad(a_Message : types.message, state : FSMContext): user_id = str(a_Message.from_user.id) - user_group = groups.GetUserGroupData(user_id) + user_groups = groups.GetUserGroupData(user_id) async with state.proxy() as prjData: prjData['desc'] = a_Message.text project_id = prjData['prjID'] @@ -332,22 +332,22 @@ async def ProjectEditDescLoad(a_Message : types.message, state : FSMContext): EditProject(project_id, prjPhoto, prjName, prjDesc) log.Success(f'Изменён проект {prjName} пользователем {a_Message.from_user.id}.') await state.finish() - await a_Message.answer(project_success_edit_message, reply_markup = GetEditProjectKeyboardButtons(user_group)) + await a_Message.answer(project_success_edit_message, reply_markup = GetEditProjectKeyboardButtons(user_groups)) # Удаление проекта async def ProjectDelete(a_Message : types.message): user_id = str(a_Message.from_user.id) - user_group = groups.GetUserGroupData(user_id) - await bot.send_message(a_Message.from_user.id, project_select_to_delete_message, reply_markup = GetProjectsListKeyboardButtons(user_group, delete_project_callback_prefix)) + user_groups = groups.GetUserGroupData(user_id) + await bot.send_message(a_Message.from_user.id, project_select_to_delete_message, reply_markup = GetProjectsListKeyboardButtons(user_groups, delete_project_callback_prefix)) async def prjDelete(a_CallbackQuery : types.CallbackQuery): user_id = str(a_CallbackQuery.from_user.id) - user_group = groups.GetUserGroupData(user_id) + user_groups = groups.GetUserGroupData(user_id) projectID = str(a_CallbackQuery.data).replace(delete_project_callback_prefix, '') DelProject(projectID) log.Success(f'Проект №{projectID} был удалён пользователем {a_CallbackQuery.from_user.id}.') - await bot.send_message(a_CallbackQuery.from_user.id, project_success_delete_message, reply_markup = GetEditProjectKeyboardButtons(user_group)) + await bot.send_message(a_CallbackQuery.from_user.id, project_success_delete_message, reply_markup = GetEditProjectKeyboardButtons(user_groups)) # --------------------------------------------------------- # Работа с базой данных проектов diff --git a/bot_modules/start.py b/bot_modules/start.py index e7fc911..7bc2865 100644 --- a/bot_modules/start.py +++ b/bot_modules/start.py @@ -40,11 +40,14 @@ def GetStartKeyboardButtons(a_UserGroups): # Первичное привестивие async def StartMenu(a_Message): user_id = int(a_Message.from_user.id) - user_group = groups.GetUserGroupData(user_id) + user_groups = groups.GetUserGroupData(user_id) + if not user_access.CheckAccessString(GetAccess(), user_groups, user_access.AccessMode.VIEW): + return await bot.send_message(user_id, access.access_denied_message, reply_markup = GetStartKeyboardButtons(user_groups)) + user_name = str(a_Message.from_user.username) profile.AddUser(user_id, user_name) log.Info(f'Пользователь {user_id} {user_name} авторизовался в боте') - await a_Message.answer(start_message, reply_markup=GetStartKeyboardButtons(user_group), parse_mode='HTML') + await a_Message.answer(start_message, reply_markup=GetStartKeyboardButtons(user_groups), parse_mode='HTML') # --------------------------------------------------------- # API diff --git a/bot_sys/keyboard.py b/bot_sys/keyboard.py index b900aa4..b7a7a9e 100644 --- a/bot_sys/keyboard.py +++ b/bot_sys/keyboard.py @@ -21,13 +21,19 @@ def GetButtons(a_ModList): buttons += b return buttons +import math + +def Chunks(a_List, a_ChunkSize): + for i in range(0, len(a_List), a_ChunkSize): + yield a_List[i: i + a_ChunkSize] + def MakeKeyboard(a_ButtonList : [ButtonWithAccess], a_UserGroups): - key = types.ReplyKeyboardMarkup(resize_keyboard = True) + buttons = [] for b in a_ButtonList: if user_access.CheckAccessString(b.access_string, a_UserGroups, b.access_mode): - k = types.KeyboardButton(b.label) - key.add(k) - + buttons += [types.KeyboardButton(b.label)] + step = max(int(math.sqrt(len(buttons)) // 1), 1) + key = types.ReplyKeyboardMarkup(keyboard=Chunks(buttons, step), resize_keyboard = True) return key def MakeKeyboardRemove(): @@ -42,9 +48,11 @@ class Button: self.label = a_Label self.callback_data = a_CallBackData -def MakeInlineKeyboard(a_ButtonList, a_CallBackPrefix): # class Button - inline_keyboard = InlineKeyboardMarkup() +def MakeInlineKeyboard(a_ButtonList : [Button], a_CallBackPrefix : str): + buttons = [] for b in a_ButtonList: - inline_keyboard.insert(types.InlineKeyboardButton(text = b.label, callback_data = f'{a_CallBackPrefix}{b.callback_data}')) + buttons += [types.InlineKeyboardButton(text = b.label, callback_data = f'{a_CallBackPrefix}{b.callback_data}')] + step = max(int(math.sqrt(len(buttons)) // 1), 1) + inline_keyboard = InlineKeyboardMarkup(inline_keyboard=Chunks(buttons, step)) return inline_keyboard diff --git a/bot_sys/user_access.py b/bot_sys/user_access.py index 63a4bdd..78caa3e 100644 --- a/bot_sys/user_access.py +++ b/bot_sys/user_access.py @@ -36,7 +36,7 @@ class AccessMode(Enum): class UserGroups: def __init__(self, a_UserID : str, a_GroupNamesList : [str]): - self.user_id = a_UserID + self.user_id = str(a_UserID) self.group_names_list = a_GroupNamesList # --------------------------------------------------------- @@ -65,6 +65,9 @@ def CheckAccessString(a_AccessValue : str, a_UserGroups : UserGroups, a_AccessMo return False def Test(): + assert '1234' in ['123', '1234'] + assert not '1234' in ['123', '12345'] + 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)