Fixed updating user data after changing permissions with the new view

This commit is contained in:
FinnStutzenstein 2019-05-24 12:14:21 +02:00
parent 309b8118f6
commit d57c5bd132
3 changed files with 47 additions and 41 deletions

View File

@ -82,7 +82,7 @@ export class GroupRepositoryService extends BaseRepository<ViewGroup, Group, Gro
*/ */
public async togglePerm(group: ViewGroup, perm: string): Promise<void> { public async togglePerm(group: ViewGroup, perm: string): Promise<void> {
const set = !group.permissions.includes(perm); 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, perm: perm,
set: set set: set
}); });

View File

@ -42,6 +42,7 @@ def get_permission_change_data(sender, permissions, **kwargs):
elif permission.codename == "can_manage_projector": elif permission.codename == "can_manage_projector":
yield core_app.get_model("ProjectorMessage") yield core_app.get_model("ProjectorMessage")
yield core_app.get_model("Countdown") yield core_app.get_model("Countdown")
yield core_app.get_model("ProjectionDefault")
def autoupdate_for_many_to_many_relations(sender, instance, **kwargs): def autoupdate_for_many_to_many_relations(sender, instance, **kwargs):

View File

@ -1,6 +1,6 @@
import smtplib import smtplib
import textwrap import textwrap
from typing import List, Set from typing import Iterable, List, Set, Union
from asgiref.sync import async_to_sync from asgiref.sync import async_to_sync
from django.conf import settings from django.conf import settings
@ -357,45 +357,10 @@ class GroupViewSet(ModelViewSet):
# Check status code and send 'permission_change' signal. # Check status code and send 'permission_change' signal.
if response.status_code == 200: if response.status_code == 200:
changed_permissions = list(
# Delete the user chaches of all affected users set(old_permissions).symmetric_difference(set(given_permissions))
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)() self.inform_permission_change(group, changed_permissions)
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.
return response return response
@ -449,10 +414,50 @@ class GroupViewSet(ModelViewSet):
group.permissions.add(permission) group.permissions.add(permission)
else: else:
group.permissions.remove(permission) group.permissions.remove(permission)
self.inform_permission_change(group, permission)
inform_changed_data(group) inform_changed_data(group)
return Response() 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): class PersonalNoteViewSet(ModelViewSet):
""" """