sql_app: create and link iot device

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2022-04-13 08:34:12 +03:00
parent a28183ddd8
commit fe3a193a4f
4 changed files with 47 additions and 20 deletions

View File

@ -8,6 +8,8 @@ from . import models, schemas, crypto, auth_helper
def get_user(db: Session, user_id: int): def get_user(db: Session, user_id: int):
return db.query(models.User).filter(models.User.id == user_id).first() 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): def get_user_by_email(db: Session, email: str):
return db.query(models.User).filter(models.User.email == email).first() 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): def get_user_by_username(db: Session, username: str):
return db.query(models.User).filter(models.User.username == username).first() return db.query(models.User).filter(models.User.username == username).first()
def get_users(db: Session, skip: int = 0, limit: int = 100): def get_users(db: Session, skip: int = 0, limit: int = 100):
return db.query(models.User).offset(skip).limit(limit).all() return db.query(models.User).offset(skip).limit(limit).all()
def create_user(db: Session, user: schemas.UserCreate): def create_user(db: Session, user: schemas.UserCreate):
key = crypto.gen_new_key(user.password) key = crypto.gen_new_key(user.password)
salt = key[1] salt = key[1]
hashed_pass = key[0] 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_user = models.User(email=user.email, username=user.username,hashed_password=hashed_pass, passwd_salt=salt)
db.add(db_user) db.add(db_user)
db.commit() 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() return db.query(models.IotEntity).offset(skip).limit(limit).all()
def create_iot_entity(db: Session, item: schemas.IotEntityCreate, user_id: int): def create_iot_entity(db: Session, iot_entity: schemas.IotEntityCreate):
db_item = models.Item(**item.dict(), owner_id=user_id) db_item = models.IotEntity(id=iot_entity.id, description=iot_entity.description)
db.add(db_item) db.add(db_item)
db.commit() db.commit()
db.refresh(db_item) db.refresh(db_item)
return 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

View File

@ -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) iot_entities = crud.get_iot_entities(db, skip=skip, limit=limit)
return iot_entities 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']) @app.get("/admin/users/{user_id}", response_model=schemas.User, tags=['Admin'])
def read_user(user_id: int, db: Session = Depends(get_db)): def read_user(user_id: int, db: Session = Depends(get_db)):
db_user = crud.get_user(db, user_id=user_id) 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") raise HTTPException(status_code=404, detail="User not found")
return db_user 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']) @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)): 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) user = crud.get_user_by_username(db, current_user.username)
return user.authorized_devices return user.authorized_devices
@app.post("/tkn", response_model=schemas.Token, tags=['Users']) @app.post("/tkn", response_model=schemas.Token, tags=['Users'])
async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(), db: Session = Depends(get_db)): 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) user = auth_helper.authenticate_user(db, form_data.username, form_data.password)

View File

@ -22,7 +22,6 @@ class IotEntity(Base):
id = Column(Integer, primary_key=True, index=True) id = Column(Integer, primary_key=True, index=True)
description = Column(String, index=True) description = Column(String, index=True)
owner_id = Column(Integer, ForeignKey("user_accounts.id"))
authorized_users = relationship("User", secondary= 'user_iot_link') authorized_users = relationship("User", secondary= 'user_iot_link')

View File

@ -7,28 +7,25 @@ class IotEntityBase(BaseModel):
id: int id: int
description: str description: str
class UserBase(BaseModel):
email: str
username: str
class IotEntityCreate(IotEntityBase): class IotEntityCreate(IotEntityBase):
pass pass
class IotEntity(IotEntityBase):
id: int
description: str
class Config:
orm_mode = True
class UserBase(BaseModel):
email: str
username: str
class UserCreate(UserBase): class UserCreate(UserBase):
password: str password: str
class IotEntity(IotEntityBase):
id: int
description: str
#authorized_users: List[User] = []
class Config:
orm_mode = True
class User(UserBase): class User(UserBase):
id: int id: int
is_active: bool is_active: bool
@ -37,6 +34,7 @@ class User(UserBase):
class Config: class Config:
orm_mode = True orm_mode = True
class Token(BaseModel): class Token(BaseModel):
access_token : str access_token : str
token_type : str token_type : str
@ -45,3 +43,7 @@ class TokenData(BaseModel):
username : str username : str
# Token can conatin information. But we are already recording this in a database # Token can conatin information. But we are already recording this in a database
# for scalability. # for scalability.
class UserAllowForIotEntityRequest(BaseModel):
user_id: int
iot_entity_id: int