Updated action permission checker
authorThierry Florac <thierry.florac@onf.fr>
Fri, 08 Dec 2017 10:57:36 +0100
changeset 316 b50158f7050d
parent 315 8654d1faa27d
child 317 578c0df290b5
Updated action permission checker
src/pyams_content/component/gallery/zmi/file.py
--- a/src/pyams_content/component/gallery/zmi/file.py	Fri Dec 08 10:48:16 2017 +0100
+++ b/src/pyams_content/component/gallery/zmi/file.py	Fri Dec 08 10:57:36 2017 +0100
@@ -9,6 +9,8 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
+from pyams_content.shared.common import IWfSharedContent
+from pyams_form.interfaces.form import IFormContextPermissionChecker
 
 __docformat__ = 'restructuredtext'
 
@@ -16,14 +18,14 @@
 # import standard library
 
 # import interfaces
-from pyams_content.component.gallery.interfaces import IGallery, IGalleryFile, IGalleryTarget
+from pyams_content.component.gallery.interfaces import IGallery, IGalleryFile
 from pyams_content.component.gallery.zmi.interfaces import IGalleryImageAddFields, IGalleryImagesView
 from pyams_content.interfaces import MANAGE_CONTENT_PERMISSION
 from pyams_file.interfaces.archive import IArchiveExtractor
 from pyams_i18n.interfaces import II18n
 from pyams_skin.interfaces.viewlet import IWidgetTitleViewletManager, IContextActions
 from pyams_skin.layer import IPyAMSLayer
-from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION, VIEW_PERMISSION
+from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION, FORBIDDEN_PERMISSION
 from z3c.form.interfaces import NOT_CHANGED
 from zope.schema.interfaces import WrongType
 
@@ -130,10 +132,23 @@
 
 @viewlet_config(name='file.showhide.action', context=IGalleryFile, layer=IPyAMSLayer, view=IGalleryImagesView,
                 manager=IContextActions, permission=VIEW_SYSTEM_PERMISSION, weight=1)
-class GalleryFileShowAddAction(JsToolbarActionItem):
+class GalleryFileShowHideAction(WfSharedContentPermissionMixin, JsToolbarActionItem):
     """Gallery file show/hide action"""
 
-    label = _("Show/hide image")
+    edit_permission = FORBIDDEN_PERMISSION
+
+    def __init__(self, context, request, view, manager):
+        super(GalleryFileShowHideAction, self).__init__(context, request, view, manager)
+        content = get_parent(context, IWfSharedContent)
+        if content is not None:
+            self.edit_permission = IFormContextPermissionChecker(content).edit_permission
+
+    @property
+    def label(self):
+        if self.request.has_permission(self.edit_permission, context=self.context):
+            return _("Show/hide image")
+        else:
+            return ''
 
     @property
     def label_css_class(self):
@@ -146,6 +161,10 @@
 
     url = 'PyAMS_content.galleries.switchImageVisibility'
 
+    def get_url(self):
+        if self.request.has_permission(self.edit_permission, context=self.context):
+            return self.url
+
 
 @viewlet_config(name='file.properties.action', context=IGalleryFile, layer=IPyAMSLayer, view=IGalleryImagesView,
                 manager=IContextActions, permission=VIEW_SYSTEM_PERMISSION, weight=5)
@@ -212,20 +231,6 @@
         return output
 
 
-@viewlet_config(name='gallery-file-download.action', context=IGalleryFile, layer=IPyAMSLayer, view=IGalleryImagesView,
-                manager=IContextActions, permission=VIEW_PERMISSION, weight=89)
-class GalleryFileDownloaderAction(JsToolbarActionItem):
-    """Gallery file downloader action"""
-
-    label = _("Download image...")
-    label_css_class = 'fa fa-fw fa-download'
-    hint_gravity = 'nw'
-
-    @property
-    def url(self):
-        return absolute_url(self.context.data, self.request, query={'download': '1'})
-
-
 @viewlet_config(name='gallery-file-remover.action', context=IGalleryFile, layer=IPyAMSLayer, view=IGalleryImagesView,
                 manager=IContextActions, weight=90)
 class GalleryFileRemoverAction(WfSharedContentPermissionMixin, JsToolbarActionItem):