This commit is contained in:
parent
bce4c91002
commit
509231966b
@ -29,22 +29,33 @@ def create_admin_user():
|
|||||||
|
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
try:
|
try:
|
||||||
|
# First, delete any old admin users with different emails
|
||||||
|
old_admins = db.query(User).filter(
|
||||||
|
User.is_admin == True,
|
||||||
|
User.email != settings.admin_email
|
||||||
|
).all()
|
||||||
|
|
||||||
|
if old_admins:
|
||||||
|
print(f"🗑️ Removing old admin users...")
|
||||||
|
for old_admin in old_admins:
|
||||||
|
print(f" - Deleting old admin: {old_admin.email}")
|
||||||
|
db.delete(old_admin)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
# Check if admin with current email exists
|
# Check if admin with current email exists
|
||||||
admin = db.query(User).filter(User.email == settings.admin_email).first()
|
admin = db.query(User).filter(User.email == settings.admin_email).first()
|
||||||
|
|
||||||
if admin:
|
if admin:
|
||||||
# Admin exists, update password if needed
|
# Admin exists, update password and details
|
||||||
admin.hashed_password = get_password_hash(settings.admin_password)
|
admin.hashed_password = get_password_hash(settings.admin_password)
|
||||||
admin.full_name = settings.admin_full_name
|
admin.full_name = settings.admin_full_name
|
||||||
|
admin.is_active = True
|
||||||
|
admin.is_admin = True
|
||||||
db.commit()
|
db.commit()
|
||||||
print(f"ℹ️ Admin user updated: {settings.admin_email}")
|
print(f"✅ Admin user updated: {settings.admin_email}")
|
||||||
|
print(f"📧 Email: {settings.admin_email}")
|
||||||
|
print(f"🔐 Password: {settings.admin_password}")
|
||||||
else:
|
else:
|
||||||
# Delete any other admin users (old ones with different emails)
|
|
||||||
old_admins = db.query(User).filter(User.is_admin == True).all()
|
|
||||||
for old_admin in old_admins:
|
|
||||||
db.delete(old_admin)
|
|
||||||
db.commit()
|
|
||||||
|
|
||||||
# Create new admin user
|
# Create new admin user
|
||||||
admin = User(
|
admin = User(
|
||||||
email=settings.admin_email,
|
email=settings.admin_email,
|
||||||
@ -52,14 +63,17 @@ def create_admin_user():
|
|||||||
hashed_password=get_password_hash(settings.admin_password),
|
hashed_password=get_password_hash(settings.admin_password),
|
||||||
is_admin=True,
|
is_admin=True,
|
||||||
is_active=True,
|
is_active=True,
|
||||||
must_change_password=True # Force password change on first login
|
must_change_password=False # Let user decide when to change
|
||||||
)
|
)
|
||||||
db.add(admin)
|
db.add(admin)
|
||||||
db.commit()
|
db.commit()
|
||||||
print(f"✅ Admin user created: {settings.admin_email}")
|
print(f"✅ Admin user created: {settings.admin_email}")
|
||||||
print(f"⚠️ Default password: {settings.admin_password} (CHANGE THIS!)")
|
print(f"📧 Email: {settings.admin_email}")
|
||||||
|
print(f"🔐 Password: {settings.admin_password}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"❌ Error creating admin user: {e}")
|
print(f"❌ Error creating admin user: {e}")
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
db.rollback()
|
db.rollback()
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
db.close()
|
||||||
|
|||||||
@ -7,5 +7,6 @@ pydantic>=2.5.0
|
|||||||
pydantic-settings>=2.1.0
|
pydantic-settings>=2.1.0
|
||||||
python-multipart>=0.0.6
|
python-multipart>=0.0.6
|
||||||
python-jose[cryptography]>=3.3.0
|
python-jose[cryptography]>=3.3.0
|
||||||
passlib[bcrypt]>=1.7.4
|
passlib>=1.7.4
|
||||||
|
bcrypt>=3.2.0,<5.0.0
|
||||||
email-validator>=2.1.0
|
email-validator>=2.1.0
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user