Esempio n. 1
0
def external_register(request):
    if request.method == 'POST' and 'bnewaccount' in request.POST:
        form1 = SimpleRegistrationForm(request.POST)

        if form1.is_valid():
            user_ = User(username=form1.cleaned_data['username'], email=form1.cleaned_data['email'])
            user_.email_isvalid = request.session.get('auth_validated_email', '') == form1.cleaned_data['email']
            user_.set_unusable_password()

            if User.objects.all().count() == 0:
                user_.is_superuser = True
                user_.is_staff = True

            user_.save()
            UserJoinsAction(user=user_, ip=request.META['REMOTE_ADDR']).save()

            try:
                assoc_key = request.session['assoc_key']
                auth_provider = request.session['auth_provider']
            except:
                request.session['auth_error'] = _(
                        "Oops, something went wrong in the middle of this process. Please try again. Note that you need to have cookies enabled for the authentication to work."
                        )
                logging.error("Missing session data when trying to complete user registration: %s" % ", ".join(
                        ["%s: %s" % (k, v) for k, v in request.META.items()]))
                return HttpResponseRedirect(reverse('auth_signin'))

            uassoc = AuthKeyUserAssociation(user=user_, key=assoc_key, provider=auth_provider)
            uassoc.save()

            del request.session['assoc_key']
            del request.session['auth_provider']

            return login_and_forward(request, user_, message=_("A welcome email has been sent to your email address. "))
    else:
        auth_provider = request.session.get('auth_provider', None)
        if not auth_provider:
            request.session['auth_error'] = _(
                    "Oops, something went wrong in the middle of this process. Please try again.")
            logging.error("Missing session data when trying to complete user registration: %s" % ", ".join(
                    ["%s: %s" % (k, v) for k, v in request.META.items()]))
            return HttpResponseRedirect(reverse('auth_signin'))

        provider_class = AUTH_PROVIDERS[auth_provider].consumer

        # Hack: Facebook auth expects an access token, not the assoc_key.
        identifier = request.session['assoc_key']
        if auth_provider == 'facebook':
            identifier = request.session['access_token']
        user_data = provider_class.get_user_data(identifier)

        if not user_data:
            user_data = request.session.get('auth_consumer_data', {})

        username = user_data.get('username', '')
        email = user_data.get('email', '')

        if email:
            request.session['auth_validated_email'] = email

        form1 = SimpleRegistrationForm(initial={
        'next': '/',
        'username': username,
        'email': email,
        })

    provider_context = AUTH_PROVIDERS[request.session['auth_provider']].context

    return render_to_response('auth/complete.html', {
    'form1': form1,
    'provider':provider_context and mark_safe(provider_context.human_name) or _('unknown'),
    'login_type':provider_context.id,
    'gravatar_faq_url':reverse('faq') + '#gravatar',
    }, context_instance=RequestContext(request))
Esempio n. 2
0
def external_register(request):
    if request.method == 'POST' and 'bnewaccount' in request.POST:
        form1 = SimpleRegistrationForm(request.POST)

        if form1.is_valid():
            user_ = User(username=form1.cleaned_data['username'], email=form1.cleaned_data['email'])
            user_.email_isvalid = request.session.get('auth_validated_email', '') == form1.cleaned_data['email']
            user_.set_unusable_password()

            if User.objects.all().count() == 0:
                user_.is_superuser = True
                user_.is_staff = True

            user_.save()
            UserJoinsAction(user=user_, ip=request.META['REMOTE_ADDR']).save()

            try:
                assoc_key = request.session['assoc_key']
                auth_provider = request.session['auth_provider']
            except:
                request.session['auth_error'] = _(
                        "Oops, something went wrong in the middle of this process. Please try again. Note that you need to have cookies enabled for the authentication to work."
                        )
                logging.error("Missing session data when trying to complete user registration: %s" % ", ".join(
                        ["%s: %s" % (k, v) for k, v in request.META.items()]))
                return HttpResponseRedirect(reverse('auth_signin'))

            uassoc = AuthKeyUserAssociation(user=user_, key=assoc_key, provider=auth_provider)
            uassoc.save()

            del request.session['assoc_key']
            del request.session['auth_provider']

            return login_and_forward(request, user_, message=_("A welcome email has been sent to your email address. "))
    else:
        auth_provider = request.session.get('auth_provider', None)
        if not auth_provider:
            request.session['auth_error'] = _(
                    "Oops, something went wrong in the middle of this process. Please try again.")
            logging.error("Missing session data when trying to complete user registration: %s" % ", ".join(
                    ["%s: %s" % (k, v) for k, v in request.META.items()]))
            return HttpResponseRedirect(reverse('auth_signin'))

        provider_class = AUTH_PROVIDERS[auth_provider].consumer
        user_data = provider_class.get_user_data(request.session['assoc_key'])

        if not user_data:
            user_data = request.session.get('auth_consumer_data', {})

        username = user_data.get('username', '')
        email = user_data.get('email', '')

        if email:
            request.session['auth_validated_email'] = email

        form1 = SimpleRegistrationForm(initial={
        'next': '/',
        'username': username,
        'email': email,
        })

    provider_context = AUTH_PROVIDERS[request.session['auth_provider']].context

    return render_to_response('auth/complete.html', {
    'form1': form1,
    'provider':provider_context and mark_safe(provider_context.human_name) or _('unknown'),
    'login_type':provider_context.id,
    'gravatar_faq_url':reverse('faq') + '#gravatar',
    }, context_instance=RequestContext(request))
