Ejemplo n.º 1
0
    try:
        FacebookProfile.objects.get(facebook_id=request.facebook.uid)
        # already setup, move along please
        return HttpResponseRedirect(redirect_url)
    except FacebookProfile.DoesNotExist, e:
        # not in the db, ok to continue
        pass

    #user submitted a form - which one?
    if request.method == "POST":
        log.debug('Submitted form')
        #lets setup a facebook only account. The user will have to use
        #facebook to login.
        if request.POST.get('facebook_only', False):
            log.debug('Facebook Only')
            profile = FacebookProfile(facebook_id=request.facebook.uid)
            user = User(username=request.facebook.uid, email=profile.email)
            user.set_unusable_password()
            user.save()
            profile.user = user
            profile.save()
            log.info("Added user and profile for %s!" % request.facebook.uid)

            user = authenticate(request=request)
            login(request, user)

            # Add the user to EzSteroids if it is enabled
            add_user_to_EzSteroids("http://" + request.get_host(), user)

            return HttpResponseRedirect(redirect_url)
Ejemplo n.º 2
0
def facebook_login(request,
                   redirect_url="/",
                   template_name='facebook/login.html',
                   extra_context=None):
    """
    facebook_login
    ===============================
    
    Handles logging in a facebook user. Usually handles the django side of
    what happens when you click the facebook connect button. The user will get
    redirected to the 'setup' view if thier facebook account is not on file.
    If the user is on file, they will get redirected. You can specify the
    redirect url in the following order of presidence:
    
     1. whatever url is in the 'next' get parameter passed to the facebook_login url
     2. whatever url is passed to the facebook_login view when the url is defined
     3. whatever url is defined in the LOGIN_REDIRECT_URL setting directive
    
    Sending a user here without login will display a login template.
    
    If you define a url to use this view, you can pass the following parameters:
     * redirect_url: defines where to send the user after they are logged in. This
                     can get overridden by the url in the 'next' get param passed on 
                     the url.
     * template_name: Template to use if a user arrives at this page without submitting
                      to it. Uses 'facebook/login.html' by default.
     * extra_context: A context object whose contents will be passed to the template.
    """
    # User is logging in
    if request.method == "POST":
        log.debug("OK logging in...")
        url = reverse('facebook_setup')
        if request.POST.get(REDIRECT_FIELD_NAME, False):
            url += "?%s=%s" % (REDIRECT_FIELD_NAME,
                               request.POST[REDIRECT_FIELD_NAME])
        elif redirect_url:
            url += "?%s=%s" % (REDIRECT_FIELD_NAME, redirect_url)
        user = authenticate(request=request)
        if user is not None:
            if user.is_active:
                login(request, user)
                # Redirect to a success page.
                log.debug("Redirecting to %s" % url)
                return HttpResponseRedirect(url)
            else:
                log.debug("This account is disabled.")
                raise FacebookAuthError('This account is disabled.')
        elif request.facebook.uid:
            if getattr(settings, "FACEBOOK_USE_DUMMY_ACCOUNT", False):
                #check that this fb user is not already in the system
                try:
                    FacebookProfile.objects.get(
                        facebook_id=request.facebook.uid)
                    # already setup, move along please
                    return HttpResponseRedirect(redirect_url)
                except FacebookProfile.DoesNotExist, e:
                    # not in the db, ok to continue
                    pass

                profile = FacebookProfile(facebook_id=request.facebook.uid)
                user = User(username=request.facebook.uid, email=profile.email)
                user.set_unusable_password()
                user.save()
                profile.user = user
                profile.save()
                log.info("Added user and profile for %s!" %
                         request.facebook.uid)
                user = authenticate(request=request)
                login(request, user)
                return HttpResponseRedirect(redirect_url)
            else:
                #we have to set this user up
                log.debug("Redirecting to setup")
                return HttpResponseRedirect(url)