Use named permissions
authorThierry Florac <thierry.florac@onf.fr>
Thu, 08 Oct 2015 09:42:02 +0200
changeset 10 446671b704bb
parent 9 c03bd115684e
child 11 b3155bf5858e
Use named permissions
src/pyams_zmi/admin.py
src/pyams_zmi/control_panel.py
src/pyams_zmi/interfaces/__init__.py
src/pyams_zmi/site.py
src/pyams_zmi/viewlet/menu/__init__.py
--- a/src/pyams_zmi/admin.py	Thu Aug 27 11:27:44 2015 +0200
+++ b/src/pyams_zmi/admin.py	Thu Oct 08 09:42:02 2015 +0200
@@ -17,6 +17,7 @@
 
 # import interfaces
 from pyams_skin.layer import IPyAMSLayer
+from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION
 from pyams_zmi.layer import IAdminLayer
 
 # import packages
@@ -26,7 +27,7 @@
 from zope.interface import Interface
 
 
-@pagelet_config(name='admin.html', layer=IPyAMSLayer, context=Interface, permission='system.view')
+@pagelet_config(name='admin.html', layer=IPyAMSLayer, context=Interface, permission=VIEW_SYSTEM_PERMISSION)
 @template_config(template='templates/admin.pt', layer=IAdminLayer)
 class MainAdminPage(AdminView):
     """Main administration page"""
--- a/src/pyams_zmi/control_panel.py	Thu Aug 27 11:27:44 2015 +0200
+++ b/src/pyams_zmi/control_panel.py	Thu Oct 08 09:42:02 2015 +0200
@@ -20,6 +20,7 @@
 from pyams_skin.interfaces import IInnerPage, IPageHeader, IContentHelp
 from pyams_skin.interfaces.container import ITableWithActions
 from pyams_skin.layer import IPyAMSLayer
+from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION, MANAGE_SYSTEM_PERMISSION
 from pyams_utils.interfaces.site import IOptionalUtility
 from pyams_zmi.interfaces.menu import IControlPanelMenu, IUtilitiesMenu
 from pyams_zmi.layer import IAdminLayer
@@ -52,7 +53,7 @@
 #
 
 @viewlet_config(name='utilities.menu', layer=IAdminLayer, context=ISite, manager=IControlPanelMenu,
-                permission='system.view', weight=1)
+                permission=VIEW_SYSTEM_PERMISSION, weight=1)
 @viewletmanager_config(name='utilities.menu', layer=IAdminLayer, context=ISite, provides=IUtilitiesMenu)
 @implementer(IUtilitiesMenu)
 class UtilitiesMenuItem(MenuItem):
@@ -73,10 +74,10 @@
 
     @property
     def data_attributes(self):
-        attrs = super(UtilitiesTable, self).data_attributes
-        attrs['table'] = {'data-ams-location': absolute_url(self.context, self.request),
-                          'data-ams-delete-target': 'delete-utility.json'}
-        return attrs
+        attributes = super(UtilitiesTable, self).data_attributes
+        attributes['table'] = {'data-ams-location': absolute_url(self.context, self.request),
+                               'data-ams-delete-target': 'delete-utility.json'}
+        return attributes
 
 
 @adapter_config(name='trash', context=(Interface, IAdminLayer, UtilitiesTable), provides=IColumn)
@@ -84,7 +85,7 @@
     """Utilities trash column"""
 
     icon_hint = _("Delete utility")
-    permission = 'system.manage'
+    permission = MANAGE_SYSTEM_PERMISSION
 
     checker = lambda col, x: IOptionalUtility.providedBy(x)
 
@@ -98,7 +99,7 @@
         return list(self.context.getSiteManager().values())
 
 
-@pagelet_config(name='utilities.html', context=ISite, layer=IPyAMSLayer, permission='system.view')
+@pagelet_config(name='utilities.html', context=ISite, layer=IPyAMSLayer, permission=VIEW_SYSTEM_PERMISSION)
 @implementer(IInnerPage)
 class UtilitiesView(AdminView, ContainerView):
     """Control panel view"""
