Added ability to delete account of a user

This commit is contained in:
StNicolay 2022-10-14 20:01:05 +03:00
parent b1017082a9
commit 267e54bb29
3 changed files with 31 additions and 3 deletions

View File

@ -31,4 +31,8 @@ def create_bot(token: str, engine: mariadb.Connection) -> telebot.TeleBot:
functools.partial(handlers.reset_master_pass, bot, engine), functools.partial(handlers.reset_master_pass, bot, engine),
commands=["reset_master_pass"], commands=["reset_master_pass"],
) )
bot.register_message_handler(
functools.partial(handlers.delete_account, bot, engine),
commands=["delete_account"],
)
return bot return bot

View File

@ -99,13 +99,14 @@ def get_account(
if len(data) != 3: if len(data) != 3:
return _send_tmp_message(bot, mes.chat.id, "Неправильное количество аргументов") return _send_tmp_message(bot, mes.chat.id, "Неправильное количество аргументов")
if data[1] not in database.get.get_accounts(engine, mes.from_user.id):
return _send_tmp_message(bot, mes.chat.id, "Нет такого аккаунта")
master_pass = database.get.get_master_pass(engine, mes.from_user.id) master_pass = database.get.get_master_pass(engine, mes.from_user.id)
if master_pass is None: if master_pass is None:
return _send_tmp_message(bot, mes.chat.id, "Нет мастер пароля") return _send_tmp_message(bot, mes.chat.id, "Нет мастер пароля")
master_salt, hash_pass = master_pass master_salt, hash_pass = master_pass
if data[1] not in database.get.get_accounts(engine, mes.from_user.id):
return _send_tmp_message(bot, mes.chat.id, "Нет такого аккаунта")
if ( if (
cryptography.master_pass.encrypt_master_pass_known_salt(data[2], master_salt) cryptography.master_pass.encrypt_master_pass_known_salt(data[2], master_salt)
!= hash_pass != hash_pass
@ -150,3 +151,18 @@ def reset_master_pass(
_send_tmp_message( _send_tmp_message(
bot, mes.chat.id, "Все ваши аккаунты удалены, а мастер пароль изменён" bot, mes.chat.id, "Все ваши аккаунты удалены, а мастер пароль изменён"
) )
def delete_account(
bot: telebot.TeleBot, engine: Engine, mes: telebot.types.Message
) -> None:
data = shlex.split(mes.text)
bot.delete_message(mes.chat.id, mes.id)
if len(data) != 2:
return _send_tmp_message(bot, mes.chat.id, "Неправильное количество аргументов")
if data[1] not in database.get.get_accounts(engine, mes.from_user.id):
return _send_tmp_message(bot, mes.chat.id, "Нет такого аккаунта")
database.delete.delete_account(engine, mes.from_user.id, data[1])

View File

@ -16,3 +16,11 @@ def delete_master_pass(engine: Engine, user_id: int) -> None:
) )
with sqlmodel.Session(engine) as session: with sqlmodel.Session(engine) as session:
session.exec(statement) session.exec(statement)
def delete_account(engine: Engine, user_id: int, name: str) -> None:
statement = sqlmodel.delete(models.Account).where(
models.Account.user_id == user_id and models.Account.name == name
)
with sqlmodel.Session(engine) as session:
session.exec(statement)