From d57c5bd13264de6a1b238a0d52b2afe22327019c Mon Sep 17 00:00:00 2001 From: FinnStutzenstein Date: Fri, 24 May 2019 12:14:21 +0200 Subject: [PATCH] Fixed updating user data after changing permissions with the new view --- .../users/group-repository.service.ts | 2 +- openslides/core/signals.py | 1 + openslides/users/views.py | 85 ++++++++++--------- 3 files changed, 47 insertions(+), 41 deletions(-) diff --git a/client/src/app/core/repositories/users/group-repository.service.ts b/client/src/app/core/repositories/users/group-repository.service.ts index 8bd4afeea..a1a005cdc 100644 --- a/client/src/app/core/repositories/users/group-repository.service.ts +++ b/client/src/app/core/repositories/users/group-repository.service.ts @@ -82,7 +82,7 @@ export class GroupRepositoryService extends BaseRepository { const set = !group.permissions.includes(perm); - return await this.http.post(`rest/${group.collectionString}/${group.id}/set_permission/`, { + return await this.http.post(`/rest/${group.collectionString}/${group.id}/set_permission/`, { perm: perm, set: set }); diff --git a/openslides/core/signals.py b/openslides/core/signals.py index 6323088f3..12bce2314 100644 --- a/openslides/core/signals.py +++ b/openslides/core/signals.py @@ -42,6 +42,7 @@ def get_permission_change_data(sender, permissions, **kwargs): elif permission.codename == "can_manage_projector": yield core_app.get_model("ProjectorMessage") yield core_app.get_model("Countdown") + yield core_app.get_model("ProjectionDefault") def autoupdate_for_many_to_many_relations(sender, instance, **kwargs): diff --git a/openslides/users/views.py b/openslides/users/views.py index 2cf44f709..42c113095 100644 --- a/openslides/users/views.py +++ b/openslides/users/views.py @@ -1,6 +1,6 @@ import smtplib import textwrap -from typing import List, Set +from typing import Iterable, List, Set, Union from asgiref.sync import async_to_sync from django.conf import settings @@ -357,45 +357,10 @@ class GroupViewSet(ModelViewSet): # Check status code and send 'permission_change' signal. if response.status_code == 200: - - # Delete the user chaches of all affected users - for user in group.user_set.all(): - async_to_sync(element_cache.del_user)(user.pk) - - def diff(full, part): - """ - This helper function calculates the difference of two lists: - The result is a list of all elements of 'full' that are - not in 'part'. - """ - part = set(part) - return [item for item in full if item not in part] - - new_permissions = diff(given_permissions, old_permissions) - - # Some permissions are added. - if new_permissions: - elements: List[Element] = [] - signal_results = permission_change.send( - None, permissions=new_permissions, action="added" - ) - all_full_data = async_to_sync(element_cache.get_all_full_data)() - for __, signal_collections in signal_results: - for cachable in signal_collections: - for full_data in all_full_data.get( - cachable.get_collection_string(), {} - ): - elements.append( - Element( - id=full_data["id"], - collection_string=cachable.get_collection_string(), - full_data=full_data, - disable_history=True, - ) - ) - inform_changed_elements(elements) - - # TODO: Some permissions are deleted. + changed_permissions = list( + set(old_permissions).symmetric_difference(set(given_permissions)) + ) + self.inform_permission_change(group, changed_permissions) return response @@ -449,10 +414,50 @@ class GroupViewSet(ModelViewSet): group.permissions.add(permission) else: group.permissions.remove(permission) + self.inform_permission_change(group, permission) inform_changed_data(group) return Response() + def inform_permission_change( + self, + group: Group, + changed_permissions: Union[None, Permission, Iterable[Permission]], + ) -> None: + """ + Updates every users, if some permission changes. For this, every affected collection + is fetched via the permission_change signal and every object of the collection passed + into the cache/autoupdate system. Also the personal (restrcited) cache of every affected + user (all users of the group) will be deleted, so it is rebuild after this permission change. + """ + if isinstance(changed_permissions, Permission): + changed_permissions = [changed_permissions] + + if not changed_permissions: + return # either None or empty list. + + # Delete the user chaches of all affected users + for user in group.user_set.all(): + async_to_sync(element_cache.del_user)(user.pk) + + elements: List[Element] = [] + signal_results = permission_change.send(None, permissions=changed_permissions) + all_full_data = async_to_sync(element_cache.get_all_full_data)() + for _, signal_collections in signal_results: + for cachable in signal_collections: + for full_data in all_full_data.get( + cachable.get_collection_string(), {} + ): + elements.append( + Element( + id=full_data["id"], + collection_string=cachable.get_collection_string(), + full_data=full_data, + disable_history=True, + ) + ) + inform_changed_elements(elements) + class PersonalNoteViewSet(ModelViewSet): """