sql_app: Implemented access list counter:

A counter is associated with every
	Iot Device. The counter is always increment
	when a user is allowed or disallowed to use
	the device, hence, ensuring coherency.
	It is also now exposed in the
	IotDoorPollingRequest schema, enabling the
	Iot Device to fetch the new access list.

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2022-06-06 16:38:35 +03:00
parent 3a6a1ccefd
commit 2b4d3bf7f5
4 changed files with 13 additions and 4 deletions

View File

@ -141,4 +141,11 @@ def record_room_sensor_data(db: Session, entry: schemas.IotMonitorRoomInfo):
timestamp=datetime.now()) timestamp=datetime.now())
db.add(db_item) db.add(db_item)
db.commit() db.commit()
db.refresh(db_item) db.refresh(db_item)
def increment_door_access_list_counter(db: Session, iot_entity: models.IotEntity):
#setattr(iot_entity, "acces_list_counter", iot_entity.acces_list_counter + 1)
iot_entity.acces_list_counter = iot_entity.acces_list_counter + 1
db.add(iot_entity)
db.commit()
db.refresh(iot_entity)

View File

@ -113,6 +113,7 @@ def allow_user_for_iot_entity_by_id(request: schemas.UserAllowForIotEntityReques
if not res: if not res:
raise HTTPException(status_code=500, detail="Could not complete operation") raise HTTPException(status_code=500, detail="Could not complete operation")
crud.increment_door_access_list_counter(db, iot_entity)
return return
@app.post("/admin/users/disallowdevice/id", tags=['Admin']) @app.post("/admin/users/disallowdevice/id", tags=['Admin'])
@ -128,7 +129,8 @@ def disallow_user_for_iot_entity_by_id(request: schemas.UserAllowForIotEntityReq
res = crud.remove_user_link_to_iot(db, request.user_id, request.iot_entity_id) res = crud.remove_user_link_to_iot(db, request.user_id, request.iot_entity_id)
if not res: if not res:
raise HTTPException(status_code=500, detail="Could not complete operation") raise HTTPException(status_code=500, detail="Could not complete operation")
crud.increment_door_access_list_counter(db, iot_entity)
return return
@app.post("/admin/users/allowdevice/name", tags=['Admin']) @app.post("/admin/users/allowdevice/name", tags=['Admin'])

View File

@ -16,7 +16,6 @@ class User(Base):
last_token = Column(String, nullable=True) last_token = Column(String, nullable=True)
authorized_devices = relationship("IotEntity", secondary="user_iot_link", back_populates="authorized_users") authorized_devices = relationship("IotEntity", secondary="user_iot_link", back_populates="authorized_users")
#authorized_devices = relationship("IotEntity", secondary= 'user_iot_link')
class IotEntity(Base): class IotEntity(Base):
@ -27,8 +26,8 @@ class IotEntity(Base):
description = Column(String(512)) description = Column(String(512))
open_request = Column(Boolean, default=False) open_request = Column(Boolean, default=False)
time_seconds = Column(Integer, default=10) time_seconds = Column(Integer, default=10)
acces_list_counter = Column(Integer, default=0)
authorized_users = relationship("User", secondary="user_iot_link", back_populates="authorized_devices") authorized_users = relationship("User", secondary="user_iot_link", back_populates="authorized_devices")
#authorized_users = relationship("User", secondary= 'user_iot_link')
class UserAuthToIoTDev(Base): class UserAuthToIoTDev(Base):
__tablename__ = "user_iot_link" __tablename__ = "user_iot_link"

View File

@ -26,6 +26,7 @@ class IotEntity(IotEntityBase):
#authorized_users: List[User] = [] #authorized_users: List[User] = []
open_request: bool # Flag to open open_request: bool # Flag to open
time_seconds: int time_seconds: int
acces_list_counter: int
class Config: class Config:
orm_mode = True orm_mode = True