Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions auth_backend/auth_method/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from fastapi_sqlalchemy import db
from sqlalchemy.orm import Session as DbSession

from auth_backend.auth_method import AUTH_METHODS, LoginableMixin
from auth_backend.base import Base
from auth_backend.exceptions import LastAuthMethodDelete
from auth_backend.models.db import AuthMethod, User, UserSession
Expand Down Expand Up @@ -78,11 +79,22 @@ async def _delete_auth_methods(cls, user: User, *, db_session) -> list[AuthMetho
)
.all()
)
all_auth_methods = AuthMethod.query(session=db_session).filter(AuthMethod.user_id == user.id).all()
if len(all_auth_methods) - len(auth_methods) == 0:
raise LastAuthMethodDelete()
if issubclass(cls, LoginableMixin):
loginable_auth_methods_count: int = (
AuthMethod.query(session=db_session)
.filter(
AuthMethod.user_id == user.id,
AuthMethod.auth_method.in_(
[method.get_name() for method in AUTH_METHODS.values() if issubclass(method, LoginableMixin)]
),
)
.count()
)
if len(auth_methods) == loginable_auth_methods_count:
raise LastAuthMethodDelete
logger.debug(auth_methods)
for method in auth_methods:
method.is_deleted = True
Comment thread
Zimovchik marked this conversation as resolved.
db_session.flush()
db_session.commit()
return {m.param: m.value for m in auth_methods}