fix: 确保默认Admin账户存在且登录用户名不区分大小写
This commit is contained in:
@@ -17,6 +17,26 @@ def create_app():
|
||||
csrf.init_app(app)
|
||||
with app.app_context():
|
||||
db.create_all()
|
||||
from .models import User, UserStatus, Profile
|
||||
from sqlalchemy import text
|
||||
try:
|
||||
db.session.execute(text("ALTER TABLE user ADD COLUMN must_change_password BOOLEAN DEFAULT 0"))
|
||||
db.session.commit()
|
||||
except Exception:
|
||||
pass
|
||||
from .models import User, UserStatus, Profile
|
||||
from werkzeug.security import generate_password_hash
|
||||
admin_any = User.query.filter_by(role="admin").first()
|
||||
admin_named = User.query.filter_by(username="Admin").first()
|
||||
if not admin_named:
|
||||
email = "admin@example.com"
|
||||
if User.query.filter_by(email=email).first():
|
||||
email = "admin2@example.com"
|
||||
u = User(email=email, username="Admin", password_hash=generate_password_hash("lzgzsystem"), role="admin", status=UserStatus.approved, must_change_password=True)
|
||||
db.session.add(u)
|
||||
db.session.flush()
|
||||
db.session.add(Profile(user_id=u.id))
|
||||
db.session.commit()
|
||||
@app.context_processor
|
||||
def inject_csrf():
|
||||
return dict(csrf_token=generate_csrf())
|
||||
@@ -42,15 +62,4 @@ def create_app():
|
||||
app.register_blueprint(admin_bp)
|
||||
from .cli import register_cli
|
||||
register_cli(app)
|
||||
@app.before_request
|
||||
def ensure_admin_setup():
|
||||
from .models import User
|
||||
from flask import request, redirect, url_for
|
||||
try:
|
||||
has_admin = User.query.filter_by(role="admin").first() is not None
|
||||
except Exception:
|
||||
has_admin = True
|
||||
if not has_admin:
|
||||
if not (request.path.startswith("/setup/admin") or request.path.startswith("/static")):
|
||||
return redirect(url_for("setup.admin"))
|
||||
return app
|
||||
|
||||
Reference in New Issue
Block a user