2019-04-23 16:57:35 +02:00
|
|
|
from typing import Any, Dict, List
|
2017-08-24 12:26:55 +02:00
|
|
|
|
2017-09-04 00:25:45 +02:00
|
|
|
from ..utils.access_permissions import BaseAccessPermissions
|
2018-11-01 17:30:18 +01:00
|
|
|
from ..utils.auth import async_has_perm
|
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.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
|
|
|
base_permission = "agenda.can_see"
|
2016-02-11 22:58:32 +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.
|
2018-11-01 17:30:18 +01:00
|
|
|
async def get_restricted_data(
|
2019-01-06 16:22:33 +01:00
|
|
|
self, full_data: List[Dict[str, Any]], user_id: int
|
|
|
|
) -> List[Dict[str, Any]]:
|
2016-03-02 00:46:19 +01:00
|
|
|
"""
|
|
|
|
Returns the restricted serialized data for the instance prepared
|
2019-04-23 16:57:35 +02:00
|
|
|
for the user. If the user does not have agenda.can_see, no data will
|
|
|
|
be retuned.
|
2017-05-01 23:12:42 +02:00
|
|
|
|
2019-04-23 16:57:35 +02:00
|
|
|
Hidden items can only be seen by managers with can_manage permission. If a user
|
|
|
|
does not have this permission, he is not allowed to see comments.
|
2018-08-15 11:15:54 +02:00
|
|
|
|
2019-04-23 16:57:35 +02:00
|
|
|
Internal items can only be seen by users with can_see_internal_items. If a user
|
|
|
|
does not have this permission, he is not allowed to see the duration.
|
2016-03-02 00:46:19 +01:00
|
|
|
"""
|
2019-01-06 16:22:33 +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.
|
2019-01-06 16:22:33 +01:00
|
|
|
if full_data and await async_has_perm(user_id, "agenda.can_see"):
|
2019-04-23 16:57:35 +02:00
|
|
|
# Assume the user has all permissions. Restrict this below.
|
|
|
|
data = full_data
|
|
|
|
|
|
|
|
blocked_keys: List[str] = []
|
|
|
|
|
|
|
|
# Restrict data for non managers
|
|
|
|
if not await async_has_perm(user_id, "agenda.can_manage"):
|
2019-01-06 16:22:33 +01:00
|
|
|
data = [
|
2019-04-23 16:57:35 +02:00
|
|
|
full for full in data if not full["is_hidden"]
|
2019-01-06 16:22:33 +01:00
|
|
|
] # filter hidden items
|
2019-04-23 16:57:35 +02:00
|
|
|
blocked_keys.append("comment")
|
|
|
|
|
|
|
|
# Restrict data for users without can_see_internal_items
|
|
|
|
if not await async_has_perm(user_id, "agenda.can_see_internal_items"):
|
|
|
|
data = [full for full in data if not full["is_internal"]]
|
|
|
|
blocked_keys.append("duration")
|
|
|
|
|
|
|
|
if len(blocked_keys) > 0:
|
|
|
|
data = [filtered_data(full, blocked_keys) for full in data]
|
2017-05-01 23:12:42 +02:00
|
|
|
else:
|
|
|
|
data = []
|
|
|
|
|
2017-09-04 00:25:45 +02:00
|
|
|
return data
|
2019-04-23 16:57:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ListOfSpeakersAccessPermissions(BaseAccessPermissions):
|
|
|
|
"""
|
|
|
|
Access permissions container for ListOfSpeakers and ListOfSpeakersViewSet.
|
|
|
|
No data will be restricted, because everyone can see the list of speakers
|
|
|
|
at any time.
|
|
|
|
"""
|
|
|
|
|
|
|
|
base_permission = "agenda.can_see_list_of_speakers"
|