@@ -118,7 +119,7 @@
 
 
 @view_config(name='delete-utility.json', context=ISite, request_type=IPyAMSLayer,
-             permission='system.manage', renderer='json', xhr=True)
+             permission=MANAGE_SYSTEM_PERMISSION, renderer='json', xhr=True)
 def delete_utility(request):
     """Delete utility from site manager"""
     translate = request.localizer.translate
@@ -201,7 +202,7 @@
 #
 
 @viewlet_config(name='local-registrations.menu', layer=IAdminLayer, context=ISite, manager=IUtilitiesMenu,
-                permission='system.view', weight=1)
+                permission=VIEW_SYSTEM_PERMISSION, weight=1)
 class LocalRegistrationsMenuItem(MenuItem):
     """Local registrations menu"""
 
@@ -228,7 +229,7 @@
         return list(self.context.getSiteManager().registeredUtilities())
 
 
-@pagelet_config(name='local-registrations.html', context=ISite, layer=IPyAMSLayer, permission='system.view')
+@pagelet_config(name='local-registrations.html', context=ISite, layer=IPyAMSLayer, permission=VIEW_SYSTEM_PERMISSION)
 @implementer(IInnerPage)
 class LocalRegistrationsView(AdminView, ContainerView):
     """Registrations view"""
@@ -265,7 +266,7 @@
 #
 
 @viewlet_config(name='global-registrations.menu', layer=IAdminLayer, context=ISite, manager=IUtilitiesMenu,
-                permission='system.view', weight=2)
+                permission=VIEW_SYSTEM_PERMISSION, weight=2)
 class GlobalRegistrationsMenuItem(MenuItem):
     """Global registrations menu"""
 
@@ -292,7 +293,7 @@
         return list(registered_utilities())
 
 
-@pagelet_config(name='global-registrations.html', context=ISite, layer=IPyAMSLayer, permission='system.view')
+@pagelet_config(name='global-registrations.html', context=ISite, layer=IPyAMSLayer, permission=VIEW_SYSTEM_PERMISSION)
 @implementer(IInnerPage)
 class GlobalRegistrationsView(AdminView, ContainerView):
     """Global registrations view"""
--- a/src/pyams_zmi/interfaces/__init__.py	Thu Aug 27 11:27:44 2015 +0200
+++ b/src/pyams_zmi/interfaces/__init__.py	Thu Oct 08 09:42:02 2015 +0200
@@ -12,13 +12,14 @@
 
 __docformat__ = 'restructuredtext'
 
+
 # import standard library
 
 # import interfaces
-from zope.interface import Interface
+from pyramid.interfaces import IView
 
 # import packages
 
 
-class IAdminView(Interface):
+class IAdminView(IView):
     """Marker interface for administration views"""
--- a/src/pyams_zmi/site.py	Thu Aug 27 11:27:44 2015 +0200
+++ b/src/pyams_zmi/site.py	Thu Oct 08 09:42:02 2015 +0200
@@ -17,9 +17,10 @@
 
 # import interfaces
 from pyams_form.interfaces.form import IWidgetForm
-from pyams_skin.interfaces import IInnerPage, IPageHeader
-from pyams_skin.interfaces.configuration import IConfiguration
+from pyams_skin.interfaces import IInnerPage, IPageHeader, IContentTitle
+from pyams_skin.interfaces.configuration import IConfiguration, IBackOfficeConfiguration
 from pyams_skin.layer import IPyAMSLayer
+from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION, MANAGE_SYSTEM_PERMISSION
 from pyams_utils.interfaces.site import ISiteRoot
 from pyams_zmi.interfaces.menu import ISiteManagementMenu, IPropertiesMenu
 from pyams_zmi.layer import IAdminLayer
@@ -28,11 +29,11 @@
 from pyams_form.form import AJAXEditForm
 from pyams_pagelet.pagelet import pagelet_config
 from pyams_skin.page import DefaultPageHeaderAdapter
