You've already forked RekomenciBackend
feat(): migrate to auth identity
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
from typing import override
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from template_project.adapters.data_gateways.tables import auth_identity_table
|
||||
from template_project.application.auth_identity.data_gateway import AuthIdentityDataGateway
|
||||
from template_project.application.auth_identity.entity import AuthIdentity, AuthMethod
|
||||
from template_project.application.user.entity import UserId
|
||||
|
||||
|
||||
class DefaultAuthIdentityDataGateway(AuthIdentityDataGateway):
|
||||
def __init__(self, session: AsyncSession) -> None:
|
||||
self._session = session
|
||||
|
||||
@override
|
||||
async def load_by_method_and_identifier(self, method: AuthMethod, identifier: str) -> AuthIdentity | None:
|
||||
statement = select(AuthIdentity).where(
|
||||
auth_identity_table.c.method == method, auth_identity_table.c.identifier == identifier
|
||||
)
|
||||
result = await self._session.execute(statement)
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
@override
|
||||
async def load_all_for_user(self, user_id: UserId) -> list[AuthIdentity]:
|
||||
statement = select(AuthIdentity).where(auth_identity_table.c.user_id == user_id)
|
||||
result = await self._session.execute(statement)
|
||||
return list(result.scalars().all())
|
||||
@@ -1,23 +1,30 @@
|
||||
__all__ = (
|
||||
"access_token_table",
|
||||
"meta_data",
|
||||
"user_table",
|
||||
from sqlalchemy import (
|
||||
Boolean,
|
||||
Column,
|
||||
DateTime,
|
||||
Enum,
|
||||
ForeignKey,
|
||||
MetaData,
|
||||
String,
|
||||
Table,
|
||||
UniqueConstraint,
|
||||
)
|
||||
|
||||
from sqlalchemy import UUID, Boolean, Column, DateTime, ForeignKey, MetaData, String, Table
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.orm import registry
|
||||
|
||||
from template_project.application.access_token.entity import AccessToken
|
||||
from template_project.application.auth_identity.entity import AuthIdentity, AuthMethod
|
||||
from template_project.application.user.entity import User
|
||||
|
||||
meta_data = MetaData()
|
||||
mapper_registry = registry()
|
||||
|
||||
user_table = Table(
|
||||
"users",
|
||||
meta_data,
|
||||
Column("id", UUID, primary_key=True),
|
||||
Column("email", String, unique=True, nullable=False),
|
||||
Column("hashed_password", String, nullable=False),
|
||||
Column("email", String, unique=True, nullable=True),
|
||||
Column("hashed_password", String, nullable=True),
|
||||
Column("deleted_at", DateTime(timezone=True)),
|
||||
Column("created_at", DateTime(timezone=True), nullable=False),
|
||||
)
|
||||
@@ -33,7 +40,18 @@ access_token_table = Table(
|
||||
Column("created_at", DateTime(timezone=True), nullable=False),
|
||||
)
|
||||
|
||||
mapper_registry = registry()
|
||||
auth_identity_table = Table(
|
||||
"auth_identities",
|
||||
meta_data,
|
||||
Column("id", UUID, primary_key=True),
|
||||
Column("user_id", UUID, ForeignKey("users.id", ondelete="CASCADE"), nullable=False),
|
||||
Column("method", Enum(AuthMethod, name="auth_method"), nullable=False),
|
||||
Column("identifier", String, nullable=False),
|
||||
Column("secret_key", String, nullable=True),
|
||||
Column("created_at", DateTime(timezone=True), nullable=False),
|
||||
UniqueConstraint("method", "identifier", name="uq_auth_method_identifier"),
|
||||
)
|
||||
|
||||
mapper_registry.map_imperatively(User, user_table)
|
||||
mapper_registry.map_imperatively(AccessToken, access_token_table)
|
||||
mapper_registry.map_imperatively(AuthIdentity, auth_identity_table)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from typing import cast, override
|
||||
from typing import override
|
||||
|
||||
from sqlalchemy import exists, select
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from template_project.adapters.data_gateways.tables import user_table
|
||||
@@ -17,12 +17,3 @@ class DefaultUserDataGateway(UserDataGateway):
|
||||
statement = select(User).where(user_table.c.id == id_)
|
||||
result = await self._session.execute(statement)
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
@override
|
||||
async def exists_by_email(self, email: str) -> bool:
|
||||
statement = select(exists(select(user_table).where(user_table.c.email == email)))
|
||||
result = await self._session.execute(statement)
|
||||
result_fetchone = result.fetchone()
|
||||
if result_fetchone is None:
|
||||
return False
|
||||
return cast(bool, result_fetchone[0])
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
import copy
|
||||
from typing import Any, override
|
||||
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from template_project.application.common.entity import Entity
|
||||
from template_project.application.common.unit_of_work import UnitOfWork
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user