Browse Source

Модуль доступа

test_bot
Alexei 2 years ago
parent
commit
0593c69c54
  1. 54
      bot_modules/access.py
  2. 41
      bot_modules/groups.py
  3. 4
      bot_modules/profile.py
  4. 4
      bot_modules/projects.py
  5. 15
      bot_modules/start.py
  6. 49
      bot_sys/user_access.py
  7. 4
      main.py

54
bot_modules/access.py

@ -1,10 +1,10 @@
# -*- coding: utf8 -*-
# Общественное достояние 2023, Алексей Безбородов (Alexei Bezborodov) <AlexeiBv+mirocod_platform_bot@narod.ru>
# Профиль пользователя
# Права пользователей
from bot_sys import bot_bd, log, config, keyboard
from bot_modules import start
from bot_sys import bot_bd, log, config, keyboard, user_access
from bot_modules import start, groups
from aiogram import Bot, types
import sqlite3
@ -15,17 +15,57 @@ bot = Bot(token=config.GetTelegramBotApiToken(), parse_mode=types.ParseMode.HTML
# ---------------------------------------------------------
# БД
init_bd_cmds = []
init_bd_cmds = ["""CREATE TABLE IF NOT EXISTS module_access(
modName TEXT,
modAccess TEXT,
UNIQUE(modName)
);""",
"INSERT OR IGNORE INTO module_access (modName, modAccess) VALUES ('access', 'other=-');"
]
# ---------------------------------------------------------
# Сообщения
access_start_message = '''
<b> Права пользователей находятся в стадии разработки</b>
Пока можете воспользоваться хардкорным способом через запросы к БД
'''
request_start_message = '''
**Задайте запрос к БД**
Можете воспользоваться следующими шаблонами:
1. `SELECT * FROM users` - Все пользователи
'''
help_message = '''
Существует БД для работы с правами
`module_access (modName, modAccess)` - содержит права для модулей
modAccess - строка
''' + user_access.user_access_readme
access_button_name = "📰 Доступ пользователей"
sql_request_button_name = "📰 Запрос к БД для редактирования доступа"
help_button_name = "📰 Информация по редактированию доступа"
# ---------------------------------------------------------
# Работа с кнопками
def GetEditAccessKeyboardButtons(a_UserAccess):
cur_buttons = [sql_request_button_name, help_button_name]
mods = [start]
return keyboard.MakeKeyboard(keyboard.GetButtons(mods, a_UserAccess) + cur_buttons)
# ---------------------------------------------------------
# Обработка сообщений
# Приветствие
async def AccessStart(a_Message):
user_id = str(a_Message.from_user.id)
user_access = GetUserAccess(a_Message.from_user.id)
await bot.send_message(user_id, access_start_message, reply_markup = GetEditAccessKeyboardButtons(user_access))
# ---------------------------------------------------------
# Работа с базой данных
@ -42,8 +82,10 @@ def GetInitBDCommands():
# Доступные кнопки
def GetButtonNames(a_UserAccess):
return []
return [access_button_name]
# Обработка кнопок
def RegisterHandlers(dp : Dispatcher):
return
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)

41
bot_modules/groups.py

