2017-09-04 00:25:45 +02:00
|
|
|
from typing import Any, Dict, List, Optional
|
2017-08-24 12:26:55 +02:00
|
|
|
|
2017-01-26 15:34:24 +01:00
|
|
|
from django.contrib.auth.models import AnonymousUser
|
|
|
|
|
2017-04-10 16:28:38 +02:00
|
|
|
from ..core.signals import user_data_required
|
2017-09-04 00:25:45 +02:00
|
|
|
from ..utils.access_permissions import BaseAccessPermissions # noqa
|
2017-01-26 15:34:24 +01:00
|
|
|
from ..utils.auth import anonymous_is_enabled, has_perm
|
2017-09-04 00:25:45 +02:00
|
|
|
from ..utils.collection import CollectionElement
|
2016-02-11 22:58:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
class UserAccessPermissions(BaseAccessPermissions):
|
|
|
|
"""
|
|
|
|
Access permissions container for User and UserViewSet.
|
|
|
|
"""
|
2016-09-17 22:26:23 +02:00
|
|
|
def check_permissions(self, user):
|
2016-02-11 22:58:32 +01:00
|
|
|
"""
|
2017-02-10 14:51:44 +01:00
|
|
|
Every user has read access for their model instnces.
|
2016-02-11 22:58:32 +01:00
|
|
|
"""
|
2017-02-10 14:51:44 +01:00
|
|
|
return True
|
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 different serializer classes with respect user's permissions.
|
|
|
|
"""
|
2016-09-18 16:00:31 +02:00
|
|
|
from .serializers import UserFullSerializer
|
2016-02-11 22:58:32 +01:00
|
|
|
|
2016-09-18 16:00:31 +02:00
|
|
|
return UserFullSerializer
|
2016-03-02 00:46:19 +01: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. Removes several fields for non admins so that they do
|
2016-08-31 16:53:02 +02:00
|
|
|
not get the fields they should not get.
|
2016-03-02 00:46:19 +01:00
|
|
|
"""
|
2016-08-31 16:53:02 +02:00
|
|
|
from .serializers import USERCANSEESERIALIZER_FIELDS, USERCANSEEEXTRASERIALIZER_FIELDS
|
2016-03-02 00:46:19 +01:00
|
|
|
|
2017-05-01 23:12:42 +02:00
|
|
|
def filtered_data(full_data, whitelist):
|
2017-04-28 00:50:37 +02:00
|
|
|
"""
|
2017-05-01 23:12:42 +02:00
|
|
|
Returns a new dict like full_data but only with whitelisted keys.
|
2017-04-28 00:50:37 +02:00
|
|
|
"""
|
2017-05-01 23:12:42 +02:00
|
|
|
return {key: full_data[key] for key in whitelist}
|
2017-04-28 00:50:37 +02:00
|
|
|
|
2018-07-09 23:22:26 +02:00
|
|
|
# We have five sets of data to be sent:
|
|
|
|
# * full data i. e. all fields (including session_auth_hash),
|
|
|
|
# * all data i. e. all fields but not session_auth_hash,
|
|
|
|
# * many data i. e. all fields but not the default password and session_auth_hash,
|
|
|
|
# * little data i. e. all fields but not the default password, session_auth_hash, comments and active status,
|
2017-05-01 23:12:42 +02:00
|
|
|
# * no data.
|
|
|
|
|
2018-07-09 23:22:26 +02:00
|
|
|
# Prepare field set for users with "all" data, "many" data and with "little" data.
|
|
|
|
all_data_fields = set(USERCANSEEEXTRASERIALIZER_FIELDS)
|
|
|
|
all_data_fields.add('groups_id')
|
|
|
|
all_data_fields.discard('groups')
|
|
|
|
all_data_fields.add('default_password')
|
|
|
|
many_data_fields = all_data_fields.copy()
|
|
|
|
many_data_fields.discard('default_password')
|
2017-05-01 23:12:42 +02:00
|
|
|
litte_data_fields = set(USERCANSEESERIALIZER_FIELDS)
|
|
|
|
litte_data_fields.add('groups_id')
|
|
|
|
litte_data_fields.discard('groups')
|
2016-09-17 22:26:23 +02:00
|
|
|
|
|
|
|
# Check user permissions.
|
2016-12-17 09:30:20 +01:00
|
|
|
if has_perm(user, 'users.can_see_name'):
|
|
|
|
if has_perm(user, 'users.can_see_extra_data'):
|
|
|
|
if has_perm(user, 'users.can_manage'):
|
2018-07-09 23:22:26 +02:00
|
|
|
data = [filtered_data(full, all_data_fields) for full in full_data]
|
2016-09-17 22:26:23 +02:00
|
|
|
else:
|
2017-05-01 23:12:42 +02:00
|
|
|
data = [filtered_data(full, many_data_fields) for full in full_data]
|
2016-09-17 22:26:23 +02:00
|
|
|
else:
|
2017-05-01 23:12:42 +02:00
|
|
|
data = [filtered_data(full, litte_data_fields) for full in full_data]
|
2016-09-17 22:26:23 +02:00
|
|
|
else:
|
2017-05-01 23:12:42 +02:00
|
|
|
# Build a list of users, that can be seen without any permissions (with little fields).
|
|
|
|
|
|
|
|
user_ids = set()
|
|
|
|
|
|
|
|
# Everybody can see himself. Also everybody can see every user
|
|
|
|
# that is required e. g. as speaker, motion submitter or
|
|
|
|
# assignment candidate.
|
|
|
|
|
|
|
|
# Add oneself.
|
2017-04-28 00:50:37 +02:00
|
|
|
if user is not None:
|
2017-05-01 23:12:42 +02:00
|
|
|
user_ids.add(user.id)
|
2017-04-28 00:50:37 +02:00
|
|
|
|
2017-05-01 23:12:42 +02:00
|
|
|
# Get a list of all users, that are required by another app.
|
2017-04-10 16:28:38 +02:00
|
|
|
receiver_responses = user_data_required.send(
|
|
|
|
sender=self.__class__,
|
2017-05-01 23:12:42 +02:00
|
|
|
request_user=user)
|
2017-04-10 16:28:38 +02:00
|
|
|
for receiver, response in receiver_responses:
|
2017-05-01 23:12:42 +02:00
|
|
|
user_ids.update(response)
|
2016-09-17 22:26:23 +02:00
|
|
|
|
2017-05-01 23:12:42 +02:00
|
|
|
# Parse data.
|
2017-04-28 00:50:37 +02:00
|
|
|
data = [
|
2017-05-01 23:12:42 +02:00
|
|
|
filtered_data(full, litte_data_fields)
|
2017-04-28 00:50:37 +02:00
|
|
|
for full
|
|
|
|
in full_data
|
2017-05-01 23:12:42 +02:00
|
|
|
if full['id'] in user_ids]
|
2017-04-28 00:50:37 +02:00
|
|
|
|
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 several fields.
|
|
|
|
"""
|
|
|
|
from .serializers import USERCANSEESERIALIZER_FIELDS
|
|
|
|
|
2017-06-13 21:44:45 +02:00
|
|
|
def filtered_data(full_data, whitelist):
|
|
|
|
"""
|
|
|
|
Returns a new dict like full_data but only with whitelisted keys.
|
|
|
|
"""
|
|
|
|
return {key: full_data[key] for key in whitelist}
|
|
|
|
|
|
|
|
# Parse data.
|
|
|
|
litte_data_fields = set(USERCANSEESERIALIZER_FIELDS)
|
|
|
|
litte_data_fields.add('groups_id')
|
|
|
|
litte_data_fields.discard('groups')
|
|
|
|
data = [filtered_data(full, litte_data_fields) for full in full_data]
|
|
|
|
|
2017-09-04 00:25:45 +02:00
|
|
|
return data
|
2016-12-17 09:30:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
class GroupAccessPermissions(BaseAccessPermissions):
|
|
|
|
"""
|
|
|
|
Access permissions container for Groups. Everyone can see them
|
|
|
|
"""
|
|
|
|
def check_permissions(self, user):
|
|
|
|
"""
|
|
|
|
Returns True if the user has read access model instances.
|
|
|
|
"""
|
|
|
|
# Every authenticated user can retrieve groups. Anonymous users can do
|
|
|
|
# so if they are enabled.
|
2017-01-26 15:34:24 +01:00
|
|
|
return not isinstance(user, AnonymousUser) or anonymous_is_enabled()
|
2016-12-17 09:30:20 +01:00
|
|
|
|
|
|
|
def get_serializer_class(self, user=None):
|
|
|
|
"""
|
|
|
|
Returns serializer class.
|
|
|
|
"""
|
|
|
|
from .serializers import GroupSerializer
|
|
|
|
|
|
|
|
return GroupSerializer
|
2017-05-23 14:07:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
class PersonalNoteAccessPermissions(BaseAccessPermissions):
|
|
|
|
"""
|
|
|
|
Access permissions container for personal notes. Every authenticated user
|
|
|
|
can handle personal notes.
|
|
|
|
"""
|
|
|
|
def check_permissions(self, user):
|
|
|
|
"""
|
|
|
|
Returns True if the user has read access model instances.
|
|
|
|
"""
|
|
|
|
# Every authenticated user can retrieve personal notes.
|
|
|
|
return not isinstance(user, AnonymousUser)
|
|
|
|
|
|
|
|
def get_serializer_class(self, user=None):
|
|
|
|
"""
|
|
|
|
Returns serializer class.
|
|
|
|
"""
|
|
|
|
from .serializers import PersonalNoteSerializer
|
|
|
|
|
|
|
|
return PersonalNoteSerializer
|
|
|
|
|
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]]:
|
2017-05-23 14:07:06 +02:00
|
|
|
"""
|
|
|
|
Returns the restricted serialized data for the instance prepared
|
|
|
|
for the user. Everybody gets only his own personal notes.
|
|
|
|
"""
|
|
|
|
# Parse data.
|
2017-08-31 21:12:39 +02:00
|
|
|
if user is None:
|
2017-08-24 12:26:55 +02:00
|
|
|
data = [] # type: List[Dict[str, Any]]
|
2017-08-31 21:12:39 +02:00
|
|
|
else:
|
|
|
|
for full in full_data:
|
|
|
|
if full['user_id'] == user.id:
|
|
|
|
data = [full]
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
data = []
|
2017-05-23 14:07:06 +02:00
|
|
|
|
2017-09-04 00:25:45 +02:00
|
|
|
return data
|