diff --git a/sql_app/crud.py b/sql_app/crud.py index bac5a18..e087575 100644 --- a/sql_app/crud.py +++ b/sql_app/crud.py @@ -8,6 +8,8 @@ from . import models, schemas, crypto, auth_helper def get_user(db: Session, user_id: int): return db.query(models.User).filter(models.User.id == user_id).first() +def get_iot_entity(db: Session, id: int): + return db.query(models.IotEntity).filter(models.IotEntity.id == id).first() def get_user_by_email(db: Session, email: str): return db.query(models.User).filter(models.User.email == email).first() @@ -15,16 +17,13 @@ def get_user_by_email(db: Session, email: str): def get_user_by_username(db: Session, username: str): return db.query(models.User).filter(models.User.username == username).first() - def get_users(db: Session, skip: int = 0, limit: int = 100): return db.query(models.User).offset(skip).limit(limit).all() - def create_user(db: Session, user: schemas.UserCreate): key = crypto.gen_new_key(user.password) salt = key[1] hashed_pass = key[0] - # TODO: check if user already exists? based on name,email ... db_user = models.User(email=user.email, username=user.username,hashed_password=hashed_pass, passwd_salt=salt) db.add(db_user) db.commit() @@ -36,9 +35,17 @@ def get_iot_entities(db: Session, skip: int = 0, limit: int = 100): return db.query(models.IotEntity).offset(skip).limit(limit).all() -def create_iot_entity(db: Session, item: schemas.IotEntityCreate, user_id: int): - db_item = models.Item(**item.dict(), owner_id=user_id) +def create_iot_entity(db: Session, iot_entity: schemas.IotEntityCreate): + db_item = models.IotEntity(id=iot_entity.id, description=iot_entity.description) db.add(db_item) db.commit() db.refresh(db_item) return db_item + +def create_user_link_to_iot(db: Session, user_id: int, iot_dev_id: int): + # Ensure link is not already present and it does not allow duplicates + new_link = models.UserAuthToIoTDev(user_id=user_id, iot_entity_id=iot_dev_id) + db.add(new_link) + db.commit() + db.refresh(new_link) + return True \ No newline at end of file diff --git a/sql_app/main.py b/sql_app/main.py index a73476c..3143d11 100644 --- a/sql_app/main.py +++ b/sql_app/main.py @@ -74,6 +74,11 @@ 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 +@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) + return iot_entities + @app.get("/admin/users/{user_id}", response_model=schemas.User, tags=['Admin']) def read_user(user_id: int, db: Session = Depends(get_db)): db_user = crud.get_user(db, user_id=user_id) @@ -81,13 +86,27 @@ def read_user(user_id: int, db: Session = Depends(get_db)): raise HTTPException(status_code=404, detail="User not found") return db_user +@app.post("/admin/users/{user_id}/allow/{iot_entity_id}", tags=['Admin']) +def allow_user_for_iot_entity(request: schemas.UserAllowForIotEntityRequest, 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") + + 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") + + 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") + + return @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) return user.authorized_devices - @app.post("/tkn", response_model=schemas.Token, tags=['Users']) async 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) diff --git a/sql_app/models.py b/sql_app/models.py index 31985e9..ce708f3 100644 --- a/sql_app/models.py +++ b/sql_app/models.py @@ -22,7 +22,6 @@ class IotEntity(Base): id = Column(Integer, primary_key=True, index=True) description = Column(String, index=True) - owner_id = Column(Integer, ForeignKey("user_accounts.id")) authorized_users = relationship("User", secondary= 'user_iot_link') diff --git a/sql_app/schemas.py b/sql_app/schemas.py index 7bbe21e..b5b5272 100644 --- a/sql_app/schemas.py +++ b/sql_app/schemas.py @@ -7,28 +7,25 @@ class IotEntityBase(BaseModel): id: int description: str +class UserBase(BaseModel): + email: str + username: str class IotEntityCreate(IotEntityBase): pass -class IotEntity(IotEntityBase): - id: int - description: str - - class Config: - orm_mode = True - - -class UserBase(BaseModel): - email: str - username: str - - class UserCreate(UserBase): password: str +class IotEntity(IotEntityBase): + id: int + description: str + #authorized_users: List[User] = [] + class Config: + orm_mode = True + class User(UserBase): id: int is_active: bool @@ -37,6 +34,7 @@ class User(UserBase): class Config: orm_mode = True + class Token(BaseModel): access_token : str token_type : str @@ -45,3 +43,7 @@ class TokenData(BaseModel): username : str # Token can conatin information. But we are already recording this in a database # for scalability. + +class UserAllowForIotEntityRequest(BaseModel): + user_id: int + iot_entity_id: int \ No newline at end of file