[Zope3-checkins] SVN: Zope3/trunk/ Restructured local Role implementation a bit. Everything is backward

Stephan Richter srichter at cosmos.phy.tufts.edu
Sun Dec 5 16:16:01 EST 2004


Log message for revision 28569:
  Restructured local Role implementation a bit. Everything is backward 
  compatible. Still need to create a DB generation.
  
  

Changed:
  U   Zope3/trunk/doc/CHANGES.txt
  U   Zope3/trunk/src/zope/app/browser.zcml
  U   Zope3/trunk/src/zope/app/schema/tests/test_fieldfactory.zcml
  U   Zope3/trunk/src/zope/app/securitypolicy/browser/configure.zcml
  U   Zope3/trunk/src/zope/app/securitypolicy/configure.zcml
  U   Zope3/trunk/src/zope/app/securitypolicy/role.py
  A   Zope3/trunk/src/zope/app/securitypolicy/tests/test_role.py

-=-
Modified: Zope3/trunk/doc/CHANGES.txt
===================================================================
--- Zope3/trunk/doc/CHANGES.txt	2004-12-05 21:08:25 UTC (rev 28568)
+++ Zope3/trunk/doc/CHANGES.txt	2004-12-05 21:16:00 UTC (rev 28569)
@@ -149,6 +149,9 @@
 
     Restructuring
 
+      - Removed special `RoleRegistration` now that we have events for
+        notification. Also, renamed `PersistentRole` to `LocalRole`.
+
       - Refactored Registration component for local components
       
         + Deprecated the `activated()` and `deactivated()` method. It is now

Modified: Zope3/trunk/src/zope/app/browser.zcml
===================================================================
--- Zope3/trunk/src/zope/app/browser.zcml	2004-12-05 21:08:25 UTC (rev 28568)
+++ Zope3/trunk/src/zope/app/browser.zcml	2004-12-05 21:16:00 UTC (rev 28569)
@@ -7,6 +7,7 @@
 
   <include package="zope.app.exception.browser" />
   <include package="zope.app.traversing.browser" />
+  <include package="zope.app.security.browser" />
 
   <include package="zope.app.container.browser" />
   <include package="zope.app.registration.browser" />

Modified: Zope3/trunk/src/zope/app/schema/tests/test_fieldfactory.zcml
===================================================================
--- Zope3/trunk/src/zope/app/schema/tests/test_fieldfactory.zcml	2004-12-05 21:08:25 UTC (rev 28568)
+++ Zope3/trunk/src/zope/app/schema/tests/test_fieldfactory.zcml	2004-12-05 21:16:00 UTC (rev 28569)
@@ -5,6 +5,7 @@
   <include package="zope.app.component" file="meta.zcml" />
   <include package="zope.app.publisher" file="meta.zcml" />
   <include package="zope.app.security" file="meta.zcml" />
+  <include package="zope.app.utility" file="meta.zcml" />
   <include package="zope.app.security" file="configure.zcml" />
 
   <content class="zope.schema.Field">

Modified: Zope3/trunk/src/zope/app/securitypolicy/browser/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/securitypolicy/browser/configure.zcml	2004-12-05 21:08:25 UTC (rev 28568)
+++ Zope3/trunk/src/zope/app/securitypolicy/browser/configure.zcml	2004-12-05 21:16:00 UTC (rev 28569)
@@ -1,10 +1,10 @@
 <zope:configure 
-   xmlns:zope="http://namespaces.zope.org/zope"
-   xmlns="http://namespaces.zope.org/browser"
-   i18n_domain="zope">
+    xmlns:zope="http://namespaces.zope.org/zope"
+    xmlns="http://namespaces.zope.org/browser"
+    i18n_domain="zope">
 
   <tool
-      interface="zope.app.securitypolicy.interfaces.IRole"
+      interface="..interfaces.IRole"
       title="Role"
       description="Roles are used to combine permissions and can be assigned
       to principals."
@@ -15,46 +15,32 @@
   <addform
       label="New Role"
       name="addRole.html"
-      schema="zope.app.securitypolicy.interfaces.IRole"
+      schema="..interfaces.IRole"
       permission="zope.Security"
-      content_factory="zope.app.securitypolicy.role.PersistentRole"
+      content_factory="..role.LocalRole"
       arguments="title"
       keyword_arguments="description"
       fields="title description" />
 
   <editform
-      schema="zope.app.securitypolicy.interfaces.IRole"
+      schema="..interfaces.IRole"
       name="edit.html"
       menu="zmi_views" title="Edit"
       permission="zope.ManageContent"/>
 
   <defaultView
-      for="zope.app.securitypolicy.interfaces.IRole"
+      for="..interfaces.IRole"
       name="edit.html" />
 
   <!-- Menu entry for "Add Utility" menu -->
   <addMenuItem
