From 2811b8a5ef90abbb48f2ebf19539c2ae82c39e7a Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Tue, 7 Jun 2022 21:29:08 +0300 Subject: [PATCH 01/10] sql_app: update TODO Signed-off-by: HeshamTB --- sql_app/TODO | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/sql_app/TODO b/sql_app/TODO index efbcbcd..7775caa 100644 --- a/sql_app/TODO +++ b/sql_app/TODO @@ -2,6 +2,7 @@ - [x] Issue requests from Lap to Pi or vica versa - [x] Decide HTTP vs MQTT vs CoAP - [x] Hash passwords +- [x] Salt passwords - [x] User registraion - [x] User login in API - [x] JWT token access @@ -12,8 +13,8 @@ - [X] Expose door lock function - [X] Record access log - [X] Expose data analysis -- [ ] Load backend onto RPi -- [ ] Test connections in lab network +- [X] Load backend onto RPi +- [X] Test connections in lab network - [ ] Define emrgancy triggers (manual and automatic) - [ ] Expose temporary control in case of emergancy - Triggers @@ -26,6 +27,9 @@ - [X] Issue door open command - [X] Make functions to gen a IotEntity token - [ ] Write a small program/script to generate new Iot token and add new Iot Device into database -- [ ] Make inital database entries automatic for easy reset +- [X] Make inital database entries automatic for easy reset +- [X] Expose access list endpoint for doors +- [X] Access list coutner for iot door +- [X] Force close in middle of timed open request eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJibHVldG9vdGhfbWFjIjoic3RyaW5nIn0.ELl5AfBR1NdM4_OFhl_SCTm9EMPpqjiCKOSS0CrOJps \ No newline at end of file From 84db98b6f6e74c81f829d2bedd9d781ecf899463 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Tue, 7 Jun 2022 21:40:01 +0300 Subject: [PATCH 02/10] sql_app: Remove hard coded HTTPException codes Signed-off-by: HeshamTB --- sql_app/TODO | 6 ++++++ sql_app/main.py | 39 +++++++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/sql_app/TODO b/sql_app/TODO index 7775caa..1f2d03e 100644 --- a/sql_app/TODO +++ b/sql_app/TODO @@ -31,5 +31,11 @@ - [X] Expose access list endpoint for doors - [X] Access list coutner for iot door - [X] Force close in middle of timed open request +- [ ] Record user connections and time +- [ ] Record Iot dev connection and time +- [ ] Write unit tests +- [ ] Develop a program to visualize the data +- [ ] CLI frontend + eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJibHVldG9vdGhfbWFjIjoic3RyaW5nIn0.ELl5AfBR1NdM4_OFhl_SCTm9EMPpqjiCKOSS0CrOJps \ No newline at end of file diff --git a/sql_app/main.py b/sql_app/main.py index 323e15a..de94b1d 100644 --- a/sql_app/main.py +++ b/sql_app/main.py @@ -155,7 +155,8 @@ def get_iot_access_list_for_user(db: Session = Depends(get_db), current_user: sc for device in user.authorized_devices: dev_db : models.IotEntity = device sensors = crud.get_room_data_now(db) - if not sensors: raise HTTPException(status_code=500, detail="No Room link") + if not sensors: raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, + detail="No Room link") entry : schemas.RoomOverview = schemas.RoomOverview( id=dev_db.id, description=dev_db.description, @@ -192,22 +193,26 @@ def create_iot_entities(iot_entity: schemas.IotEntityCreate, db: Session = Depen def read_user(user_id: int, db: Session = Depends(get_db)): db_user = crud.get_user(db, user_id=user_id) if db_user is None: - raise HTTPException(status_code=404, detail="User not found") + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, + detail="User not found") return db_user @app.post("/admin/users/allowdevice/id", tags=['Admin']) def allow_user_for_iot_entity_by_id(request: schemas.UserAllowForIotEntityRequestByID, db: Session = Depends(get_db)): user = crud.get_user(db, request.user_id) if not user: - raise HTTPException(status_code=404, detail="User not found") + raise HTTPException(status.HTTP_404_NOT_FOUND, + detail="User not found") iot_entity = crud.get_iot_entity(db, request.iot_entity_id) if not iot_entity: - raise HTTPException(status_code=404, detail="Iot Entity not found") + raise HTTPException(status.HTTP_404_NOT_FOUND, + detail="Iot Entity not found") res = crud.create_user_link_to_iot(db, request.user_id, request.iot_entity_id) if not res: - raise HTTPException(status_code=500, detail="Could not complete operation") + raise HTTPException(status.HTTP_500_INTERNAL_SERVER_ERROR, + detail="Could not complete operation") crud.increment_door_access_list_counter(db, iot_entity) return @@ -216,15 +221,18 @@ def allow_user_for_iot_entity_by_id(request: schemas.UserAllowForIotEntityReques def disallow_user_for_iot_entity_by_id(request: schemas.UserAllowForIotEntityRequestByID, db: Session = Depends(get_db)): user = crud.get_user(db, request.user_id) if not user: - raise HTTPException(status_code=404, detail="User not found") + raise HTTPException(status.HTTP_404_NOT_FOUND, + detail="User not found") iot_entity = crud.get_iot_entity(db, request.iot_entity_id) if not iot_entity: - raise HTTPException(status_code=404, detail="Iot Entity not found") + raise HTTPException(status.HTTP_404_NOT_FOUND, + detail="Iot Entity not found") res = crud.remove_user_link_to_iot(db, request.user_id, request.iot_entity_id) if not res: - raise HTTPException(status_code=500, detail="Could not complete operation") + raise HTTPException(status.HTTP_500_INTERNAL_SERVER_ERROR, + detail="Could not complete operation") crud.increment_door_access_list_counter(db, iot_entity) return @@ -233,15 +241,18 @@ def disallow_user_for_iot_entity_by_id(request: schemas.UserAllowForIotEntityReq def allow_user_for_iot_entity_by_name(request: schemas.UserAllowForIotEntityRequestByUsername, 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") + raise HTTPException(status.HTTP_404_NOT_FOUND, + detail="User not found") iot_entity = crud.get_iot_entity_by_description(db, request.description) if not iot_entity: - raise HTTPException(status_code=404, detail="Iot Entity not found") + raise HTTPException(status.HTTP_404_NOT_FOUND, + detail="Iot Entity not found") res = crud.create_user_link_to_iot(db, user.id, iot_entity.id) if not res: - raise HTTPException(status_code=500, detail="Could not complete operation") + raise HTTPException(status.HTTP_500_INTERNAL_SERVER_ERROR, + detail="Could not complete operation") return @@ -265,21 +276,21 @@ def generate_token_for_iot_device(bluetooth_mac : schemas.IotBluetoothMac, def get_access_log_for_door(request : schemas.AccessLogRequest, db : Session = Depends(get_db)): device = crud.get_iot_entity_by_bluetooth_mac(db, request.bluetooth_mac) - if not device: raise HTTPException(status_code=404, detail="Iot Entity not found") + if not device: raise HTTPException(status.HTTP_404_NOT_FOUND, 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") + if not user: raise HTTPException(status.HTTP_404_NOT_FOUND, 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") + if not user: raise HTTPException(status.HTTP_404_NOT_FOUND, detail="User not found") return crud.get_access_log_for_user_by_id(db, user.id) @app.get("/admin/roominfo/now/", tags=['Admin']) From c2048d8dba1a12d03d18d51ae74d8c4094e83caa Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Wed, 8 Jun 2022 12:00:12 +0300 Subject: [PATCH 03/10] sql_app: User can change password Signed-off-by: HeshamTB --- sql_app/crud.py | 10 ++++++++++ sql_app/main.py | 43 +++++++++++++++++++++++++------------------ sql_app/schemas.py | 5 +++-- 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/sql_app/crud.py b/sql_app/crud.py index 2c53795..fe704e3 100644 --- a/sql_app/crud.py +++ b/sql_app/crud.py @@ -56,6 +56,16 @@ def create_user(db: Session, user: schemas.UserCreate): db.refresh(db_user) return db_user +def update_user_password(db: Session, user: models.User, request: schemas.UserUpdatePassword): + key = crypto.gen_new_key(request.password) + salt = key[1] + hashed_pass = key[0] + user.passwd_salt = salt + user.hashed_password = hashed_pass + db.add(user) + db.commit() + db.refresh(user) + def get_iot_entities(db: Session, skip: int = 0, limit: int = 100): return db.query(models.IotEntity).offset(skip).limit(limit).all() diff --git a/sql_app/main.py b/sql_app/main.py index de94b1d..a183260 100644 --- a/sql_app/main.py +++ b/sql_app/main.py @@ -130,24 +130,6 @@ def issue_close_door_command(command: schemas.CloseDoorRequest, return device -@app.post("/users/tkn", response_model=schemas.Token, tags=['Users']) -@app.post("/tkn", response_model=schemas.Token, tags=['Users']) -def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(), db: Session = Depends(get_db)): - user = auth_helper.authenticate_user(db, form_data.username, form_data.password) - if not user: - raise HTTPException( - status_code=status.HTTP_401_UNAUTHORIZED, - detail="Incorrect username or password", - headers={"WWW-Authenticate": "Bearer"}, - ) - #access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES) - access_token = auth_helper.create_access_token( - 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']) 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) @@ -173,6 +155,31 @@ def get_iot_access_list_for_user(db: Session = Depends(get_db), current_user: sc #crud.record_user_connection(db, user, datetime.now()) return access_list +@app.patch("/users/updatepassword", tags=['Users']) +def change_user_password(request: schemas.UserUpdatePassword, + current_user: models.User = Depends(get_current_active_user), + db: Session = Depends(get_db)): + crud.update_user_password(db, current_user, request) + return + +@app.post("/users/tkn", response_model=schemas.Token, tags=['Users']) +@app.post("/tkn", response_model=schemas.Token, tags=['Users']) +def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(), db: Session = Depends(get_db)): + user = auth_helper.authenticate_user(db, form_data.username, form_data.password) + if not user: + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="Incorrect username or password", + headers={"WWW-Authenticate": "Bearer"}, + ) + #access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES) + access_token = auth_helper.create_access_token( + 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("/admin/users/", response_model=List[schemas.User], tags=['Admin']) def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): users = crud.get_users(db, skip=skip, limit=limit) diff --git a/sql_app/schemas.py b/sql_app/schemas.py index 5db9c40..d152f88 100644 --- a/sql_app/schemas.py +++ b/sql_app/schemas.py @@ -60,6 +60,9 @@ class UserAllowForIotEntityRequestByUsername(BaseModel): username: str description: str +class UserUpdatePassword(BaseModel): + password: str + class OpenDoorRequestBase(BaseModel): username: str bluetooth_mac: str @@ -74,8 +77,6 @@ class IotDoorPollingRequest(BaseModel): bluetooth_mac : str state: int token : str - class Config: - orm_mode = True class IotDoorPollingResponse(BaseModel): open_command : bool From fdb5be583b5a5b4a9d3ca412bf3013dd7c1d52ec Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Wed, 8 Jun 2022 13:01:47 +0300 Subject: [PATCH 04/10] sql_app: data: Endpoint to fetch sensor data Signed-off-by: HeshamTB --- sql_app/crud.py | 4 ++++ sql_app/init_db.py | 23 ++++++++++++++++++++++- sql_app/main.py | 16 +++++++++++----- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/sql_app/crud.py b/sql_app/crud.py index fe704e3..ad2b23f 100644 --- a/sql_app/crud.py +++ b/sql_app/crud.py @@ -185,3 +185,7 @@ def record_user_connection(db: Session, user: models.User, time: datetime): db.add(entry) db.commit() db.refresh(entry) + +def get_all_sensor_data_for_room(db: Session, room_id: int): + data = db.query(models.RoomSensorData).offset(0).limit(50).all() + return data \ No newline at end of file diff --git a/sql_app/init_db.py b/sql_app/init_db.py index 27857fa..341ea1a 100644 --- a/sql_app/init_db.py +++ b/sql_app/init_db.py @@ -2,7 +2,8 @@ from . import crud, main, schemas, auth_helper from decouple import config from .database import SessionLocal -from datetime import timedelta +from datetime import timedelta, datetime +from random import randint db = SessionLocal() @@ -63,9 +64,29 @@ def init_monitor(): def init_allowance(): crud.create_user_link_to_iot(db, 1, 1) +def init_sensor_data(): + # humidity : int + # people : int + # temperature : int + # smoke_sensor_reading : int + # token: str + + for i in range(50): + room_data = \ + schemas.\ + IotMonitorRoomInfo\ + (humidity=randint(20, 80), + people=randint(0, 10), + temperature=randint(18, 27), + smoke_sensor_reading=randint(150, 700), + token='dummy') + crud.record_room_sensor_data(db, room_data) + + def init(): init_user() init_door() init_monitor() init_allowance() + init_sensor_data() \ No newline at end of file diff --git a/sql_app/main.py b/sql_app/main.py index a183260..eded5e6 100644 --- a/sql_app/main.py +++ b/sql_app/main.py @@ -204,7 +204,7 @@ def read_user(user_id: int, db: Session = Depends(get_db)): detail="User not found") return db_user -@app.post("/admin/users/allowdevice/id", tags=['Admin']) +@app.patch("/admin/users/allowdevice/id", tags=['Admin']) def allow_user_for_iot_entity_by_id(request: schemas.UserAllowForIotEntityRequestByID, db: Session = Depends(get_db)): user = crud.get_user(db, request.user_id) if not user: @@ -224,7 +224,7 @@ def allow_user_for_iot_entity_by_id(request: schemas.UserAllowForIotEntityReques crud.increment_door_access_list_counter(db, iot_entity) return -@app.post("/admin/users/disallowdevice/id", tags=['Admin']) +@app.patch("/admin/users/disallowdevice/id", tags=['Admin']) def disallow_user_for_iot_entity_by_id(request: schemas.UserAllowForIotEntityRequestByID, db: Session = Depends(get_db)): user = crud.get_user(db, request.user_id) if not user: @@ -244,7 +244,7 @@ def disallow_user_for_iot_entity_by_id(request: schemas.UserAllowForIotEntityReq crud.increment_door_access_list_counter(db, iot_entity) return -@app.post("/admin/users/allowdevice/name", tags=['Admin']) +@app.patch("/admin/users/allowdevice/name", tags=['Admin']) def allow_user_for_iot_entity_by_name(request: schemas.UserAllowForIotEntityRequestByUsername, db: Session = Depends(get_db)): user = crud.get_user_by_username(db, request.username) if not user: @@ -263,11 +263,11 @@ def allow_user_for_iot_entity_by_name(request: schemas.UserAllowForIotEntityRequ return -@app.post("/admin/users/{user_id}/deactiveate", tags=['Admin']) +@app.patch("/admin/users/{user_id}/deactiveate", tags=['Admin']) def deactiveate_user(user_id: int, db:Session = Depends(get_db)): return -@app.post("/admin/users/{user_id}/activeate", tags=['Admin']) +@app.patch("/admin/users/{user_id}/activeate", tags=['Admin']) def deactiveate_user(user_id: int, db:Session = Depends(get_db)): return @@ -304,6 +304,12 @@ def get_access_log_history_for_user(request : schemas.UserAccessLogRequestUserna def get_room_data(db: Session = Depends(get_db)): return crud.get_room_data_now(db) +@app.get("/admin/roominfo/history/sensors/{room_id}", tags=['Admin']) +def get_all_sensor_history(room_id: int, + api_key: APIKey = Depends(auth_helper.valid_api_key), + db: Session = Depends(get_db)): + return crud.get_all_sensor_data_for_room(db, room_id) + @app.post("/iotdevice/door/status", response_model=schemas.IotDoorPollingResponse, tags=['Iot']) def polling_method_for_iot_entity(request: schemas.IotDoorPollingRequest, db: Session = Depends(get_db)): From 0a4a560ac5bb9f0aed0b594b32ccfe59171dd6be Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Wed, 8 Jun 2022 13:26:41 +0300 Subject: [PATCH 05/10] sql_app: fixed errors in room access log endpoints Signed-off-by: HeshamTB --- sql_app/crud.py | 9 +++++---- sql_app/main.py | 21 +++++++++++---------- sql_app/schemas.py | 2 +- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/sql_app/crud.py b/sql_app/crud.py index ad2b23f..4b78790 100644 --- a/sql_app/crud.py +++ b/sql_app/crud.py @@ -34,8 +34,9 @@ def get_user_by_username(db: Session, username: str) -> models.User: def get_users(db: Session, skip: int = 0, limit: int = 100): return db.query(models.User).offset(skip).limit(limit).all() -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_door_by_door_mac(db: Session, iot_id: str): + return db.query(models.DoorAccessLog)\ + .filter(models.DoorAccessLog.iot_id == iot_id).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() @@ -186,6 +187,6 @@ def record_user_connection(db: Session, user: models.User, time: datetime): db.commit() db.refresh(entry) -def get_all_sensor_data_for_room(db: Session, room_id: int): - data = db.query(models.RoomSensorData).offset(0).limit(50).all() +def get_sensor_data_for_room(db: Session, skip: int = 0, limit: int = 100): + data = db.query(models.RoomSensorData).offset(skip).limit(limit).all() return data \ No newline at end of file diff --git a/sql_app/main.py b/sql_app/main.py index eded5e6..9eed5b2 100644 --- a/sql_app/main.py +++ b/sql_app/main.py @@ -279,13 +279,6 @@ def generate_token_for_iot_device(bluetooth_mac : schemas.IotBluetoothMac, tkn = auth_helper.create_iot_dev_token(data) return {"access_token": tkn, "token_type": "bearer"} -@app.post("/admin/iotdevice/accesslog/", tags=['Admin']) -def get_access_log_for_door(request : schemas.AccessLogRequest, - db : Session = Depends(get_db)): - device = crud.get_iot_entity_by_bluetooth_mac(db, request.bluetooth_mac) - if not device: raise HTTPException(status.HTTP_404_NOT_FOUND, 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)): @@ -304,11 +297,19 @@ def get_access_log_history_for_user(request : schemas.UserAccessLogRequestUserna def get_room_data(db: Session = Depends(get_db)): return crud.get_room_data_now(db) -@app.get("/admin/roominfo/history/sensors/{room_id}", tags=['Admin']) -def get_all_sensor_history(room_id: int, +@app.get("/admin/roominfo/history/sensors", tags=['Admin']) +def get_all_sensor_history(skip: int = 0, limit: int = 100, api_key: APIKey = Depends(auth_helper.valid_api_key), db: Session = Depends(get_db)): - return crud.get_all_sensor_data_for_room(db, room_id) + return crud.get_sensor_data_for_room(db, skip, limit) + +@app.post("/admin/roominfo/accesslog", tags=['Admin']) +def get_access_log_for_door(request : schemas.AccessLogRequest, + api_key: APIKey = Depends(auth_helper.valid_api_key), + db : Session = Depends(get_db)): + device = crud.get_iot_entity(db, request.iot_id) + if not device: raise HTTPException(status.HTTP_404_NOT_FOUND, detail="Iot Entity not found") + return crud.get_access_log_for_door_by_door_mac(db, request.iot_id) @app.post("/iotdevice/door/status", response_model=schemas.IotDoorPollingResponse, tags=['Iot']) def polling_method_for_iot_entity(request: schemas.IotDoorPollingRequest, diff --git a/sql_app/schemas.py b/sql_app/schemas.py index d152f88..081548c 100644 --- a/sql_app/schemas.py +++ b/sql_app/schemas.py @@ -107,7 +107,7 @@ class DoorAccessLog(BaseModel): orm_mode = True class AccessLogRequest(BaseModel): - bluetooth_mac : str + iot_id : int class UserAccessLogRequestUsername(BaseModel): username : str From ae5f4e040d656b2a050977743a0f5877812f559f Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Wed, 8 Jun 2022 14:18:43 +0300 Subject: [PATCH 06/10] sql_app: init_db: added connections and open/close commands Signed-off-by: HeshamTB --- sql_app/init_db.py | 46 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/sql_app/init_db.py b/sql_app/init_db.py index 341ea1a..333b907 100644 --- a/sql_app/init_db.py +++ b/sql_app/init_db.py @@ -65,11 +65,6 @@ def init_allowance(): crud.create_user_link_to_iot(db, 1, 1) def init_sensor_data(): - # humidity : int - # people : int - # temperature : int - # smoke_sensor_reading : int - # token: str for i in range(50): room_data = \ @@ -82,11 +77,52 @@ def init_sensor_data(): token='dummy') crud.record_room_sensor_data(db, room_data) +def init_open_close_requests(): + user = crud.get_user_by_email(db, "hisham@banafa.com.sa") + crud.set_open_door_request(db, 1, 10) + log_entry = schemas.DoorAccessLog(user_id=user.id, + iot_id=1, + command="OPEN", + timestamp=datetime.now()) + crud.record_door_access_log(db, log_entry) + log_entry = schemas.DoorAccessLog(user_id=user.id, + iot_id=1, + command="OPEN", + timestamp=datetime.now()) + crud.record_door_access_log(db, log_entry) + + log_entry = schemas.DoorAccessLog(user_id=user.id, + iot_id=1, + command="OPEN", + timestamp=datetime.now()) + crud.record_door_access_log(db, log_entry) + + + log_entry = schemas.DoorAccessLog(user_id=user.id, + iot_id=1, + command="CLOSE", + timestamp=datetime.now()) + crud.record_door_access_log(db, log_entry) + +def init_user_connections(): + users = [ crud.get_user(db, 1), + crud.get_user(db, 2), + crud.get_user(db, 3)] + + for i in range(3): + print(users[i]) + crud.record_user_connection(db, users[i], datetime.now()) + crud.record_user_connection(db, users[i], datetime.now()) + crud.record_user_connection(db, users[i], datetime.now()) + + def init(): init_user() init_door() init_monitor() init_allowance() init_sensor_data() + init_open_close_requests() + init_user_connections() \ No newline at end of file From 6a3d9d9e95e01f5b78a74c056945a87300b02114 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Wed, 8 Jun 2022 14:19:27 +0300 Subject: [PATCH 07/10] sql_app: Use more relations in database Instead of using manual db lookup for access log, use device.access_list to fetch the data from access_log table. This does the SQL query underneath. To do so, instructions from SQLAlchemey were followed to to many-to-one relations for users and devices, respectevly. Signed-off-by: HeshamTB --- sql_app/crud.py | 3 +++ sql_app/init_db.py | 1 - sql_app/main.py | 6 +++--- sql_app/models.py | 12 ++++++++---- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/sql_app/crud.py b/sql_app/crud.py index 4b78790..e330f54 100644 --- a/sql_app/crud.py +++ b/sql_app/crud.py @@ -6,6 +6,7 @@ from sqlalchemy.orm import Session from . import models, schemas, crypto, auth_helper from datetime import datetime +from warnings import warn # TODO: Data we can collect or log # - Last user connection (link to user) @@ -35,6 +36,8 @@ def get_users(db: Session, skip: int = 0, limit: int = 100): return db.query(models.User).offset(skip).limit(limit).all() def get_access_log_for_door_by_door_mac(db: Session, iot_id: str): + warn("Manual access log read is deprecated. Use device.access_log", + DeprecationWarning, stacklevel=2) return db.query(models.DoorAccessLog)\ .filter(models.DoorAccessLog.iot_id == iot_id).all() diff --git a/sql_app/init_db.py b/sql_app/init_db.py index 333b907..547d81a 100644 --- a/sql_app/init_db.py +++ b/sql_app/init_db.py @@ -111,7 +111,6 @@ def init_user_connections(): crud.get_user(db, 3)] for i in range(3): - print(users[i]) crud.record_user_connection(db, users[i], datetime.now()) crud.record_user_connection(db, users[i], datetime.now()) crud.record_user_connection(db, users[i], datetime.now()) diff --git a/sql_app/main.py b/sql_app/main.py index 9eed5b2..e4636c5 100644 --- a/sql_app/main.py +++ b/sql_app/main.py @@ -303,13 +303,13 @@ def get_all_sensor_history(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): return crud.get_sensor_data_for_room(db, skip, limit) -@app.post("/admin/roominfo/accesslog", tags=['Admin']) +@app.post("/admin/roominfo/accesslog",response_model=List[schemas.DoorAccessLog], tags=['Admin']) def get_access_log_for_door(request : schemas.AccessLogRequest, api_key: APIKey = Depends(auth_helper.valid_api_key), db : Session = Depends(get_db)): - device = crud.get_iot_entity(db, request.iot_id) + device: models.IotEntity = crud.get_iot_entity(db, request.iot_id) if not device: raise HTTPException(status.HTTP_404_NOT_FOUND, detail="Iot Entity not found") - return crud.get_access_log_for_door_by_door_mac(db, request.iot_id) + return device.access_log @app.post("/iotdevice/door/status", response_model=schemas.IotDoorPollingResponse, tags=['Iot']) def polling_method_for_iot_entity(request: schemas.IotDoorPollingRequest, diff --git a/sql_app/models.py b/sql_app/models.py index fbc22e9..aee9f9c 100644 --- a/sql_app/models.py +++ b/sql_app/models.py @@ -16,7 +16,8 @@ class User(Base): last_token = Column(String, nullable=True) connections = relationship("UserConnectionHistory") authorized_devices = relationship("IotEntity", secondary="user_iot_link", back_populates="authorized_users") - + connections = relationship("UserConnectionHistory") + access_log = relationship("DoorAccessLog", back_populates="user") class IotEntity(Base): __tablename__ = "iot_entities" @@ -30,6 +31,7 @@ class IotEntity(Base): force_close = Column(Boolean, default=False) state = Column(Boolean, default=False) # True is open, False is closed authorized_users = relationship("User", secondary="user_iot_link", back_populates="authorized_devices") + access_log = relationship("DoorAccessLog", back_populates="iot_device") class UserAuthToIoTDev(Base): __tablename__ = "user_iot_link" @@ -42,8 +44,10 @@ class DoorAccessLog(Base): __tablename__ = "door_access_log" entry_id = Column(Integer, primary_key=True, index=True) - user_id = Column(Integer, ForeignKey('user_accounts.id')) - iot_id = Column(Integer, ForeignKey('iot_entities.id')) + user_id = Column(Integer, ForeignKey('user_accounts.id'), index=True) + user = relationship("User", back_populates="access_log") + iot_id = Column(Integer, ForeignKey('iot_entities.id'), index=True) + iot_device = relationship("IotEntity", back_populates="access_log") command = Column(String(16)) timestamp = Column(DateTime) @@ -62,7 +66,7 @@ class UserConnectionHistory(Base): __tablename__ = "user_connection_history" reading_id = Column(Integer, primary_key=True) - user_id = Column(Integer,ForeignKey("user_accounts.id"), index=True) + user_id = Column(Integer, ForeignKey("user_accounts.id"), index=True) timestamp = Column(DateTime) # TODO: add ip From 6942d4881d4ddc3fae8cf6ac9512bfe17fbf88cc Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Wed, 8 Jun 2022 14:31:08 +0300 Subject: [PATCH 08/10] sql_app: missing attribute force_close Signed-off-by: HeshamTB --- sql_app/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sql_app/main.py b/sql_app/main.py index e4636c5..7aa5c7f 100644 --- a/sql_app/main.py +++ b/sql_app/main.py @@ -149,7 +149,8 @@ def get_iot_access_list_for_user(db: Session = Depends(get_db), current_user: sc humidity=sensors.humidity, people=sensors.people, temperature=sensors.temperature, - smoke_sensor_reading=sensors.smoke_sensor_reading + smoke_sensor_reading=sensors.smoke_sensor_reading, + force_close=dev_db.force_close ) access_list.append(entry) #crud.record_user_connection(db, user, datetime.now()) From 35f0e8abb45767bfa235a3c2ae51094e9f73ef56 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Wed, 8 Jun 2022 14:31:47 +0300 Subject: [PATCH 09/10] sql_app: Use ORM relations for access log requests Signed-off-by: HeshamTB --- sql_app/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sql_app/main.py b/sql_app/main.py index 7aa5c7f..3e31d02 100644 --- a/sql_app/main.py +++ b/sql_app/main.py @@ -285,14 +285,14 @@ 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.HTTP_404_NOT_FOUND, detail="User not found") - return crud.get_access_log_for_user_by_id(db, user.id) + return user.access_log @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.HTTP_404_NOT_FOUND, detail="User not found") - return crud.get_access_log_for_user_by_id(db, user.id) + return user.access_log @app.get("/admin/roominfo/now/", tags=['Admin']) def get_room_data(db: Session = Depends(get_db)): From cdf3a6dfb14a3d8cadace0e4c8af5181723566cf Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Wed, 8 Jun 2022 14:37:26 +0300 Subject: [PATCH 10/10] sql_app: implement user activate/deactivate Previous code now rejects all user authenticated endpoints. Even /users/me. Signed-off-by: HeshamTB --- sql_app/crud.py | 8 +++++++- sql_app/main.py | 6 ++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/sql_app/crud.py b/sql_app/crud.py index e330f54..f166f45 100644 --- a/sql_app/crud.py +++ b/sql_app/crud.py @@ -192,4 +192,10 @@ def record_user_connection(db: Session, user: models.User, time: datetime): def get_sensor_data_for_room(db: Session, skip: int = 0, limit: int = 100): data = db.query(models.RoomSensorData).offset(skip).limit(limit).all() - return data \ No newline at end of file + return data + +def update_user_status(db: Session, user: models.User, state: bool): + user.is_active = state + db.add(user) + db.commit() + db.refresh(user) diff --git a/sql_app/main.py b/sql_app/main.py index 3e31d02..e0230b2 100644 --- a/sql_app/main.py +++ b/sql_app/main.py @@ -266,11 +266,13 @@ def allow_user_for_iot_entity_by_name(request: schemas.UserAllowForIotEntityRequ @app.patch("/admin/users/{user_id}/deactiveate", tags=['Admin']) def deactiveate_user(user_id: int, db:Session = Depends(get_db)): - return + user = crud.get_user(db, user_id) + crud.update_user_status(db, user, False) @app.patch("/admin/users/{user_id}/activeate", tags=['Admin']) def deactiveate_user(user_id: int, db:Session = Depends(get_db)): - return + user = crud.get_user(db, user_id) + crud.update_user_status(db, user, True) @app.post("/admin/iotdevice/gentoken/", response_model=schemas.Token, tags=['Admin']) def generate_token_for_iot_device(bluetooth_mac : schemas.IotBluetoothMac,