-from pyams_skin.viewlet.menu import MenuItem
-from pyams_utils.adapter import adapter_config
+from pyams_skin.viewlet.menu import MenuItem, MenuDivider
+from pyams_utils.adapter import adapter_config, ContextRequestViewAdapter
 from pyams_viewlet.manager import viewletmanager_config
 from pyams_viewlet.viewlet import viewlet_config
-from pyams_zmi.form import AdminEditForm
+from pyams_zmi.form import AdminEditForm, AdminDialogEditForm
 from pyramid.decorator import reify
 from pyramid.view import view_config
 from z3c.form import field
@@ -41,8 +42,21 @@
 from pyams_zmi import _
 
 
+@adapter_config(context=(ISiteRoot, IAdminLayer, Interface), provides=IContentTitle)
+class SiteRootTitleAdapter(ContextRequestViewAdapter):
+    """Site root title adapter"""
+
+    @property
+    def title(self):
+        return IBackOfficeConfiguration(self.context).title
+
+
+#
+# Configuration properties
+#
+
 @viewlet_config(name='properties.menu', layer=IAdminLayer, context=ISiteRoot, manager=ISiteManagementMenu,
-                permission='system.view', weight=1)
+                permission=VIEW_SYSTEM_PERMISSION, weight=1)
 @viewletmanager_config(name='properties.menu', layer=IAdminLayer, context=ISiteRoot, provides=IPropertiesMenu)
 @implementer(IPropertiesMenu)
 class PropertiesMenuItem(MenuItem):
@@ -53,14 +67,14 @@
     url = '#properties.html'
 
 
-@pagelet_config(name='properties.html', context=ISiteRoot, layer=IPyAMSLayer, permission='system.view')
+@pagelet_config(name='properties.html', context=ISiteRoot, layer=IPyAMSLayer, permission=VIEW_SYSTEM_PERMISSION)
 @implementer(IWidgetForm, IInnerPage)
 class PropertiesEditForm(AdminEditForm):
     """Properties edit form"""
 
     legend = _("Update main site properties")
-    ajax_handler = 'edit_properties.json'
-    edit_permission = 'system.manage'
+    ajax_handler = 'properties.json'
+    edit_permission = MANAGE_SYSTEM_PERMISSION
 
     @reify
     def fields(self):
@@ -77,8 +91,8 @@
         return IConfiguration(self.context)
 
 
-@view_config(name='edit_properties.json', context=ISiteRoot, request_type=IPyAMSLayer,
-             permission='system.manage', renderer='json', xhr=True)
+@view_config(name='properties.json', context=ISiteRoot, request_type=IPyAMSLayer,
+             permission=MANAGE_SYSTEM_PERMISSION, renderer='json', xhr=True)
 class PropertiesAJAXEditForm(AJAXEditForm, PropertiesEditForm):
     """Properties edit form, AJAX renderer"""
 
@@ -98,7 +112,75 @@
 
     @property
     def title(self):
-        config = IConfiguration(self.context)
-        return config.title
+        return IBackOfficeConfiguration(self.context).title
 
     icon_class = 'fa fa-fw fa-twitch'