Esempio n. 3
0
def external_register(request):
    if request.method == 'POST' and 'bnewaccount' in request.POST:
        form1 = SimpleRegistrationForm(request.POST)        

        if form1.is_valid():
            message= u''
            try:
                user_ = User(username=form1.cleaned_data['username'], email=form1.cleaned_data['email'], 
                             real_name=form1.cleaned_data.get('real_name', ''))
                user_.email_isvalid = request.session.get('auth_validated_email', '') == form1.cleaned_data['email']
                user_.set_unusable_password()
    
                if User.objects.all().count() == 0:
                    user_.is_superuser = True
                    user_.is_staff = True
            
                assoc_key = request.session['assoc_key']
                auth_provider = request.session['auth_provider']
                
                user_.save()
                UserJoinsAction(user=user_, ip=request.META['REMOTE_ADDR']).save()
                if user_.email_isvalid:
                    EmailValidationAction(user=user_, ip=request.META['REMOTE_ADDR']).save()
                    message += _(u"A welcome email has been sent to Your email address.")
                else:
                    message += _(u"A welcome email has been sent to You.<br />Remember to validate Your email address to be able to fully participate in %s community." % settings.APP_TITLE)
                
                uassoc = AuthKeyUserAssociation(user=user_, key=assoc_key, provider=auth_provider)
                uassoc.save()
                logging.info(u"User %s joined via %s" % (user_.username, auth_provider))
            except:
                request.session['auth_error'] = _(
                        "Oops, something went wrong in the middle of this process. Please try again. Note that you need to have cookies enabled for the authentication to work."
                        )
                logging.exception("Missing session data when trying to complete user registration: %s" % ", ".join(
                        ["%s: %s" % (k, v) for k, v in request.META.items()]))
                return HttpResponseRedirect(reverse('auth_signin'))           

            _cleanup_after_external_signin(request)
            return login_and_forward(request, user_, message=message)
        else:
            logging.info("Form not validated in external_register %s", str(form1.errors))
    else:        
        auth_provider = request.session.get('auth_provider', None)
        if not auth_provider:
            request.session['auth_error'] = _(
                    "Oops, something went wrong in the middle of this process. Please try again.")
            logging.error("Missing session data when trying to complete user registration: %s" % ", ".join(
                    ["%s: %s" % (k, v) for k, v in request.META.items()]))
            return HttpResponseRedirect(reverse('auth_signin'))

        provider_class = AUTH_PROVIDERS[auth_provider].consumer

        if provider_class.__class__.__name__ == 'FacebookAuthConsumer' or provider_class.__class__.__name__ == 'GoogleAuthConsumer':
            user_data = provider_class.get_user_data(request.session['access_token'])
        else:
            user_data = provider_class.get_user_data(request.session['assoc_key'])


        if not user_data:
            user_data = request.session.get('auth_consumer_data', {})

        username = user_data.get('username', '')
        email = user_data.get('email', '')
        real_name = user_data.get('real_name', '')

        if email:
            try:
                old_user = User.objects.get(email=email)
                message=_("Welcome back %s! You can now login via %s.") % (old_user.username, auth_provider.capitalize())
                if not old_user.email_isvalid:
                    message += _("<br />Since this email was not previously validated we cleared your password.<br />Set new one to be able to login with it")
                    old_user.set_unusable_password()
                    old_user.email_isvalid = True
                    old_user.save()
                    logging.warning("Clearing password for user %s with unvalidated email after signing with %s" % 
                                    (old_user.username, auth_provider))
                    EmailValidationAction(user=old_user, ip=request.META['REMOTE_ADDR']).save()
                uassoc = AuthKeyUserAssociation(user=old_user, key=request.session['assoc_key'], provider=auth_provider)
                uassoc.save()
                logging.info(u"User %s added %s sign in provider" % (old_user.username, auth_provider))
                _cleanup_after_external_signin(request)
                return login_and_forward(request, old_user, message=mark_safe(message))
            except:
                pass
            request.session['auth_validated_email'] = email

        form1 = SimpleRegistrationForm(initial={
        'next': '/',
        'username': username,
        'email': email,
        'real_name': real_name,
        })

    provider_context = AUTH_PROVIDERS[request.session['auth_provider']].context

    return render_to_response('auth/complete.html', {
    'form1': form1,
    'provider':provider_context and mark_safe(provider_context.human_name) or _('unknown'),
    'login_type':provider_context.id,
    'gravatar_faq_url':reverse('faq') + '#gravatar',
    }, context_instance=RequestContext(request))
