src/pyams_security/site.py
changeset 0 f04e1d0a0723
child 40 5ff311e24fdf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_security/site.py	Thu Feb 19 10:53:29 2015 +0100
@@ -0,0 +1,66 @@
+#
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+from pyams_security.interfaces import ISecurityManager
+from pyams_utils.interfaces.site import ISiteGenerations
+from zope.lifecycleevent.interfaces import IObjectCreatedEvent
+from zope.site.interfaces import INewLocalSite
+
+# import packages
+from pyams_security.plugin.admin import AdminAuthenticationPlugin
+from pyams_security.utility import SecurityManager
+from pyams_utils.registry import utility_config
+from pyams_utils.request import check_request
+from pyams_utils.site import check_required_utilities
+from pyramid.events import subscriber
+from zope.lifecycleevent import ObjectCreatedEvent
+
+
+REQUIRED_UTILITIES = ((ISecurityManager, '', SecurityManager, 'Security manager'),)
+
+
+@subscriber(INewLocalSite)
+def handle_new_local_site(event):
+    """Create a new negotiator when a site is created"""
+    site = event.manager.__parent__
+    check_required_utilities(site, REQUIRED_UTILITIES)
+
+
+@utility_config(name='PyAMS security', provides=ISiteGenerations)
+class SecurityGenerationsChecker(object):
+    """I18n generations checker"""
+
+    generation = 1
+
+    def evolve(self, site, current=None):
+        """Check for required utilities"""
+        check_required_utilities(site, REQUIRED_UTILITIES)
+
+
+@subscriber(IObjectCreatedEvent, context_selector=ISecurityManager)
+def handle_new_security_manager(event):
+    """Automatically create a new administration login"""
+    admin_auth = AdminAuthenticationPlugin()
+    admin_auth.prefix = 'system'
+    admin_auth.title = 'System manager authentication'
+    admin_auth.login = 'admin'
+    admin_auth.password = 'admin'
+    request = check_request()
+    request.registry.notify(ObjectCreatedEvent(admin_auth))
+    utility = event.object
+    utility['__system__'] = admin_auth