Use named permissions
authorThierry Florac <thierry.florac@onf.fr>
Thu, 08 Oct 2015 12:24:42 +0200
changeset 2 619200513bbc
parent 1 b9ee7c6e9deb
child 3 b636161c2fe0
Use named permissions
src/pyams_portal/__init__.py
src/pyams_portal/portlets/context/__init__.py
src/pyams_portal/portlets/image/__init__.py
src/pyams_portal/zmi/container.py
src/pyams_portal/zmi/portlets/context.py
src/pyams_portal/zmi/portlets/image.py
src/pyams_portal/zmi/template/config.py
src/pyams_portal/zmi/template/page.py
--- a/src/pyams_portal/__init__.py	Wed Jun 17 09:58:38 2015 +0200
+++ b/src/pyams_portal/__init__.py	Thu Oct 08 12:24:42 2015 +0200
@@ -19,6 +19,8 @@
 from pyramid.i18n import TranslationStringFactory
 _ = TranslationStringFactory('pyams_portal')
 
+from pyams_utils.interfaces import VIEW_PERMISSION, VIEW_SYSTEM_PERMISSION
+
 
 def includeme(config):
     """Pyramid include"""
@@ -33,4 +35,4 @@
     # register custom roles
     config.register_role({'id': 'portal.TemplatesManager',
                           'title': _("Portal templates manager"),
-                          'permissions': {'portal.templates.manage', 'view', 'system.view'}})
+                          'permissions': {'portal.templates.manage', VIEW_PERMISSION, VIEW_SYSTEM_PERMISSION}})
--- a/src/pyams_portal/portlets/context/__init__.py	Wed Jun 17 09:58:38 2015 +0200
+++ b/src/pyams_portal/portlets/context/__init__.py	Thu Oct 08 12:24:42 2015 +0200
@@ -19,6 +19,7 @@
 from .interfaces import IContextPortletConfiguration
 from pyams_portal.interfaces import IPortletRenderer, IPortalContext
 from pyams_skin.layer import IPyAMSLayer
+from pyams_utils.interfaces import VIEW_PERMISSION
 
 # import packages
 from pyams_portal.portlet import Portlet, portlet_config, PortletRenderer, PortletConfiguration
@@ -32,7 +33,7 @@
 CONTEXT_PORTLET_NAME = 'pyams_portal.portlet.context'
 
 
-@portlet_config(permission='view')
+@portlet_config(permission=VIEW_PERMISSION)
 class ContextPortlet(Portlet):
     """Context portlet
 
--- a/src/pyams_portal/portlets/image/__init__.py	Wed Jun 17 09:58:38 2015 +0200
+++ b/src/pyams_portal/portlets/image/__init__.py	Thu Oct 08 12:24:42 2015 +0200
@@ -9,7 +9,6 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
-from pyams_file.property import FileProperty
 
 __docformat__ = 'restructuredtext'
 
@@ -20,8 +19,10 @@
 from .interfaces import IImagePortletConfiguration
 from pyams_portal.interfaces import IPortalContext, IPortletRenderer
 from pyams_skin.layer import IPyAMSLayer
+from pyams_utils.interfaces import VIEW_PERMISSION
 
 # import packages
+from pyams_file.property import FileProperty
 from pyams_portal.portlet import portlet_config, Portlet, PortletConfiguration, PortletRenderer
 from pyams_template.template import template_config
 from pyams_utils.adapter import adapter_config
@@ -33,7 +34,7 @@
 IMAGE_PORTLET_NAME = 'pyams_portal.portlet.image'
 
 
-@portlet_config(permission='view')
+@portlet_config(permission=VIEW_PERMISSION)
 class ImagePortlet(Portlet):
     """Image portlet
 
--- a/src/pyams_portal/zmi/container.py	Wed Jun 17 09:58:38 2015 +0200
+++ b/src/pyams_portal/zmi/container.py	Thu Oct 08 12:24:42 2015 +0200
@@ -21,6 +21,7 @@
 from pyams_skin.interfaces import IInnerPage, IPageHeader
 from pyams_skin.interfaces.container import ITable, ITableElementEditor
 from pyams_skin.layer import IPyAMSLayer
