From 36123b10901c0a0fae010674deec6a264b4d866b Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Mon, 23 May 2022 19:44:04 +0300 Subject: [PATCH] sql_app: Access log for user (access history) Signed-off-by: HeshamTB --- sql_app/crud.py | 2 ++ sql_app/main.py | 15 ++++++++++++++- sql_app/schemas.py | 9 +++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/sql_app/crud.py b/sql_app/crud.py index ed0f498..f8e3b24 100644 --- a/sql_app/crud.py +++ b/sql_app/crud.py @@ -36,6 +36,8 @@ def get_users(db: Session, skip: int = 0, limit: int = 100): def get_access_log_for_door_by_door_mac(db: Session, bluetooth_mac : str): return db.query(models.DoorAccessLog).filter(models.DoorAccessLog.iot_dev_bluetooth_mac == bluetooth_mac).all() +def get_access_log_for_user_by_id(db: Session, id : str): + return db.query(models.DoorAccessLog).filter(models.DoorAccessLog.user_id == id).all() def create_user(db: Session, user: schemas.UserCreate): key = crypto.gen_new_key(user.password) diff --git a/sql_app/main.py b/sql_app/main.py index 7956bbd..3934ec1 100644 --- a/sql_app/main.py +++ b/sql_app/main.py @@ -16,7 +16,6 @@ oauth = OAuth2PasswordBearer(tokenUrl="tkn") app = FastAPI() -# Dependency def get_db(): db = SessionLocal() try: @@ -171,6 +170,20 @@ def get_access_log_for_door(request : schemas.AccessLogRequest, if not device: raise HTTPException(status_code=404, detail="Iot Entity not found") return crud.get_access_log_for_door_by_door_mac(db, request.bluetooth_mac) +@app.post("/admin/user/accesslog/email/", tags=['Admin']) +def get_access_log_history_for_user(request : schemas.UserAccessLogRequestEmail, + db : Session = Depends(get_db)): + user = crud.get_user_by_email(db, request.email) + if not user: raise HTTPException(status_code=404, detail="User not found") + return crud.get_access_log_for_user_by_id(db, user.id) + +@app.post("/admin/user/accesslog/username/", tags=['Admin']) +def get_access_log_history_for_user(request : schemas.UserAccessLogRequestUsername, + db : Session = Depends(get_db)): + user = crud.get_user_by_username(db, request.username) + if not user: raise HTTPException(status_code=404, detail="User not found") + return crud.get_access_log_for_user_by_id(db, user.id) + @app.get("/users/acesslist/", response_model=List[schemas.IotEntity], tags=['Users']) def get_iot_access_list_for_user(db: Session = Depends(get_db), current_user: schemas.User = Depends(get_current_active_user)): user = crud.get_user_by_username(db, current_user.username) diff --git a/sql_app/schemas.py b/sql_app/schemas.py index 5871a51..79aa70b 100644 --- a/sql_app/schemas.py +++ b/sql_app/schemas.py @@ -100,3 +100,12 @@ class DoorAccessLog(BaseModel): class AccessLogRequest(BaseModel): bluetooth_mac : str + +class UserAccessLogRequestUsername(BaseModel): + username : str + +class UserAccessLogRequestEmail(BaseModel): + email : str + +class UserAccessLogRequestID(BaseModel): + id : int