def find_ldap_groups_from_user(username=None):
    groups = []
    if features.LDAP_ENABLED and username:
        import ldap
        user_search = settings.AUTH_LDAP_USER_SEARCH_STR
        group_search = settings.AUTH_LDAP_GROUP_SEARCH_STR
        LOG.debug("group search: %s" % group_search)
        LOG.debug("searching ldap groups for username %s" % username)
        try:
            conn = get_ldap_connection()
            # searchFilter = "username=%s" % username
            # user_result = conn.search_s(user_search, ldap.SCOPE_SUBTREE, search_filter,
            #                         None)
            # LOG.debug(user_result)
            group_result = conn.search_s(
                group_search, ldap.SCOPE_SUBTREE, "memberUid=%s" % username, None)

            for entry in group_result:
                group_name = entry[0].split(',')[0].split('=')[1].strip()
                if group_name != username:
                    groups.append("ldap_" + group_name)
        except Exception, e:
            LOG.error("Ops... something went wrong: %s" % e)

        finally:
            conn.unbind()
            LOG.info("LDAP connection closed")
    LOG.info("ldap groups found for username %s: %s" % (username, groups))
    return groups
dataset/ETHPy150Open globocom/database-as-a-service/dbaas/account/helper.py/find_ldap_groups_from_user/original