+from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION, MANAGE_SYSTEM_PERMISSION
 from pyams_workflow.interfaces import IWorkflowVersions
 from pyams_zmi.interfaces.menu import IControlPanelMenu
 from pyams_zmi.layer import IAdminLayer
@@ -69,14 +70,14 @@
 
 
 @viewlet_config(name='portal-templates.menu', context=ISite, layer=IAdminLayer, manager=IControlPanelMenu,
-                permission='system.view', weight=20)
+                permission=VIEW_SYSTEM_PERMISSION, weight=20)
 @viewletmanager_config(name='portal-templates.menu', context=ISite, layer=IAdminLayer)
 @implementer(IPortalTemplateContainerMenu)
 class PortalTemplateContainerMenuItem(MenuItem):
     """Portal template container menu"""
 
     label = _("Portal templates")
-    icon_class = 'fa fa-fw fa-columns'
+    icon_class = 'fa-columns'
     url = '#portal-templates.html'
 
 
@@ -144,7 +145,7 @@
         return ()
 
 
-@pagelet_config(name='portal-templates.html', context=ISite, layer=IPyAMSLayer, permission='system.view')
+@pagelet_config(name='portal-templates.html', context=ISite, layer=IPyAMSLayer, permission=VIEW_SYSTEM_PERMISSION)
 @implementer(IInnerPage)
 class PortalTemplateContainerView(AdminView, ContainerView):
     """Portal template container view"""
@@ -169,7 +170,7 @@
 #
 
 @viewlet_config(name='templates-container-configuration.menu', context=ISite, layer=IAdminLayer,
-                manager=IPortalTemplateContainerMenu, permission='system.view', weight=1)
+                manager=IPortalTemplateContainerMenu, permission=VIEW_SYSTEM_PERMISSION, weight=1)
 class PortalTemplatesContainerPropertiesMenu(MenuItem):
     """Portal template container configuration menu"""
 
@@ -185,7 +186,7 @@
 
 
 @pagelet_config(name='properties.html', context=IPortalTemplateContainer, layer=IPyAMSLayer,
-                permission='system.view')
+                permission=VIEW_SYSTEM_PERMISSION)
 class PortalTemplateContainerPropertiesEditForm(AdminDialogEditForm):
     """Portal template container properties edit form"""
 
@@ -195,10 +196,10 @@
 
     fields = field.Fields(IPortalTemplateContainerConfiguration)
     ajax_handler = 'properties.json'
-    edit_permission = 'system.manage'
+    edit_permission = MANAGE_SYSTEM_PERMISSION
 
 
 @view_config(name='properties.json', context=IPortalTemplateContainer, request_type=IPyAMSLayer,
-             permission='system.manage', renderer='json', xhr=True)
+             permission=MANAGE_SYSTEM_PERMISSION, renderer='json', xhr=True)
 class PortalTemplateContainerPropertiesAJAXEditForm(AJAXEditForm, PortalTemplateContainerPropertiesEditForm):
     """Portal template container properties edit form, JSON renderer"""
--- a/src/pyams_portal/zmi/portlets/context.py	Wed Jun 17 09:58:38 2015 +0200
+++ b/src/pyams_portal/zmi/portlets/context.py	Thu Oct 08 12:24:42 2015 +0200
@@ -20,6 +20,7 @@
 from pyams_portal.interfaces import IPortletPreviewer
 from pyams_portal.portlets.context.interfaces import IContextPortletConfiguration
 from pyams_skin.layer import IPyAMSLayer
+from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION
 
 # import packages
 from pyams_form.form import AJAXEditForm
@@ -32,7 +33,7 @@
 
 
 @pagelet_config(name='properties.html', context=IContextPortletConfiguration, request_type=IPyAMSLayer,
-                permission='system.view')
+                permission=VIEW_SYSTEM_PERMISSION)
 class ContextPortletConfigurationEditor(PortletConfigurationEditor):
     """Context portlet configuration editor"""
 
