[Zope3-checkins] SVN: ldapauth/trunk/ Added a search scope vocabulary

Roger Ineichen roger at projekt01.ch
Thu Jul 22 21:20:53 EDT 2004


Log message for revision 26692:
  Added a search scope vocabulary


Changed:
  U   ldapauth/trunk/browser/source.zcml
  U   ldapauth/trunk/configure.zcml
  U   ldapauth/trunk/interfaces.py
  U   ldapauth/trunk/source.py
  A   ldapauth/trunk/tests/test_vocabulary.py
  A   ldapauth/trunk/vocabulary.py


-=-
Modified: ldapauth/trunk/browser/source.zcml
===================================================================
--- ldapauth/trunk/browser/source.zcml	2004-07-23 01:16:46 UTC (rev 26691)
+++ ldapauth/trunk/browser/source.zcml	2004-07-23 01:20:53 UTC (rev 26692)
@@ -48,7 +48,8 @@
         label="Edit LDAP-based Principal Source" 
         name="edit.html"        
         menu="zmi_views" title="Edit"
-        fields="host port basedn login_attribute manager_dn manager_passwd" 
+        fields="host port basedn login_attribute manager_dn manager_passwd
+                search_scope" 
         permission="zope.ManageContent" />
 
     <!-- connetion test view -->

Modified: ldapauth/trunk/configure.zcml
===================================================================
--- ldapauth/trunk/configure.zcml	2004-07-23 01:16:46 UTC (rev 26691)
+++ ldapauth/trunk/configure.zcml	2004-07-23 01:20:53 UTC (rev 26692)
@@ -33,6 +33,11 @@
 
     </content>
 
+    <!-- LDAP search scope vocabulary -->
+    <vocabulary
+        name="LDAP_SEARCH_SCOPE"
+        factory=".vocabulary.SearchScopeVocabulary" />
+
     <!-- Pluggable LDAP manager adapter -->
     <adapter
         factory="ldapauth.manager.LDAPManagerAdapter"

