From ca56b4f8b400b47c21dddedce3ac610156cca69a Mon Sep 17 00:00:00 2001 From: Finn Stutzenstein Date: Mon, 19 Oct 2020 15:29:55 +0200 Subject: [PATCH] Use cache in management commands This requires to have a correct setup when using these commands --- .../commands/changedefaultadminpassword.py | 2 +- .../users/management/commands/changepassword.py | 16 ++++++++++++++++ .../management/commands/createopenslidesuser.py | 4 +++- .../users/management/commands/createsuperuser.py | 6 +++--- server/openslides/users/models.py | 3 +++ 5 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 server/openslides/users/management/commands/changepassword.py diff --git a/server/openslides/users/management/commands/changedefaultadminpassword.py b/server/openslides/users/management/commands/changedefaultadminpassword.py index c34a1d4b6..824034888 100644 --- a/server/openslides/users/management/commands/changedefaultadminpassword.py +++ b/server/openslides/users/management/commands/changedefaultadminpassword.py @@ -24,7 +24,7 @@ class Command(BaseCommand): if user.check_password("admin"): user.set_password(options["password"]) - user.save(skip_autoupdate=True) + user.save() self.stdout.write( self.style.SUCCESS("Password of user admin successfully changed.") ) diff --git a/server/openslides/users/management/commands/changepassword.py b/server/openslides/users/management/commands/changepassword.py new file mode 100644 index 000000000..469908650 --- /dev/null +++ b/server/openslides/users/management/commands/changepassword.py @@ -0,0 +1,16 @@ +import sys + +from django.core.management.base import BaseCommand + + +class Command(BaseCommand): + """ + Overwrites the django auth's changepassword. It does not respect our cache and our + own implementation should be used instead. + """ + + def run_from_argv(self, *args, **kwargs): + self.stderr.write( + "This command is disabled, use insecurepasswordchange instead." + ) + sys.exit(1) diff --git a/server/openslides/users/management/commands/createopenslidesuser.py b/server/openslides/users/management/commands/createopenslidesuser.py index d85bba7bf..69eecbe0f 100644 --- a/server/openslides/users/management/commands/createopenslidesuser.py +++ b/server/openslides/users/management/commands/createopenslidesuser.py @@ -1,5 +1,7 @@ from django.core.management.base import BaseCommand +from openslides.utils.autoupdate import inform_changed_data + from ...models import User @@ -43,11 +45,11 @@ class Command(BaseCommand): user = User.objects.create_user( options["username"], options["password"], - skip_autoupdate=True, **user_data, ) if options["groups_id"].isdigit(): user.groups.add(int(options["groups_id"])) + inform_changed_data(user) self.stdout.write( self.style.SUCCESS(f"Created user {options['username']}.") ) diff --git a/server/openslides/users/management/commands/createsuperuser.py b/server/openslides/users/management/commands/createsuperuser.py index b3c053976..f2d2fb3ae 100644 --- a/server/openslides/users/management/commands/createsuperuser.py +++ b/server/openslides/users/management/commands/createsuperuser.py @@ -11,8 +11,8 @@ class Command(BaseCommand): help = "Creates or resets the admin user." def handle(self, *args, **options): - created = User.objects.create_or_reset_admin_user(skip_autoupdate=True) + created = User.objects.create_or_reset_admin_user() if created: - self.stdout.write("Admin user successfully created.") + self.stdout.write(self.style.SUCCESS("Admin user successfully created.")) else: - self.stdout.write("Admin user successfully reset.") + self.stdout.write(self.style.SUCCESS("Admin user successfully reset.")) diff --git a/server/openslides/users/models.py b/server/openslides/users/models.py index ad3c45fe5..087d196ec 100644 --- a/server/openslides/users/models.py +++ b/server/openslides/users/models.py @@ -22,6 +22,7 @@ from openslides.utils.manager import BaseManager from ..core.config import config from ..utils.auth import GROUP_ADMIN_PK +from ..utils.autoupdate import inform_changed_data from ..utils.models import ( CASCADE_AND_AUTOUPDATE, SET_NULL_AND_AUTOUPDATE, @@ -87,6 +88,8 @@ class UserManager(BaseUserManager): admin.password = make_password(admin.default_password) admin.save(skip_autoupdate=skip_autoupdate) admin.groups.add(GROUP_ADMIN_PK) + if not skip_autoupdate: + inform_changed_data(admin) return created def generate_username(self, first_name, last_name):