@ -30,6 +30,7 @@ init_bd_cmds = ["""CREATE TABLE IF NOT EXISTS user_groups(
group_id INTEGER,
UNIQUE(user_id, group_id)
);""",
"INSERT OR IGNORE INTO module_access (modName, modAccess) VALUES ('groups', 'other=-');"
]
# ---------------------------------------------------------
@ -53,19 +54,26 @@ request_start_message = '''
6. `INSERT INTO user_in_groups(user_id, group_id) VALUES(USERID, GROUPID)` - добавление пользователя USERID в группу с GROUPID
'''
help_message = '''
Существует две БД для работы с группами
`user_groups (group_id, groupName)` - содержит названия групп
`user_in_groups(user_id, group_id)` - содержит соответсвия ID пользователей и групп
'''
request_cancel_message = '''
Запрос к БД отменён
'''
user_group_button_name = "📰 Группы пользователей"
sql_request_button_name = "📰 Запрос к БД"
sql_request_button_name = "📰 Запрос к БД для редактирования групп"
help_button_name = "📰 Информация по группам"
canсel_button_name = "📰 Отменить"
# ---------------------------------------------------------
# Работа с кнопками
def GetEditGroupKeyboardButtons(a_UserAccess):
cur_buttons = [sql_request_button_name]
cur_buttons = [sql_request_button_name, help_button_name]
mods = [start]
return keyboard.MakeKeyboard(keyboard.GetButtons(mods, a_UserAccess) + cur_buttons)
@ -86,10 +94,18 @@ async def RequestToBDCancel(a_Message : types.message, state : FSMContext):
await state.finish()
await a_Message.answer(request_cancel_message, reply_markup = GetEditGroupKeyboardButtons(user_access))
async def RequestToBDStart(a_Message : types.message):
user_access = access.GetUserAccess(a_Message.from_user.id)
await FSMRequestToBD.sqlRequest.set()
await a_Message.answer(request_start_message, reply_markup = GetCancelKeyboardButtons(user_access), parse_mode='Markdown')
def HelpTemplate(a_HelpMessage, a_GetButtonsFunc):
async def Help(a_Message : types.message):
user_access = access.GetUserAccess(a_Message.from_user.id)
await a_Message.answer(a_HelpMessage, reply_markup = a_GetButtonsFunc(user_access)) #, parse_mode='Markdown')
return Help
def RequestToBDTemplate(a_StartMessage):
async def RequestToBDStart(a_Message : types.message):
user_access = access.GetUserAccess(a_Message.from_user.id)
await FSMRequestToBD.sqlRequest.set()
await a_Message.answer(a_StartMessage, reply_markup = GetCancelKeyboardButtons(user_access), parse_mode='Markdown')
return RequestToBDStart
async def RequestToBD(a_Message : types.message, state : FSMContext):
user_access = access.GetUserAccess(a_Message.from_user.id)
@ -108,9 +124,13 @@ async def RequestToBD(a_Message : types.message, state : FSMContext):
def SQLRequestToBD(a_Request : str):
db = sqlite3.connect(bot_bd.GetBDFileName())
cursor = db.cursor()
cursor.execute(a_Request)
result = cursor.fetchall()
db.commit()
result = []
try:
cursor.execute(a_Request)
result = cursor.fetchall()
db.commit()
except sqlite3.Error as e:
result = "Ошибка sqlite3:" + str(e)
cursor.close()
db.close()
return result
@ -188,6 +208,7 @@ def GetButtonNames(a_UserAccess):
# Обработка кнопок
def RegisterHandlers(dp : Dispatcher):
dp.register_message_handler(GroupStart, text = user_group_button_name)
dp.register_message_handler(RequestToBDStart, text = sql_request_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)

4
bot_modules/profile.py

