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
|
2016-02-11 22:58:32 +01:00
|
|
|
from ..utils.access_permissions import BaseAccessPermissions
|
2017-01-26 15:34:24 +01:00
|
|
|
from ..utils.auth import anonymous_is_enabled, has_perm
|
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
|
|
|
|
|
|
|
def get_restricted_data(self, full_data, user):
|
|
|
|
"""
|
|
|
|
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-04-28 00:50:37 +02:00
|
|
|
def filtered_data(full_data, only_keys):
|
|
|
|
"""
|
|
|
|
Returns a new dict like full_data but with all blocked_keys removed.
|
|
|
|
"""
|
|
|
|
return {key: full_data[key] for key in only_keys}
|
|
|
|
|
|
|
|
# many_items is True, when there are more then one items in full_data.
|
|
|
|
many_items = not isinstance(full_data, dict)
|
|
|
|
full_data = full_data if many_items else [full_data]
|
|
|
|
|
|
|
|
many_fields = set(USERCANSEEEXTRASERIALIZER_FIELDS)
|
|
|
|
little_fields = set(USERCANSEESERIALIZER_FIELDS)
|
|
|
|
many_fields.add('groups_id')
|
|
|
|
many_fields.discard('groups')
|
|
|
|
little_fields.add('groups_id')
|
|
|
|
little_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'):
|
2017-04-28 00:50:37 +02:00
|
|
|
data = full_data
|
2016-09-17 22:26:23 +02:00
|
|
|
else:
|
2017-04-28 00:50:37 +02:00
|
|
|
data = [filtered_data(full, many_fields) for full in full_data]
|
2016-09-17 22:26:23 +02:00
|
|
|
else:
|
2017-04-28 00:50:37 +02:00
|
|
|
data = [filtered_data(full, little_fields) for full in full_data]
|
2016-09-17 22:26:23 +02:00
|
|
|
else:
|
2017-04-28 00:50:37 +02:00
|
|
|
# Build a list of users, that can be seen without permissions.
|
|
|
|
no_perm_users = set()
|
|
|
|
if user is not None:
|
|
|
|
no_perm_users.add(user.id)
|
|
|
|
|
|
|
|
# Get a list of all users, that are needed by another app
|
2017-04-10 16:28:38 +02:00
|
|
|
receiver_responses = user_data_required.send(
|
|
|
|
sender=self.__class__,
|
|
|
|
request_user=user,
|
|
|
|
user_data=full_data)
|
|
|
|
for receiver, response in receiver_responses:
|
2017-04-28 00:50:37 +02:00
|
|
|
no_perm_users.update(response)
|
2016-09-17 22:26:23 +02:00
|
|
|
|
2017-04-28 00:50:37 +02:00
|
|
|
data = [
|
|
|
|
filtered_data(full, little_fields)
|
|
|
|
for full
|
|
|
|
in full_data
|
|
|
|
if full['id'] in no_perm_users]
|
|
|
|
|
|
|
|
# Set data to [None] if data is empty
|
|
|
|
data = data or [None]
|
|
|
|
|
|
|
|
return data if many_items else data[0]
|
2016-09-17 22:26:23 +02:00
|
|
|
|
|
|
|
def get_projector_data(self, full_data):
|
|
|
|
"""
|
|
|
|
Returns the restricted serialized data for the instance prepared
|
|
|
|
for the projector. Removes several fields.
|
|
|
|
"""
|
|
|
|
from .serializers import USERCANSEESERIALIZER_FIELDS
|
|
|
|
|
|
|
|
# Let only some fields pass this method.
|
|
|
|
data = {}
|
|
|
|
for key in full_data.keys():
|
|
|
|
if key in USERCANSEESERIALIZER_FIELDS:
|
|
|
|
data[key] = full_data[key]
|
|
|
|
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
|