init auth
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
from datetime import datetime, timedelta
|
||||
from enum import Enum
|
||||
from typing import Optional
|
||||
|
||||
from aiogram.types import User
|
||||
from motor.motor_asyncio import AsyncIOMotorClient
|
||||
from utils.security import get_password_hash
|
||||
|
||||
|
||||
class UserStatus:
|
||||
@@ -19,10 +21,49 @@ class UsersRepo:
|
||||
async def get_user(self, user_id: int):
|
||||
return await self.collection.find_one({"user_id": user_id})
|
||||
|
||||
async def get_user_by_username(self, username: str):
|
||||
return await self.collection.find_one({"username": username})
|
||||
|
||||
async def create_user(self, username: str, password: str, full_name: Optional[str] = None):
|
||||
"""Создает нового пользователя с username/паролем"""
|
||||
existing = await self.get_user_by_username(username)
|
||||
if existing:
|
||||
raise ValueError("User with this username already exists")
|
||||
|
||||
user_doc = {
|
||||
"username": username,
|
||||
"hashed_password": get_password_hash(password),
|
||||
"full_name": full_name,
|
||||
"status": UserStatus.PENDING, # По умолчанию PENDING
|
||||
"created_at": datetime.now(),
|
||||
"is_email_user": False, # Теперь это просто "обычный" юзер, не телеграм (хотя поле можно переименовать)
|
||||
"is_web_user": True,
|
||||
"is_admin": False
|
||||
}
|
||||
result = await self.collection.insert_one(user_doc)
|
||||
return await self.collection.find_one({"_id": result.inserted_id})
|
||||
|
||||
async def get_pending_users(self):
|
||||
"""Возвращает список пользователей со статусом PENDING"""
|
||||
cursor = self.collection.find({"status": UserStatus.PENDING})
|
||||
return await cursor.to_list(length=100)
|
||||
|
||||
async def approve_user(self, username: str):
|
||||
await self.collection.update_one(
|
||||
{"username": username},
|
||||
{"$set": {"status": UserStatus.ALLOWED}}
|
||||
)
|
||||
|
||||
async def deny_user(self, username: str):
|
||||
await self.collection.update_one(
|
||||
{"username": username},
|
||||
{"$set": {"status": UserStatus.DENIED}}
|
||||
)
|
||||
|
||||
async def create_or_update_request(self, user: User):
|
||||
"""
|
||||
Обновляет дату последнего запроса и ставит статус PENDING.
|
||||
Сохраняет всю инфу о юзере.
|
||||
Сохраняет всю инфу о юзере (для Telegram пользователей).
|
||||
"""
|
||||
now = datetime.now()
|
||||
data = {
|
||||
@@ -30,7 +71,8 @@ class UsersRepo:
|
||||
"username": user.username,
|
||||
"full_name": user.full_name,
|
||||
"status": UserStatus.PENDING,
|
||||
"last_request_date": now
|
||||
"last_request_date": now,
|
||||
"is_email_user": False
|
||||
}
|
||||
await self.collection.update_one(
|
||||
{"user_id": user.id},
|
||||
|
||||
Reference in New Issue
Block a user