<s> from __future__ import absolute_import , unicode_literals <EOL> from django . conf import settings <EOL> from dbaas import features <EOL> import logging <EOL> LOG = logging . getLogger ( __name__ ) <EOL> def get_ldap_connection ( ) : <EOL> import ldap <EOL> server = settings . AUTH_LDAP_SERVER_URI <EOL> user_pw = settings . AUTH_LDAP_BIND_PASSWORD <EOL> dn = settings . AUTH_LDAP_BIND_DN <EOL> conn = None <EOL> LOG . debug ( "<STR_LIT>" ) <EOL> conn = ldap . initialize ( server ) <EOL> conn . bind_s ( dn , user_pw ) <EOL> LOG . debug ( "<STR_LIT>" ) <EOL> return conn <EOL> def find_ldap_groups_from_user ( username = None ) : <EOL> groups = [ ] <EOL> if features . LDAP_ENABLED and username : <EOL> import ldap <EOL> user_search = settings . AUTH_LDAP_USER_SEARCH_STR <EOL> group_search = settings . AUTH_LDAP_GROUP_SEARCH_STR <EOL> LOG . debug ( "<STR_LIT>" % group_search ) <EOL> LOG . debug ( "<STR_LIT>" % username ) <EOL> try : <EOL> conn = get_ldap_connection ( ) <EOL> group_result = conn . search_s ( <EOL> group_search , ldap . SCOPE_SUBTREE , "<STR_LIT>" % username , None ) <EOL> for entry in group_result : <EOL> group_name = entry [ <NUM_LIT:0> ] . split ( '<STR_LIT:U+002C>' ) [ <NUM_LIT:0> ] . split ( '<STR_LIT:=>' ) [ <NUM_LIT:1> ] . strip ( ) <EOL> if group_name != username : <EOL> groups . append ( "<STR_LIT>" + group_name ) <EOL> except Exception , e : <EOL> LOG . error ( "<STR_LIT>" % e ) <EOL> finally : <EOL> conn . unbind ( ) <EOL> LOG . info ( "<STR_LIT>" ) <EOL> LOG . info ( "<STR_LIT>" % ( username , groups ) ) <EOL> return groups </s>