Esempio n. 4
0
def external_register(request):
    if request.method == "POST" and "bnewaccount" in request.POST:
        form1 = SimpleRegistrationForm(request.POST)

        if form1.is_valid():
            user_ = User(
                username=form1.cleaned_data["username"],
                email=form1.cleaned_data["email"],
                real_name=form1.cleaned_data["real_name"],
            )
            user_.email_isvalid = request.session.get("auth_validated_email", "") == form1.cleaned_data["email"]
            user_.set_unusable_password()

            if User.objects.all().count() == 0:
                user_.is_superuser = True
                user_.is_staff = True

            user_.save()
            UserJoinsAction(user=user_, ip=request.META["REMOTE_ADDR"]).save()

            try:
                assoc_key = request.session["assoc_key"]
                auth_provider = request.session["auth_provider"]
            except:
                request.session["auth_error"] = _(
                    "Oops, something went wrong in the middle of this process. Please try again. Note that you need to have cookies enabled for the authentication to work."
                )
                logging.error(
                    "Missing session data when trying to complete user registration: %s"
                    % ", ".join(["%s: %s" % (k, v) for k, v in request.META.items()])
                )
                return HttpResponseRedirect(reverse("auth_signin"))

            uassoc = AuthKeyUserAssociation(user=user_, key=assoc_key, provider=auth_provider)
            uassoc.save()

            del request.session["assoc_key"]
            del request.session["auth_provider"]

            return login_and_forward(request, user_, message=_("A welcome email has been sent to your email address. "))
    else:
        auth_provider = request.session.get("auth_provider", None)
        if not auth_provider:
            request.session["auth_error"] = _(
                "Oops, something went wrong in the middle of this process. Please try again."
            )
            logging.error(
                "Missing session data when trying to complete user registration: %s"
                % ", ".join(["%s: %s" % (k, v) for k, v in request.META.items()])
            )
            return HttpResponseRedirect(reverse("auth_signin"))

        provider_class = AUTH_PROVIDERS[auth_provider].consumer

        if provider_class.__class__.__name__ == "FacebookAuthConsumer":
            user_data = provider_class.get_user_data(request.session["access_token"])
        else:
            user_data = provider_class.get_user_data(request.session["assoc_key"])

        if not user_data:
            user_data = request.session.get("auth_consumer_data", {})

        username = user_data.get("username", "")
        email = user_data.get("email", "")
        real_name = user_data.get("real_name", "")

        if email:
            request.session["auth_validated_email"] = email

        form1 = SimpleRegistrationForm(
            initial={"next": "/", "username": username, "email": email, "real_name": real_name}
        )

    provider_context = AUTH_PROVIDERS[request.session["auth_provider"]].context

    return render_to_response(
        "auth/complete.html",
        {
            "form1": form1,
            "provider": provider_context and mark_safe(provider_context.human_name) or _("unknown"),
            "login_type": provider_context.id,
            "gravatar_faq_url": reverse("faq") + "#gravatar",
        },
        context_instance=RequestContext(request),
    )
