summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJens Vagelpohl <jens@netz.ooo>2001-08-27 17:52:59 +0000
committerJens Vagelpohl <jens@netz.ooo>2001-08-27 17:52:59 +0000
commit4b352301aa012b253ab47bc59beb85206fdfe41f (patch)
tree3ab1bb0271c4c3c443796a9922c56c0c83438584
parentd4dd251a63788f639f21844dace41ac0e06b8fe9 (diff)
downloadLDAPUserManager-4b352301aa012b253ab47bc59beb85206fdfe41f.zip
LDAPUserManager-4b352301aa012b253ab47bc59beb85206fdfe41f.tar.gz
Check-in of unicode changes by Florent Guillaume
-rw-r--r--LDAPShared.py54
-rw-r--r--LDAPUserManager.py29
2 files changed, 45 insertions, 38 deletions
diff --git a/LDAPShared.py b/LDAPShared.py
index 6573d11..ac1c18b 100644
--- a/LDAPShared.py
+++ b/LDAPShared.py
@@ -29,14 +29,46 @@ try:
except ImportError:
unicode_enabled = 0
+ # backward-compatible conversion functions
+ # Kindly provided by Florent Guillaume
+ def utf8_to_latin1_subone(m):
+ xy = m.group()
+ v = ((ord(xy[0])-0xc0)<<6) + ord(xy[1])-0x80
+ if v < 0x80: return xy # illegal UTF-8, don't translate
+ return chr(v)
+
+ def latin1_to_utf8_subone(m):
+ c = m.group()
+ v = ord(c)
+ if v < 0x80: return c
+ return chr((v>>6)+0xc0)+chr((v&0x3f)+0x80)
+
class LDAPShared:
security = ClassSecurityInfo()
+ def utf8_to_latin1_nonunicode(self, s):
+ return self.utf8_match.sub(utf8_to_latin1_subone, s)
+
+ def latin1_to_utf8_nonunicode(self, s):
+ return self.latin1_match.sub(latin1_to_utf8_subone, s)
+
+ def utf8_to_latin1_unicode(self, s):
+ return self.encodeLatin1(self.decodeUTF8(s)[0])[0]
+
+ def latin1_to_utf8_unicode(self, s):
+ return unicode(s, 'latin1').encode('UTF-8')
+
if unicode_enabled:
- nonascii = re.compile(r'[^\000-\177]')
encodeLatin1, decodeLatin1 = codecs.lookup('latin1')[:2]
encodeUTF8, decodeUTF8 = codecs.lookup('UTF-8')[:2]
+ latin1_to_utf8 = latin1_to_utf8_unicode
+ utf8_to_latin1 = utf8_to_latin1_unicode
+ else:
+ utf8_match = re.compile(r'([\300-\337][\200-\277])')
+ latin1_match = re.compile(r'([\200-\377])')
+ latin1_to_utf8 = latin1_to_utf8_nonunicode
+ utf8_to_latin1 = utf8_to_latin1_nonunicode
def _connect(self, client_method=None, REQUEST=None):
@@ -100,23 +132,16 @@ class LDAPShared:
self._disconnect(connection)
raise
- if unicode_enabled and self.nonascii.search(search_string):
- search_string = unicode(search_string, 'latin1').encode('UTF-8')
+ search_string = self.latin1_to_utf8(search_string)
try:
res = connection.search_s(search_base, search_scope,
search_string, attrs)
- if unicode_enabled:
- is_unicode = self.nonascii.search
- for rec_dn, rec_dict in res:
- for key, value in rec_dict.items():
- for i in range(len(value)):
- val = value[i]
- if is_unicode(val):
- uni_str = self.decodeUTF8(val)[0]
- value[i] = self.encodeLatin1(uni_str)[0]
-
+ for rec_dn, rec_dict in res:
+ for key, value in rec_dict.items():
+ for i in range(len(value)):
+ value[i] = self.utf8_to_latin1(value[i])
except _ldap.NO_SUCH_OBJECT:
res = '###ERROR###: Cannot find %s under %s!' % (
@@ -189,9 +214,6 @@ class LDAPShared:
lscope = ldap_scopes[self.users_scope]
users = []
- if unicode_enabled and self.nonascii.search(search_term):
- search_term = unicode(search_term, 'latin1').encode('UTF-8')
-
if search_param == 'dn':
users_base = search_term
search_str = 'objectClass=*'
diff --git a/LDAPUserManager.py b/LDAPUserManager.py
index 6fca40a..665ca21 100644
--- a/LDAPUserManager.py
+++ b/LDAPUserManager.py
@@ -13,7 +13,7 @@ from base64 import encodestring, decodestring
from Globals import DTMLFile, MessageDialog, package_home, InitializeClass
from OFS.SimpleItem import SimpleItem
from AccessControl import ClassSecurityInfo
-from LDAPShared import LDAPShared, ldap_scopes, unicode_enabled
+from LDAPShared import LDAPShared
try:
import _ldap
@@ -159,12 +159,8 @@ class LDAPUserManager( SimpleItem, LDAPShared ):
attr_val = map(string.strip, string.split(attr_val, ','))
if attr_val != ['']:
- if unicode_enabled:
- for i in range(len(attr_val)):
- val = attr_val[i]
- if self.nonascii.search(val):
- uni_str = self.decodeLatin1(val)[0]
- attr_val[i] = self.encodeUTF8(uni_str)[0]
+ for i in range(len(attr_val)):
+ attr_val[i] = self.latin1_to_utf8(attr_val[i])
attribute_list.append((attribute, attr_val))
@@ -366,12 +362,8 @@ class LDAPUserManager( SimpleItem, LDAPShared ):
if type(prop_value) == type(''):
prop_value = map(string.strip, string.split(prop_value, ','))
- if unicode_enabled:
- for i in range(len(prop_value)):
- val = prop_value[i]
- if self.nonascii.search(val):
- uni_str = self.decodeLatin1(val)[0]
- prop_value[i] = self.encodeUTF8(uni_str)[0]
+ for i in range(len(prop_value)):
+ prop_value[i] = self.latin1_to_utf8(prop_value[i])
mod_list = ((_ldap.MOD_REPLACE, prop_name, prop_value),)
@@ -419,12 +411,8 @@ class LDAPUserManager( SimpleItem, LDAPShared ):
if type(new) == type(''):
new = map(string.strip, string.split(new, ','))
- if unicode_enabled:
- for i in range(len(new)):
- val = new[i]
- if self.nonascii.search(val):
- uni_str = self.decodeLatin1(val)[0]
- new[i] = self.encodeUTF8(uni_str)[0]
+ for i in range(len(new)):
+ new[i] = self.latin1_to_utf8(new[i])
if current != new:
if new != ['']:
@@ -480,9 +468,6 @@ class LDAPUserManager( SimpleItem, LDAPShared ):
about to be created. It guards uniqueness of names by
warning for items with the same name.
"""
- if unicode_enabled and self.nonascii.search(value):
- value = unicode(value, 'latin1').encode('UTF-8')
-
search_str = '(%s=%s)' % (attr, str(value))
res = self._searchResults(self.users_base, self.users_scope,
search_str)