--- a/src/pyams_portal/zmi/portlets/image.py	Wed Jun 17 09:58:38 2015 +0200
+++ b/src/pyams_portal/zmi/portlets/image.py	Thu Oct 08 12:24:42 2015 +0200
@@ -9,9 +9,6 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
-from pyams_portal.interfaces import IPortletPreviewer
-from pyams_portal.portlet import PortletPreviewer
-from pyams_template.template import template_config
 
 __docformat__ = 'restructuredtext'
 
@@ -20,19 +17,23 @@
 
 # import interfaces
 from pyams_pagelet.interfaces import IPagelet
+from pyams_portal.interfaces import IPortletPreviewer
 from pyams_portal.portlets.image.interfaces import IImagePortletConfiguration
 from pyams_skin.layer import IPyAMSLayer
+from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION
 
 # import packages
 from pyams_form.form import AJAXEditForm
 from pyams_pagelet.pagelet import pagelet_config
+from pyams_portal.portlet import PortletPreviewer
 from pyams_portal.zmi.portlet import PortletConfigurationEditor
+from pyams_template.template import template_config
 from pyams_utils.adapter import adapter_config
 from zope.interface import Interface
 
 
 @pagelet_config(name='properties.html', context=IImagePortletConfiguration, request_type=IPyAMSLayer,
-                permission='system.view')
+                permission=VIEW_SYSTEM_PERMISSION)
 class ImagePortletConfigurationEditor(PortletConfigurationEditor):
     """Image portlet configuration editor"""
 
--- a/src/pyams_portal/zmi/template/config.py	Wed Jun 17 09:58:38 2015 +0200
+++ b/src/pyams_portal/zmi/template/config.py	Thu Oct 08 12:24:42 2015 +0200
@@ -9,7 +9,6 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
-from pyams_skin.page import DefaultPageHeaderAdapter
 
 __docformat__ = 'restructuredtext'
 
@@ -22,10 +21,11 @@
 from pyams_portal.interfaces import IPortalTemplate, IPortalTemplateConfiguration, ISlot, \
     IPortletAddingInfo, IPortlet, ISlotConfiguration, IPortletPreviewer, IPortalTemplateContainer, \
     IPortalTemplateContainerConfiguration
-from pyams_skin.interfaces import IInnerPage, IPageHeader, IContentTitle
+from pyams_skin.interfaces import IInnerPage, IPageHeader
 from pyams_skin.interfaces.viewlet import IToolbarAddingMenu
 from pyams_skin.layer import IPyAMSLayer
-from pyams_workflow.interfaces import IWorkflowState, IWorkflowVersions
+from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION
+from pyams_workflow.interfaces import IWorkflowState
 from pyams_zmi.interfaces.menu import ISiteManagementMenu, IPropertiesMenu
 from pyams_zmi.layer import IAdminLayer
 from transaction.interfaces import ITransactionManager
@@ -34,11 +34,12 @@
 # import packages
 from pyams_form.form import AJAXAddForm, AJAXEditForm
 from pyams_pagelet.pagelet import pagelet_config
-from pyams_portal.workflow import STATUS_LABELS, STATUS_IDS, PUBLISHED, ARCHIVED
+from pyams_portal.workflow import PUBLISHED, ARCHIVED
+from pyams_skin.page import DefaultPageHeaderAdapter
 from pyams_skin.viewlet.menu import MenuItem
 from pyams_skin.viewlet.toolbar import JsToolbarMenuItem, ToolbarMenuDivider, ToolbarMenuItem
 from pyams_template.template import template_config
-from pyams_utils.adapter import adapter_config, ContextRequestViewAdapter
+from pyams_utils.adapter import adapter_config
 from pyams_utils.registry import query_utility
 from pyams_viewlet.manager import viewletmanager_config
 from pyams_viewlet.viewlet import viewlet_config