Esempio n. 5
0
def external_register(request):
    if request.method == 'POST' and 'bnewaccount' in request.POST:
        form1 = SimpleRegistrationForm(request.POST)

        if form1.is_valid():
            user_ = User(username=form1.cleaned_data['username'], email=form1.cleaned_data['email'])
            user_.email_isvalid = request.session.get('auth_validated_email', '') == form1.cleaned_data['email']
            user_.set_unusable_password()

            if User.objects.all().count() == 0:
                user_.is_superuser = True
                user_.is_staff = True

            user_.save()

            try:
                assoc_key = request.session['assoc_key']
                auth_provider = request.session['auth_provider']
                if auth_provider == "weibo":
                    token = ast.literal_eval(assoc_key)
                    assoc_key = token["access_token"]
                    weibo_uid = token["uid"]
                    weibo_remind_in = token["remind_in"]
                    weibo_expires_at = token["expires_at"]

            except:
                request.session['auth_error'] = _(
                        "Oops, something went wrong in the middle of this process. Please try again. Note that you need to have cookies enabled for the authentication to work."
                        )
                logging.error("Missing session data when trying to complete user registration: %s" % ", ".join(
                        ["%s: %s" % (k, v) for k, v in request.META.items()]))
                return HttpResponseRedirect(reverse('auth_signin'))

            if auth_provider == "weibo":
                uassoc = AuthKeyUserAssociation(user=user_, key=assoc_key, provider=auth_provider,weibo_uid=weibo_uid,weibo_remind_in=weibo_remind_in,weibo_expires_at=weibo_expires_at)
            else:
                uassoc = AuthKeyUserAssociation(user=user_, key=assoc_key, provider=auth_provider)
            uassoc.save()

            UserJoinsAction(user=user_, ip=request.META['REMOTE_ADDR']).save()
            del request.session['assoc_key']
            del request.session['auth_provider']

            return login_and_forward(request, user_, message=_("A welcome email has been sent to your email address. "))
    else:
        auth_provider = request.session.get('auth_provider', None)
        if not auth_provider:
            request.session['auth_error'] = _(
                    "Oops, something went wrong in the middle of this process. Please try again.")
            logging.error("Missing session data when trying to complete user registration: %s" % ", ".join(
                    ["%s: %s" % (k, v) for k, v in request.META.items()]))
            return HttpResponseRedirect(reverse('auth_signin'))

        if auth_provider == 'weibo':
            assoc_key = request.session['assoc_key']
            token = ast.literal_eval(assoc_key)
            client = weibo_client(str(settings.WEIBO_API_KEY), str(settings.WEIBO_API_SECRET),urlparse.urljoin(settings.APP_URL,'/weibo/signin/check'),token)
            user=client.get('users/show', uid=token["uid"])
            username = user["name"]
            email = None
            provider_context = None

        else: # weibo user register
            provider_class = AUTH_PROVIDERS[auth_provider].consumer
            user_data = provider_class.get_user_data(request.session['assoc_key'])

            if not user_data:
                user_data = request.session.get('auth_consumer_data', {})

            username = user_data.get('username', '')
            email = user_data.get('email', '')

            provider_context = AUTH_PROVIDERS[request.session['auth_provider']].context

        if email:
            request.session['auth_validated_email'] = email

        form1 = SimpleRegistrationForm(initial={
        'next': '/',
        'username': username,
        'email': email,
        })


    return render_to_response('auth/complete.html', {
    'form1': form1,
    #'provider':provider_context and mark_safe(provider_context.human_name) or _('unknown'),
    #'login_type':provider_context.id,
    'gravatar_faq_url':reverse('faq') + '#gravatar',
    }, context_instance=RequestContext(request))