Modified: ldapauth/trunk/interfaces.py
===================================================================
--- ldapauth/trunk/interfaces.py	2004-07-23 01:16:46 UTC (rev 26691)
+++ ldapauth/trunk/interfaces.py	2004-07-23 01:20:53 UTC (rev 26692)
@@ -17,14 +17,16 @@
 """
 from zope.interface import Interface
 
-from zope.schema import TextLine, Int, List, Password
+from zope.schema import TextLine, Int, List, Password, Choice
 from zope.app.i18n import ZopeMessageIDFactory as _
 from zope.app.pluggableauth.interfaces import IPrincipalSource
 
 
 
 class ILDAPBasedPrincipalSource(IPrincipalSource):
-    """Describe LDAP-based authentication sources."""
+    """Describe LDAP-based authentication sources.
+    
+    """
 
     host = TextLine(
             title = _(u'Hostname'),
@@ -52,8 +54,14 @@
             title = _(u'Manager password'),
             description = _(u"Manager's password"))
 
+    search_scope = Choice(
+            title = _(u'Search Scope'),
+            description = _(u"Scope for the LDAP search"),
+            default= 1,
+            vocabulary = "LDAP_SEARCH_SCOPE")
 
 
+
 class ILDAPManager(Interface):
     """A LDAP server manager."""
 

Modified: ldapauth/trunk/source.py
===================================================================
--- ldapauth/trunk/source.py	2004-07-23 01:16:46 UTC (rev 26691)
+++ ldapauth/trunk/source.py	2004-07-23 01:20:53 UTC (rev 26692)
@@ -45,6 +45,7 @@
         self.login_attribute = login_attribute
         self.manager_dn = manager_dn
         self.manager_passwd = manager_passwd
+        self.search_scope = 1
         self.__cached = []
     
     ### IContainer-related methods
@@ -121,7 +122,7 @@
     def __findInLDAP(self, login):
         l = self.__connect()
         l.simple_bind_s(self.manager_dn, self.manager_passwd)
-        lsearch = l.search_s(self.basedn, ldap.SCOPE_ONELEVEL,
+        lsearch = l.search_s(self.basedn, self.search_scope,
                 '(%s=%s)' % (self.login_attribute, login))
         if lsearch:
             uid_dn, uid_dict = lsearch[0]
@@ -149,7 +150,7 @@
             search = '(%s=*%s*)' % (self.login_attribute, name)
         l = self.__connect()
         l.simple_bind_s(self.manager_dn, self.manager_passwd)
-        lsearch = l.search_s(self.basedn, ldap.SCOPE_ONELEVEL, search)
+        lsearch = l.search_s(self.basedn, self.search_scope, search)
         
         principals = []
         for node in lsearch:

Added: ldapauth/trunk/tests/test_vocabulary.py
===================================================================
--- ldapauth/trunk/tests/test_vocabulary.py	2004-07-23 01:16:46 UTC (rev 26691)
+++ ldapauth/trunk/tests/test_vocabulary.py	2004-07-23 01:20:53 UTC (rev 26692)
@@ -0,0 +1,89 @@
+##############################################################################
+#
+# Copyright (c) 2003 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.
+#
+##############################################################################
+"""Tests for Global Wiki Source Type Service.
+
+$Id: test_vocabulary.py 26248 2004-07-08 22:26:40Z srichter $
+"""
+import unittest
+
+from ldapauth.vocabulary import SearchScopeVocabulary
+from zope.component.tests.placelesssetup import PlacelessSetup
+from zope.schema.interfaces import \
+     ITokenizedTerm, IVocabulary, IVocabularyTokenized
+
+
+
+class SearchScopeVocabularyTest(PlacelessSetup, unittest.TestCase):
+
+    def setUp(self):
+        super(SearchScopeVocabularyTest, self).setUp()
+        self.vocab = SearchScopeVocabulary(None)
+
+    def test_Interface(self):
+        self.failUnless(IVocabulary.providedBy(self.vocab))
+        self.failUnless(IVocabularyTokenized.providedBy(self.vocab))
+
+    def test_contains(self):
+        self.failUnless(None in self.vocab)
+        self.failUnless(0 in self.vocab)
+        self.failUnless(1 in self.vocab)
+        self.failUnless(2 in self.vocab)
+        self.failIf('' in self.vocab)
+        self.failIf('base' in self.vocab)
+        self.failIf('one' in self.vocab)
+        self.failIf('sub' in self.vocab)
+
+    def test_iter(self):
+        self.failUnless(None in [term.value for term in self.vocab])
+        self.failUnless(0 in [term.value for term in self.vocab])
+        self.failUnless(1 in [term.value for term in self.vocab])
+        self.failUnless(2 in [term.value for term in self.vocab])
+        self.failIf('' in [term.value for term in iter(self.vocab)])
+        self.failIf('one' in [term.value for term in iter(self.vocab)])
+        self.failIf('base' in [term.value for term in iter(self.vocab)])
+        self.failIf('sub' in [term.value for term in iter(self.vocab)])
+
+    def test_len(self):
+        self.assertEqual(len(self.vocab), 4)
+
+    def test_getQuery(self):
+        self.assertEqual(self.vocab.getQuery(), None)
+
+    def test_getTerm(self):
+        self.assertEqual(self.vocab.getTerm(None).title, '')
+        self.assertEqual(self.vocab.getTerm(0).title, 'base')
+        self.assertEqual(self.vocab.getTerm(1).title, 'one')
+        self.assertEqual(self.vocab.getTerm(2).title, 'sub')
+        self.assertRaises(
+            LookupError, self.vocab.getTerm, ('base',))
+
+    def test_getTermByToken(self):
+        vocab = self.vocab
+        self.assertEqual(vocab.getTermByToken('None').title, '')
+        self.assertEqual(vocab.getTermByToken('0').title, 'base')
+        self.assertEqual(vocab.getTermByToken('1').title, 'one')
+        self.assertEqual(vocab.getTermByToken('2').title, 'sub')
+        self.assertRaises(
+            LookupError, vocab.getTermByToken, (None,))
+        self.assertRaises(
+            LookupError, vocab.getTermByToken, ('base',))
+
+
+def test_suite():
+    return unittest.TestSuite((
+        unittest.makeSuite(SearchScopeVocabularyTest),
+        ))
+
+if __name__ == '__main__':
+    unittest.main()


Property changes on: ldapauth/trunk/tests/test_vocabulary.py
___________________________________________________________________
Name: svn:eol-style
   + native

Added: ldapauth/trunk/vocabulary.py
===================================================================
--- ldapauth/trunk/vocabulary.py	2004-07-23 01:16:46 UTC (rev 26691)
+++ ldapauth/trunk/vocabulary.py	2004-07-23 01:20:53 UTC (rev 26692)
@@ -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.
+#
+##############################################################################
+"""Vocabulary for the search scope.
+
+$Id: vocabulary.py 25177 2004-06-02 13:17:31Z rogerineichen $
+"""
+import ldapurl
+
+from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
+
+from zope.app import zapi
+from zope.app.renderer.interfaces import ISource
+
+
+
+def SearchScopeVocabulary(context):
+    """Provide a select field for to select the search scope.
+       
+    For to select the search scope the ldap package use the 
+    following definition:
+    
+    LDAP_SCOPE_BASE = 0
+    LDAP_SCOPE_ONELEVEL = 1
+    LDAP_SCOPE_SUBTREE = 2
+    
+    SEARCH_SCOPE_STR = {None:'',0:'base',1:'one',2:'sub'}
+    
+    SEARCH_SCOPE = {
+      '':None,
+      # the search scope strings defined in RFC2255
+      'base':LDAP_SCOPE_BASE,
+      'one':LDAP_SCOPE_ONELEVEL,
+      'sub':LDAP_SCOPE_SUBTREE,
+    }
+    
+    """
+    scopeDict = ldapurl.SEARCH_SCOPE_STR
+    
+    #SEARCH_SCOPE_STR = {None:'',0:'base',1:'one',2:'sub'}
+    
+    return SimpleVocabulary(
+        [SimpleTerm(key, title=value) for key, value in 
+         scopeDict.items()])



More information about the Zope3-Checkins mailing list