From 0d38b784bc2c450202f049a7ede3236ec038fc52 Mon Sep 17 00:00:00 2001 From: FinnStutzenstein Date: Fri, 29 Nov 2019 12:59:34 +0100 Subject: [PATCH] Fixed late autoupdates with deleted users --- openslides/utils/auth.py | 14 +++++++++++--- openslides/utils/consumers.py | 7 ++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/openslides/utils/auth.py b/openslides/utils/auth.py index bfadeb6d9..7407370a0 100644 --- a/openslides/utils/auth.py +++ b/openslides/utils/auth.py @@ -19,6 +19,14 @@ group_collection_string = "users/group" user_collection_string = "users/user" +class UserDoesNotExist(Exception): + """ + This is raised, if some permissions checks are done on not existing users. + """ + + pass + + def get_group_model() -> Model: """ Return the Group model that is active in this project. @@ -78,7 +86,7 @@ async def async_has_perm(user_id: int, perm: str) -> bool: user_collection_string, user_id ) if user_data is None: - raise RuntimeError(f"User with id {user_id} does not exist.") + raise UserDoesNotExist() if GROUP_ADMIN_PK in user_data["groups_id"]: # User in admin group (pk 2) grants all permissions. has_perm = True @@ -92,7 +100,7 @@ async def async_has_perm(user_id: int, perm: str) -> bool: ) if group is None: raise RuntimeError( - f"User is in non existing group with id {group_id}." + f"User {user_id} is in non existing group {group_id}." ) if perm in group["permissions"]: @@ -135,7 +143,7 @@ async def async_in_some_groups(user_id: int, groups: List[int]) -> bool: user_collection_string, user_id ) if user_data is None: - raise RuntimeError(f"User with id {user_id} does not exist.") + raise UserDoesNotExist() if GROUP_ADMIN_PK in user_data["groups_id"]: # User in admin group (pk 2) grants all permissions. in_some_groups = True diff --git a/openslides/utils/consumers.py b/openslides/utils/consumers.py index f8f7b5bfe..610044dc6 100644 --- a/openslides/utils/consumers.py +++ b/openslides/utils/consumers.py @@ -7,7 +7,7 @@ from channels.generic.websocket import AsyncWebsocketConsumer from ..utils.websocket import WEBSOCKET_CHANGE_ID_TOO_HIGH from . import logging -from .auth import async_anonymous_is_enabled +from .auth import UserDoesNotExist, async_anonymous_is_enabled from .autoupdate import AutoupdateFormat from .cache import ChangeIdTooLowError, element_cache, split_element_id from .utils import get_worker_id @@ -149,6 +149,11 @@ class SiteConsumer(ProtocollAsyncJsonWebsocketConsumer): changed_elements = await element_cache.get_all_data_list(user_id) all_data = True deleted_elements: Dict[str, List[int]] = {} + except UserDoesNotExist: + # Maybe the user was deleted, but a websocket connection is still open to the user. + # So we can close this connection and return. + await self.close() + return else: all_data = False deleted_elements = defaultdict(list)