コード例 #1
0
def search_ldap(request):
    """
    Search the LDAP store for an alumna's record.
    POST required, which is sent via Ajax request.
    If we find a record, we check Informix to see
    if we have their LDAP username stored, and
    update it if not. Lastly, display login form.
    If no record, allow the user to create one.
    """
    if request.method == 'POST':
        form = RegistrationSearchForm(request.POST)
        if form.is_valid():
            # data dictionary
            data = form.cleaned_data
            # search ldap
            # we use the regular ldap server here
            l = LDAPManager()
            user = l.search(data['alumna'])
            if user:
                # we have a user
                user = user[0][1]
                # update informix if no ldap_user
                if not settings.DEBUG and data['ldap_name'] == '':
                    sql = '''
                        UPDATE cvid_rec SET ldap_name='{}',
                        ldap_add_date = TODAY
                        WHERE cx_id = '{}'
                    '''.format(user['cn'][0], data['alumna'])
                    results = do_sql(sql, key=settings.INFORMIX_DEBUG)
                # check for challenge questions
                l = LDAPBackend()
                request.session['ldap_questions'] = l.get_questions(
                    user['cn'][0]
                )
                # display the login form
                form = {'data':{'username':user['cn'][0],}}
                redir = reverse_lazy('alumni_directory_home')
                extra_context = {
                    'user':user,'form':form,
                    'next':redir,'action':settings.LOGIN_URL
                }
                template = 'login'
            else:
                # display the create form
                data['carthageNameID'] = data['alumna']
                request.session['ldap_name'] = data.get('ldap_name')
                form = CreateLdapForm(initial=data)
                action = reverse_lazy('registration_create_ldap')
                extra_context = {'action':action,'form':form,}
                template = 'create'
            return render(
                request,
                'registration/{}_ldap.inc.html'.format(template),
                extra_context
            )
    else:
        # POST required
        # or doing something nefarious
        return HttpResponseRedirect(reverse_lazy('registration_search'))