Browse Source

HUE-1309 [core] ldap attributes are case sensitive

Abraham Elmahrek 12 years ago
parent
commit
22f65fb946

+ 10 - 4
apps/useradmin/src/useradmin/ldap_access.py

@@ -18,13 +18,15 @@
 This module provides access to LDAP servers, along with some basic functionality required for Hue and
 This module provides access to LDAP servers, along with some basic functionality required for Hue and
 User Admin to work seamlessly with LDAP.
 User Admin to work seamlessly with LDAP.
 """
 """
+import ldap
+import ldap.filter
+import logging
 import re
 import re
 
 
 import desktop.conf
 import desktop.conf
-import ldap
-import ldap.filter
+from desktop.lib.python_util import CaseInsensitiveDict
+
 
 
-import logging
 LOG = logging.getLogger(__name__)
 LOG = logging.getLogger(__name__)
 
 
 CACHED_LDAP_CONN = None
 CACHED_LDAP_CONN = None
@@ -114,6 +116,8 @@ class LdapConnection(object):
       for dn, data in result_data:
       for dn, data in result_data:
         # Skip Active Directory # refldap entries.
         # Skip Active Directory # refldap entries.
         if dn is not None:
         if dn is not None:
+          # Case insensitivity
+          data = CaseInsensitiveDict.from_dict(data)
 
 
           # Skip unnamed entries.
           # Skip unnamed entries.
           if user_name_attr not in data:
           if user_name_attr not in data:
@@ -122,7 +126,7 @@ class LdapConnection(object):
 
 
           ldap_info = {
           ldap_info = {
             'dn': dn,
             'dn': dn,
-            'username': data[user_name_attr][0]
+            'name': data[user_name_attr][0]
           }
           }
 
 
           if 'givenName' in data:
           if 'givenName' in data:
@@ -145,6 +149,8 @@ class LdapConnection(object):
     group_info = []
     group_info = []
     if result_data:
     if result_data:
       for dn, data in result_data:
       for dn, data in result_data:
+        # Case insensitivity
+        data = CaseInsensitiveDict.from_dict(data)
 
 
         # Skip Active Directory # refldap entries.
         # Skip Active Directory # refldap entries.
         if dn is not None:
         if dn is not None:

+ 33 - 0
desktop/core/src/desktop/lib/python_util.py

@@ -0,0 +1,33 @@
+#!/usr/bin/env python
+# Licensed to Cloudera, Inc. under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  Cloudera, Inc. licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# 
+# Extra python utils
+
+
+class CaseInsensitiveDict(dict):
+  def __setitem__(self, key, value):
+    super(CaseInsensitiveDict, self).__setitem__(key.lower(), value)
+
+  def __getitem__(self, key):
+    return super(CaseInsensitiveDict, self).__getitem__(key.lower())
+
+  def __contains__(self, key):
+    return super(CaseInsensitiveDict, self).__contains__(key.lower())
+
+  @classmethod
+  def from_dict(cls, _dict):
+    return CaseInsensitiveDict([(isinstance(key, basestring) and key.lower() or key, _dict[key]) for key in _dict])

+ 33 - 0
desktop/core/src/desktop/lib/python_util_test.py

@@ -0,0 +1,33 @@
+# Tests for django_util
+# Some parts based on http://www.djangosnippets.org/snippets/1044/
+# Licensed to Cloudera, Inc. under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  Cloudera, Inc. licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from nose.tools import assert_true, assert_equal, assert_not_equal
+
+from desktop.lib.python_util import CaseInsensitiveDict
+
+
+class TestPythonUtil(object):
+  def test_case_insensitive_dictionary(self):
+    d = CaseInsensitiveDict()
+    d["Test"] = "Test"
+    assert_true("Test" in d)
+    assert_true("test" in d)
+    assert_equal("Test", d['Test'])
+    assert_equal("Test", d['test'])
+    assert_not_equal("test", d['Test'])
+    assert_not_equal("test", d['test'])