69 lines
2.3 KiB
Python
69 lines
2.3 KiB
Python
# DRF Rack
|
|
# Библиотека классов расширений для Django REST Framework
|
|
#
|
|
# Автор: Николай В. Анохин <n.anokhin@xeaf.net>
|
|
# Все права защищены. Лицензия: MIT
|
|
|
|
"""
|
|
Описание класса AccountManager
|
|
"""
|
|
|
|
from net.xeaf.rack.enums import AuthRejectReason
|
|
from net.xeaf.rack.models.account_model import AccountModel
|
|
|
|
|
|
class AccountManager:
|
|
"""
|
|
Реализует методы работы с Аккаунтами
|
|
"""
|
|
|
|
@classmethod
|
|
def check_account_auth(cls, account: AccountModel | None) -> AuthRejectReason | None:
|
|
"""
|
|
Проверяет возможность авторизации Аккаунта
|
|
|
|
:param account: Аккаунт
|
|
:return: Причина отказа в авторизации или None, если авторизация возможна
|
|
"""
|
|
|
|
if account is None:
|
|
return AuthRejectReason.INVALID_USERNAME_OR_PASSWORD
|
|
if not account.is_active:
|
|
return AuthRejectReason.ACCOUNT_IS_NOT_ACTIVE
|
|
if account.joined_at is None:
|
|
return AuthRejectReason.REGISTRATION_IS_NOT_COMPLETE
|
|
|
|
return None
|
|
|
|
@classmethod
|
|
def check_username_auth(cls, username: str) -> AuthRejectReason | None:
|
|
"""
|
|
Проверяет возможность авторизации Аккаунта с заданным именем
|
|
|
|
:param username: Имя Аккаунта
|
|
:return: Причина отказа в авторизации или None, если авторизация возможна
|
|
"""
|
|
|
|
account = cls.find_by_username(username)
|
|
return cls.check_account_auth(account)
|
|
|
|
@classmethod
|
|
def find_by_username(cls, username: str) -> AccountModel | None:
|
|
"""
|
|
Возвращает Аккаунт с указанным именем
|
|
|
|
:param username: Имя Аккаунта
|
|
:return: Аккаунт или None, если Аккаунт не найден
|
|
"""
|
|
|
|
return AccountModel.objects.filter(username=username).first()
|
|
|
|
@classmethod
|
|
def restore_account(cls, account: AccountModel) -> None:
|
|
"""
|
|
Восстанавливает удаленный Аккаунт
|
|
"""
|
|
|
|
if account.is_deleted():
|
|
account.undelete(save=True)
|