@ -19,7 +19,9 @@ init_bd_cmds = ["""CREATE TABLE IF NOT EXISTS users(
user_id INTEGER,
userName TEXT,
UNIQUE(user_id)
);"""]
);""",
"INSERT OR IGNORE INTO module_access (modName, modAccess) VALUES ('profile', 'other=+');"
]
# ---------------------------------------------------------
# Сообщения

4
bot_modules/projects.py

@ -31,7 +31,9 @@ init_bd_cmds = ['''CREATE TABLE IF NOT EXISTS projects(
projectName TEXT,
projectDesc TEXT,
projectID INTEGER PRIMARY KEY
)''']
)''',
"INSERT OR IGNORE INTO module_access (modName, modAccess) VALUES ('project', 'other=va');"
]
# ---------------------------------------------------------
# Сообщения

15
bot_modules/start.py

@ -4,11 +4,17 @@
# Стартовое меню
from bot_sys import log, config, keyboard
from bot_modules import profile, projects, groups
from bot_modules import profile, projects, groups, access
from aiogram.dispatcher import Dispatcher
# ---------------------------------------------------------
# БД
init_bd_cmds = [
"INSERT OR IGNORE INTO module_access (modName, modAccess) VALUES ('start', 'other=+');"
]
# ---------------------------------------------------------
# Сообщения
start_message = '''
@ -23,7 +29,7 @@ start_menu_button_name = "☰ Главное меню"
# Работа с кнопками
def GetStartKeyboardButtons(a_UserAccess):
mods = [profile, projects, groups]
mods = [profile, projects, groups, access]
return keyboard.MakeKeyboardForMods(mods, a_UserAccess)
# ---------------------------------------------------------
@ -32,17 +38,18 @@ def GetStartKeyboardButtons(a_UserAccess):
# Первичное привестивие
async def StartMenu(a_Message):
user_id = int(a_Message.from_user.id)
user_access = access.GetUserAccess(user_id)
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(None), parse_mode='HTML')
await a_Message.answer(start_message, reply_markup=GetStartKeyboardButtons(user_access), parse_mode='HTML')
# ---------------------------------------------------------
# API
# Инициализация БД
def GetInitBDCommands():
return None
return init_bd_cmds
# Имена доступных кнопок
def GetButtonNames(a_UserAccess):

49
bot_sys/user_access.py

@ -8,9 +8,7 @@ from bot_sys import config
user_access_readme = '''
Доступ к пользователям задаётся в виде строки
```
user1=daver;user2=av;Group1=v;Group2=-;Group3=+
```
`user1=daver;user2=av;Group1=v;Group2=-;Group3=+;other=-`
Где через ';' располагаются различные варианты доступа
user1 и user2 - id пользоватлей
Group1, Group2, Group3 - Имена групп пользоватлей
@ -23,6 +21,7 @@ DELETE = 'd' - удаление
ACCEES_EDIT = 'r' - изменение прав доступа
'+' - всё включено
'-' - всё выключено
группа 'other' - остальные
'''
# ---------------------------------------------------------
@ -60,7 +59,7 @@ def CheckAccessString(a_AccessValue : str, a_UserAccessData : UserAccess, a_Acce
continue
name = d[0]
access = d[1]
if name == a_UserAccessData.user_id or name in a_UserAccessData.group_names_list:
if name == a_UserAccessData.user_id or name in a_UserAccessData.group_names_list or name == 'other':
if CheckAccessItem(access, a_AccessMode):
return True
return False
@ -120,3 +119,45 @@ def Test():
assert not CheckAccessString('123=-;gr1=ea', UserAccess('1234', ['gr']), AccessMode.ACCEES_EDIT)
assert not CheckAccessString('123=-;gr1=ad', UserAccess('1234', ['gr']), AccessMode.VIEW)
assert CheckAccessString('123=-;other=a', UserAccess('1234', []), AccessMode.ADD)
assert CheckAccessString('123=-;other=d', UserAccess('1234', []), AccessMode.DELETE)
assert CheckAccessString('123=-;other=e', UserAccess('1234', []), AccessMode.EDIT)
assert CheckAccessString('123=-;other=r', UserAccess('1234', []), AccessMode.ACCEES_EDIT)
assert CheckAccessString('123=-;other=v', UserAccess('1234', []), AccessMode.VIEW)
assert CheckAccessString('123=-;other=a', UserAccess('1234', ['gr1']), AccessMode.ADD)
assert CheckAccessString('123=-;other=d', UserAccess('1234', ['gr1']), AccessMode.DELETE)
assert CheckAccessString('123=-;other=e', UserAccess('1234', ['gr1']), AccessMode.EDIT)
assert CheckAccessString('123=-;other=r', UserAccess('1234', ['gr1']), AccessMode.ACCEES_EDIT)
assert CheckAccessString('123=-;other=v', UserAccess('1234', ['gr1']), AccessMode.VIEW)
assert CheckAccessString('123=-;other=daver', UserAccess('1234', []), AccessMode.ADD)
assert CheckAccessString('123=-;other=dav', UserAccess('1234', []), AccessMode.DELETE)
assert CheckAccessString('123=-;other=edv', UserAccess('1234', []), AccessMode.EDIT)
assert CheckAccessString('123=-;other=rav', UserAccess('1234', []), AccessMode.ACCEES_EDIT)
assert CheckAccessString('123=-;other=va', UserAccess('1234', []), AccessMode.VIEW)
assert CheckAccessString('123=-;other=avr', UserAccess('1234', ['gr1']), AccessMode.ADD)
assert CheckAccessString('123=-;other=daver', UserAccess('1234', ['gr1']), AccessMode.DELETE)
assert CheckAccessString('123=-;other=eva', UserAccess('1234', ['gr1']), AccessMode.EDIT)
assert CheckAccessString('123=-;other=re', UserAccess('1234', ['gr1']), AccessMode.ACCEES_EDIT)
assert CheckAccessString('123=-;other=vad', UserAccess('1234', ['gr1']), AccessMode.VIEW)
assert not CheckAccessString('123=-;other=d', UserAccess('1234', []), AccessMode.ADD)
assert not CheckAccessString('123=-;other=a', UserAccess('1234', []), AccessMode.DELETE)
assert not CheckAccessString('123=-;other=r', UserAccess('1234', []), AccessMode.EDIT)
assert not CheckAccessString('123=-;other=v', UserAccess('1234', []), AccessMode.ACCEES_EDIT)
assert not CheckAccessString('123=-;other=e', UserAccess('1234', []), AccessMode.VIEW)
assert not CheckAccessString('123=-;gr1=d;other=-', UserAccess('1234', ['gr']), AccessMode.ADD)
assert not CheckAccessString('123=-;gr1=a;other=-', UserAccess('1234', ['gr']), AccessMode.DELETE)
assert not CheckAccessString('123=-;gr1=v;other=-', UserAccess('1234', ['gr']), AccessMode.EDIT)
assert not CheckAccessString('123=-;gr1=e;other=-', UserAccess('1234', ['gr']), AccessMode.ACCEES_EDIT)
assert not CheckAccessString('123=-;gr1=r;other=-', UserAccess('1234', ['gr']), AccessMode.VIEW)
assert not CheckAccessString('123=-;other=dver', UserAccess('1234', []), AccessMode.ADD)
assert not CheckAccessString('123=-;other=av', UserAccess('1234', []), AccessMode.DELETE)
assert not CheckAccessString('123=-;other=dv', UserAccess('1234', []), AccessMode.EDIT)
assert not CheckAccessString('123=-;other=av', UserAccess('1234', []), AccessMode.ACCEES_EDIT)
assert not CheckAccessString('123=-;other=a', UserAccess('1234', []), AccessMode.VIEW)
assert not CheckAccessString('123=-;other=vr', UserAccess('1234', ['gr']), AccessMode.ADD)
assert not CheckAccessString('123=-;other=aver', UserAccess('1234', ['gr']), AccessMode.DELETE)
assert not CheckAccessString('123=-;other=va', UserAccess('1234', ['gr']), AccessMode.EDIT)
assert not CheckAccessString('123=-;other=ea', UserAccess('1234', ['gr']), AccessMode.ACCEES_EDIT)
assert not CheckAccessString('123=-;other=ad', UserAccess('1234', ['gr']), AccessMode.VIEW)

4
main.py

@ -10,13 +10,13 @@ from aiogram.dispatcher import Dispatcher
from aiogram.contrib.fsm_storage.memory import MemoryStorage
import sqlite3
from bot_sys import config, log, bot_bd, user_access
from bot_modules import profile, start, projects, groups
from bot_modules import profile, start, projects, groups, access
storage = MemoryStorage()
bot = Bot(token=config.GetTelegramBotApiToken(), parse_mode = types.ParseMode.HTML)
dp = Dispatcher(bot, storage = storage)
mods = [profile, start, projects, groups]
mods = [access, profile, start, projects, groups]
init_bd_cmd = []
for m in mods:

Loading…
Cancel
Save