Skip to content

Commit a7cc0db

Browse files
authored
logginable deleting bug fix (#198)
1 parent ec039c6 commit a7cc0db

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

auth_backend/auth_method/oauth.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from fastapi_sqlalchemy import db
66
from sqlalchemy.orm import Session as DbSession
77

8+
from auth_backend.auth_method import AUTH_METHODS, LoginableMixin
89
from auth_backend.base import Base
910
from auth_backend.exceptions import LastAuthMethodDelete
1011
from auth_backend.models.db import AuthMethod, User, UserSession
@@ -78,11 +79,22 @@ async def _delete_auth_methods(cls, user: User, *, db_session) -> list[AuthMetho
7879
)
7980
.all()
8081
)
81-
all_auth_methods = AuthMethod.query(session=db_session).filter(AuthMethod.user_id == user.id).all()
82-
if len(all_auth_methods) - len(auth_methods) == 0:
83-
raise LastAuthMethodDelete()
82+
if issubclass(cls, LoginableMixin):
83+
loginable_auth_methods_count: int = (
84+
AuthMethod.query(session=db_session)
85+
.filter(
86+
AuthMethod.user_id == user.id,
87+
AuthMethod.auth_method.in_(
88+
[method.get_name() for method in AUTH_METHODS.values() if issubclass(method, LoginableMixin)]
89+
),
90+
)
91+
.count()
92+
)
93+
if len(auth_methods) == loginable_auth_methods_count:
94+
raise LastAuthMethodDelete
8495
logger.debug(auth_methods)
8596
for method in auth_methods:
8697
method.is_deleted = True
8798
db_session.flush()
99+
db_session.commit()
88100
return {m.param: m.value for m in auth_methods}

0 commit comments

Comments
 (0)