def main():
    """
    main method
    """

    # initialize the manager
    l = LDAPManager()

    # If there is only one value in the attribute value list,
    # the value can be just a string – it need not be a list.
    # Example: ('ou', 'user') is an acceptable alternative to
    # ('ou', ['user']).

    person = {
        "objectclass":settings.LDAP_OJBECT_CLASS,
        "givenName":givenName,"sn":sn,"cn":cn,"loginDisabled":"false",
        "carthageDob":carthageDob,"carthageNameID":carthageNameID,
        "mail":mail,"userPassword":userPassword
    }

    if group.lower()=="faculty":
        person["carthageFacultyStatus"] = "A"
    elif group.lower()=="staff":
        person["carthageStaffStatus"] = "A"
    elif group.lower()=="student":
        person["carthageStudentStatus"] = "A"
    elif group.lower()=="alumni":
        person["carthageFormerStudentStatus"] = "A"
    else:
        person["carthageOtherStatus"] = "A"

    print cn
    print person
    user = l.create(person)
    print user
Example #2
0
def create_ldap(request):
    """
    Creates an LDAP account.
    Requires POST.
    After successful create, we update Informix with
    the LDAP username.
    """
    if request.method == 'POST':
        form = CreateLdapForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            # dob format: YYYY-MM-DD
            data['carthageDob'] = data['carthageDob'].strftime('%Y-%m-%d')
            # username (cn) will be email address
            data['cn'] = data['mail']
            # remove confirmation password
            data.pop('confPassword',None)
            # python ldap wants strings, not unicode
            for k,v in data.items():
                data[k] = str(v)
            data['objectclass'] = settings.LDAP_OBJECT_CLASS_LIST
            data['carthageFacultyStatus'] = ''
            data['carthageStaffStatus'] = ''
            data['carthageStudentStatus'] = ''
            data['carthageFormerStudentStatus'] = 'A'
            data['carthageOtherStatus'] = ''

            # create the ldap user
            # we have to use the PWM server here
            l = LDAPManager(
                protocol=settings.LDAP_PROTOCOL_PWM,
                server=settings.LDAP_SERVER_PWM,
                port=settings.LDAP_PORT_PWM,
                user=settings.LDAP_USER_PWM,
                password=settings.LDAP_PASS_PWM,
                base=settings.LDAP_BASE_PWM
            )

            try:
                user = l.create(data)
                # set session ldap_cn, why?
                request.session['ldap_cn'] = user[0][1]['cn'][0]
                if not settings.DEBUG:
                    # update informix cvid_rec.ldap_user
                    sql = '''
                        UPDATE cvid_rec SET ldap_name='{}',
                        ldap_add_date = TODAY
                        WHERE cx_id = '{}'
                    '''.format(
                        user[0][1]['cn'][0], user[0][1]['carthageNameID'][0]
                    )
                    ln = do_sql(sql, key=settings.INFORMIX_DEBUG)
                # create the django user
                djuser = l.dj_create(user)
                data['djuser'] = djuser
                # authenticate user
                djuser.backend = 'django.contrib.auth.backends.ModelBackend'
                login(request, djuser)

                # send email to admins
                subject = "[LDAP][Create] {} {}".format(
                    user[0][1]['givenName'][0],
                    user[0][1]['sn'][0]
                )

                if settings.DEBUG:
                    to_list = [settings.SERVER_EMAIL]
                else:
                    to_list = settings.LDAP_CREATE_TO_LIST

                send_mail(
                    request,to_list, subject, data['mail'],
                    'registration/create_ldap_email.html', data
                )
                return HttpResponseRedirect(
                    reverse_lazy('alumni_directory_home')
                )
            except Exception as e:

                # log it for later
                ldap_logger.debug('ldap error: {}\n{}'.format(e,data))

                if '16019' in str(e):
                    error = """
                        There was an error creating your account. Verify that
                        your password does not contain any English words like
                        the names of months, colors, etc.
                    """
                else:
                    error = """
                        There was an error creating your account. Verify that
                        your passwords meet the criteria.
                    """

                messages.add_message(
                    request, messages.ERROR, error, extra_tags='alert alert-danger'
                )

                return render(
                    request,
                    'registration/create_ldap.html', {'form':form,}
                )

        else:
            return render(
                request,
                'registration/create_ldap.html', {'form':form,}
            )
    elif settings.DEBUG:
        form = CreateLdapForm(initial={'carthageNameID':'901257',})
        return render(
            request,
            'registration/create_ldap.html', {'form':form,}
        )
    else:
        # POST required
        return HttpResponseRedirect(reverse_lazy('registration_search'))