# coding=utf-8
from datetime import datetime
from flask_login import UserMixin
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from hashlib import md5
app = Flask(__name__)
db = SQLAlchemy(app)
ip = '127.0.0.1'
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://mysql:cy888888@{ip}:3306/management'.format(ip=ip)
SQLALCHEMY_BINDS = {
'user': 'mysql+pymysql://mysql:cy888888@{ip}:3306/user'.format(ip=ip),
}
app.config['SQLALCHEMY_BINDS'] = SQLALCHEMY_BINDS
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
class BaseDB(object):
createTime = db.Column(db.DateTime, default=datetime.now, nullable=False)
isDeleted = db.Column(db.Boolean, default=False, nullable=False)
class User(db.Model, BaseDB, UserMixin):
__tablename__ = 'users'
__bind_key__ = 'user'
userId = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=False)
username = db.Column(db.String(255), unique=True, nullable=False)
passwordHash = db.Column(db.String(255), nullable=False)
lastLoginTime = db.Column(db.DateTime, default=datetime.now, nullable=False)
isLogin = db.Column(db.Boolean, default=False, nullable=False)
isLocked = db.Column(db.Boolean, default=False, nullable=False)
isDisabled = db.Column(db.Boolean, default=True, nullable=False) # True:禁用
isDefault = db.Column(db.Boolean, default=False, nullable=False)
loginTimes = db.Column(db.Integer, default=0, nullable=False)
firstLogFailTimeStamp = db.Column(db.Integer, default=0)
lastLogFailTimeStamp = db.Column(db.Integer, default=0)
lockedTimeStamp = db.Column(db.Integer, default=0)
userAuthority = db.Column(db.Integer, db.ForeignKey('roles.authority'), nullable=False)
@property # work in with load_user
def id(self):
return self.userId
def refresh(self):
self.lastLoginTime = datetime.now()
self.isLogin = True
self.loginTimes = 0
# self.lockedTimeStamp = 0
self.firstLogFailTimeStamp = 0
self.lastLogFailTimeStamp = 0
self.isLocked = False
db.session.commit()
def logout(self):
self.isLogin = False
db.session.commit()
def __repr__(self):
return self.username
role_resource = db.Table('role_resource',
db.metadata,
db.Column('roles_id', db.Integer, db.ForeignKey('roles.roleId')),
db.Column('resources_id', db.Integer, db.ForeignKey('resources.id')),
info={'bind_key': 'user'})
class Role(db.Model, UserMixin):
__tablename__ = 'roles'
__bind_key__ = 'user'
roleId = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=False)
roleName = db.Column(db.String(255), unique=True, nullable=False)
authority = db.Column(db.Integer, unique=True, nullable=False) # 100:admin 10:operator 1:auditor
users = db.relationship('User', backref='role')
# 包含资源
resources = db.relationship('Resource',
secondary=role_resource,
backref=db.backref('roles', lazy='dynamic'), lazy='dynamic') # 资源所属角色
default_url = db.Column(db.String(255))
class Resource(db.Model):
__tablename__ = 'resources'
__bind_key__ = 'user'
id = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=False)
name = db.Column(db.String(255), nullable=False)
type = db.Column(db.Integer, nullable=False) # 类型,1:菜单,2:功能
url = db.Column(db.String(255))
description = db.Column(db.String(255))
createdAt = db.Column(db.DateTime, default=datetime.now, nullable=False)
parent_id = db.Column(db.Integer)
url_hash = db.Column(db.Integer)
def str_to_md5_hash(str_):
salt = '34fd982698d4640dc35c267fe7b62833'
md5_handler = md5()
md5_handler.update(str_.encode('utf-8')) # hash first
salt1 = md5_handler.hexdigest() + salt # hash second add salt
md5_handler.update(salt1.encode('utf-8'))
salt2 = ''.join([md5_handler.hexdigest(), str_, '13', salt, str_, '14'])
md5_handler.update(salt2.encode('utf-8'))
return md5_handler.hexdigest()
def default_pwd():
item = User.query.filter_by(username ='admin').first()
item.passwordHash = str_to_md5_hash('abcde')
db.session.add(item)
db.session.commit()
if __name__ == '__main__':
default_pwd()