From 5aefe118c04a550067f2da7d9b0f57598b912cb9 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Pasquier <contact@jbpasquier.eu> Date: Tue, 13 Oct 2020 12:15:15 +0000 Subject: [PATCH] feature: add context to filter_user_perms and Model.get_permissions --- djangoldp/models.py | 4 ++-- djangoldp/permissions.py | 4 ++-- djangoldp/serializers.py | 11 +++++------ 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/djangoldp/models.py b/djangoldp/models.py index 261518f5..4bd5b10e 100644 --- a/djangoldp/models.py +++ b/djangoldp/models.py @@ -257,10 +257,10 @@ class Model(models.Model): return getattr(model_class._meta, meta_name, meta) @staticmethod - def get_permissions(obj_or_model, user_or_group, filter): + def get_permissions(obj_or_model, context, filter): permissions = filter for permission_class in Model.get_permission_classes(obj_or_model, [LDPPermissions]): - permissions = permission_class().filter_user_perms(user_or_group, obj_or_model, permissions) + permissions = permission_class().filter_user_perms(context, obj_or_model, permissions) return [{'mode': {'@type': name.split('_')[0]}} for name in permissions] @classmethod diff --git a/djangoldp/permissions.py b/djangoldp/permissions.py index 682ed354..c8f71aa8 100644 --- a/djangoldp/permissions.py +++ b/djangoldp/permissions.py @@ -97,9 +97,9 @@ class LDPPermissions(DjangoObjectPermissions): return self.perms_cache[perms_cache_key] # return list(perms) - def filter_user_perms(self, user, obj_or_model, permissions): + def filter_user_perms(self, context, obj_or_model, permissions): # Only used on Model.get_permissions to translate permissions to LDP - return [perm for perm in permissions if perm in self.user_permissions(user, obj_or_model)] + return [perm for perm in permissions if perm in self.user_permissions(context['request'].user, obj_or_model)] # perms_map defines the permissions required for different methods perms_map = { diff --git a/djangoldp/serializers.py b/djangoldp/serializers.py index 6fc13ee3..e45a55a7 100644 --- a/djangoldp/serializers.py +++ b/djangoldp/serializers.py @@ -107,7 +107,7 @@ class LDListMixin: return self.to_representation_cache.get(cache_key) filtered_values = value - container_permissions = Model.get_permissions(child_model, self.context['request'].user, ['view', 'add']) + container_permissions = Model.get_permissions(child_model, self.context, ['view', 'add']) else: # this is a container. Parent model is the containing object, child the model contained @@ -127,10 +127,9 @@ class LDListMixin: filtered_values = list( filter(lambda v: Model.get_permission_classes(v, [LDPPermissions])[0]().has_object_permission( self.context['request'], self.context['view'], v), value)) - container_permissions = Model.get_permissions(child_model, self.context['request'].user, ['add']) + container_permissions = Model.get_permissions(child_model, self.context, ['add']) container_permissions.extend( - Model.get_permissions(parent_model, self.context['request'].user, - ['view'])) + Model.get_permissions(parent_model, self.context, ['view'])) self.to_representation_cache.set(self.id, {'@id': self.id, '@type': 'ldp:Container', @@ -345,7 +344,7 @@ class LDPSerializer(HyperlinkedModelSerializer): data['@type'] = rdf_type if rdf_context is not None: data['@context'] = rdf_context - data['permissions'] = Model.get_permissions(obj, self.context['request'].user, + data['permissions'] = Model.get_permissions(obj, self.context, ['view', 'change', 'control', 'delete']) return data @@ -382,7 +381,7 @@ class LDPSerializer(HyperlinkedModelSerializer): item in data], 'permissions': Model.get_permissions(self.parent.Meta.model, - self.context['request'].user, + self.context, ['view', 'add']) } else: -- GitLab