[Checkins] SVN: zope.security/trunk/ Move PermissionsVocabulary and PermissionIdsVocabulary from ``zope.app.security`` to here.

Dan Korostelev nadako at gmail.com
Wed Mar 11 16:54:43 EDT 2009


Log message for revision 97913:
  Move PermissionsVocabulary and PermissionIdsVocabulary from ``zope.app.security`` to here.
  
  Add zcml permission definition for the special "zope.Public" permission.
  
  Add coverage buildout parts and improve test coverage a little.
  

Changed:
  _U  zope.security/trunk/
  U   zope.security/trunk/CHANGES.txt
  U   zope.security/trunk/buildout.cfg
  A   zope.security/trunk/src/zope/security/configure.zcml
  U   zope.security/trunk/src/zope/security/permission.py

-=-

Property changes on: zope.security/trunk
___________________________________________________________________
Modified: svn:ignore
   - bin
build
dist
lib
develop-eggs
eggs
parts
.installed.cfg

   + bin
build
dist
lib
develop-eggs
eggs
parts
.installed.cfg
coverage


Modified: zope.security/trunk/CHANGES.txt
===================================================================
--- zope.security/trunk/CHANGES.txt	2009-03-11 20:04:22 UTC (rev 97912)
+++ zope.security/trunk/CHANGES.txt	2009-03-11 20:54:42 UTC (rev 97913)
@@ -5,8 +5,15 @@
 3.6.2 (unreleased)
 ------------------
 
-- ...
+- Added PermissionsVocabulary and PermissionIdsVocabulary vocabularies
+  to the ``zope.security.permission`` module. They were moved from
+  the ``zope.app.security`` package.
 
+- Add zcml permission definition for the special "zope.Public"
+  permission.
+
+- Improve test coverage.
+
 3.6.1 (2009-03-10)
 ------------------
 

Modified: zope.security/trunk/buildout.cfg
===================================================================
--- zope.security/trunk/buildout.cfg	2009-03-11 20:04:22 UTC (rev 97912)
+++ zope.security/trunk/buildout.cfg	2009-03-11 20:54:42 UTC (rev 97913)
@@ -1,6 +1,6 @@
 [buildout]
 develop = .
-parts = test python
+parts = test python coverage-test coverage-report
 
 [test]
 recipe = zc.recipe.testrunner
@@ -10,3 +10,14 @@
 recipe = zc.recipe.egg
 eggs = zope.security [untrustedpython]
 interpreter = python
+
+[coverage-test]
+recipe = zc.recipe.testrunner
+eggs = zope.security [test]
+defaults = ['--coverage', '../../coverage']
+
+[coverage-report]
+recipe = zc.recipe.egg
+eggs = z3c.coverage
+scripts = coverage=coverage-report
+arguments = ('coverage', 'coverage/report')

Added: zope.security/trunk/src/zope/security/configure.zcml
===================================================================
--- zope.security/trunk/src/zope/security/configure.zcml	                        (rev 0)
+++ zope.security/trunk/src/zope/security/configure.zcml	2009-03-11 20:54:42 UTC (rev 97913)
@@ -0,0 +1,23 @@
+<configure
+    xmlns="http://namespaces.zope.org/zope"
+    i18n_domain="zope"
+    >
+
+  <utility
+      component=".vocabulary.PermissionsVocabulary"
+      name="Permissions"
+      />
+
+  <utility
+      component=".vocabulary.PermissionIdsVocabulary"
+      name="Permission Ids"
+      />
+
+  <permission
+      id="zope.Public"
+      title="[public-permission] Public"
+      description="Special permission indicating unconditional access.
+                   Public resources are always accessible."
+      />
+
+</configure>

Modified: zope.security/trunk/src/zope/security/permission.py
===================================================================
--- zope.security/trunk/src/zope/security/permission.py	2009-03-11 20:04:22 UTC (rev 97912)
+++ zope.security/trunk/src/zope/security/permission.py	2009-03-11 20:54:42 UTC (rev 97913)
@@ -17,8 +17,10 @@
 """
 __docformat__ = "reStructuredText"
 
-from zope.interface import implements
+from zope.interface import implements, directlyProvides
 from zope.component import queryUtility, getUtilitiesFor
+from zope.schema.interfaces import IVocabularyFactory
+from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
 from zope.security.checker import CheckerPublic
 from zope.security.interfaces import IPermission
 
@@ -41,6 +43,11 @@
     Traceback (most recent call last):
     ...
     ValueError: ('Undefined permission id', 'y')
+    
+    The CheckerPublic always exists:
+    
+    >>> checkPermission(None, CheckerPublic)
+    
     """
     if permission_id is CheckerPublic:
         return
