Exemplo n.º 1
0
def login(request):
    # Redirect if already logged in
    if request.user.is_authenticated():
        messages.add_message(request, messages.INFO, _('You are logged in.'))
        return HttpResponseRedirect('/order/')
    redirect_to = request.REQUEST.get('next', '')
    if request.method == 'POST':
        form = forms.LoginForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data[u'email']
            password = form.cleaned_data[u'password']
            user = auth.authenticate(username=username, password=password)
            if user is not None:
                if user.is_active:  # success
                    auth.login(request, user)
                    if redirect_to:
                        return HttpResponseRedirect(redirect_to)
                    else:
                        return HttpResponseRedirect('/order/')
                else:  # inactive user
                    messages.add_message(
                        request, messages.ERROR,
                        _('This account is not activated. Please check your email for instructions '
                          +
                          'to activate this account, or request a new activation key.'
                          ))
                    return HttpResponseRedirect('/activationrequest/')
            else:  # invalid login info
                messages.add_message(request, messages.ERROR,
                                     _('Invalid username or password.'))
                return render_to_response(
                    "account/login.html",
                    RequestContext(request, {
                        'form': form,
                        'redirect_to': redirect_to,
                    }))
        else:  # form data invalid
            messages.add_message(request, messages.ERROR,
                                 MessageCatalog.form_invalid)
            return render_to_response(
                "account/login.html",
                RequestContext(request, {
                    'form': form,
                    'redirect_to': redirect_to,
                }))
    else:  # get unbound form
        form = forms.LoginForm()
        return render_to_response(
            "account/login.html",
            RequestContext(request, {
                'form': form,
                'redirect_to': redirect_to,
            }))
Exemplo n.º 2
0
def login(request):
    if request.get_full_path().split("/")[-1] != "" and request.get_full_path().split("/")[-1] != "login": # handling remote connections
        params = request.get_full_path().split("/")[-1].split("&")
        username = params[0]
        password = params[1]
        user = auth.authenticate(username=username, password=password)
        if user is not None and user.is_active:
            auth.login(request, user)
            return HttpResponse("Remote credentials correct")
        else:
            return HttpResponse("Remote credentials incorrect")
    if request.method == 'POST':
        username = request.POST.get('username', '')
        password = request.POST.get('password', '')
        print("USERNAME: "******"PASSWORD: "******"active"
            auth.login(request, user)
        # Redirect to a success page.
            return HttpResponseRedirect("/success")
            #return render(request, 'hub/account/success.html')
        else:
            # Show an error page
            return HttpResponseRedirect("/failure")
    else:
        form = forms.LoginForm()
        return render(request, "hub/login.html", {"form":form})
Exemplo n.º 3
0
def login(request):
    if request.method == 'GET':
        form = forms.LoginForm()
        return render_to_response('login.html', {'form': form},
                                  context_instance=RequestContext(request))
    else:
        form = forms.LoginForm(request.POST)
        if form.is_valid:
            username = request.POST.get('username')
            password = request.POST.get('password')
            user = auth.authenticate(username=username, password=password)
            if user is not None and user.is_active:
                auth.login(request, user)
                request.session['username'] = username
                return HttpResponseRedirect('/entry/write')
            else:
                return render_to_response(
                    'person.html', {'login_state': '0'},
                    context_instance=RequestContext(request))
        return HttpResponseRedirect('/')
Exemplo n.º 4
0
def login_view(request):

    if request.method == "GET":
        form = forms.LoginForm()
    else:
        form = forms.LoginForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']

            user = authenticate(request, username=username, password=password)

            if user is not None:
                login(request, user)

                try:
                    return HttpResponseRedirect(request.GET['next'])
                except KeyError:
                    return HttpResponseRedirect(reverse('index'))
    context = {"form": form}
    return render(request, 'main/login.html', context)
Exemplo n.º 5
0
def login_view(request, *args, **kwargs):

    # TODO: Support the "next" parameter in the template javascript redirect code.

    # This POST method is called by javascript and expects some JSON in return.
    # The goal here is to authenticate the user with oauth and then encrypt the
    # oauth information.  The encrypted information will be stored in a browser cookie,
    # to be later decrypted in the middleware level to set the "Authorization" header.
    if request.method == 'POST':

        if 'username' not in request.POST or 'password' not in request.POST:
            raise ValueError

        # Manually do django authentication.
        username = request.POST['username']
        password = request.POST['password']

        user = authenticate(request=request,
                            username=username,
                            password=password)

        if user is not None and user.is_active:

            # Log our user in to django
            login(request, user)

            # Create and encrypt the access token based on this user
            enc = encrypt_access_token(
                create_access_token(user, user.profile.oauth_scope))

            # Setup login redirect
            if 'next' in request.GET:
                redirect = request.GET['next']
            else:
                redirect = settings.LOGIN_REDIRECT_URL

            # Format our response
            response = JsonResponse(
                dict([('status', 'OK'), ('next', redirect)]))

            # TODO: find out why this delays forever when celery service is not running
            # user_security_event.delay(username, 'login')

            # Set the encrypted token in the response.
            response.set_cookie('token', enc.decode('UTF-8'))
            response.set_cookie('fade-page-in', 1)

        else:
            # Send our error message
            response = JsonResponse(dict([('status', 'ERROR')]))
            # user_security_event.delay(username, 'login', success=False)

        return response

    else:

        form = forms.LoginForm()
        context = {
            'form': form,
            'next': request.GET['next'] if 'next' in request.GET else None,
        }

    return render(request, 'accounts/login.html', context)