Add first-run setup wizard, README, remove default users, add easter egg
This commit is contained in:
@@ -147,9 +147,52 @@ def _guest_matches_search(guest, query):
|
||||
# Auth
|
||||
# ---------------------------
|
||||
|
||||
def _is_first_run():
|
||||
"""True, если в базе ещё нет ни одного пользователя."""
|
||||
return User.query.first() is None
|
||||
|
||||
|
||||
@main_bp.route("/setup", methods=["GET", "POST"])
|
||||
@limiter.limit("10 per minute", methods=["POST"])
|
||||
def setup():
|
||||
"""Первоначальная настройка: создание первого администратора."""
|
||||
if not _is_first_run():
|
||||
return redirect(url_for("main.login"))
|
||||
|
||||
if request.method == "POST":
|
||||
username = request.form.get("username", "").strip()
|
||||
password = request.form.get("password", "").strip()
|
||||
password_confirm = request.form.get("password_confirm", "").strip()
|
||||
|
||||
if not username or not password:
|
||||
flash("Логин и пароль обязательны", "danger")
|
||||
return redirect(url_for("main.setup"))
|
||||
|
||||
if password != password_confirm:
|
||||
flash("Пароли не совпадают", "danger")
|
||||
return redirect(url_for("main.setup"))
|
||||
|
||||
if User.query.filter_by(username=username).first():
|
||||
flash("Пользователь с таким логином уже существует", "danger")
|
||||
return redirect(url_for("main.setup"))
|
||||
|
||||
admin = User(username=username, role="admin", is_active=True)
|
||||
admin.set_password(password)
|
||||
db.session.add(admin)
|
||||
db.session.commit()
|
||||
|
||||
flash("Первый администратор создан. Войдите в систему.", "success")
|
||||
return redirect(url_for("main.login"))
|
||||
|
||||
return render_template("setup.html")
|
||||
|
||||
|
||||
@main_bp.route("/login", methods=["GET", "POST"])
|
||||
@limiter.limit("10 per minute", methods=["POST"])
|
||||
def login():
|
||||
if _is_first_run():
|
||||
return redirect(url_for("main.setup"))
|
||||
|
||||
if request.method == "POST":
|
||||
username = request.form.get("username", "").strip()
|
||||
password = request.form.get("password", "").strip()
|
||||
@@ -1341,6 +1384,9 @@ def api_guest_detail(token):
|
||||
|
||||
@main_bp.route("/")
|
||||
def index():
|
||||
if _is_first_run():
|
||||
return redirect(url_for("main.setup"))
|
||||
|
||||
user = current_user()
|
||||
if not user:
|
||||
return redirect(url_for("main.login"))
|
||||
|
||||
Reference in New Issue
Block a user