1 | ''' |
---|
2 | Created on 29 Oct 2012 |
---|
3 | |
---|
4 | @author: mnagni |
---|
5 | ''' |
---|
6 | from django.contrib.auth import authenticate |
---|
7 | from django.shortcuts import render_to_response |
---|
8 | from dj_security.login_form import LoginForm |
---|
9 | from django.core.context_processors import csrf |
---|
10 | from django.conf import settings |
---|
11 | import logging |
---|
12 | from dj_security.encoder import SecurityEncoder |
---|
13 | |
---|
14 | # Get an instance of a logger |
---|
15 | LOGGER = logging.getLogger(__name__) |
---|
16 | |
---|
17 | def 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 | |
---|
23 | def 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 = request.POST['username'] |
---|
30 | password = request.POST['password'] |
---|
31 | user = authenticate(username=username, password=password) |
---|
32 | if user is not None: |
---|
33 | if user.is_active: |
---|
34 | context['user'] = SecurityEncoder().encode(user) |
---|
35 | request.auth_user = context['user'] |
---|
36 | return _encode_authenticated_response(request, context) |
---|
37 | else: |
---|
38 | # Return a 'disabled account' error message |
---|
39 | context['error'] = u'account disabled' |
---|
40 | return error_handle(request, context) |
---|
41 | else: |
---|
42 | # Return an 'invalid login' error message. |
---|
43 | context['error'] = u'invalid login' |
---|
44 | return error_handle(request, context) |
---|
45 | else: |
---|
46 | context['error'] = u'form is invalid' |
---|
47 | return error_handle(request, context) |
---|
48 | else: |
---|
49 | if getattr(settings, "FAKE_AUTHENTICATION", False): |
---|
50 | context['user'] = '{"username": "mnagni", ' |
---|
51 | '"first_name": "Maurizio", "last_name": "Nagni", ' |
---|
52 | '"is_active": true, "email": "maurizio.nagni@stfc.ac.uk", ' |
---|
53 | '"is_superuser": false, "is_staff": true, ' |
---|
54 | '"last_login": "2012-10-18 11:05:28.700139+00:00", ' |
---|
55 | '"date_joined": "2012-03-22 14:20:56+00:00", "id": 29, ' |
---|
56 | '"permissions": "auth.add_user,proginfo.add_dataproduct, ' |
---|
57 | 'proginfo.change_programme, cedainfoapp.delete_vmrequest"}' |
---|
58 | return _encode_authenticated_response(request, context) |
---|
59 | |
---|
60 | # An unbound form |
---|
61 | form = LoginForm() |
---|
62 | context['form'] = form |
---|
63 | context.update(csrf(request)) |
---|
64 | return render_to_response('login.html', context) |
---|
65 | |
---|
66 | def _encode_authenticated_response(request, context): |
---|
67 | redirect_parameter = getattr(settings, 'REDIRECT_URL', 'r') |
---|
68 | context['redirect_url'] = request.GET.get(redirect_parameter, '') |
---|
69 | return render_to_response('logged_in.html', context) |
---|