Example #1
0
def password_change(request, template_name='gallery/password_change.html',
                    post_change_redirect=None,
                    password_change_form=PasswordChangeForm):
    if post_change_redirect is None:
        post_change_redirect = reverse('bm.gallery.views.index')
    if request.method == 'POST':
        if 'save' not in request.POST:
            request.notifications.add(_('Password change canceled.'))
            return HttpResponseRedirect(post_change_redirect)
        form = password_change_form(user=request.user, data=request.POST)
        if form.is_valid():
            # we can't use form.save b/c that will update the p/w on the
            # model object, we need to  do it in LDAP
            if settings.USE_LDAP:
                ldapper = get_ldap_connection()
                dn = get_user_dn(request.user.username)
                old_password = request.POST.get('old_password')
                new_password = request.POST.get('new_password1')
                # we've already validated the old_password is correct, so
                # this shouldn't fail for password mismatch
                ldapper.passwd_s(dn, old_password, new_password)
                ldapper.unbind_s()
                request.notifications.add(_('Password change successful.'))
            else:
                form.save()
            return HttpResponseRedirect(post_change_redirect)
    else:
        form = password_change_form(user=request.user)
    context = RequestContext(request, {'form': form})
    return render_to_response(template_name, context_instance=context)
Example #2
0
def password_reset_confirm(request, uidb36=None, token=None,
                           template_name='gallery/password_reset_confirm.html',
                           token_generator=default_token_generator,
                           set_password_form=SetPasswordForm,
                           post_reset_redirect=None):
    """
    View that checks the hash in a password reset link and presents a
    form for entering a new password.
    """
    assert uidb36 is not None and token is not None # checked by URLconf
    if post_reset_redirect is None:
        post_reset_redirect = reverse('django.contrib.auth.views.password_reset_complete')
    try:
        uid_int = base36_to_int(uidb36)
    except ValueError:
        raise HttpResponseNotFound

    user = get_object_or_404(authmodels.User, id=uid_int)
    context_instance = RequestContext(request)

    if token_generator.check_token(user, token):
        context_instance['validlink'] = True
        if request.method == 'POST':
            form = set_password_form(user, request.POST)
            if form.is_valid():
                # we can't use form.save b/c that will update the p/w on the
                # model object, we need to  do it in LDAP
                if settings.USE_LDAP:
                    ldapper = get_ldap_connection()
                    dn = get_user_dn(user.username)
                    new_password = request.POST.get('new_password1')
                    ldapper.passwd_s(dn, None, new_password)
                    ldapper.unbind_s()
                    request.notifications.add(_('Password change successful.'))
                else:
                    form.save()
                return HttpResponseRedirect(post_reset_redirect)
        else:
            form = set_password_form(None)
    else:
        context_instance['validlink'] = False
        form = None
    context_instance['form'] = form
    return render_to_response(template_name, context_instance=context_instance)
Example #3
0
def register(request):
    if not request.user.is_anonymous():
        return HttpResponseRedirect(reverse('bm.gallery.views.index'))
    emailerror = ''
    if request.method == 'POST':
        if 'save' not in request.POST:
            request.notifications.add(_('Registration canceled.'))
            return HttpResponseRedirect(reverse('bm.gallery.views.index'))
        if not request.POST.get('email'):
            emailerror = _(u'This field is required.')
        regform = RegForm(request.POST)
        profileform = ProfileForm(request.POST)
        if regform.is_valid() and profileform.is_valid() and not emailerror:
            if settings.USE_LDAP:
                # first create the LDAP entry
                regdata = regform.cleaned_data
                profiledata = profileform.cleaned_data
                username = regdata['username']
                password = regdata['password']
                dn = get_user_dn(username)
                modlist_map = {
                    'givenName': regdata['first_name'],
                    'sn': regdata['last_name'],
                    'mail': regdata['email'],
                    'objectclass': ['inetOrgPerson', 'shadowAccount'],
                    'uid': username,
                    'cn': ' '.join([regdata['first_name'], regdata['last_name']]),
                    'labeledURI': profiledata['url'],
                    'userPassword': password,
                    }
                ldap_add(dn, modlist_map)

                # now add user to the 'galleries' group
                groupname = 'galleries'
                ldapper = get_ldap_connection()
                groupdn = 'cn=%s,ou=groups,dc=burningman,dc=com' % groupname
                groupdict = ldapper.search_s(groupdn, ldap.SCOPE_BASE)[0][1]
                contribs = set(groupdict['uniqueMember'])
                if isinstance(dn, unicode):
                    dn = dn.encode('utf-8')
                contribs.add(dn)
                new_groupdict = groupdict.copy()
                new_groupdict['uniqueMember'] = list(contribs)
                modlist = ldap.modlist.modifyModlist(groupdict, new_groupdict)
                ldapper.modify_s(groupdn, modlist)

            # user now exists in LDAP, first authentication triggers
            # creation of Django user and profile model objects
            authed_user = authenticate(username=username,
                                       password=password)
            profile = authed_user.get_profile()
            profile.url = profiledata['url']
            profile.save()
            login(request, authed_user)
            request.notifications.add(_('Account created.'))
            return HttpResponseRedirect('/')
        else: # failed validation
            request.notifications.add(_('Registration failed.'))
            # falls through to template rendering
    else: # not a POST
        regform = RegForm()
        profileform = ProfileForm()
    template_map = dict(forms=(regform, profileform),
                        emailerror=emailerror,
                        register=True,)
    context = RequestContext(request, template_map)
    return render_to_response('gallery/profile_edit.html',
                              context_instance=context)