class Devices(db.Model): __tablename__ = "devices" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), unique=True) description = db.Column(db.String(100)) remote_design_id = db.Column(db.Integer, ForeignKey('remote_design.id')) remote_design = db.relationship("RemoteDesign", backref="devices") def __init__(self, name, description): self.name = name self.description = description
class RemoteDesign(db.Model): __tablename__ = "remote_design" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100)) ext_topic = db.Column(db.String(100)) description = db.Column(db.String(100)) data = db.Column(db.String(1000)) def __init__(self, name, description, data, ext_topic): self.name = name self.description = description self.data = data self.ext_topic = ext_topic
class Room(db.Model): __tablename__ = "room" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), unique=True) description = db.Column(db.String(100)) def __init__(self, name, description): self.name = name self.description = description def __repr__(self): return 'Room( name = {self.name!r}, description = {self.description})'.format( self=self)
class Permission(db.Model): __tablename__ = "permission" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), unique=True) description = db.Column(db.String(100)) def __init__(self, name, description): self.name = name self.description = description def __eq__(self, other): if not isinstance(other, Permission): return NotImplemented return self.name == other.name def __repr__(self): return 'Permission(name = {self.name!r}, description = {self.description!r})'.format( self=self)
class AuditTrail(db.Model): __tablename__ = "audit_trail" id = db.Column(db.Integer, primary_key=True) timestamp = db.Column(db.DateTime(timezone=True)) cardID = db.Column(db.String(100)) action = db.Column(db.String(100)) user_id = db.Column(db.Integer, ForeignKey('users.id')) room_id = db.Column(db.Integer, ForeignKey('room.id')) permission_id = db.Column(db.Integer, ForeignKey('permission.id')) user = db.relationship("Users", backref="audit_trail") room = db.relationship("Room", backref="audit_trail") permission = db.relationship("Permission", backref="audit_trail") def __init__(self, timestamp, action, cardID): self.timestamp = timestamp self.action = action self.cardID = cardID
class UsersLogs(db.Model): __tablename__ = "users_logs" id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(100)) public_id = db.Column(db.String(300)) time_login = db.Column(db.DateTime(timezone=True)) last_request_time = db.Column(db.DateTime(timezone=True)) status = db.Column(db.String(100)) room_control_real_time_data = db.Column( db.Boolean(), nullable=True) # if true means user is updated and false is not def __init__(self, public_id, time_login, last_request_time, status, username, room_control_real_time_data): self.time_login = time_login self.public_id = public_id self.status = status self.last_request_time = last_request_time self.username = username self.room_control_real_time_data = room_control_real_time_data
class RoomStatus(db.Model): __tablename__ = "room_status" id = db.Column(db.Integer, primary_key=True) room_id = db.Column(db.Integer, ForeignKey('room.id')) device_id = db.Column(db.Integer, ForeignKey('devices.id')) status = db.Column(db.String(100)) timestamp = db.Column(db.DateTime(timezone=True)) room = db.relationship("Room", backref="room_status") device = db.relationship("Devices", backref="room_status") def __init__(self, status, timestamp): self.status = status self.timestamp = timestamp
class Users(db.Model): __tablename__ = "users" id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(100), unique=True) userpassword = db.Column(db.String(255)) Fname = db.Column(db.String(100)) Lname = db.Column(db.String(100)) cardID = db.Column(db.String(100), unique=True) has_profile_picture = db.Column(db.Boolean) def __init__(self, username, userpassword, Fname, Lname, cardID, has_profile_picture): self.username = username self.userpassword = userpassword self.Fname = Fname self.Lname = Lname self.cardID = cardID self.has_profile_picture = has_profile_picture def _globalPermissions(self): perms = set() [perms.update(m.group.globalPermissions) for m in self.group_member] return perms def _allPermissions(self): perms = set() [perms.update(m.group.allPermissions) for m in self.group_member] return perms def _roomPermissions(self): perms = {} for m in self.group_member: for a in m.group.group_access: if a.room is not None: perms.setdefault(a.permission.name, []).append(a.room) return perms def getAccessibleRooms(self, permissions): if set(permissions) & self.globalPermissions: log.debug("{} globally allowed".format(permissions)) rooms = Room.query.all() else: room_perms = dict([(k, v) for k, v in self.roomPermissions.items() if k in permissions]) log.debug("Room permissions: {}".format(room_perms)) rooms = set(chain(*room_perms.values())) return rooms def getRoomPermission(self, room): perms = set() [ perms.update(m.group.getRoomPermission(room)) for m in self.group_member ] return perms globalPermissions = property(_globalPermissions) allPermissions = property(_allPermissions) roomPermissions = property(_roomPermissions) def _userType(self): perms = self.allPermissions log.debug("Permissions for user {}: {}".format(self.id, perms)) if "Admin" in perms: return "Admin" if "User" in perms: return "User" else: return "Guest" userType = property(_userType)
class Group(db.Model): __tablename__ = "group" id = db.Column(db.Integer, primary_key = True) name = db.Column(db.String(100), unique = True) description = db.Column(db.String(100)) group_access = db.relationship("GroupAccess", backref = "group") def __init__(self, name, description): self.name = name self.description = description def __repr__(self): return 'Group( name = {self.name!r}, description = {self.description})'.format(self=self) def _globalPermissions(self): return set([p.name for p in self.getGlobalPermissions()]) def getGlobalPermissions(self): return [a.permission for a in self.group_access if a.room is None] def _permission(self): return self.getGlobalPermissions()[0] def _set_permission(self, permission): for a in self.group_access: if a.room is None: log.debug("Removing permission {}".format(a.permission.name)) db.session.delete(a) ga = GroupAccess(permission=permission) db.session.add(ga) self.group_access.append(ga) log.debug("Adding permission {}".format(permission.name)) db.session.commit() def _allPermissions(self): log.debug("Group Access for group {self.id}: {self.group_access}".\ format(self=self)) return set([a.permission.name for a in self.group_access]) def getRoomPermission(self, room): perms = set([a.permission.name for a in self.group_access if a.room == room]) perms.update(self.globalPermissions) return perms def _permission_id(self): return self.permission.id def _set_permission_id(self, permission_id): self.permission = Permission.query.get(permission_id) globalPermissions = property(_globalPermissions) allPermissions = property(_allPermissions) permission = property(_permission, _set_permission) permission_id = property(_permission_id, _set_permission_id) def _groupType(self): perms = self.allPermissions log.debug("Permissions for group {}: {}".format(self.id, perms)) if "Admin" in perms: return "Admin" if "User" in perms: return "User" else: return "Guest" groupType = property(_groupType)