source: mauRepo/dj_security/trunk/dj_security/views/dj_security_login.py @ 8712

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/dj_security/trunk/dj_security/views/dj_security_login.py@8712
Revision 8712, 3.4 KB checked in by mnagni, 7 years ago (diff)

corrects the previous (wrong) workflow

  • Property svn:mime-type set to text/plain
Line 
1'''
2Created on 29 Oct 2012
3
4@author: mnagni
5'''
6from django.contrib.auth import authenticate
7from django.shortcuts import render_to_response
8from django.core.context_processors import csrf
9from django.conf import settings
10
11import logging
12import base64
13
14from dj_security.login_form import LoginForm
15from dj_security.encoder import SecurityEncoder
16
17# Get an instance of a logger
18LOGGER = logging.getLogger(__name__)
19
20def error_handle(request, context):
21    form = LoginForm()
22    context['form'] = form
23    context.update(csrf(request))
24    return render_to_response('login.html', context)
25
26def login(request):       
27    context = {}
28    if request.method == 'POST': # If the form has been submitted...
29        form = LoginForm(request.POST) # A form bound to the POST data
30        context['form'] = form
31        if form.is_valid(): # All validation rules pass
32            username = request.POST.get('username')
33            password = request.POST.get('password')
34            LOGGER.debug("logging: %s" % (username))           
35            user = authenticate(username=username, password=password)             
36            if user is not None:
37                LOGGER.debug("authenticated: %s" % (username))
38                if user.is_active:
39                    LOGGER.debug("user %s is Active" % (username))           
40                    context['user'] = SecurityEncoder().encode(user)
41                    request.auth_user = context.get('user')
42                    return _encode_authenticated_response(request, context)         
43                else:
44                    # Return a 'disabled account' error message
45                    context['error'] = u'account disabled'
46                    LOGGER.debug("Account Disabled for user %s" % (username))                   
47                    return error_handle(request, context)
48            else:
49                # Return an 'invalid login' error message.
50                context['error'] = u'invalid login'               
51                LOGGER.debug("Invalid login for user %s" % (username))               
52                return error_handle(request, context)
53        else:
54            context['error'] = u'form is invalid'
55            return error_handle(request, context)
56    else:
57        if getattr(settings, "FAKE_AUTHENTICATION", False):
58            context['user'] = '{"username": "mnagni", '
59            '"first_name": "Maurizio", "last_name": "Nagni", '
60            '"is_active": true, "email": "maurizio.nagni@stfc.ac.uk", '
61            '"is_superuser": false, "is_staff": true, '
62            '"last_login": "2012-10-18 11:05:28.700139+00:00", '
63            '"date_joined": "2012-03-22 14:20:56+00:00", "id": 29, '
64            '"permissions": "auth.add_user,proginfo.add_dataproduct, '
65            'proginfo.change_programme, cedainfoapp.delete_vmrequest"}'
66            return _encode_authenticated_response(request, context) 
67       
68        # An unbound form
69        form = LoginForm()
70        context['form'] = form
71        context.update(csrf(request))
72        return render_to_response('login.html', context)
73
74def logged_in(request):
75    return _encode_authenticated_response(request, context = {})   
76   
77def _encode_authenticated_response(request, context):
78    redirect_parameter = getattr(settings, 'REDIRECT_URL', 'r')
79    context['redirect_url'] = \
80        base64.b64decode(request.session.get(redirect_parameter, ''))   
81    return render_to_response('logged_in.html', context)
Note: See TracBrowser for help on using the repository browser.