Skip to content

Commit

Permalink
[DOP-19835] Fix GET /v1/groups skipping some groups
Browse files Browse the repository at this point in the history
  • Loading branch information
dolfinus committed Sep 19, 2024
1 parent 59795ae commit 1e8b6f5
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 23 deletions.
5 changes: 0 additions & 5 deletions syncmaster/backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from starlette.middleware.cors import CORSMiddleware

from syncmaster.backend.api.deps import (
AuthMarker,
DatabaseEngineMarker,
DatabaseSessionMarker,
SettingsMarker,
Expand All @@ -15,7 +14,6 @@
http_exception_handler,
syncmsater_exception_handler,
)
from syncmaster.backend.services import get_auth_scheme
from syncmaster.config import Settings
from syncmaster.db.factory import create_engine, create_session_factory, get_uow
from syncmaster.exceptions import SyncmasterError
Expand All @@ -41,15 +39,12 @@ def application_factory(settings: Settings) -> FastAPI:
engine = create_engine(connection_uri=settings.build_db_connection_uri())
session_factory = create_session_factory(engine=engine)

auth_scheme = get_auth_scheme(settings)

application.dependency_overrides.update(
{
SettingsMarker: lambda: settings,
DatabaseEngineMarker: lambda: engine,
DatabaseSessionMarker: lambda: session_factory,
UnitOfWorkMarker: get_uow(session_factory, settings),
AuthMarker: auth_scheme,
},
)

Expand Down
4 changes: 0 additions & 4 deletions syncmaster/backend/api/deps.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# SPDX-FileCopyrightText: 2023-2024 MTS PJSC
# SPDX-License-Identifier: Apache-2.0
class AuthMarker:
pass


class SettingsMarker:
pass

Expand Down
2 changes: 1 addition & 1 deletion syncmaster/backend/services/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2023-2024 MTS PJSC
# SPDX-License-Identifier: Apache-2.0
from syncmaster.backend.services.auth import get_auth_scheme, get_user
from syncmaster.backend.services.auth import get_user
from syncmaster.backend.services.unit_of_work import UnitOfWork
16 changes: 5 additions & 11 deletions syncmaster/backend/services/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@
# SPDX-License-Identifier: Apache-2.0
from collections.abc import Awaitable, Callable

from fastapi import Depends, Request, status
from fastapi import Depends, status
from fastapi.exceptions import HTTPException
from fastapi.security import OAuth2PasswordBearer

from syncmaster.backend.api.deps import AuthMarker, SettingsMarker, UnitOfWorkMarker
from syncmaster.backend.api.deps import SettingsMarker, UnitOfWorkMarker
from syncmaster.backend.api.v1.auth.utils import decode_jwt
from syncmaster.backend.services.unit_of_work import UnitOfWork
from syncmaster.config import Settings
from syncmaster.db.models import User

oauth_schema = OAuth2PasswordBearer(tokenUrl="v1/auth/token")


def get_user(
is_active: bool = False,
is_superuser: bool = False,
) -> Callable[[str, Settings, UnitOfWork], Awaitable[User]]:
async def wrapper(
token: str = Depends(AuthMarker),
token: str = Depends(oauth_schema),
settings: Settings = Depends(SettingsMarker),
unit_of_work: UnitOfWork = Depends(UnitOfWorkMarker),
) -> User:
Expand Down Expand Up @@ -48,11 +50,3 @@ async def wrapper(
return user

return wrapper


def get_auth_scheme(
settings: Settings,
) -> Callable[[Request], str] | OAuth2PasswordBearer:
if settings.DEBUG:
return OAuth2PasswordBearer(tokenUrl=settings.AUTH_TOKEN_URL)
return lambda request: "Here will be keycloak"
1 change: 0 additions & 1 deletion syncmaster/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class Settings(BaseSettings):

SECRET_KEY: str = "secret"
SECURITY_ALGORITHM: str = "HS256"
AUTH_TOKEN_URL: str = "v1/auth/token"
CRYPTO_KEY: str = "UBgPTioFrtH2unlC4XFDiGf5sYfzbdSf_VgiUSaQc94="

POSTGRES_HOST: str
Expand Down
2 changes: 1 addition & 1 deletion syncmaster/db/repositories/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ async def paginate_for_user(
):
stmt = (
select(Group)
.distinct()
.join(
UserGroup,
UserGroup.group_id == Group.id,
Expand All @@ -53,7 +54,6 @@ async def paginate_for_user(
Group.owner_id == current_user_id,
),
)
.group_by(Group.id)
)

return await self._paginate_scalar_result(query=stmt.order_by(Group.name), page=page, page_size=page_size)
Expand Down

0 comments on commit 1e8b6f5

Please sign in to comment.