perf: 移动到信号中

pull/7950/head
ibuler 2022-03-21 18:06:33 +08:00 committed by Jiangjie.Bai
parent 87c6eec619
commit 54fd1fb0c8
2 changed files with 14 additions and 0 deletions

View File

@ -320,6 +320,7 @@ class RoleMixin:
_system_roles = None
PERM_CACHE_KEY = 'USER_PERMS_{}_{}'
_is_superuser = None
_update_superuser = False
@lazyproperty
def roles(self):
@ -367,6 +368,8 @@ class RoleMixin:
@is_superuser.setter
def is_superuser(self, value):
self._is_superuser = value
self._update_superuser = True
if value:
self.system_roles.add_role_system_admin()
else:

View File

@ -60,9 +60,20 @@ def save_passwd_change(sender, instance: User, **kwargs):
)
def update_role_superuser_if_need(user):
if not user._update_superuser:
return
value = user._is_superuser
if value:
user.system_roles.add_role_system_admin()
else:
user.system_roles.remove_role_system_admin()
@receiver(post_save, sender=User)
@on_transaction_commit
def on_user_create_set_default_system_role(sender, instance, created, **kwargs):
update_role_superuser_if_need(instance)
if not created:
return
has_system_role = instance.system_roles.all().exists()