Added custom file modifier action and permission checker
authorThierry Florac <thierry.florac@onf.fr>
Wed, 12 Jul 2017 12:05:13 +0200
changeset 42 63b5882984e2
parent 41 0d45e77a9892
child 43 1a33309aef31
Added custom file modifier action and permission checker
src/pyams_file/zmi/__init__.py
--- a/src/pyams_file/zmi/__init__.py	Wed Jul 12 12:04:31 2017 +0200
+++ b/src/pyams_file/zmi/__init__.py	Wed Jul 12 12:05:13 2017 +0200
@@ -16,5 +16,41 @@
 # import standard library
 
 # import interfaces
+from pyams_file.interfaces import IFile, IFileModifierForm
+from pyams_form.interfaces.form import IFormContextPermissionChecker
+from pyams_utils.interfaces import MANAGE_PERMISSION, FORBIDDEN_PERMISSION
 
 # import packages
+from pyams_skin.viewlet.toolbar import ToolbarActionItem
+from pyams_utils.adapter import adapter_config, ContextRequestViewAdapter
+from pyramid.location import lineage
+from zope.interface import Interface
+
+
+class FileModifierAction(ToolbarActionItem):
+    """Base file modifier action"""
+
+    @property
+    def permission(self):
+        try:
+            return self.__parent__.form.edit_permission
+        except AttributeError:
+            return MANAGE_PERMISSION
+
+
+@adapter_config(context=(IFile, Interface, IFileModifierForm), provides=IFormContextPermissionChecker)
+class FileModifierFormPermissionChecker(ContextRequestViewAdapter):
+    """File form permission checker
+
+    File modifier forms are files properties and images resizer/crop/thumbnails selection forms, for
+    which edit permission is not based directly on form's context (the *file*), but on parent.
+    """
+
+    @property
+    def edit_permission(self):
+        registry = self.request.registry
+        for parent in lineage(self.context):
+            checker = registry.queryAdapter(parent, IFormContextPermissionChecker)
+            if checker is not None:
+                return checker.edit_permission
+        return FORBIDDEN_PERMISSION