@@ -62,3 +69,108 @@
     for id, permission in getUtilitiesFor(IPermission, context):
         if id != u'zope.Public':
             yield id
+
+def PermissionsVocabulary(context=None):
+    """A vocabulary of permission IDs.
+
+    Term values are permissions, while term tokens are permission IDs.
+    
+    To illustrate, we need to register the permission IDs vocab:
+
+    >>> from zope.schema.vocabulary import getVocabularyRegistry
+    >>> registry = getVocabularyRegistry()
+    >>> registry.register('Permissions', PermissionsVocabulary)
+
+    We also need to register some sample permission utilities:
+
+    >>> from zope.security.interfaces import IPermission
+    >>> from zope.security.permission import Permission
+    >>> from zope.component import provideUtility
+    >>> a = Permission('a')
+    >>> b = Permission('b')
+    >>> provideUtility(a, IPermission, 'a')
+    >>> provideUtility(b, IPermission, 'b')
+
+    We can now lookup these permissions using the vocabulary:
+
+    >>> vocab = registry.get(None, 'Permissions')
+    >>> vocab.getTermByToken('a').value is a
+    True
+    >>> vocab.getTermByToken('b').value is b
+    True
+
+    """
+    terms = []
+    for id, permission in getUtilitiesFor(IPermission, context):
+        terms.append(SimpleTerm(permission, id))
+    return SimpleVocabulary(terms)
+
+directlyProvides(PermissionsVocabulary, IVocabularyFactory)
+
+def PermissionIdsVocabulary(context=None):
+    """A vocabulary of permission IDs.
+
+    Term values are the permission ID strings except for 'zope.Public', which
+    is the global permission CheckerPublic.
+
+    Term titles are the permission ID strings except for 'zope.Public', which
+    is shortened to 'Public'.
+
+    Terms are sorted by title except for 'Public', which always appears as
+    the first term.
+
+    To illustrate, we need to register the permission IDs vocab:
+
+    >>> from zope.schema.vocabulary import getVocabularyRegistry
+    >>> registry = getVocabularyRegistry()
+    >>> registry.register('Permission Ids', PermissionIdsVocabulary)
+
+    We also need to register some sample permission utilities, including
+    the special permission 'zope.Public':
+
+    >>> from zope.security.interfaces import IPermission
+    >>> from zope.security.permission import Permission
+    >>> from zope.component import provideUtility
+    >>> provideUtility(Permission('zope.Public'), IPermission, 'zope.Public')
+    >>> provideUtility(Permission('b'), IPermission, 'b')
+    >>> provideUtility(Permission('a'), IPermission, 'a')
+
+    We can now lookup these permissions using the vocabulary:
+
+    >>> vocab = registry.get(None, 'Permission Ids')
+
+    The non-public permissions 'a' and 'b' are string values:
+
+    >>> vocab.getTermByToken('a').value
+    u'a'
+    >>> vocab.getTermByToken('b').value
+    u'b'
+
+    However, the public permission value is CheckerPublic:
+
+    >>> vocab.getTermByToken('zope.Public').value is CheckerPublic
+    True
+
+    and its title is shortened:
+
+    >>> vocab.getTermByToken('zope.Public').title
+    u'Public'
+
+    The terms are sorted by title except for the public permission, which is
+    listed first:
+
+    >>> [term.title for term in vocab]
+    [u'Public', u'a', u'b']
+    """
+
+    terms = []
+    for name, permission in getUtilitiesFor(IPermission, context):
+        if name == 'zope.Public':
+            terms.append(SimpleTerm(CheckerPublic, 'zope.Public', u'Public'))
+        else:
+            terms.append(SimpleTerm(name, name, name))
+    terms.sort(cmp=lambda lhs, rhs: \
+               (lhs.token == 'zope.Public' and -1) or cmp(lhs.title, rhs.title))
+    return SimpleVocabulary(terms)
+
+directlyProvides(PermissionsVocabulary, IVocabularyFactory)



More information about the Checkins mailing list