diff --git a/sql_app/crud.py b/sql_app/crud.py index 2144d12..ffdac1d 100644 --- a/sql_app/crud.py +++ b/sql_app/crud.py @@ -163,3 +163,9 @@ def increment_door_access_list_counter(db: Session, iot_entity: models.IotEntity db.add(iot_entity) db.commit() db.refresh(iot_entity) + +def record_user_connection(db: Session, user: models.User, time: datetime): + entry = models.UserConnectionHistory(user_id=user.id, timestamp=time) + db.add(entry) + db.commit() + db.refresh(entry) diff --git a/sql_app/main.py b/sql_app/main.py index 7be744d..2ebd041 100644 --- a/sql_app/main.py +++ b/sql_app/main.py @@ -77,11 +77,13 @@ def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)): data={"sub": db_user.username}, expires_delta=timedelta(minutes=15) ) crud.set_user_last_token(db, db_user.username, access_token) - + crud.record_user_connection(db, db_user, datetime.now()) return db_user @app.get("/users/me/", response_model=schemas.User, tags=['Users']) -def get_user_details(current_user: schemas.User = Depends(get_current_active_user)): +def get_user_details(db: Session = Depends(get_db), + current_user: schemas.User = Depends(get_current_active_user)): + crud.record_user_connection(db, current_user, datetime.now()) return current_user @app.post("/users/open", tags=['Users']) @@ -102,6 +104,8 @@ def issue_open_door_command(command: schemas.OpenDoorRequestTime, command="OPEN", timestamp=datetime.now()) crud.record_door_access_log(db, log_entry) + crud.record_user_connection(db, current_user, datetime.now()) + return device raise err @@ -122,6 +126,8 @@ def issue_close_door_command(command: schemas.CloseDoorRequest, command="CLOSE", timestamp=datetime.now()) crud.record_door_access_log(db, log_entry) + crud.record_user_connection(db, current_user, datetime.now()) + return device @app.post("/users/tkn", response_model=schemas.Token, tags=['Users']) @@ -139,6 +145,7 @@ def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(), db: data={"sub": form_data.username}, expires_delta=timedelta(minutes=15) ) crud.set_user_last_token(db, form_data.username, access_token) + crud.record_user_connection(db, user, datetime.now()) return {"access_token": access_token, "token_type": "bearer"} @app.get("/users/acesslist/", response_model=List[schemas.RoomOverview], tags=['Users']) @@ -162,6 +169,7 @@ def get_iot_access_list_for_user(db: Session = Depends(get_db), current_user: sc smoke_sensor_reading=sensors.smoke_sensor_reading ) access_list.append(entry) + crud.record_user_connection(db, user, datetime.now()) return access_list @app.get("/admin/users/", response_model=List[schemas.User], tags=['Admin']) diff --git a/sql_app/models.py b/sql_app/models.py index 2690cf7..f68662c 100644 --- a/sql_app/models.py +++ b/sql_app/models.py @@ -14,7 +14,7 @@ class User(Base): passwd_salt = Column(String, nullable=False) is_active = Column(Boolean, default=True, nullable=False) last_token = Column(String, nullable=True) - + connections = relationship("UserConnectionHistory") authorized_devices = relationship("IotEntity", secondary="user_iot_link", back_populates="authorized_users") @@ -57,4 +57,10 @@ class RoomSensorData(Base): smoke_sensor_reading = Column(Integer) timestamp = Column(DateTime) +class UserConnectionHistory(Base): + __tablename__ = "user_connection_history" + + reading_id = Column(Integer, primary_key=True) + user_id = Column(Integer,ForeignKey("user_accounts.id"), index=True) + timestamp = Column(DateTime) # TODO: Add table to store active sessions. May periodically clear. \ No newline at end of file