Browse Source

Del access_utils

pull/2/head
Alexei 2 years ago
parent
commit
0a0171ff39
  1. 23
      bot_modules/access.py
  2. 41
      bot_modules/access_utils.py
  3. 13
      bot_modules/groups.py
  4. 14
      bot_modules/mod_simple_message.py
  5. 8
      bot_modules/profile.py
  6. 4
      template/simple_message.py

23
bot_modules/access.py

@ -106,7 +106,7 @@ moduleaccess_success_edit_message = '''✅ Проект успешно отре
# ---------------------------------------------------------
# Работа с кнопками
'''
def GetEditAccessKeyboardButtons(a_Message, a_UserGroups):
cur_buttons = [
keyboard.ButtonWithAccess(sql_request_button_name, user_access.AccessMode.ACCEES_EDIT, GetAccess()),
@ -204,3 +204,24 @@ def RegisterHandlers(dp : Dispatcher):
)
RegisterEdit(edit_moduleaccess_access_button_name, FSMEditAccessItem, moduleaccess_edit_access_message, moduleaccess_field, bd_item.FieldType.text, user_access.AccessMode.ACCEES_EDIT)
RegisterEdit(edit_moduleaccess_default_access_button_name, FSMEditDefaultAccessItem, moduleaccess_edit_default_access_message, mod_default_access_field, bd_item.FieldType.text, user_access.AccessMode.EDIT)
'''
def GetAccessForModuleRequest(module_name, access, default_access):
return f"INSERT OR IGNORE INTO {table_name} ({mod_name_field}, {moduleaccess_field}, {mod_default_access_field}) VALUES ('{module_name}', '{access}', '{default_access}');"
def GetModulesAccessList(a_Bot):
return bot_bd.RequestSelectTemplate(a_Bot.m_BDFileName, table_name)()
def GetAccessForModule(a_Bot, a_ModuleName):
alist = GetModulesAccessList(a_Bot)
for i in alist:
if i[0] == a_ModuleName:
return i[1]
return ''
def GetItemDefaultAccessForModule(a_Bot, a_ModuleName):
alist = GetModulesAccessList(a_Bot)
for i in alist:
if i[0] == a_ModuleName:
return i[2]
return ''

41
bot_modules/access_utils.py

@ -1,41 +0,0 @@
# -*- coding: utf8 -*-
# Общественное достояние, 2023, Алексей Безбородов (Alexei Bezborodov) <AlexeiBv+mirocod_platform_bot@narod.ru>
# Права пользователей. Утилиты
from bot_sys import user_access, bot_bd
table_name = 'module_access'
mod_name_field = 'modName'
moduleaccess_field = 'modAccess'
mod_default_access_field = 'itemDefaultAccess'
def GetAccessForModuleRequest(module_name, access, default_access):
return f"INSERT OR IGNORE INTO {table_name} ({mod_name_field}, {moduleaccess_field}, {mod_default_access_field}) VALUES ('{module_name}', '{access}', '{default_access}');"
def GetUserGroupData(a_Bot, a_UserID):
def GetGroupNamesForUser(a_UserID):
return a_Bot.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(a_Bot):
return bot_bd.RequestSelectTemplate(a_Bot.m_BDFileName, table_name)()
def GetAccessForModule(a_Bot, a_ModuleName):
alist = GetModulesAccessList(a_Bot)
for i in alist:
if i[0] == a_ModuleName:
return i[1]
return ''
def GetItemDefaultAccessForModule(a_Bot, a_ModuleName):
alist = GetModulesAccessList(a_Bot)
for i in alist:
if i[0] == a_ModuleName:
return i[2]
return ''

13
bot_modules/groups.py

@ -79,7 +79,7 @@ help_button_name = "📄 Информация по группам"
# ---------------------------------------------------------
# Работа с кнопками
'''
def GetEditGroupKeyboardButtons(a_Message, a_UserGroups):
cur_buttons = [
keyboard.ButtonWithAccess(sql_request_button_name, user_access.AccessMode.EDIT, GetAccess()),
@ -128,3 +128,14 @@ def RegisterHandlers(dp : Dispatcher):
dp.register_message_handler(simple_message.InfoMessageTemplateLegacy(help_message, GetEditGroupKeyboardButtons, GetAccess), text = help_button_name)
sql_request.RequestToBDRegisterHandlers(dp, sql_request_button_name, request_start_message, FSMRequestToBD, GetEditGroupKeyboardButtons, user_access.AccessMode.EDIT, GetAccess)
'''
def GetUserGroupData(a_Bot, a_UserID):
def GetGroupNamesForUser(a_UserID):
return a_Bot.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)

14
bot_modules/mod_simple_message.py

@ -4,7 +4,7 @@
# Простой модуль с одним сообщением
from bot_sys import keyboard, user_access, keyboard
from bot_modules import access_utils, mod_interface
from bot_modules import access, mod_interface
from template import simple_message, bd_item
class SimpleMessageModule(mod_interface.IModule):
@ -17,8 +17,8 @@ class SimpleMessageModule(mod_interface.IModule):
self.m_BotButtons = a_BotButtons
self.m_Log = a_Log
self.m_StartButtonName = self.CreateButton(f'{self.GetName()}_start', a_StartButtonName)
self.m_StartMessage = self.CreateMessage(f'{self.GetName()}_start', a_StartMessage)
self.m_StartButtonName = self.CreateButton('start', a_StartButtonName)
self.m_StartMessage = self.CreateMessage('start', a_StartMessage)
async def StartMessageHandler(a_Message, state = None):
return await self.StartMessageHandler(a_Message, state)
@ -45,11 +45,11 @@ class SimpleMessageModule(mod_interface.IModule):
return simple_message.WorkFuncResult(self.m_StartMessage)
def CreateMessage(self, a_MessageName, a_MessageDesc):
msg = self.m_BotMessages.CreateMessage(a_MessageName, a_MessageDesc, self.m_Log.GetTimeNow())
msg = self.m_BotMessages.CreateMessage(f'{self.GetName()}_{a_MessageName}', a_MessageDesc, self.m_Log.GetTimeNow())
return msg
def CreateButton(self, a_ButtonName, a_ButtonDesc):
btn = self.m_BotButtons.CreateMessage(a_ButtonName, a_ButtonDesc, self.m_Log.GetTimeNow())
btn = self.m_BotButtons.CreateMessage(f'{self.GetName()}_{a_ButtonName}', a_ButtonDesc, self.m_Log.GetTimeNow())
return btn
def GetStartKeyboardButtons(self, a_Message, a_UserGroups):
@ -67,11 +67,11 @@ class SimpleMessageModule(mod_interface.IModule):
def GetInitBDCommands(self):
return [
access_utils.GetAccessForModuleRequest(self.GetName(), self.m_InitAccess, self.m_InitAccess),
access.GetAccessForModuleRequest(self.GetName(), self.m_InitAccess, self.m_InitAccess),
]
def GetAccess(self):
return access_utils.GetAccessForModule(self.m_Bot, self.GetName())
return access.GetAccessForModule(self.m_Bot, self.GetName())
def GetModuleButtons(self):
return [

8
bot_modules/profile.py

@ -4,7 +4,7 @@
# Профиль пользователя
from bot_sys import user_access, bot_bd
from bot_modules import mod_simple_message, groups, access_utils
from bot_modules import mod_simple_message, groups, access
from template import bd_item, simple_message
# ---------------------------------------------------------
@ -82,16 +82,16 @@ class ModuleProfile(mod_simple_message.SimpleMessageModule):
# Добавление пользователя, если он уже есть, то игнорируем
def AddUser(a_Bot, a_UserID, a_UserName, a_UserName1, a_UserName2, a_UserIsBot, a_LanguageCode):
a_Bot.SQLRequest(f"INSERT OR IGNORE INTO users ({key_name}, {name_field}, {name1_field}, {name2_field}, {is_bot_field}, {language_code_field}, {access_field}, {create_datetime_field}) VALUES (?, ?, ?, ?, ?, ?, ?, {bot_bd.GetBDDateTimeNow()});",
commit=True, param = (a_UserID, a_UserName, a_UserName1, a_UserName2, a_UserIsBot, a_LanguageCode, access_utils.GetItemDefaultAccessForModule(a_Bot, module_name)))
commit=True, param = (a_UserID, a_UserName, a_UserName1, a_UserName2, a_UserIsBot, a_LanguageCode, access.GetItemDefaultAccessForModule(a_Bot, module_name)))
'''
user_groups = access_utils.GetUserGroupData(a_Bot, a_UserID)
user_groups = access.GetUserGroupData(a_Bot, a_UserID)
# Если пользователь не состоит ни в одной группе, то добавляем его в группу user_access.user_access_group_new
if len(user_groups.group_names_list) == 0:
new_group_id = a_Bot.SQLRequest(f'SELECT {groups.key_table_groups_name} FROM {groups.table_groups_name} WHERE {groups.name_table_groups_field} = ?',
param = [user_access.user_access_group_new])
if new_group_id and new_group_id[0]:
a_Bot.SQLRequest(f"INSERT OR IGNORE INTO {groups.table_user_in_groups_name} ({groups.user_id_field}, {groups.key_table_groups_name}, {groups.access_field}, {groups.create_datetime_field}) VALUES (?, ?, ?, {bot_bd.GetBDDateTimeNow()});",
commit=True, param = (a_UserID, new_group_id[0][0], access_utils.GetItemDefaultAccessForModule(a_Bot, module_name)))
commit=True, param = (a_UserID, new_group_id[0][0], access.GetItemDefaultAccessForModule(a_Bot, module_name)))
'''
def GetUserInfo(a_Bot, a_UserID):
user_info = a_Bot.SQLRequest('SELECT * FROM users WHERE user_id = ?', param = [a_UserID])

4
template/simple_message.py

@ -4,7 +4,7 @@
# Простые информационные сообщения
from bot_sys import user_access
from bot_modules import access, access_utils
from bot_modules import access, groups
from aiogram import types
class WorkFuncResult():
@ -39,7 +39,7 @@ def SimpleMessageTemplate(a_Bot, a_WorkFunc, a_GetButtonsFunc, a_GetInlineButton
async def SimpleMessage(a_Message : types.message, state = None):
user_id = str(a_Message.from_user.id)
lang = str(a_Message.from_user.language_code)
user_groups = access_utils.GetUserGroupData(a_Bot, user_id)
user_groups = groups.GetUserGroupData(a_Bot, 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)

Loading…
Cancel
Save