From 17aa68ba9ae3b31f93d2f01201602a4d97bc576d Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Thu, 14 Apr 2022 01:37:30 +0300 Subject: [PATCH] allow user: allow acces to room by name or id Signed-off-by: HeshamTB --- sql_app/crud.py | 3 +++ sql_app/main.py | 29 +++++++++++++++++++++++------ sql_app/schemas.py | 8 ++++++-- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/sql_app/crud.py b/sql_app/crud.py index 8812087..4d856c1 100644 --- a/sql_app/crud.py +++ b/sql_app/crud.py @@ -11,6 +11,9 @@ def get_user(db: Session, user_id: int): def get_iot_entity(db: Session, id: int): return db.query(models.IotEntity).filter(models.IotEntity.id == id).first() +def get_iot_entity_by_description(db: Session, description: str): + return db.query(models.IotEntity).filter(models.IotEntity.description == description).first() + def get_user_by_email(db: Session, email: str): return db.query(models.User).filter(models.User.email == email).first() diff --git a/sql_app/main.py b/sql_app/main.py index acbb265..413db35 100644 --- a/sql_app/main.py +++ b/sql_app/main.py @@ -74,6 +74,7 @@ def read_iot_entities(skip: int = 0, limit: int = 100, db: Session = Depends(get iot_entities = crud.get_iot_entities(db, skip=skip, limit=limit) return iot_entities +# TODO: Can duplicate @app.post("/admin/iotentities/create", response_model=schemas.IotEntity, tags=['Admin']) def create_iot_entities(iot_entity: schemas.IotEntityCreate, db: Session = Depends(get_db)): iot_entities = crud.create_iot_entity(db, iot_entity) @@ -86,9 +87,9 @@ def read_user(user_id: int, db: Session = Depends(get_db)): raise HTTPException(status_code=404, detail="User not found") return db_user -# Add admin, disable user, activeate user -@app.post("/admin/users/allowdevice/", tags=['Admin']) -def allow_user_for_iot_entity(request: schemas.UserAllowForIotEntityRequest, db: Session = Depends(get_db)): +# TODO: Can duplicate +@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") @@ -101,10 +102,10 @@ def allow_user_for_iot_entity(request: schemas.UserAllowForIotEntityRequest, db: if not res: raise HTTPException(status_code=500, detail="Could not complete operation") - return + return user -@app.post("/admin/users/disallowdevice/", tags=['Admin']) -def allow_user_for_iot_entity(request: schemas.UserAllowForIotEntityRequest, db: Session = Depends(get_db)): +@app.post("/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: raise HTTPException(status_code=404, detail="User not found") @@ -120,6 +121,22 @@ def allow_user_for_iot_entity(request: schemas.UserAllowForIotEntityRequest, db: return +@app.post("/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: + raise HTTPException(status_code=404, 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") + + 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") + + return + @app.post("/admin/users/{user_id}/deactiveate", tags=['Admin']) def deactiveate_user(user_id: int, db:Session = Depends(get_db)): return diff --git a/sql_app/schemas.py b/sql_app/schemas.py index 348d0c0..64405f8 100644 --- a/sql_app/schemas.py +++ b/sql_app/schemas.py @@ -44,6 +44,10 @@ class TokenData(BaseModel): # Token can conatin information. But we are already recording this in a database # for scalability. -class UserAllowForIotEntityRequest(BaseModel): +class UserAllowForIotEntityRequestByID(BaseModel): user_id: int - iot_entity_id: int \ No newline at end of file + iot_entity_id: int + +class UserAllowForIotEntityRequestByUsername(BaseModel): + username: str + description: str \ No newline at end of file