2017-09-04 00:25:45 +02:00
|
|
|
from typing import Any, Dict, Iterable, List, Optional # noqa
|
2017-08-24 12:26:55 +02:00
|
|
|
|
2017-09-04 00:25:45 +02:00
|
|
|
from ..utils.access_permissions import BaseAccessPermissions
|
2016-12-17 09:30:20 +01:00
|
|
|
from ..utils.auth import has_perm
|
2017-09-04 00:25:45 +02:00
|
|
|
from ..utils.collection import CollectionElement
|
2016-02-11 22:58:32 +01:00
|
|
|
|
2016-02-11 11:29:19 +01:00
|
|
|
|
2016-02-11 22:58:32 +01:00
|
|
|
class ItemAccessPermissions(BaseAccessPermissions):
|
|
|
|
"""
|
|
|
|
Access permissions container for Item and ItemViewSet.
|
|
|
|
"""
|
2016-09-17 22:26:23 +02:00
|
|
|
def check_permissions(self, user):
|
2016-02-11 11:29:19 +01:00
|
|
|
"""
|
2016-02-11 22:58:32 +01:00
|
|
|
Returns True if the user has read access model instances.
|
2016-02-11 11:29:19 +01:00
|
|
|
"""
|
2016-12-17 09:30:20 +01:00
|
|
|
return has_perm(user, 'agenda.can_see')
|
2016-02-11 22:58:32 +01:00
|
|
|
|
2016-03-02 00:46:19 +01:00
|
|
|
def get_serializer_class(self, user=None):
|
2016-02-11 22:58:32 +01:00
|
|
|
"""
|
|
|
|
Returns serializer class.
|
|
|
|
"""
|
|
|
|
from .serializers import ItemSerializer
|
|
|
|
|
|
|
|
return ItemSerializer
|
2016-03-02 00:46:19 +01:00
|
|
|
|
2018-08-15 11:15:54 +02:00
|
|
|
# TODO: In the following method we use full_data['is_hidden'] and
|
|
|
|
# full_data['is_internal'] but this can be out of date.
|
2016-09-17 22:26:23 +02:00
|
|
|
|
2017-09-04 00:25:45 +02:00
|
|
|
def get_restricted_data(
|
|
|
|
self,
|
|
|
|
full_data: List[Dict[str, Any]],
|
|
|
|
user: Optional[CollectionElement]) -> List[Dict[str, Any]]:
|
2016-03-02 00:46:19 +01:00
|
|
|
"""
|
|
|
|
Returns the restricted serialized data for the instance prepared
|
|
|
|
for the user.
|
2017-05-01 23:12:42 +02:00
|
|
|
|
2018-08-15 11:15:54 +02:00
|
|
|
Hidden items can only be seen by managers with can_manage permission.
|
|
|
|
|
2017-05-01 23:12:42 +02:00
|
|
|
We remove comments for non admins/managers and a lot of fields of
|
2018-08-15 11:15:54 +02:00
|
|
|
internal items for users without permission to see internal items.
|
2016-03-02 00:46:19 +01:00
|
|
|
"""
|
2017-04-28 00:50:37 +02:00
|
|
|
def filtered_data(full_data, blocked_keys):
|
|
|
|
"""
|
|
|
|
Returns a new dict like full_data but with all blocked_keys removed.
|
|
|
|
"""
|
|
|
|
whitelist = full_data.keys() - blocked_keys
|
|
|
|
return {key: full_data[key] for key in whitelist}
|
|
|
|
|
2017-05-01 23:12:42 +02:00
|
|
|
# Parse data.
|
2018-08-23 09:48:47 +02:00
|
|
|
if full_data and has_perm(user, 'agenda.can_see'):
|
2018-08-15 11:15:54 +02:00
|
|
|
if has_perm(user, 'agenda.can_manage') and has_perm(user, 'agenda.can_see_internal_items'):
|
2017-05-01 23:12:42 +02:00
|
|
|
# Managers with special permission can see everything.
|
2017-04-28 00:50:37 +02:00
|
|
|
data = full_data
|
2018-08-15 11:15:54 +02:00
|
|
|
elif has_perm(user, 'agenda.can_see_internal_items'):
|
|
|
|
# Non managers with special permission can see everything but
|
|
|
|
# comments and hidden items.
|
|
|
|
data = [full for full in full_data if not full['is_hidden']] # filter hidden items
|
2017-04-28 00:50:37 +02:00
|
|
|
blocked_keys = ('comment',)
|
2018-08-15 11:15:54 +02:00
|
|
|
data = [filtered_data(full, blocked_keys) for full in data] # remove blocked_keys
|
2017-04-28 00:50:37 +02:00
|
|
|
else:
|
2018-08-15 11:15:54 +02:00
|
|
|
# Users without special permission for internal items.
|
2017-05-01 23:12:42 +02:00
|
|
|
|
2018-08-15 11:15:54 +02:00
|
|
|
# In internal and hidden case managers and non managers see only some fields
|
|
|
|
# so that list of speakers is provided regardless. Hidden items can only be seen by managers.
|
2018-08-23 09:48:47 +02:00
|
|
|
# We know that full_data has at least one entry which can be used to parse the keys.
|
2018-08-15 11:15:54 +02:00
|
|
|
blocked_keys_internal_hidden_case = set(full_data[0].keys()) - set((
|
2017-03-03 10:54:41 +01:00
|
|
|
'id',
|
|
|
|
'title',
|
|
|
|
'speakers',
|
|
|
|
'speaker_list_closed',
|
2017-08-24 12:26:55 +02:00
|
|
|
'content_object'))
|
2017-05-01 23:12:42 +02:00
|
|
|
|
2018-08-15 11:15:54 +02:00
|
|
|
# In non internal case managers see everything and non managers see
|
2017-05-01 23:12:42 +02:00
|
|
|
# everything but comments.
|
|
|
|
if has_perm(user, 'agenda.can_manage'):
|
2018-08-15 11:15:54 +02:00
|
|
|
blocked_keys_non_internal_hidden_case = [] # type: Iterable[str]
|
|
|
|
can_see_hidden = True
|
2017-05-01 23:12:42 +02:00
|
|
|
else:
|
2018-08-15 11:15:54 +02:00
|
|
|
blocked_keys_non_internal_hidden_case = ('comment',)
|
|
|
|
can_see_hidden = False
|
2017-05-01 23:12:42 +02:00
|
|
|
|
|
|
|
data = []
|
2017-04-28 00:50:37 +02:00
|
|
|
for full in full_data:
|
2018-08-15 11:15:54 +02:00
|
|
|
if full['is_hidden'] and can_see_hidden:
|
|
|
|
# Same filtering for internal and hidden items
|
|
|
|
data.append(filtered_data(full, blocked_keys_internal_hidden_case))
|
2018-08-22 17:00:08 +02:00
|
|
|
elif full['is_internal']:
|
2018-08-15 11:15:54 +02:00
|
|
|
data.append(filtered_data(full, blocked_keys_internal_hidden_case))
|
|
|
|
else: # agenda item
|
|
|
|
data.append(filtered_data(full, blocked_keys_non_internal_hidden_case))
|
2017-05-01 23:12:42 +02:00
|
|
|
else:
|
|
|
|
data = []
|
|
|
|
|
2017-09-04 00:25:45 +02:00
|
|
|
return data
|
2016-09-17 22:26:23 +02:00
|
|
|
|
2017-09-04 00:25:45 +02:00
|
|
|
def get_projector_data(self, full_data: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
2016-09-17 22:26:23 +02:00
|
|
|
"""
|
|
|
|
Returns the restricted serialized data for the instance prepared
|
|
|
|
for the projector. Removes field 'comment'.
|
|
|
|
"""
|
2017-06-13 21:44:45 +02:00
|
|
|
def filtered_data(full_data, blocked_keys):
|
|
|
|
"""
|
|
|
|
Returns a new dict like full_data but with all blocked_keys removed.
|
|
|
|
"""
|
|
|
|
whitelist = full_data.keys() - blocked_keys
|
|
|
|
return {key: full_data[key] for key in whitelist}
|
|
|
|
|
|
|
|
# Parse data.
|
|
|
|
blocked_keys = ('comment',)
|
|
|
|
data = [filtered_data(full, blocked_keys) for full in full_data]
|
|
|
|
|
2017-09-04 00:25:45 +02:00
|
|
|
return data
|