Add user auth, personal cabinet, admin panel and first admin bootstrap
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import os
|
||||
|
||||
from sqlalchemy import inspect, text
|
||||
|
||||
from app import db
|
||||
from app.models import User
|
||||
|
||||
|
||||
def ensure_schema():
|
||||
inspector = inspect(db.engine)
|
||||
tables = inspector.get_table_names()
|
||||
|
||||
if "photos" in tables:
|
||||
columns = {col["name"] for col in inspector.get_columns("photos")}
|
||||
if "user_id" not in columns:
|
||||
db.session.execute(
|
||||
text("ALTER TABLE photos ADD COLUMN user_id INTEGER REFERENCES users(id)")
|
||||
)
|
||||
db.session.commit()
|
||||
|
||||
|
||||
def create_first_admin(app):
|
||||
username = os.getenv("ADMIN_USERNAME", "").strip()
|
||||
email = os.getenv("ADMIN_EMAIL", "").strip()
|
||||
password = os.getenv("ADMIN_PASSWORD", "").strip()
|
||||
|
||||
if not username or not email or not password:
|
||||
app.logger.info("ADMIN_* env vars not set — first admin was not created automatically")
|
||||
return None
|
||||
|
||||
if User.query.filter_by(is_admin=True).first():
|
||||
return None
|
||||
|
||||
if User.query.filter_by(username=username).first():
|
||||
user = User.query.filter_by(username=username).first()
|
||||
user.is_admin = True
|
||||
user.set_password(password)
|
||||
db.session.commit()
|
||||
app.logger.info("Existing user '%s' promoted to admin", username)
|
||||
return user
|
||||
|
||||
user = User(username=username, email=email, is_admin=True)
|
||||
user.set_password(password)
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
app.logger.info("First admin '%s' created", username)
|
||||
return user
|
||||
Reference in New Issue
Block a user