@@ -55,7 +56,7 @@
 
 
 @viewlet_config(name='template-properties.menu', context=IPortalTemplate, layer=IAdminLayer,
-                manager=ISiteManagementMenu, permission='system.view', weight=1)
+                manager=ISiteManagementMenu, permission=VIEW_SYSTEM_PERMISSION, weight=1)
 @viewletmanager_config(name='template-properties.menu', layer=IAdminLayer, provides=IPropertiesMenu)
 @implementer(IPropertiesMenu)
 class PortalTemplatePropertiesMenu(MenuItem):
@@ -66,7 +67,7 @@
     url = '#properties.html'
 
 
-@pagelet_config(name='properties.html', context=IPortalTemplate, layer=IPyAMSLayer, permission='system.view')
+@pagelet_config(name='properties.html', context=IPortalTemplate, layer=IPyAMSLayer, permission=VIEW_SYSTEM_PERMISSION)
 @template_config(template='templates/config.pt', layer=IAdminLayer)
 @implementer(IInnerPage)
 class PortalTemplateConfigView(AdminView):
@@ -238,7 +239,7 @@
 
 
 @view_config(name='get-slots-width.json', context=IPortalTemplate, request_type=IPyAMSLayer,
-             permission='system.view', renderer='json', xhr=True)
+             permission=VIEW_SYSTEM_PERMISSION, renderer='json', xhr=True)
 def get_template_slots_width(request):
     """Get template slots width"""
     config = IPortalTemplateConfiguration(request.context)
@@ -256,7 +257,8 @@
     return config.get_slots_width(request.params.get('device'))
 
 
-@pagelet_config(name='slot-properties.html', context=IPortalTemplate, layer=IPyAMSLayer, permission='system.view')
+@pagelet_config(name='slot-properties.html', context=IPortalTemplate, layer=IPyAMSLayer,
+                permission=VIEW_SYSTEM_PERMISSION)
 class PortalTemplateSlotPropertiesEditForm(AdminDialogEditForm):
     """Slot properties edit form"""
 
@@ -416,7 +418,7 @@
 
 
 @view_config(name='portlet-properties.html', context=IPortalTemplate, request_type=IPyAMSLayer,
-             permission='system.view')
+             permission=VIEW_SYSTEM_PERMISSION)
 class PortalTemplatePortletEditForm(AdminDialogEditForm):
     """Portal template portlet edit form"""
 
--- a/src/pyams_portal/zmi/template/page.py	Wed Jun 17 09:58:38 2015 +0200
+++ b/src/pyams_portal/zmi/template/page.py	Thu Oct 08 12:24:42 2015 +0200
@@ -9,25 +9,6 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
-from pyramid.exceptions import NotFound
-from pyramid.view import view_config
-from pyams_form.form import AJAXEditForm
-from pyams_pagelet.interfaces import PageletCreatedEvent, IPagelet
-from pyams_pagelet.pagelet import pagelet_config
-from pyams_portal.interfaces import IPortalContext, IPortalPage, IPortalTemplateConfiguration
-from pyams_portal.workflow import PUBLISHED, ARCHIVED
-from pyams_portal.zmi.template.config import PortalTemplateConfigView
-from pyams_skin.interfaces import IInnerPage
-from pyams_skin.layer import IPyAMSLayer
-from pyams_skin.viewlet.menu import MenuItem
-from pyams_template.template import template_config
-from pyams_utils.url import absolute_url
-from pyams_viewlet.viewlet import viewlet_config
-from pyams_workflow.interfaces import IWorkflowVersions, IWorkflowState
-from pyams_zmi.form import AdminDialogEditForm
-from pyams_zmi.interfaces.menu import ISiteManagementMenu, IPropertiesMenu
-from pyams_zmi.layer import IAdminLayer
-from pyams_zmi.view import AdminView
 
 __docformat__ = 'restructuredtext'
 
@@ -35,17 +16,33 @@
 # import standard library
 
 # import interfaces
+from pyams_pagelet.interfaces import PageletCreatedEvent, IPagelet
+from pyams_portal.interfaces import IPortalContext, IPortalPage, IPortalTemplateConfiguration
+from pyams_skin.layer import IPyAMSLayer
+from pyams_utils.interfaces import MANAGE_PERMISSION
+from pyams_workflow.interfaces import IWorkflowVersions, IWorkflowState
+from pyams_zmi.interfaces.menu import ISiteManagementMenu, IPropertiesMenu
+from pyams_zmi.layer import IAdminLayer
 
 # import packages
