38 lines
1.2 KiB
Python
38 lines
1.2 KiB
Python
# auth/setup_admin.py
|
|
|
|
import mysql.connector
|
|
from werkzeug.security import generate_password_hash
|
|
from util.logger import logger
|
|
|
|
def initialize_admin_user(config):
|
|
try:
|
|
conn = mysql.connector.connect(**config)
|
|
cursor = conn.cursor()
|
|
|
|
cursor.execute("""
|
|
CREATE TABLE IF NOT EXISTS users (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
username VARCHAR(50) NOT NULL UNIQUE,
|
|
password VARCHAR(255) NOT NULL,
|
|
role VARCHAR(20) NOT NULL
|
|
)
|
|
""")
|
|
|
|
# 🔍 Prüfe, ob es bereits einen Admin gibt
|
|
cursor.execute("SELECT COUNT(*) FROM users WHERE role = 'admin'")
|
|
if cursor.fetchone()[0] == 0:
|
|
cursor.execute(
|
|
"INSERT INTO users (username, password, role) VALUES (%s, %s, %s)",
|
|
("admin", generate_password_hash("admin"), "admin")
|
|
)
|
|
logger.info("Admin user created (username: admin, password: admin)")
|
|
else:
|
|
logger.info("Admin user already exists.")
|
|
|
|
conn.commit()
|
|
cursor.close()
|
|
conn.close()
|
|
|
|
except Exception as e:
|
|
logger.error(f"[Admin-Setup] Failed to initialize admin user: {e}")
|