Exemple #1
0
def shib_register(request, RegisterForm=BaseRegisterForm, register_template_name='shibboleth/register.html'):

    attr, error = parse_attributes(request.META)

    # In some cases, user might have Shibboleth names set but no email
    if attr[settings.SHIB_EMAIL] == None:
        attr[settings.SHIB_EMAIL] = '*****@*****.**'

    was_redirected = False
    if request.REQUEST.has_key('next'):
        was_redirected = True
    redirect_url = request.REQUEST.get('next', settings.LOGIN_REDIRECT_URL)
    context = {'shib_attrs': attr, 
               'was_redirected': was_redirected}
    if error:
        return render_to_response('shibboleth/attribute_error.html', context, context_instance=RequestContext(request))
    try:
        username = attr[settings.SHIB_USERNAME]
    except:
        return render_to_response('shibboleth/attribute_error.html', context, context_instance=RequestContext(request))

    if not attr[settings.SHIB_USERNAME] or attr[settings.SHIB_USERNAME] == '':
        return render_to_response('shibboleth/attribute_error.html', context, context_instance=RequestContext(request))

    if request.method == 'POST':
        form = RegisterForm(request.POST)
        if form.is_valid():
            user = form.save(attr)
    try:
        user = User.objects.get(username=attr[settings.SHIB_USERNAME])
    except User.DoesNotExist:
        form = RegisterForm()
        context = {'form': form, 'next': redirect_url, 'shib_attrs': attr, 'was_redirected': was_redirected, }
        return render_to_response(register_template_name, context, context_instance=RequestContext(request))

    user.set_unusable_password()
    try:
        user.first_name = attr[settings.SHIB_FIRST_NAME]
        user.last_name = attr[settings.SHIB_LAST_NAME]
        user.email = attr[settings.SHIB_EMAIL]
    except:
        pass
    user.save()

    profile = user.get_profile()
    if attr["student_id"] and attr["student_id"] != "":
        # This is because a student might previously have had a student id. In that case, we don't want to erase it.
        profile.student_id = attr["student_id"].split(':')[-1]
    profile.save()

    user.backend = 'django.contrib.auth.backends.ModelBackend'
    login(request, user)
    shib_logon_done.send(sender=shib_register, user=user, shib_attrs=attr)

    if not redirect_url or '//' in redirect_url or ' ' in redirect_url:
        redirect_url = settings.LOGIN_REDIRECT_URL

    return HttpResponseRedirect(redirect_url)
Exemple #2
0
def shib_register(request, RegisterForm=BaseRegisterForm, register_template_name='shibboleth/register.html'):

    attr, error = parse_attributes(request.META)

    was_redirected = False
    if request.REQUEST.has_key('next'):
        was_redirected = True
    redirect_url = request.REQUEST.get('next', settings.LOGIN_REDIRECT_URL)
    context = {'shib_attrs': attr, 
               'was_redirected': was_redirected}
    if error:
        return render_to_response('shibboleth/attribute_error.html', context, context_instance=RequestContext(request))
    try:
        username = attr[settings.SHIB_USERNAME]
    except:
        return render_to_response('shibboleth/attribute_error.html', context, context_instance=RequestContext(request))

    if not attr[settings.SHIB_USERNAME] or attr[settings.SHIB_USERNAME] == '':
        return render_to_response('shibboleth/attribute_error.html', context, context_instance=RequestContext(request))

    if request.method == 'POST':
        form = RegisterForm(request.POST)
        if form.is_valid():
            user = form.save(attr)
    try:
        user = User.objects.get(username=attr[settings.SHIB_USERNAME])
    except User.DoesNotExist:
        form = RegisterForm()
        context = {'form': form, 'next': redirect_url, 'shib_attrs': attr, 'was_redirected': was_redirected, }
        return render_to_response(register_template_name, context, context_instance=RequestContext(request))

    user.set_unusable_password()
    try:
        user.first_name = attr[settings.SHIB_FIRST_NAME]
        user.last_name = attr[settings.SHIB_LAST_NAME]
        user.email = attr[settings.SHIB_EMAIL]
    except:
        pass
    user.save()

    user.backend = 'django.contrib.auth.backends.ModelBackend'
    login(request, user)
    shib_logon_done.send(sender=shib_register, user=user, shib_attrs=attr)

    if not redirect_url or '//' in redirect_url or ' ' in redirect_url:
        redirect_url = settings.LOGIN_REDIRECT_URL

    return HttpResponseRedirect(redirect_url)
Exemple #3
0
def shib_register(request,
                  RegisterForm=BaseRegisterForm,
                  register_template_name='shibboleth/register.html'):

    attr, error = parse_attributes(request.META)

    was_redirected = False
    if request.REQUEST.has_key('next'):
        was_redirected = True
    redirect_url = request.REQUEST.get('next', settings.LOGIN_REDIRECT_URL)
    context = {'shib_attrs': attr, 'was_redirected': was_redirected}
    if error:
        return render_to_response('shibboleth/attribute_error.html',
                                  context,
                                  context_instance=RequestContext(request))
    try:
        username = attr[settings.SHIB_USERNAME]
    except:
        return render_to_response('shibboleth/attribute_error.html',
                                  context,
                                  context_instance=RequestContext(request))

    if not attr[settings.SHIB_USERNAME] or attr[settings.SHIB_USERNAME] == '':
        return render_to_response('shibboleth/attribute_error.html',
                                  context,
                                  context_instance=RequestContext(request))

    if request.method == 'POST':
        form = RegisterForm(request.POST)
        if form.is_valid():
            user = form.save(attr)
    try:
        user = User.objects.get(username=attr[settings.SHIB_USERNAME])
    except User.DoesNotExist:
        form = RegisterForm()
        context = {
            'form': form,
            'next': redirect_url,
            'shib_attrs': attr,
            'was_redirected': was_redirected,
        }
        return render_to_response(register_template_name,
                                  context,
                                  context_instance=RequestContext(request))

    user.set_unusable_password()
    try:
        user.first_name = attr[settings.SHIB_FIRST_NAME]
        user.last_name = attr[settings.SHIB_LAST_NAME]
        user.email = attr[settings.SHIB_EMAIL]
    except:
        pass
    user.save()

    user.backend = 'django.contrib.auth.backends.ModelBackend'
    login(request, user)
    shib_logon_done.send(sender=shib_register, user=user, shib_attrs=attr)

    if not redirect_url or '//' in redirect_url or ' ' in redirect_url:
        redirect_url = settings.LOGIN_REDIRECT_URL

    return HttpResponseRedirect(redirect_url)