From 7b0e166adfa2cb172122626fd9c69d9e862b1a70 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 26 Jul 2025 22:54:11 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=84=20Create=20fresh=20admin=20user=20?= =?UTF-8?q?reset=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Delete all existing admin users (admin@netcop.ai, admin@quantumtaskai.com) - Create completely fresh admin user with proper Django methods - Test authentication to verify working credentials - Add detailed logging for troubleshooting This ensures clean admin state without any database artifacts. Admin Credentials: - Email: admin@quantumtaskai.com - Username: admin - Password: QuantumAI2024! šŸ¤– Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- core/management/commands/check_admin.py | 73 +++++++++++++++++++++ core/management/commands/reset_admin.py | 86 +++++++++++++++++++++++++ railway.json | 2 +- 3 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 core/management/commands/check_admin.py create mode 100644 core/management/commands/reset_admin.py diff --git a/core/management/commands/check_admin.py b/core/management/commands/check_admin.py new file mode 100644 index 0000000..e9b127d --- /dev/null +++ b/core/management/commands/check_admin.py @@ -0,0 +1,73 @@ +from django.core.management.base import BaseCommand +from django.contrib.auth import get_user_model + +User = get_user_model() + + +class Command(BaseCommand): + help = 'Check and fix admin user status' + + def handle(self, *args, **options): + # Check both possible admin emails + possible_emails = ['admin@netcop.ai', 'admin@quantumtaskai.com'] + username = 'admin' + password = 'QuantumAI2024!' + + user = None + found_email = None + + # Try to find existing admin user + for email in possible_emails: + try: + user = User.objects.get(email=email) + found_email = email + break + except User.DoesNotExist: + continue + + if user: + # User found with one of the emails + self.stdout.write(f"āœ… User found: {user.email}") + self.stdout.write(f"Username: {user.username}") + self.stdout.write(f"Is superuser: {user.is_superuser}") + self.stdout.write(f"Is staff: {user.is_staff}") + self.stdout.write(f"Is active: {user.is_active}") + self.stdout.write(f"Email verified: {user.email_verified}") + + # Fix user permissions if needed + if not user.is_superuser or not user.is_staff: + user.is_superuser = True + user.is_staff = True + user.is_active = True + user.save() + self.stdout.write("šŸ”§ Fixed user permissions") + + # Reset password to ensure it's correct + user.set_password(password) + user.save() + self.stdout.write("šŸ”‘ Password reset successfully") + + # Show login instructions + self.stdout.write("\nšŸ“ Login Instructions:") + self.stdout.write(f"URL: https://quantum-ai.up.railway.app/admin/") + self.stdout.write(f"Email: {found_email}") + self.stdout.write(f"Username: {username}") + self.stdout.write(f"Password: {password}") + + else: + self.stdout.write("āŒ Admin user not found! Creating new admin user...") + + # Create new admin user with preferred email + preferred_email = 'admin@quantumtaskai.com' + user = User.objects.create_superuser( + username=username, + email=preferred_email, + password=password, + ) + user.add_balance(100, "Initial admin balance") + + self.stdout.write("āœ… New admin user created successfully!") + self.stdout.write(f"Email: {preferred_email}") + self.stdout.write(f"Username: {username}") + self.stdout.write(f"Password: {password}") + self.stdout.write(f"Balance: {user.wallet_balance} AED") \ No newline at end of file diff --git a/core/management/commands/reset_admin.py b/core/management/commands/reset_admin.py new file mode 100644 index 0000000..996cfc6 --- /dev/null +++ b/core/management/commands/reset_admin.py @@ -0,0 +1,86 @@ +from django.core.management.base import BaseCommand +from django.contrib.auth import get_user_model + +User = get_user_model() + + +class Command(BaseCommand): + help = 'Reset admin user - delete existing and create fresh' + + def handle(self, *args, **options): + email = 'admin@quantumtaskai.com' + username = 'admin' + password = 'QuantumAI2024!' + + self.stdout.write("šŸ”„ Resetting admin user...") + + # Delete ANY existing admin users (all possible emails/usernames) + deleted_count = 0 + + # Check for users with admin emails + admin_emails = ['admin@quantumtaskai.com', 'admin@netcop.ai'] + for admin_email in admin_emails: + try: + user = User.objects.get(email=admin_email) + user.delete() + deleted_count += 1 + self.stdout.write(f"āŒ Deleted user with email: {admin_email}") + except User.DoesNotExist: + pass + + # Check for users with admin username + try: + user = User.objects.get(username=username) + if user.email not in admin_emails: # Don't double-delete + user.delete() + deleted_count += 1 + self.stdout.write(f"āŒ Deleted user with username: {username}") + except User.DoesNotExist: + pass + + self.stdout.write(f"šŸ—‘ļø Deleted {deleted_count} existing admin user(s)") + + # Create fresh admin user + self.stdout.write("šŸ†• Creating fresh admin user...") + + user = User.objects.create_superuser( + username=username, + email=email, + password=password, + ) + + # Add initial balance + user.add_balance(100, "Initial admin balance") + + # Verify user was created correctly + user.refresh_from_db() + + self.stdout.write("āœ… Fresh admin user created successfully!") + self.stdout.write(f"šŸ“§ Email: {user.email}") + self.stdout.write(f"šŸ‘¤ Username: {user.username}") + self.stdout.write(f"šŸ” Password: {password}") + self.stdout.write(f"⚔ Is superuser: {user.is_superuser}") + self.stdout.write(f"šŸ‘„ Is staff: {user.is_staff}") + self.stdout.write(f"āœ… Is active: {user.is_active}") + self.stdout.write(f"šŸ’° Balance: {user.wallet_balance} AED") + + self.stdout.write("\nšŸŽÆ Login Instructions:") + self.stdout.write("URL: https://quantum-ai.up.railway.app/admin/") + self.stdout.write(f"Email: {email}") + self.stdout.write(f"Username: {username}") + self.stdout.write(f"Password: {password}") + + self.stdout.write("\nšŸ” Authentication Test:") + # Test authentication + from django.contrib.auth import authenticate + auth_user = authenticate(username=email, password=password) + if auth_user: + self.stdout.write("āœ… Email authentication: WORKING") + else: + self.stdout.write("āŒ Email authentication: FAILED") + + auth_user = authenticate(username=username, password=password) + if auth_user: + self.stdout.write("āœ… Username authentication: WORKING") + else: + self.stdout.write("āŒ Username authentication: FAILED") \ No newline at end of file diff --git a/railway.json b/railway.json index d2a7073..d48c583 100644 --- a/railway.json +++ b/railway.json @@ -4,7 +4,7 @@ "builder": "NIXPACKS" }, "deploy": { - "startCommand": "python manage.py migrate --run-syncdb; python manage.py populate_agents; python manage.py create_user admin@quantumtaskai.com QuantumAI2024! --username admin --superuser --balance 100 || true; python manage.py collectstatic --noinput && gunicorn netcop_hub.wsgi:application --bind 0.0.0.0:$PORT --workers 1 --timeout 60", + "startCommand": "python manage.py migrate --run-syncdb; python manage.py populate_agents; python manage.py reset_admin; python manage.py collectstatic --noinput && gunicorn netcop_hub.wsgi:application --bind 0.0.0.0:$PORT --workers 1 --timeout 60", "restartPolicyType": "ON_FAILURE", "restartPolicyMaxRetries": 3 }