[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/security/browser/ Sprinting with Jim:

Stephan Richter srichter at cosmos.phy.tufts.edu
Wed Oct 13 03:28:51 EDT 2004


Log message for revision 28036:
  Sprinting with Jim:
  
  Provide an ITerms implementation for the PrincipalSource.
  

Changed:
  A   Zope3/trunk/src/zope/app/security/browser/principalterms.py
  A   Zope3/trunk/src/zope/app/security/browser/principalterms.txt
  A   Zope3/trunk/src/zope/app/security/browser/tests.py

-=-
Added: Zope3/trunk/src/zope/app/security/browser/principalterms.py
===================================================================
--- Zope3/trunk/src/zope/app/security/browser/principalterms.py	2004-10-12 22:05:10 UTC (rev 28035)
+++ Zope3/trunk/src/zope/app/security/browser/principalterms.py	2004-10-13 07:28:49 UTC (rev 28036)
@@ -0,0 +1,54 @@
+##############################################################################
+#
+# 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.
+#
+##############################################################################
+"""Terms view for Principal Source
+
+$Id$
+"""
+__docformat__ = "reStructuredText"
+
+from zope.interface import implements
+from zope.publisher.interfaces.browser import IBrowserRequest
+
+from zope.app import zapi
+from zope.app.form.browser.interfaces import ITerms
+from zope.app.security.interfaces import IPrincipalSource
+
+class Term(object):
+
+    def __init__(self, token, title):
+        self.token = token
+        self.title = title
+
+
+class PrincipalTerms(object):
+    implements(ITerms)
+    __used_for__ = IPrincipalSource, IBrowserRequest
+
+    def __init__(self, context, request):
+        self.context = context
+
+    def getTerm(self, principal_id):
+        if principal_id not in self.context:
+            raise LookupError, principal_id
+
+        auth = zapi.getService(zapi.servicenames.Authentication)
+        principal = auth.getPrincipal(principal_id)
+
+        if principal is None:
+            raise LookupError, principal_id
+
+        return Term(principal_id.encode('base64').strip(), principal.title)
+
+    def getValue(self, token):
+        return token.decode('base64')

Added: Zope3/trunk/src/zope/app/security/browser/principalterms.txt
===================================================================
--- Zope3/trunk/src/zope/app/security/browser/principalterms.txt	2004-10-12 22:05:10 UTC (rev 28035)
+++ Zope3/trunk/src/zope/app/security/browser/principalterms.txt	2004-10-13 07:28:49 UTC (rev 28036)
@@ -0,0 +1,62 @@
+Principal Terms
+===============
+
+Principal Terms are used to support browser interfaces for searching principal
+sources. They provide access to tokens and titles for values. The principal
+terms view uses an authentication service to get principal titles. Let's
+create an authentication service to demonstrate how this works:
+
+  >>> class Principal:
+  ...     def __init__(self, id, title):
+  ...         self.id, self.title = id, title
+
+  >>> from zope.interface import implements
+  >>> from zope.app.security.interfaces import IAuthenticationService
+  >>> class AuthService:
+  ...     implements(IAuthenticationService)
+  ...     data = {'jim': 'Jim Fulton', 'stephan': 'Stephan Richter'}
+  ...
+  ...     def getPrincipal(self, id):
+  ...         title = self.data.get(id)
+  ...         if title is not None:
+  ...             return Principal(id, title)
+
+Now we need to install the authentication service:
+
+  >>> from zope.app.tests import ztapi
+  >>> ztapi.provideService('Authentication', AuthService(), 
+  ...                      IAuthenticationService)
+
+We need a principal source so that we can create a view from it.
+
+  >>> from zope.app import zapi
+  >>> class PrincipalSource:
+  ...     def __contains__(self, id):
+  ...          auth = zapi.getService('Authentication')
+  ...          principal = auth.getPrincipal(id)
+  ...          return principal is not None
+
+Now we can create an terms view:
+
+  >>> from zope.app.security.browser.principalterms import PrincipalTerms
+  >>> terms = PrincipalTerms(PrincipalSource(), None)
+  
+Now we can ask the terms view for terms:
+
+  >>> term = terms.getTerm('stephan')
+  >>> term.title
+  'Stephan Richter'
+  >>> term.token
+  'c3RlcGhhbg=='
+
+If we ask for a term that does not exist, we get a lookup error:
+
+  >>> terms.getTerm('bob')
+  Traceback (most recent call last):
+  ...
+  LookupError: bob
+
+If we have a token, we can get the principal id for it.
+
+  >>> terms.getValue('c3RlcGhhbg==')
+  'stephan'
\ No newline at end of file

Added: Zope3/trunk/src/zope/app/security/browser/tests.py
===================================================================
--- Zope3/trunk/src/zope/app/security/browser/tests.py	2004-10-12 22:05:10 UTC (rev 28035)
+++ Zope3/trunk/src/zope/app/security/browser/tests.py	2004-10-13 07:28:49 UTC (rev 28036)
@@ -0,0 +1,32 @@
+##############################################################################
+#
+# 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.
+#
+##############################################################################
+"""Security Views Tests
+
+$Id: tests.py 27985 2004-10-12 08:00:42Z srichter $
+"""
+__docformat__ = "reStructuredText"
+import unittest
+from zope.testing import doctest
+from zope.app.tests import placelesssetup
+
+def test_suite():
+    return unittest.TestSuite((
+        doctest.DocFileSuite('principalterms.txt',
+                             setUp=placelesssetup.setUp,
+                             tearDown=placelesssetup.tearDown),
+        ))
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')
+



More information about the Zope3-Checkins mailing list