-      class="zope.app.securitypolicy.role.PersistentRole"
+      class="..role.LocalRole"
       title="Role"
-      description="A Persistent Role"
+      description="A Security Role"
       permission="zope.Security"
       view="addRole.html"
-      />
+      />  
 
-  <!-- Custom Role registration screen -->
-  <addform
-      label="New Role Registration"
-      for="zope.app.securitypolicy.interfaces.IRole"
-      name="addRegistration.html"
-      schema="zope.app.utility.interfaces.IUtilityRegistration"
-      class="zope.app.utility.browser.AddRegistration"
-      permission="zope.ManageServices"
-      content_factory="zope.app.securitypolicy.role.RoleRegistration"
-      arguments="name interface componentPath"
-      set_after_add="status"
-      fields="name interface componentPath permission status" />
-
-
 <!-- Role Permissions -->
 
   <!-- Note that we've moved this to the site manager! -->
@@ -64,19 +50,26 @@
       for="zope.app.site.interfaces.ISiteManager"
       permission="zope.Security"
       class=".rolepermissionview.RolePermissionView">
-
-    <page name="AllRolePermissions.html" template="manage_access.pt" 
-          menu="zmi_actions" title="Role-Permissions"
-    />
-          <!-- menu="zmi_actions" title="Role Permissions" / -->
-    <page name="RolePermissions.html" template="manage_roleform.pt" />
-    <page name="RolesWithPermission.html" template="manage_permissionform.pt"/>
-
+    <page
+        name="AllRolePermissions.html" 
+        template="manage_access.pt" 
+        menu="zmi_actions" title="Role-Permissions"
+        />
+    <!-- menu="zmi_actions" title="Role Permissions" / -->
+    <page
+        name="RolePermissions.html" 
+        template="manage_roleform.pt" 
+        />
+    <page
+        name="RolesWithPermission.html" 
+        template="manage_permissionform.pt"
+        />
   </pages>
 
   <zope:class class=".rolepermissionview.PermissionRoles">
-    <zope:require permission="zope.Security"
-                  attributes="roles rolesInfo id title description" />
+    <zope:require 
+        permission="zope.Security"
+        attributes="roles rolesInfo id title description" />
   </zope:class>
 
   <zope:class class=".rolepermissionview.RolePermissions">
@@ -88,12 +81,13 @@
 <!-- Granting Roles and Permissions to Principals -->
 
   <page
-    for="zope.app.annotation.interfaces.IAnnotatable"
-    name="grant.html"
-    permission="zope.Security"
-    template="granting.pt" 
-    class=".granting.Granting"
-    menu="zmi_actions" title="Grant" />
+      for="zope.app.annotation.interfaces.IAnnotatable"
+      name="grant.html"
+      permission="zope.Security"
+      template="granting.pt" 
+      class=".granting.Granting"
+      menu="zmi_actions" title="Grant" 
+      />
 
 
 </zope:configure>

Modified: Zope3/trunk/src/zope/app/securitypolicy/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/securitypolicy/configure.zcml	2004-12-05 21:08:25 UTC (rev 28568)
+++ Zope3/trunk/src/zope/app/securitypolicy/configure.zcml	2004-12-05 21:16:00 UTC (rev 28569)
@@ -58,7 +58,7 @@
     <allow interface=".interfaces.IRole" />
   </content>
 
-  <localUtility class=".role.PersistentRole">
+  <localUtility class=".role.LocalRole">
     <factory
         id="zope.security.role.Role"
         />
@@ -68,14 +68,16 @@
         />
   </localUtility>
 
-  <content class=".role.RoleRegistration">
-    <require
-      permission="zope.ManageServices"
-      attributes="required with provided factory"
-      interface="zope.app.utility.interfaces.IUtilityRegistration"
-      set_schema="zope.app.utility.interfaces.IUtilityRegistration" />
-  </content>
+  <subscriber
+     for="..registration.interfaces.IRegistrationActivatedEvent"
+     factory=".role.setIdOnActivation"
+     />
 
+  <subscriber
+     for="..registration.interfaces.IRegistrationDeactivatedEvent"
+     factory=".role.unsetIdOnDeactivation"
+     />
+
   <!-- Include browser package -->
   <include package=".browser"/>
 

Modified: Zope3/trunk/src/zope/app/securitypolicy/role.py
===================================================================
--- Zope3/trunk/src/zope/app/securitypolicy/role.py	2004-12-05 21:08:25 UTC (rev 28568)
+++ Zope3/trunk/src/zope/app/securitypolicy/role.py	2004-12-05 21:16:00 UTC (rev 28569)
@@ -19,10 +19,11 @@
 from zope.interface import implements
 
 from zope.app import zapi
