class Groups(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(255), unique=True) description = db.Column(db.String(255)) confirmed_at = db.Column(db.DateTime(), default=datetime.now()) roles = db.relationship('Role', backref='groups', lazy='dynamic') def to_json(self): doc = self.__dict__ if "_sa_instance_state" in doc: del doc["_sa_instance_state"] if doc.get('confirmed_at', None): doc['confirmed_at'] = doc['confirmed_at'].strftime('%F %T') return doc
class Role(db.Model, RoleMixin): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(255)) permissions = db.Column(db.Integer, default=Permission.VIEW) description = db.Column(db.String(255)) groups_id = db.Column(db.Integer, db.ForeignKey('groups.id')) def to_json(self): doc = self.__dict__ if "_sa_instance_state" in doc: del doc["_sa_instance_state"] if doc.get('confirmed_at', None): doc['confirmed_at'] = doc['confirmed_at'].strftime('%F %T') return doc
class CloudHost(db.Model): __tablename__ = "cloud_host" id = db.Column(db.Integer, primary_key=True, autoincrement=True) room_id = db.Column(db.Integer, db.ForeignKey("cloud_room.id")) public_ip = db.Column(db.String(32)) private_ip = db.Column(db.String(32)) ssh_port = db.Column(db.String(16)) update_time = db.Column(db.DateTime) host_info = db.Column(db.String(255)) def updatetime(self): now_time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) self.update_time = now_time_str def to_dict(self): doc = self.__dict__ if "_sa_instance_state" in doc: del doc["_sa_instance_state"] result = {} for key in doc.keys(): if isinstance(doc[key], datetime): result[key] = doc[key].strftime("%Y-%m-%d %H:%M:%S") else: result[key] = doc[key] return result
class CloudRoom(db.Model): __tablename__ = "cloud_room" id = db.Column(db.Integer, primary_key=True, autoincrement=True) supplier = db.Column(db.String(length=64, collation='utf8_general_ci')) region = db.Column(db.String(length=64, collation='utf8_general_ci')) zore = db.Column(db.String(length=64, collation='utf8_general_ci')) cloud_host = db.relationship("CloudHost", backref="cloud_room", lazy='dynamic') def to_dict(self): doc = self.__dict__ if "_sa_instance_state" in doc: del doc["_sa_instance_state"] result = {} for key in doc.keys(): if isinstance(doc[key], datetime): result[key] = doc[key].strftime("%Y-%m-%d %H:%M:%S") else: result[key] = doc[key] return result
class Server(db.Model): id = db.Column(db.Integer, primary_key=True) hostname = db.Column(db.String(255), unique=True) outside = db.Column(db.String(255), unique=True) intranet = db.Column(db.String(255)) idc = db.Column(db.String(255)) area = db.Column(db.String(255)) def to_json(self): doc = self.__dict__ if "_sa_instance_state" in doc: del doc["_sa_instance_state"] if doc.get('confirmed_at', None): doc['confirmed_at'] = doc['confirmed_at'].strftime('%F %T') return doc
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(255), unique=True) username = db.Column(db.String(255), unique=True) job = db.Column(db.String(255)) phone = db.Column(db.String(255)) password_hash = db.Column(db.String(255)) active = db.Column(db.Boolean()) confirmed_at = db.Column(db.DateTime(), default=datetime.now()) roles = db.relationship('Role', secondary=roles_users, backref=db.backref('users', lazy='dynamic')) last_login_at = db.Column(db.String(255)) current_login_at = db.Column(db.String(255)) last_login_ip = db.Column(db.String(255)) current_login_ip = db.Column(db.String(255)) login_count = db.Column(db.Integer) # 权限验证 def can(self, gid, permissions=Permission.VIEW): if self.roles is None: return False # 判断是否在组中 [ r for r in self.roles if 组 == r.组] permissions_list = [r.permissions for r in self.roles if r.groups_id == int(gid) or r.groups_id == 2] print("can:", permissions_list, gid) if permissions_list: all_perms = reduce(lambda x, y: x | y, permissions_list) else: all_perms = 0 return all_perms & permissions == permissions def can_admin(self): return self.can(gid=1, permissions=Permission.ADMIN) # password不可读 @property def password(self): raise AttributeError('`password` is not a readable attribute') # password加密 @password.setter def password(self, password): self.password_hash = generate_password_hash(password) # 验证password def verify_password(self, password): return check_password_hash(self.password_hash, password) def to_json(self, gid): doc = self.__dict__ doc['roles'] = self.roles # print(self.username, self.roles) if "_sa_instance_state" in doc: del doc["_sa_instance_state"] if "password_hash" in doc: del doc["password_hash"] if doc.get('confirmed_at', None): doc['confirmed_at'] = doc['confirmed_at'].strftime('%F %T') if doc.get('current_login_at', None): doc['current_login_at'] = doc['current_login_at'].split('.')[0] if doc.get('last_login_at', None): doc['last_login_at'] = doc['last_login_at'].split('.')[0] if doc.get('roles', None): doc['roles'] = [str(r.permissions) for r in doc['roles'] if r.groups_id == gid] return doc
class Permission(object): VIEW = 0X01 EDITOR = 0x02 OPERATOR = 0x04 ADMIN = 0xff # hex(255) SUPER_ADMIN = -0xff PERMISSION_MAP = { VIEW: ('view', 'View'), EDITOR: ('editor', 'Editor'), OPERATOR: ('op', 'Operator'), ADMIN: ('admin', 'administrator'), } roles_users = db.Table( 'roles_users', db.Column('user_id', db.Integer, db.ForeignKey('user.id')), db.Column('role_id', db.Integer, db.ForeignKey('role.id'))) class Role(db.Model, RoleMixin): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(255)) permissions = db.Column(db.Integer, default=Permission.VIEW) description = db.Column(db.String(255)) groups_id = db.Column(db.Integer, db.ForeignKey('groups.id')) def to_json(self): doc = self.__dict__ if "_sa_instance_state" in doc: del doc["_sa_instance_state"]