Files
François Voron b7dbdf6ea6 Fix #245 : handle email as case insensitive while retrieving user in DB (#250)
* Add unit tests to enforce email case insensitivity

* Handle email as case insentitive while retrieving user in DB

* Apply isort/black

* Add migration doc
2020-07-09 18:44:25 +02:00

55 lines
1.8 KiB
Python

from typing import Optional, Type
from motor.motor_asyncio import AsyncIOMotorCollection
from pydantic import UUID4
from fastapi_users.db.base import BaseUserDatabase
from fastapi_users.models import UD
class MongoDBUserDatabase(BaseUserDatabase[UD]):
"""
Database adapter for MongoDB.
:param user_db_model: Pydantic model of a DB representation of a user.
:param collection: Collection instance from `motor`.
"""
collection: AsyncIOMotorCollection
def __init__(self, user_db_model: Type[UD], collection: AsyncIOMotorCollection):
super().__init__(user_db_model)
self.collection = collection
self.collection.create_index("id", unique=True)
self.collection.create_index("email", unique=True)
async def get(self, id: UUID4) -> Optional[UD]:
user = await self.collection.find_one({"id": id})
return self.user_db_model(**user) if user else None
async def get_by_email(self, email: str) -> Optional[UD]:
user = await self.collection.find_one(
{"email": {"$regex": email, "$options": "i"}}
)
return self.user_db_model(**user) if user else None
async def get_by_oauth_account(self, oauth: str, account_id: str) -> Optional[UD]:
user = await self.collection.find_one(
{
"oauth_accounts.oauth_name": oauth,
"oauth_accounts.account_id": account_id,
}
)
return self.user_db_model(**user) if user else None
async def create(self, user: UD) -> UD:
await self.collection.insert_one(user.dict())
return user
async def update(self, user: UD) -> UD:
await self.collection.replace_one({"id": user.id}, user.dict())
return user
async def delete(self, user: UD) -> None:
await self.collection.delete_one({"id": user.id})