-from zope.app.container.contained import Contained
+from zope.app.location import Location
 from zope.app.securitypolicy.interfaces import IRole
-from zope.app.utility import UtilityRegistration
 
+from zope.app.i18n import ZopeMessageIDFactory as _
+NULL_ID = _('<role not activated>')
 
 class Role(object):
     implements(IRole)
@@ -33,28 +34,108 @@
         self.description = description
 
 
-class PersistentRole(Contained, Persistent):
+class LocalRole(Persistent, Location):
     implements(IRole)
 
     def __init__(self, title, description=""):
-        self.id = '<role not activated>'
+        self.id = NULL_ID
         self.title = title
         self.description = description
 
+# BBB: Renamed component on 12/05/2004
+PersistentRole = LocalRole
+from zope.app.utility import UtilityRegistration
+RoleRegistration = UtilityRegistration
 
-class RoleRegistration(UtilityRegistration):
-    """Role Registration
+def setIdOnActivation(event):
+    """Set the permission id upon registration activation.
 
-    We have a custom registration here, since we want active registrations to
-    set the id of the role.
+    Let's see how this notifier can be used. First we need to create an event
+    using the permission instance and a registration stub:
+
+    >>> class Registration:
+    ...     def __init__(self, obj, name):
+    ...         self.object = obj
+    ...         self.name = name
+    ...
+    ...     def getComponent(self):
+    ...         return self.object
+
+    >>> role1 = LocalRole('Role 1', 'A first role')
+    >>> role1.id
+    u'<role not activated>'
+    >>> from zope.app.registration import registration 
+    >>> event = registration.RegistrationActivatedEvent(
+    ...     Registration(role1, 'role1'))
+
+    Now we pass the event into this function, and the id of the role should be
+    set to 'role1'.
+
+    >>> setIdOnActivation(event)
+    >>> role1.id
+    'role1'
+
+    If the function is called and the component is not a local permission,
+    nothing is done:
+
+    >>> class Foo:
+    ...     id = 'no id'
+    >>> foo = Foo()
+    >>> event = registration.RegistrationActivatedEvent(
+    ...     Registration(foo, 'foo'))
+    >>> setIdOnActivation(event)
+    >>> foo.id
+    'no id'
     """
-    def activated(self):
-        role = self.getComponent()
-        role.id = self.name
+    role = event.object.getComponent()
+    if isinstance(role, LocalRole):
+        role.id = event.object.name
 
-    def deactivated(self):
-        role = self.getComponent()
-        role.id = '<role not activated>'
+
+def unsetIdOnDeactivation(event):
+    """Unset the permission id up registration deactivation.
+
+    Let's see how this notifier can be used. First we need to create an event
+    using the permission instance and a registration stub:
+
+    >>> class Registration:
+    ...     def __init__(self, obj, name):
+    ...         self.object = obj
+    ...         self.name = name
+    ...
+    ...     def getComponent(self):
+    ...         return self.object
+
+    >>> role1 = LocalRole('Role 1', 'A first role')
+    >>> role1.id = 'role1'
+
+    >>> from zope.app.registration import registration 
+    >>> event = registration.RegistrationDeactivatedEvent(
+    ...     Registration(role1, 'role1'))
+
+    Now we pass the event into this function, and the id of the role should be
+    set to NULL_ID.
+
+    >>> unsetIdOnDeactivation(event)
+    >>> role1.id
+    u'<role not activated>'
+
+    If the function is called and the component is not a local role,
+    nothing is done:
+
+    >>> class Foo:
+    ...     id = 'foo'
+    >>> foo = Foo()
+    >>> event = registration.RegistrationDeactivatedEvent(
+    ...     Registration(foo, 'foo'))
+    >>> unsetIdOnDeactivation(event)
+    >>> foo.id
+    'foo'
+    """
+    role = event.object.getComponent()
+    if isinstance(role, LocalRole):
+        role.id = NULL_ID
+
     
 
 def checkRole(context, role_id):

Added: Zope3/trunk/src/zope/app/securitypolicy/tests/test_role.py
===================================================================
--- Zope3/trunk/src/zope/app/securitypolicy/tests/test_role.py	2004-12-05 21:08:25 UTC (rev 28568)
+++ Zope3/trunk/src/zope/app/securitypolicy/tests/test_role.py	2004-12-05 21:16:00 UTC (rev 28569)
@@ -0,0 +1,29 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# 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.
+#
+##############################################################################
+"""Doctests for 'permission' module.
+
+$Id: test_permission.py 26201 2004-07-08 10:42:46Z srichter $
+"""
+import unittest
+from zope.testing.doctestunit import DocTestSuite
+
+
+def test_suite():
+    return unittest.TestSuite((
+        DocTestSuite('zope.app.securitypolicy.role'),
+        ))
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')
+



More information about the Zope3-Checkins mailing list