+from pyramid.exceptions import NotFound
+from pyramid.view import view_config
+from pyams_form.form import AJAXEditForm
+from pyams_pagelet.pagelet import pagelet_config
+from pyams_portal.workflow import PUBLISHED, ARCHIVED
+from pyams_portal.zmi.template.config import PortalTemplateConfigView
+from pyams_skin.viewlet.menu import MenuItem
+from pyams_utils.url import absolute_url
+from pyams_viewlet.viewlet import viewlet_config
+from pyams_zmi.form import AdminDialogEditForm
 from z3c.form import field
-from zope.interface import implementer
 
 from pyams_portal import _
 
 
 
 @viewlet_config(name='template-properties.menu', context=IPortalContext, layer=IAdminLayer,
-                manager=IPropertiesMenu, permission='manage', weight=5)
+                manager=IPropertiesMenu, permission=MANAGE_PERMISSION, weight=5)
 class PortalContextTemplatePropertiesMenu(MenuItem):
     """Portal context template properties menu"""
 
@@ -56,7 +53,8 @@
     modal_target = True
 
 
-@pagelet_config(name='template-properties.html', context=IPortalContext, layer=IPyAMSLayer, permission='manage')
+@pagelet_config(name='template-properties.html', context=IPortalContext, layer=IPyAMSLayer,
+                permission=MANAGE_PERMISSION)
 class PortalContextTemplatePropertiesEditForm(AdminDialogEditForm):
     """Portal context template properties edit form"""
 
@@ -74,20 +72,20 @@
         return fields
 
     ajax_handler = 'template-properties.json'
-    edit_permission = 'manage'
+    edit_permission = MANAGE_PERMISSION
 
     def getContent(self):
         return IPortalPage(self.context)
 
 
 @view_config(name='template-properties.json', context=IPortalContext, request_type=IPyAMSLayer,
-             permission='manage', renderer='json', xhr=True)
+             permission=MANAGE_PERMISSION, renderer='json', xhr=True)
 class PortalContextTemplatePropertiesAJAXEditForm(AJAXEditForm, PortalContextTemplatePropertiesEditForm):
     """Portal context template properties edit form, JSON renderer"""
 
 
 @viewlet_config(name='template-config.menu', context=IPortalContext, layer=IAdminLayer,
-                manager=ISiteManagementMenu, permission='manage', weight=20)
+                manager=ISiteManagementMenu, permission=MANAGE_PERMISSION, weight=20)
 class PortalContextTemplateConfigMenu(MenuItem):
     """Portal context template configuration menu"""
 
@@ -111,7 +109,8 @@
             return super(PortalContextTemplateConfigMenu, self).get_url()
 
 
-@pagelet_config(name='template-config.html', context=IPortalContext, layer=IPyAMSLayer, permission='manage')
+@pagelet_config(name='template-config.html', context=IPortalContext, layer=IPyAMSLayer,
+                permission=MANAGE_PERMISSION)
 class PortalContextTemplateConfigView(PortalTemplateConfigView):
     """Portal context template configuration view"""
 
@@ -125,11 +124,12 @@
     def can_change(self):
         if not IPortalPage(self.context).use_local_template:
             return False
-        return self.request.has_permission('manage') and \
+        return self.request.has_permission(MANAGE_PERMISSION) and \
                IWorkflowState(self.get_context()).state not in (PUBLISHED, ARCHIVED)
 
 
-@view_config(name='portlet-properties.html', context=IPortalContext, request_type=IPyAMSLayer, permission='manage')
+@view_config(name='portlet-properties.html', context=IPortalContext, request_type=IPyAMSLayer,
+             permission=MANAGE_PERMISSION)
 class PortalContextTemplatePortletEditForm(AdminDialogEditForm):
     """Portal context template portlet edit form"""