-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
M2-4180 Track user login operations added.
- Loading branch information
1 parent
8313194
commit cf8427b
Showing
10 changed files
with
200 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from fastapi import Body, Depends | ||
|
||
from apps.authentication.domain.login import UserLoginRequest | ||
from apps.authentication.errors import InvalidCredentials | ||
from apps.authentication.services.security import AuthenticationService | ||
from apps.users.domain import User | ||
from apps.users.errors import UserNotFound | ||
from infrastructure.database import atomic | ||
from infrastructure.database.deps import get_session | ||
|
||
|
||
async def auth_user( | ||
user_login_schema: UserLoginRequest = Body(...), | ||
session=Depends(get_session), | ||
) -> User: | ||
async with atomic(session): | ||
try: | ||
user: User = await AuthenticationService( | ||
session | ||
).authenticate_user(user_login_schema) | ||
|
||
except UserNotFound: | ||
raise InvalidCredentials(email=user_login_schema.email) | ||
|
||
return user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from apps.logs.db.schemas import UserActivityLogSchema | ||
from infrastructure.database.crud import BaseCRUD | ||
|
||
|
||
class UserActivityLogCRUD(BaseCRUD[UserActivityLogSchema]): | ||
schema_class = UserActivityLogSchema | ||
|
||
async def save( | ||
self, schema: UserActivityLogSchema | ||
) -> UserActivityLogSchema: | ||
"""Return UserActivityLogSchema instance.""" | ||
return await self._create(schema) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from enum import Enum | ||
|
||
|
||
class UserActivityEventType(str, Enum): | ||
LOGIN = "login" | ||
|
||
|
||
class UserActivityEvent(str, Enum): | ||
LOGIN = "login" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from apps.logs.crud.user_activity_log import UserActivityLogCRUD | ||
from apps.logs.db.schemas import UserActivityLogSchema | ||
|
||
|
||
class UserActivityLogService: | ||
def __init__(self, session): | ||
self.session = session | ||
|
||
async def create_log( | ||
self, schema: UserActivityLogSchema | ||
) -> UserActivityLogSchema: | ||
return await UserActivityLogCRUD(self.session).save(schema) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from fastapi import Body, Depends | ||
from starlette.requests import Request | ||
|
||
from apps.authentication.api.auth_utils import auth_user | ||
from apps.authentication.domain.login import UserLoginRequest | ||
from apps.logs.db.schemas import UserActivityLogSchema | ||
from apps.logs.domain.constants import UserActivityEvent, UserActivityEventType | ||
from apps.logs.services.user_activity_log import UserActivityLogService | ||
from infrastructure.database.deps import get_session | ||
from infrastructure.http.deps import get_mindlogger_content_source | ||
from infrastructure.http.domain import MindloggerContentSource | ||
|
||
|
||
async def user_activity_login_log( | ||
request: Request, | ||
user_login_schema: UserLoginRequest = Body(...), | ||
session=Depends(get_session), | ||
user=Depends(auth_user), | ||
mindlogger_content=Depends(get_mindlogger_content_source), | ||
) -> None: | ||
if ( | ||
mindlogger_content == MindloggerContentSource.undefined.name | ||
and user_login_schema.device_id | ||
): | ||
mindlogger_content = MindloggerContentSource.mobile.name | ||
|
||
schema = UserActivityLogSchema( | ||
user_id=user.id, | ||
device_id=user_login_schema.device_id, | ||
event_type=UserActivityEventType.LOGIN, | ||
event=UserActivityEvent.LOGIN, | ||
user_agent=request.headers.get("user-agent"), | ||
mindlogger_content=mindlogger_content, | ||
) | ||
await UserActivityLogService(session).create_log(schema) |
73 changes: 73 additions & 0 deletions
73
src/infrastructure/database/migrations/versions/2023_11_30_10_41-add_user_activity_logs.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
"""add-user-activity-logs | ||
Revision ID: bc2fcc0b0cd1 | ||
Revises: 75c9ca1f506b | ||
Create Date: 2023-11-30 10:41:15.308713 | ||
""" | ||
import sqlalchemy as sa | ||
from alembic import op | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "bc2fcc0b0cd1" | ||
down_revision = "75c9ca1f506b" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table( | ||
"user_activity_logs", | ||
sa.Column( | ||
"id", | ||
postgresql.UUID(as_uuid=True), | ||
server_default=sa.text("gen_random_uuid()"), | ||
nullable=False, | ||
), | ||
sa.Column( | ||
"created_at", | ||
sa.DateTime(), | ||
server_default=sa.text("timezone('utc', now())"), | ||
nullable=True, | ||
), | ||
sa.Column( | ||
"updated_at", | ||
sa.DateTime(), | ||
server_default=sa.text("timezone('utc', now())"), | ||
nullable=True, | ||
), | ||
sa.Column("migrated_date", sa.DateTime(), nullable=True), | ||
sa.Column("migrated_updated", sa.DateTime(), nullable=True), | ||
sa.Column("is_deleted", sa.Boolean(), nullable=True), | ||
sa.Column("user_id", postgresql.UUID(as_uuid=True), nullable=False), | ||
sa.Column("device_id", sa.String(), nullable=True), | ||
sa.Column("event_type", sa.String(), nullable=False), | ||
sa.Column("event", sa.String(), nullable=False), | ||
sa.Column("user_agent", sa.String(), nullable=True), | ||
sa.Column("mindlogger_content", sa.String(), nullable=False), | ||
sa.ForeignKeyConstraint( | ||
["user_id"], | ||
["users.id"], | ||
name=op.f("fk_user_activity_logs_user_id_users"), | ||
ondelete="RESTRICT", | ||
), | ||
sa.PrimaryKeyConstraint("id", name=op.f("pk_user_activity_logs")), | ||
) | ||
op.create_index( | ||
op.f("ix_user_activity_logs_user_id"), | ||
"user_activity_logs", | ||
["user_id"], | ||
unique=False, | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_index( | ||
op.f("ix_user_activity_logs_user_id"), table_name="user_activity_logs" | ||
) | ||
op.drop_table("user_activity_logs") | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,5 @@ class MindloggerContentSource(str, Enum): | |
|
||
web = "web" | ||
admin = "admin" | ||
mobile = "mobile" | ||
undefined = "undefined" |