[PATCH 1 of 2 jsonschema] Move permission check logic from get_entities() view to the resource

Denis Laxalde denis.laxalde at logilab.fr
Wed Jun 20 10:08:04 CEST 2018


# HG changeset patch
# User Denis Laxalde <denis.laxalde at logilab.fr>
# Date 1529478222 -7200
#      Wed Jun 20 09:03:42 2018 +0200
# Node ID 52dcc1f1a1439b0c0019646b4172fe94644b9788
# Parent  62934d2097acdbbe04182b6070b7e87ac6549dbe
# Available At https://hg.logilab.org/review/cubicweb-jsonschema
#              hg pull https://hg.logilab.org/review/cubicweb-jsonschema -r 52dcc1f1a143
# EXP-Topic resources-interfaces
Move permission check logic from get_entities() view to the resource

Instead of assuming that the context resource in get_entities() view has
an "etype" attribute (which won't be true anymore in next changeset), we
delegate permission check to the resource object in the form of an
has_perm() method. This is step towards defining abstract interfaces for
resource classes.

diff --git a/cubicweb_jsonschema/api/entities.py b/cubicweb_jsonschema/api/entities.py
--- a/cubicweb_jsonschema/api/entities.py
+++ b/cubicweb_jsonschema/api/entities.py
@@ -84,8 +84,7 @@ def get_root(context, request):
 )
 def get_entities(context, request):
     """Render multiple entities in JSON format."""
-    vreg = request.registry['cubicweb.registry']
-    if vreg.schema[context.etype].has_perm(request.cw_cnx, 'add'):
+    if context.has_perm('add'):
         request.response.allow = ['GET', 'POST']
     else:
         request.response.allow = ['GET']
diff --git a/cubicweb_jsonschema/resources.py b/cubicweb_jsonschema/resources.py
--- a/cubicweb_jsonschema/resources.py
+++ b/cubicweb_jsonschema/resources.py
@@ -313,6 +313,10 @@ class ETypeResource(Paginable, Pluggable
             'jsonschema.collection', self.request.cw_request,
             etype=self.etype, resource=self)
 
+    def has_perm(self, action):
+        vreg = self.request.registry['cubicweb.registry']
+        return vreg.schema[self.etype].has_perm(self.request.cw_cnx, action)
+
 
 @Mappable.register
 class EntityResource(PluggableResource):


More information about the cubicweb-devel mailing list