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

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

Small improves to logging

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