From cdf3a6dfb14a3d8cadace0e4c8af5181723566cf Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Wed, 8 Jun 2022 14:37:26 +0300 Subject: [PATCH] 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,