-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Disable account #344
base: main
Are you sure you want to change the base?
Disable account #344
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from sqlalchemy import update | ||
from sqlalchemy.exc import IntegrityError | ||
from sqlalchemy.ext.asyncio import AsyncSession | ||
|
||
from app.models import models_core | ||
from app.utils.types.groups_type import GroupType | ||
|
||
|
||
async def disable_external_accounts(db: AsyncSession): | ||
try: | ||
await db.execute( | ||
update(models_core.CoreUser) | ||
.where( | ||
models_core.CoreUser.groups.any( | ||
models_core.CoreGroup.id == GroupType.external.value | ||
) | ||
) | ||
.values(enabled=False) | ||
) | ||
await db.commit() | ||
|
||
except IntegrityError as error: | ||
await db.rollback() | ||
raise ValueError(error) | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,6 +81,11 @@ | |
detail="Incorrect login or password", | ||
headers={"WWW-Authenticate": "Bearer"}, | ||
) | ||
if not user.enabled: | ||
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail="Disabled account. Contact eclair@myecl.fr for more informations.", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to hard code an email in the API response? |
||
) | ||
# We put the user id in the subject field of the token. | ||
# The subject `sub` is a JWT registered claim name, see https://datatracker.ietf.org/doc/html/rfc7519#section-4.1 | ||
data = schemas_auth.TokenData(sub=user.id, scopes=ScopeType.API) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from fastapi import APIRouter, Depends | ||
from sqlalchemy.ext.asyncio import AsyncSession | ||
|
||
from app.cruds import cruds_external_account | ||
from app.dependencies import get_db, is_user_a_member_of | ||
from app.models import models_core | ||
from app.utils.types.groups_type import GroupType | ||
from app.utils.types.tags import Tags | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.get( | ||
"/external/", | ||
status_code=200, | ||
tags=[Tags.external_account], | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we could put the endpoint in |
||
async def disable_external_users( | ||
db: AsyncSession = Depends(get_db), | ||
user: models_core.CoreUser = Depends(is_user_a_member_of(GroupType.admin)), | ||
): | ||
return await cruds_external_account.disable_external_accounts(db=db) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
from datetime import date, datetime | ||
|
||
from sqlalchemy import Date, DateTime, Enum, ForeignKey, Integer, String | ||
from sqlalchemy import Boolean, Date, DateTime, Enum, ForeignKey, Integer, String | ||
from sqlalchemy.orm import Mapped, mapped_column, relationship | ||
|
||
from app.database import Base | ||
|
@@ -35,6 +35,7 @@ class CoreUser(Base): | |
phone: Mapped[str | None] = mapped_column(String) | ||
floor: Mapped[FloorsType] = mapped_column(Enum(FloorsType), nullable=False) | ||
created_on: Mapped[datetime | None] = mapped_column(DateTime(timezone=True)) | ||
enabled: Mapped[bool] = mapped_column(Boolean, nullable=False) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think of |
||
|
||
# We use list["CoreGroup"] with quotes as CoreGroup is only defined after this class | ||
# Defining CoreUser after CoreGroup would cause a similar issue | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import pytest_asyncio | ||
|
||
from app.models import models_core | ||
from app.utils.types.groups_type import GroupType | ||
|
||
# We need to import event_loop for pytest-asyncio routine defined bellow | ||
from tests.commons import event_loop # noqa | ||
from tests.commons import client, create_api_access_token, create_user_with_groups | ||
|
||
user: models_core.CoreUser | ||
|
||
token_admin: str = "" | ||
|
||
|
||
@pytest_asyncio.fixture(scope="module", autouse=True) | ||
async def init_objects(): | ||
global user | ||
user = await create_user_with_groups([GroupType.external]) | ||
|
||
global user_admin | ||
user_admin = await create_user_with_groups([GroupType.admin]) | ||
|
||
global token_admin | ||
token_admin = create_api_access_token(user_admin) | ||
|
||
|
||
def test_disable_external_account(): | ||
global user | ||
response = client.get( | ||
"/external/", | ||
follow_redirects=False, | ||
headers={"Authorization": f"Bearer {token_admin}"}, | ||
) | ||
response1 = client.get( | ||
f"/users/{user.id}", | ||
headers={"Authorization": f"Bearer {token_admin}"}, | ||
) | ||
assert response.status_code == 200 | ||
assert response1.status_code == 200 | ||
data = response1.json() | ||
assert not data["enabled"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be changed?