+
+
+#
+# Back-office configuration properties
+#
+
+@viewlet_config(name='back-office-properties.menu', layer=IAdminLayer, context=ISiteRoot, manager=IPropertiesMenu,
+                permission=MANAGE_SYSTEM_PERMISSION, weight=1)
+class BackOfficePropertiesMenuItem(MenuItem):
+    """Back-office properties menu"""
+
+    label = _("Back-office properties...")
+    icon_class = 'fa-tachometer'
+    url = 'back-office-properties.html'
+    modal_target = True
+
+
+@viewlet_config(name='back-office-properties.divider', layer=IAdminLayer, context=ISiteRoot, manager=IPropertiesMenu,
+                permission=MANAGE_SYSTEM_PERMISSION, weight=2)
+class BackOfficePropertiesMenuDivider(MenuDivider):
+    """Back-office properties menu divider"""
+
+
+@pagelet_config(name='back-office-properties.html', context=ISiteRoot, layer=IPyAMSLayer,
+                permission=MANAGE_SYSTEM_PERMISSION)
+class BackOfficePropertiesEditForm(AdminDialogEditForm):
+    """Back-office properties edit form"""
+
+    legend = _("Update site back-office properties")
+    ajax_handler = 'back-office-properties.json'
+    edit_permission = MANAGE_SYSTEM_PERMISSION
+
+    dialog_class = 'modal-large'
+
+    @reify
+    def fields(self):
+
+        def get_configuration_interface(context):
+            for interface in implementedBy(context.__class__).interfaces():
+                if issubclass(interface, IBackOfficeConfiguration):
+                    return interface
+
+        content = self.getContent()
+        return field.Fields(get_configuration_interface(content)).omit('__name__')
+
+    def getContent(self):
+        return IBackOfficeConfiguration(self.context)
+
+    def updateWidgets(self, prefix=None):
+        super(BackOfficePropertiesEditForm, self).updateWidgets(prefix)
+        if 'login_header' in self.widgets:
+            self.widgets['login_header'].label_css_class = 'textarea'
+        if 'login_footer' in self.widgets:
+            self.widgets['login_footer'].label_css_class = 'textarea'
+
+
+@view_config(name='back-office-properties.json', context=ISiteRoot, request_type=IPyAMSLayer,
+             permission=MANAGE_SYSTEM_PERMISSION, renderer='json', xhr=True)
+class BackOfficePropertiesAJAXEditForm(AJAXEditForm, BackOfficePropertiesEditForm):
+    """Back-office properties edit form, AJAX renderer"""
+
+    def get_ajax_output(self, changes):
+        if changes:
+            return {'status': 'reload',
+                    'location': '#properties.html',
+                    'smallbox': self.request.localizer.translate(self.successMessage),
+                    'smallbox_status': 'success'}
+        else:
+            return AJAXEditForm.get_ajax_output(self, changes)
--- a/src/pyams_zmi/viewlet/menu/__init__.py	Thu Aug 27 11:27:44 2015 +0200
+++ b/src/pyams_zmi/viewlet/menu/__init__.py	Thu Oct 08 09:42:02 2015 +0200
@@ -17,6 +17,7 @@
 
 # import interfaces
 from pyams_skin.interfaces.viewlet import IMainMenusViewletManager
+from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION, MANAGE_SYSTEM_PERMISSION
 from pyams_zmi.interfaces.menu import ISiteManagementMenu, IControlPanelMenu, IContentManagementMenu
 from pyams_zmi.layer import IAdminLayer
 
@@ -30,30 +31,30 @@
 
 
 @viewlet_config(name='content.menu', layer=IAdminLayer, manager=IMainMenusViewletManager,
-                permission='system.view', weight=1)
+                permission=VIEW_SYSTEM_PERMISSION, weight=1)
 @viewletmanager_config(name='content.menu', layer=IAdminLayer, provides=IContentManagementMenu)
 @implementer(IContentManagementMenu)
 class ContentManagementMenu(Menu):
     """Content management menu"""
 
-    header = _("Content management")
+    _header = _("Content management")
 
 
 @viewlet_config(name='manage.menu', layer=IAdminLayer, manager=IMainMenusViewletManager,
-                permission='system.view', weight=100)
+                permission=VIEW_SYSTEM_PERMISSION, weight=100)
 @viewletmanager_config(name='manage.menu', layer=IAdminLayer, provides=ISiteManagementMenu)
 @implementer(ISiteManagementMenu)
 class SiteManagementMenu(Menu):
     """Site management menu"""
 
-    header = _("Site management")
+    _header = _("Site management")
 
 
 @viewlet_config(name='control-panel.menu', layer=IAdminLayer, manager=IMainMenusViewletManager,
-                permission='system.view', weight=9999)
+                permission=VIEW_SYSTEM_PERMISSION, weight=9999)
 @viewletmanager_config(name='control-panel.menu', layer=IAdminLayer, provides=IControlPanelMenu)
 @implementer(IControlPanelMenu)
 class ControlPanelMenu(Menu):
     """Control panel menu"""
 
-    header = _("Control panel")
+    _header = _("Control panel")