root / snf-astakos-app / astakos / im / target / local.py @ 33d969b0
History | View | Annotate | Download (6.4 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 |
from django.contrib.auth.decorators import login_required |
43 |
|
44 |
from astakos.im.util import prepare_response, get_query |
45 |
from astakos.im.views import requires_anonymous, signed_terms_required |
46 |
from astakos.im.models import PendingThirdPartyUser |
47 |
from astakos.im.forms import LoginForm, ExtendedPasswordChangeForm, \ |
48 |
ExtendedSetPasswordForm
|
49 |
from astakos.im.settings import (RATELIMIT_RETRIES_ALLOWED, |
50 |
ENABLE_LOCAL_ACCOUNT_MIGRATION) |
51 |
import astakos.im.messages as astakos_messages |
52 |
from astakos.im.views import requires_auth_provider |
53 |
from astakos.im import settings |
54 |
|
55 |
from ratelimit.decorators import ratelimit |
56 |
|
57 |
retries = RATELIMIT_RETRIES_ALLOWED - 1
|
58 |
rate = str(retries) + '/m' |
59 |
|
60 |
|
61 |
@requires_auth_provider('local', login=True) |
62 |
@require_http_methods(["GET", "POST"]) |
63 |
@csrf_exempt
|
64 |
@requires_anonymous
|
65 |
@ratelimit(field='username', method='POST', rate=rate) |
66 |
def login(request, on_failure='im/login.html'): |
67 |
"""
|
68 |
on_failure: the template name to render on login failure
|
69 |
"""
|
70 |
was_limited = getattr(request, 'limited', False) |
71 |
form = LoginForm(data=request.POST, |
72 |
was_limited=was_limited, |
73 |
request=request) |
74 |
next = get_query(request).get('next', '') |
75 |
third_party_token = get_query(request).get('key', False) |
76 |
|
77 |
if not form.is_valid(): |
78 |
return render_to_response(
|
79 |
on_failure, |
80 |
{'login_form':form,
|
81 |
'next':next, |
82 |
'key': third_party_token},
|
83 |
context_instance=RequestContext(request) |
84 |
) |
85 |
# get the user from the cash
|
86 |
user = form.user_cache |
87 |
|
88 |
message = None
|
89 |
if not user: |
90 |
message = _(astakos_messages.ACCOUNT_AUTHENTICATION_FAILED) |
91 |
elif not user.is_active: |
92 |
if not user.activation_sent: |
93 |
message = _(astakos_messages.ACCOUNT_PENDING_ACTIVATION) |
94 |
else:
|
95 |
# TODO: USE astakos_messages
|
96 |
url = reverse('send_activation', kwargs={'user_id':user.id}) |
97 |
msg = _('You have not followed the activation link.')
|
98 |
if settings.MODERATION_ENABLED:
|
99 |
msg_extra = ' ' + _('Please contact support.') |
100 |
else:
|
101 |
msg_extra = _('<a href="%s">Resend activation email?</a>') % url
|
102 |
|
103 |
message = msg + msg_extra |
104 |
elif not user.can_login_with_auth_provider('local'): |
105 |
# valid user logged in with no auth providers set, add local provider
|
106 |
# and let him log in
|
107 |
if user.auth_providers.count() == 0: |
108 |
user.add_auth_provider('local')
|
109 |
else:
|
110 |
message = _(astakos_messages.NO_LOCAL_AUTH) |
111 |
|
112 |
if message:
|
113 |
messages.error(request, message) |
114 |
return render_to_response(on_failure,
|
115 |
{'login_form': form},
|
116 |
context_instance=RequestContext(request)) |
117 |
|
118 |
response = prepare_response(request, user, next)
|
119 |
if third_party_token:
|
120 |
# use requests to assign the account he just authenticated with with
|
121 |
# a third party provider account
|
122 |
# TODO: USE astakos_messages
|
123 |
try:
|
124 |
request.user.add_pending_auth_provider(third_party_token) |
125 |
messages.success(request, _('Your new login method has been added'))
|
126 |
except PendingThirdPartyUser.DoesNotExist:
|
127 |
messages.error(request, _('Account method assignment failed'))
|
128 |
|
129 |
return response
|
130 |
|
131 |
@require_http_methods(["GET", "POST"]) |
132 |
@signed_terms_required
|
133 |
@login_required
|
134 |
@requires_auth_provider('local', login=True) |
135 |
def password_change(request, template_name='registration/password_change_form.html', |
136 |
post_change_redirect=None, password_change_form=ExtendedPasswordChangeForm):
|
137 |
|
138 |
create_password = False
|
139 |
|
140 |
# no local backend user wants to create a password
|
141 |
if not request.user.has_auth_provider('local'): |
142 |
create_password = True
|
143 |
password_change_form = ExtendedSetPasswordForm |
144 |
|
145 |
if post_change_redirect is None: |
146 |
post_change_redirect = reverse('edit_profile')
|
147 |
|
148 |
if request.method == "POST": |
149 |
form_kwargs = dict(
|
150 |
user=request.user, |
151 |
data=request.POST, |
152 |
) |
153 |
if not create_password: |
154 |
form_kwargs['session_key'] = session_key=request.session.session_key
|
155 |
|
156 |
form = password_change_form(**form_kwargs) |
157 |
if form.is_valid():
|
158 |
form.save() |
159 |
return HttpResponseRedirect(post_change_redirect)
|
160 |
else:
|
161 |
form = password_change_form(user=request.user) |
162 |
return render_to_response(template_name, {
|
163 |
'form': form,
|
164 |
}, context_instance=RequestContext(request)) |
165 |
|