Пример #1
0
def get_ldap_groups_map(user=None):
    """Return a dict with group_slug, group_name for each LDAP group

    If ``user`` is provided, returns only the groups that the user is a member
    of in LDAP.

    """

    ldap_backend = backend.LDAPBackend()
    conn = ldap.initialize(ldap_backend.settings.SERVER_URI)
    conn.simple_bind_s(ldap_backend.settings.BIND_DN,
                       ldap_backend.settings.BIND_PASSWORD)
    if user:
        filter_ = _get_ldap_groups_filter(user, ldap_backend)
    else:
        filter_ = "{}".format(settings.LDAP_GROUP_PROFILE_FILTERSTR)
    remote_groups = conn.search_s(
        base=ldap_backend.settings.GROUP_SEARCH.base_dn,
        scope=ldap.SCOPE_SUBTREE,
        filterstr=filter_)
    result = {}
    for cn, attributes in remote_groups:
        group_name = " ".join(attributes[settings.LDAP_GROUP_NAME_ATTRIBUTE])
        sanitized_name = sanitize_group_name(group_name)
        result[slugify(sanitized_name)] = {
            "original": group_name,
            "sanitized": sanitized_name,
        }
    return result
Пример #2
0
    def form_valid(self, form):
        username = form.cleaned_data['username']
        password = form.cleaned_data['password']
        user = authenticate(username=username, password=password)
        ldap_backend = backend.LDAPBackend()
        ldap_user = backend.LDAPBackend.authenticate(ldap_backend,
                                                     username=username,
                                                     password=password)

        if user is not None:

            if user.is_active:

                login(self.request, user)
            return HttpResponseRedirect('/')
        if ldap_user is not None:
            login(self.request, user)
            return HttpResponseRedirect('/')
        print "here"
        return super(LoginView, self).form_valid(form)
Пример #3
0
    def setUp(self):
        self.mockldap.start()
        self.ldapobj = self.mockldap['ldap://localhost']

        self.backend = backend.LDAPBackend()
        self.backend.ldap  # Force global configuration
Пример #4
0
    def setUp(self):
        self.configure_logger()
        self.mock_ldap.reset()

        _LDAPConfig.ldap = self.mock_ldap
        self.backend = backend.LDAPBackend()