Пример #1
0
def registration(request):
    openid = request.session.get('openid', None)
    next = request.GET.get(REDIRECT_FIELD_NAME, '')
    if not openid or openid is None:
        return HttpResponseRedirect(u"%s?%s" % (reverse('oid_login'), urllib.urlencode({REDIRECT_FIELD_NAME: urlquote(next)})))
    pinitial = openid.sreg
    logger.debug(openid.sreg)
    logger.debug(openid.ax_resp)
    if openid.ax_resp:
        for k, v in openid.ax_resp.items():
            if not pinitial.get(k):
                pinitial[k] = v
    logger.debug(u"Окончательные данные \n %s" % pinitial)
    initial = {}
    for k, v in pinitial.items():
        initial[get_name(k)] = v
    if request.method == 'POST':
        form = OpenidRegisterForm(request.POST)
    else:
        form = OpenidRegisterForm(initial)
        
    if form.is_valid():
        user = User.objects.create_user(form.cleaned_data['username'], form.cleaned_data['email'])
        user.backend = "authopenid.backends.OpenIDBackend"
        if user is not None:
            uassoc = UserAssociation(openid_url=str(openid), user_id=user.id)
            uassoc.save(send_email=False)
            cauth.login(request, user)
            return HttpResponseRedirect(urlquote(next))
    return render_to_response('authopenid/registration.html',
        {
            'form': form,
            REDIRECT_FIELD_NAME: next
         },
        request
    )
Пример #2
0
def register(request):
    """
    register an openid.

    If user is already a member he can associate its openid with 
    its account.

    A new account could also be created and automaticaly associated
    to the openid.

    url : /complete/

    template : authopenid/complete.html
    """

    is_redirect = False
    next = request.GET.get('next', '').strip()
    if not next or not is_valid_next_url(next):
        next = getattr(settings, 'OPENID_REDIRECT_NEXT', '/')


    openid_ = request.session.get('openid', None)
    if not openid_:
        return HttpResponseRedirect(reverse('user_signin') + next)

    nickname = openid_.sreg.get('nickname', '')
    email = openid_.sreg.get('email', '')
    
    form1 = OpenidRegisterForm(initial={
        'next': next,
        'username': nickname,
        'email': email,
    }) 
    form2 = OpenidVerifyForm(initial={
        'next': next,
        'username': nickname,
    })
    
    if request.POST:
        just_completed = False
        if 'bnewaccount' in request.POST.keys():
            form1 = OpenidRegisterForm(request.POST)
            if form1.is_valid():
                next = form1.cleaned_data['next']
                if not next:
                    next = getattr(settings, 'OPENID_REDIRECT_NEXT', '/')
                is_redirect = True
                tmp_pwd = User.objects.make_random_password()
                user_ = User.objects.create_user(form1.cleaned_data['username'],
                         form1.cleaned_data['email'], tmp_pwd)
                
                # make association with openid
                uassoc = UserAssociation(openid_url=str(openid_),
                        user_id=user_.id)
                uassoc.save()
                    
                # login 
                user_.backend = "django.contrib.auth.backends.ModelBackend"
                login(request, user_)
        elif 'bverify' in request.POST.keys():
            form2 = OpenidVerifyForm(request.POST)
            if form2.is_valid():
                is_redirect = True
                next = form2.cleaned_data['next']
                if not next:
                    next = getattr(settings, 'OPENID_REDIRECT_NEXT', '/')
                user_ = form2.get_user()

                uassoc = UserAssociation(openid_url=str(openid_),
                        user_id=user_.id)
                uassoc.save()
                login(request, user_)
        
        # redirect, can redirect only if forms are valid.
        if is_redirect:
            return HttpResponseRedirect(next) 
    
    return render('authopenid/complete.html', {
        'form1': form1,
        'form2': form2,
        'action': reverse('user_register'),
        'nickname': nickname,
        'email': email
    }, context_instance=RequestContext(request))