From d606895d11c3231cb082df3fdbc529a85962fd2e Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Fri, 12 Feb 2016 19:46:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=87=8D=E7=BD=AE=20root=20?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=AF=86=E7=A0=81=E7=9A=84=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/management/commands/initadmin.py | 36 ++++++++++++++++++++------ 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/utils/management/commands/initadmin.py b/utils/management/commands/initadmin.py index 64cc2672..d23e7f18 100644 --- a/utils/management/commands/initadmin.py +++ b/utils/management/commands/initadmin.py @@ -6,11 +6,31 @@ from utils.shortcuts import rand_str class Command(BaseCommand): def handle(self, *args, **options): - user = User.objects.create(username="root", real_name="root", email="root@oj.com", admin_type=SUPER_ADMIN) - rand_password = rand_str(length=6) - user.set_password(rand_password) - user.save() - UserProfile.objects.create(user=user) - self.stdout.write(self.style.SUCCESS("Successfully created super admin user.\nUsername: root\nPassword: %s\n" - "Remember to change password and turn on two factors auth " - "after installation." % rand_password)) + try: + admin = User.objects.get(username="root") + if admin.admin_type == SUPER_ADMIN: + self.stdout.write(self.style.WARNING("Super admin user 'root' already exists, " + "would you like to reset it's password?\n" + "Input yes to confirm: ")) + if raw_input() == "yes": + rand_password = rand_str(length=6) + admin.set_password(rand_password) + admin.save() + self.stdout.write(self.style.SUCCESS("Successfully created super admin user password.\n" + "Username: root\nPassword: %s\n" + "Remember to change password and turn on two factors auth " + "after installation." % rand_password)) + else: + self.stdout.write(self.style.SUCCESS("Nothing happened")) + else: + self.stdout.write(self.style.ERROR("User 'root' is not super admin.")) + except User.DoesNotExist: + user = User.objects.create(username="root", real_name="root", email="root@oj.com", admin_type=SUPER_ADMIN) + rand_password = rand_str(length=6) + user.set_password(rand_password) + user.save() + UserProfile.objects.create(user=user) + self.stdout.write(self.style.SUCCESS("Successfully created super admin user.\n" + "Username: root\nPassword: %s\n" + "Remember to change password and turn on two factors auth " + "after installation." % rand_password))