root / snf-astakos-app / astakos / im / views / target / local.py @ a3e3917f
History | View | Annotate | Download (8.1 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 HttpResponseRedirect |
35 |
from django.shortcuts import render_to_response |
36 |
from django.template import RequestContext |
37 |
from django.contrib import messages |
38 |
from django.utils.translation import ugettext as _ |
39 |
from django.views.decorators.csrf import csrf_exempt |
40 |
from django.views.decorators.http import require_http_methods |
41 |
from django.core.urlresolvers import reverse |
42 |
|
43 |
import django.contrib.auth.views as django_auth_views |
44 |
|
45 |
from astakos.im.util import prepare_response, get_query |
46 |
from astakos.im.models import PendingThirdPartyUser |
47 |
from astakos.im.forms import LoginForm, ExtendedPasswordChangeForm, \ |
48 |
ExtendedSetPasswordForm
|
49 |
from astakos.im import settings |
50 |
import astakos.im.messages as astakos_messages |
51 |
from astakos.im import auth_providers as auth |
52 |
from astakos.im.views.decorators import cookie_fix, requires_anonymous, \ |
53 |
signed_terms_required, requires_auth_provider, login_required |
54 |
|
55 |
from ratelimit.decorators import ratelimit |
56 |
|
57 |
retries = settings.RATELIMIT_RETRIES_ALLOWED - 1
|
58 |
rate = str(retries) + '/m' |
59 |
|
60 |
|
61 |
@requires_auth_provider('local') |
62 |
@require_http_methods(["GET", "POST"]) |
63 |
@csrf_exempt
|
64 |
@requires_anonymous
|
65 |
@cookie_fix
|
66 |
@ratelimit(field='username', method='POST', rate=rate) |
67 |
def login(request, on_failure='im/login.html'): |
68 |
"""
|
69 |
on_failure: the template name to render on login failure
|
70 |
"""
|
71 |
if request.method == 'GET': |
72 |
return HttpResponseRedirect(reverse('login')) |
73 |
|
74 |
was_limited = getattr(request, 'limited', False) |
75 |
form = LoginForm(data=request.POST, |
76 |
was_limited=was_limited, |
77 |
request=request) |
78 |
next = get_query(request).get('next', '') |
79 |
third_party_token = get_query(request).get('key', False) |
80 |
provider = auth.get_provider('local')
|
81 |
|
82 |
if not form.is_valid(): |
83 |
if third_party_token:
|
84 |
messages.info(request, provider.get_login_to_add_msg) |
85 |
|
86 |
return render_to_response(
|
87 |
on_failure, |
88 |
{'login_form': form,
|
89 |
'next': next, |
90 |
'key': third_party_token},
|
91 |
context_instance=RequestContext(request)) |
92 |
|
93 |
# get the user from the cache
|
94 |
user = form.user_cache |
95 |
provider = auth.get_provider('local', user)
|
96 |
|
97 |
if not provider.get_login_policy: |
98 |
message = provider.get_login_disabled_msg |
99 |
messages.error(request, message) |
100 |
return HttpResponseRedirect(reverse('login')) |
101 |
|
102 |
message = None
|
103 |
if not user: |
104 |
message = provider.get_authentication_failed_msg |
105 |
elif not user.is_active: |
106 |
message = user.get_inactive_message('local')
|
107 |
|
108 |
elif not user.has_auth_provider('local'): |
109 |
# valid user logged in with no auth providers set, add local provider
|
110 |
# and let him log in
|
111 |
if not user.get_available_auth_providers(): |
112 |
user.add_auth_provider('local')
|
113 |
else:
|
114 |
message = _(astakos_messages.NO_LOCAL_AUTH) |
115 |
|
116 |
if message:
|
117 |
messages.error(request, message) |
118 |
return render_to_response(on_failure,
|
119 |
{'login_form': form},
|
120 |
context_instance=RequestContext(request)) |
121 |
|
122 |
response = prepare_response(request, user, next)
|
123 |
if third_party_token:
|
124 |
# use requests to assign the account he just authenticated with with
|
125 |
# a third party provider account
|
126 |
try:
|
127 |
request.user.add_pending_auth_provider(third_party_token) |
128 |
except PendingThirdPartyUser.DoesNotExist:
|
129 |
provider = auth.get_provider('local', request.user)
|
130 |
messages.error(request, provider.get_add_failed_msg) |
131 |
|
132 |
provider = user.get_auth_provider('local')
|
133 |
messages.success(request, provider.get_login_success_msg) |
134 |
response.set_cookie('astakos_last_login_method', 'local') |
135 |
return response
|
136 |
|
137 |
|
138 |
@require_http_methods(["GET"]) |
139 |
@cookie_fix
|
140 |
def password_reset_done(request, *args, **kwargs): |
141 |
messages.success(request, _(astakos_messages.PASSWORD_RESET_DONE)) |
142 |
return HttpResponseRedirect(reverse('index')) |
143 |
|
144 |
|
145 |
@require_http_methods(["GET"]) |
146 |
@cookie_fix
|
147 |
def password_reset_confirm_done(request, *args, **kwargs): |
148 |
messages.success(request, _(astakos_messages.PASSWORD_RESET_CONFIRM_DONE)) |
149 |
return HttpResponseRedirect(reverse('index')) |
150 |
|
151 |
|
152 |
@cookie_fix
|
153 |
def password_reset(request, *args, **kwargs): |
154 |
kwargs['post_reset_redirect'] = reverse(
|
155 |
'astakos.im.views.target.local.password_reset_done')
|
156 |
return django_auth_views.password_reset(request, *args, **kwargs)
|
157 |
|
158 |
|
159 |
@cookie_fix
|
160 |
def password_reset_confirm(request, *args, **kwargs): |
161 |
kwargs['post_reset_redirect'] = reverse(
|
162 |
'astakos.im.views.target.local.password_reset_complete')
|
163 |
return django_auth_views.password_reset_confirm(request, *args, **kwargs)
|
164 |
|
165 |
|
166 |
@cookie_fix
|
167 |
def password_reset_complete(request, *args, **kwargs): |
168 |
return django_auth_views.password_reset_complete(request, *args, **kwargs)
|
169 |
|
170 |
|
171 |
@require_http_methods(["GET", "POST"]) |
172 |
@signed_terms_required
|
173 |
@login_required
|
174 |
@cookie_fix
|
175 |
@requires_auth_provider('local', login=True) |
176 |
def password_change(request, |
177 |
template_name='registration/password_change_form.html',
|
178 |
post_change_redirect=None,
|
179 |
password_change_form=ExtendedPasswordChangeForm): |
180 |
|
181 |
create_password = False
|
182 |
|
183 |
provider = auth.get_provider('local', request.user)
|
184 |
|
185 |
# no local backend user wants to create a password
|
186 |
if not request.user.has_auth_provider('local'): |
187 |
if not provider.get_add_policy: |
188 |
messages.error(request, provider.get_add_disabled_msg) |
189 |
return HttpResponseRedirect(reverse('edit_profile')) |
190 |
|
191 |
create_password = True
|
192 |
password_change_form = ExtendedSetPasswordForm |
193 |
|
194 |
if post_change_redirect is None: |
195 |
post_change_redirect = reverse('edit_profile')
|
196 |
|
197 |
if request.method == "POST": |
198 |
form_kwargs = dict(
|
199 |
user=request.user, |
200 |
data=request.POST, |
201 |
) |
202 |
if not create_password: |
203 |
form_kwargs['session_key'] = request.session.session_key
|
204 |
|
205 |
form = password_change_form(**form_kwargs) |
206 |
if form.is_valid():
|
207 |
form.save() |
208 |
if create_password:
|
209 |
provider = auth.get_provider('local', request.user)
|
210 |
messages.success(request, provider.get_added_msg) |
211 |
else:
|
212 |
messages.success(request, |
213 |
astakos_messages.PASSWORD_RESET_CONFIRM_DONE) |
214 |
return HttpResponseRedirect(post_change_redirect)
|
215 |
else:
|
216 |
form = password_change_form(user=request.user) |
217 |
return render_to_response(template_name, {
|
218 |
'form': form,
|
219 |
}, context_instance=RequestContext(request, {'create_password':
|
220 |
create_password})) |