Files
service-finder/backend/app/tests_internal/fixes/final_admin_fix.py

82 lines
3.2 KiB
Python
Executable File

# /opt/docker/dev/service_finder/backend/app/final_admin_fix.py
import asyncio
import uuid
from sqlalchemy import text, select
from app.database import AsyncSessionLocal
from app.models.identity import User, Person, UserRole
from app.core.security import get_password_hash
async def run_fix():
print("\n" + ""*50)
print("🛠️ ADMIN RENDSZERJAVÍTÁS ÉS INICIALIZÁLÁS (MB2.0)")
print(""*50)
async with AsyncSessionLocal() as db:
# 1. LOGIKA: Séma ellenőrzése az 'identity' névtérben
# Az MB2.0-ban a felhasználók már nem a 'data', hanem az 'identity' sémában vannak.
check_query = text("""
SELECT column_name FROM information_schema.columns
WHERE table_schema = 'identity' AND table_name = 'users'
""")
res = await db.execute(check_query)
cols = [r[0] for r in res.fetchall()]
if not cols:
print("❌ HIBA: Az 'identity.users' tábla nem található. Futtasd az Alembic migrációt!")
return
if "hashed_password" not in cols:
print("❌ HIBA: A 'hashed_password' oszlop hiányzik. Az adatbázis sémája elavult.")
return
# 2. LOGIKA: Admin keresése
admin_email = "admin@profibot.hu"
stmt = select(User).where(User.email == admin_email)
existing_res = await db.execute(stmt)
existing_admin = existing_res.scalar_one_or_none()
if existing_admin:
print(f"⚠️ Információ: A(z) {admin_email} felhasználó már létezik.")
# Opcionális: Jelszó kényszerített frissítése, ha elfelejtetted
# existing_admin.hashed_password = get_password_hash("Admin123!")
# await db.commit()
else:
try:
# 3. LOGIKA: Person és User létrehozása (MB2.0 Standard)
# Előbb létrehozzuk a fizikai személyt
new_person = Person(
id_uuid=uuid.uuid4(),
first_name="Rendszer",
last_name="Adminisztrátor",
is_active=True
)
db.add(new_person)
await db.flush() # ID lekérése a mentés előtt
# Létrehozzuk a felhasználói fiókot az Admin role-al
new_admin = User(
email=admin_email,
hashed_password=get_password_hash("Admin123!"),
person_id=new_person.id,
role=UserRole.superadmin, # MB2.0 enum érték
is_active=True,
is_deleted=False,
preferred_language="hu"
)
db.add(new_admin)
await db.commit()
print(f"✅ SIKER: Superadmin létrehozva!")
print(f" 📧 Email: {admin_email}")
print(f" 🔑 Jelszó: Admin123!")
except Exception as e:
print(f"❌ HIBA a mentés során: {e}")
await db.rollback()
print("\n" + ""*50)
print("🏁 JAVÍTÁSI FOLYAMAT BEFEJEZŐDÖTT")
print(""*50 + "\n")
if __name__ == "__main__":
asyncio.run(run_fix())