Skip to content

Commit

Permalink
Responding to gabe's review
Browse files Browse the repository at this point in the history
  • Loading branch information
micahdbak committed Jun 17, 2024
1 parent 6499fda commit d6dcda0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/auth/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async def check_authentication(

if session_id:
await crud.task_clean_expired_user_sessions(db_session)
response_dict = await crud.check_user_validity(db_session, session_id)
response_dict = await crud.check_user_session(db_session, session_id)
else:
response_dict = {"is_valid": False}

Expand Down
14 changes: 13 additions & 1 deletion src/auth/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from auth import models

import logging
import sqlalchemy
from sqlalchemy.ext.asyncio import AsyncSession

Expand All @@ -20,7 +21,16 @@ async def create_user_session(db_session: AsyncSession, session_id: str, computi
query = sqlalchemy.select(models.User).where(models.User.computing_id == computing_id)
existing_user = (await db_session.scalars(query)).first()
if existing_user is None:
# @geb; throw?
# log this strange case
_logger = logging.getLogger(__name__)
_logger.warning("User session {} exists for non-existent user {}!".format(session_id, computing_id))
# create a user for this session
new_user = models.User(
computing_id=computing_id,
first_logged_in=datetime.now(),
last_logged_in=datetime.now()
)
db_session.add(new_user)
pass
else:
# update the last time the user logged in to now
Expand Down Expand Up @@ -56,6 +66,8 @@ async def check_user_session(db_session: AsyncSession, session_id: str) -> dict:
existing_user_session = (await db_session.scalars(query)).first()

if existing_user_session:
# TODO: replace this select with an sqlalchemy relationship access
# see: https://docs.sqlalchemy.org/en/20/orm/basic_relationships.html
query = sqlalchemy.select(models.User).where(models.User.computing_id == existing_user_session.computing_id)
existing_user = (await db_session.scalars(query)).first()
return {
Expand Down
3 changes: 1 addition & 2 deletions src/auth/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from datetime import datetime
from sqlalchemy import Column, DateTime, ForeignKey, Integer, String
from sqlalchemy.orm import mapped_column
from sqlalchemy import Column, DateTime, Integer, String
# from sqlalchemy.orm import relationship

from database import Base
Expand Down

0 comments on commit d6dcda0

Please sign in to comment.