root / snf-astakos-app / astakos / im / target / local.py @ bf0c6de5
History | View | Annotate | Download (5.9 kB)
1 |
# Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from django.http import HttpResponseBadRequest, HttpResponseRedirect |
35 |
from django.shortcuts import render_to_response |
36 |
from django.template import RequestContext |
37 |
from django.contrib.auth import authenticate |
38 |
from django.contrib import messages |
39 |
from django.utils.translation import ugettext as _ |
40 |
from django.views.decorators.csrf import csrf_exempt |
41 |
from django.views.decorators.http import require_http_methods |
42 |
from django.core.urlresolvers import reverse |
43 |
from django.contrib.auth.decorators import login_required |
44 |
|
45 |
from astakos.im.util import prepare_response, get_query |
46 |
from astakos.im.views import requires_anonymous, signed_terms_required |
47 |
from astakos.im.models import AstakosUser, PendingThirdPartyUser |
48 |
from astakos.im.forms import LoginForm, ExtendedPasswordChangeForm |
49 |
from astakos.im.settings import RATELIMIT_RETRIES_ALLOWED |
50 |
from astakos.im.settings import ENABLE_LOCAL_ACCOUNT_MIGRATION |
51 |
|
52 |
from ratelimit.decorators import ratelimit |
53 |
|
54 |
retries = RATELIMIT_RETRIES_ALLOWED-1
|
55 |
rate = str(retries)+'/m' |
56 |
|
57 |
@require_http_methods(["GET", "POST"]) |
58 |
@csrf_exempt
|
59 |
@requires_anonymous
|
60 |
@ratelimit(field='username', method='POST', rate=rate) |
61 |
def login(request, on_failure='im/login.html'): |
62 |
"""
|
63 |
on_failure: the template name to render on login failure
|
64 |
"""
|
65 |
was_limited = getattr(request, 'limited', False) |
66 |
form = LoginForm(data=request.POST, was_limited=was_limited, request=request) |
67 |
next = get_query(request).get('next', '') |
68 |
username = get_query(request).get('key')
|
69 |
|
70 |
if not form.is_valid(): |
71 |
return render_to_response(
|
72 |
on_failure, |
73 |
{'login_form':form,
|
74 |
'next':next, |
75 |
'key':username},
|
76 |
context_instance=RequestContext(request) |
77 |
) |
78 |
# get the user from the cash
|
79 |
user = form.user_cache |
80 |
|
81 |
message = None
|
82 |
if not user: |
83 |
message = _('Cannot authenticate account')
|
84 |
elif not user.is_active: |
85 |
if not user.activation_sent: |
86 |
message = _('Your request is pending activation')
|
87 |
else:
|
88 |
url = reverse('send_activation', kwargs={'user_id':user.id}) |
89 |
message = _('You have not followed the activation link. \
|
90 |
<a href="%s">Resend activation email?</a>' % url)
|
91 |
elif user.provider not in ('local', ''): |
92 |
message = _( |
93 |
'Local login is not the current authentication method for this account.'
|
94 |
) |
95 |
|
96 |
if message:
|
97 |
messages.error(request, message) |
98 |
return render_to_response(on_failure,
|
99 |
{'login_form':form},
|
100 |
context_instance=RequestContext(request)) |
101 |
|
102 |
# hook for switching account to use third party authentication
|
103 |
if ENABLE_LOCAL_ACCOUNT_MIGRATION and username: |
104 |
try:
|
105 |
new = PendingThirdPartyUser.objects.get( |
106 |
username=username) |
107 |
except:
|
108 |
messages.error( |
109 |
request, |
110 |
_('Account failed to switch to %(provider)s' % locals()) |
111 |
) |
112 |
return render_to_response(
|
113 |
on_failure, |
114 |
{'login_form':form,
|
115 |
'next':next}, |
116 |
context_instance=RequestContext(request) |
117 |
) |
118 |
else:
|
119 |
user.provider = new.provider |
120 |
user.third_party_identifier = new.third_party_identifier |
121 |
user.save() |
122 |
new.delete() |
123 |
messages.success( |
124 |
request, |
125 |
_('Account successfully switched to %(provider)s' % user.__dict__)
|
126 |
) |
127 |
return prepare_response(request, user, next) |
128 |
|
129 |
@require_http_methods(["GET", "POST"]) |
130 |
@signed_terms_required
|
131 |
@login_required
|
132 |
def password_change(request, template_name='registration/password_change_form.html', |
133 |
post_change_redirect=None, password_change_form=ExtendedPasswordChangeForm):
|
134 |
if post_change_redirect is None: |
135 |
post_change_redirect = reverse('django.contrib.auth.views.password_change_done')
|
136 |
if request.method == "POST": |
137 |
form = password_change_form( |
138 |
user=request.user, |
139 |
data=request.POST, |
140 |
session_key=request.session.session_key |
141 |
) |
142 |
if form.is_valid():
|
143 |
form.save() |
144 |
return HttpResponseRedirect(post_change_redirect)
|
145 |
else:
|
146 |
form = password_change_form(user=request.user) |
147 |
return render_to_response(template_name, {
|
148 |
'form': form,
|
149 |
}, context_instance=RequestContext(request)) |