]> git.openstreetmap.org Git - osqa.git/blob - forum/views/auth.py
c9f3111109b5397411a8f603a738d40509018366
[osqa.git] / forum / views / auth.py
1 # -*- coding: utf-8 -*-
2
3 import datetime
4 import logging
5
6 from django.shortcuts import render_to_response, get_object_or_404
7 from django.template import RequestContext
8 from django.core.urlresolvers import reverse
9 from django.http import HttpResponseRedirect, Http404
10 from django.utils.safestring import mark_safe
11 from django.utils.translation import ugettext as _
12 from django.utils.encoding import smart_unicode
13 from django.contrib.auth import login, logout
14
15 from writers import manage_pending_data
16
17 from forum.actions import EmailValidationAction
18 from forum.utils import html
19 from forum.views.decorators import login_required
20 from forum.modules import decorate
21 from forum.forms import SimpleRegistrationForm, TemporaryLoginRequestForm, ChangePasswordForm, SetPasswordForm
22 from forum.http_responses import HttpResponseUnauthorized
23 from forum.utils.mail import send_template_email
24 from forum.authentication.base import InvalidAuthentication
25 from forum.authentication import AUTH_PROVIDERS
26 from forum.models import User, AuthKeyUserAssociation, ValidationHash
27 from forum.actions import UserJoinsAction, UserLoginAction
28 from forum import settings
29
30 from vars import ON_SIGNIN_SESSION_ATTR, PENDING_SUBMISSION_SESSION_ATTR
31
32 def signin_page(request):
33     referer = request.META.get('HTTP_REFERER', '/')
34
35     # If the referer is equal to the sign up page, e. g. if the previous login attempt was not successful we do not
36     # change the sign in URL. The user should go to the same page.
37     if not referer.replace(settings.APP_URL, '') == reverse('auth_signin'):
38         request.session[ON_SIGNIN_SESSION_ATTR] = referer
39
40     all_providers = [provider.context for provider in AUTH_PROVIDERS.values() if provider.context]
41
42     sort = lambda c1, c2: c1.weight - c2.weight
43     can_show = lambda c: not request.user.is_authenticated() or c.show_to_logged_in_user
44
45     bigicon_providers = sorted([
46     context for context in all_providers if context.mode == 'BIGICON' and can_show(context)
47     ], sort)
48
49     smallicon_providers = sorted([
50     context for context in all_providers if context.mode == 'SMALLICON' and can_show(context)
51     ], sort)
52
53     top_stackitem_providers = sorted([
54     context for context in all_providers if context.mode == 'TOP_STACK_ITEM' and can_show(context)
55     ], sort)
56
57     stackitem_providers = sorted([
58     context for context in all_providers if context.mode == 'STACK_ITEM' and can_show(context)
59     ], sort)
60
61     try:
62         msg = request.session['auth_error']
63         del request.session['auth_error']
64     except:
65         msg = None
66
67     return render_to_response(
68             'auth/signin.html',
69             {
70             'msg': msg,
71             'all_providers': all_providers,
72             'bigicon_providers': bigicon_providers,
73             'top_stackitem_providers': top_stackitem_providers,
74             'stackitem_providers': stackitem_providers,
75             'smallicon_providers': smallicon_providers,
76             },
77             RequestContext(request))
78
79 def prepare_provider_signin(request, provider):
80     force_email_request = request.REQUEST.get('validate_email', 'yes') == 'yes'
81     request.session['force_email_request'] = force_email_request
82
83     if provider in AUTH_PROVIDERS:
84         provider_class = AUTH_PROVIDERS[provider].consumer
85
86         try:
87             request_url = provider_class.prepare_authentication_request(request,
88                                                                         reverse('auth_provider_done',
89                                                                                 kwargs={'provider': provider}))
90
91             return HttpResponseRedirect(request_url)
92         except NotImplementedError, e:
93             return process_provider_signin(request, provider)
94         except InvalidAuthentication, e:
95             request.session['auth_error'] = e.message
96
97         return HttpResponseRedirect(reverse('auth_signin'))
98     else:
99         raise Http404()
100
101
102 def process_provider_signin(request, provider):
103     if provider in AUTH_PROVIDERS:
104         provider_class = AUTH_PROVIDERS[provider].consumer
105
106         try:
107             assoc_key = provider_class.process_authentication_request(request)
108         except InvalidAuthentication, e:
109             request.session['auth_error'] = e.message
110             return HttpResponseRedirect(reverse('auth_signin'))
111
112         if request.user.is_authenticated():
113             if isinstance(assoc_key, (type, User)):
114                 if request.user != assoc_key:
115                     request.session['auth_error'] = _(
116                             "Sorry, these login credentials belong to anoother user. Plese terminate your current session and try again."
117                             )
118                 else:
119                     request.session['auth_error'] = _("You are already logged in with that user.")
120             else:
121                 try:
122                     assoc = AuthKeyUserAssociation.objects.get(key=assoc_key)
123                     if assoc.user == request.user:
124                         request.session['auth_error'] = _(
125                                 "These login credentials are already associated with your account.")
126                     else:
127                         request.session['auth_error'] = _(
128                                 "Sorry, these login credentials belong to anoother user. Plese terminate your current session and try again."
129                                 )
130                 except:
131                     uassoc = AuthKeyUserAssociation(user=request.user, key=assoc_key, provider=provider)
132                     uassoc.save()
133                     request.user.message_set.create(
134                             message=_('The new credentials are now associated with your account'))
135                     return HttpResponseRedirect(reverse('user_authsettings', args=[request.user.id]))
136
137             return HttpResponseRedirect(reverse('auth_signin'))
138         else:
139             if isinstance(assoc_key, User):
140                 return login_and_forward(request, assoc_key)
141
142         try:
143             assoc = AuthKeyUserAssociation.objects.get(key=assoc_key)
144             user_ = assoc.user
145             return login_and_forward(request, user_)
146         except AuthKeyUserAssociation.DoesNotExist:
147             request.session['assoc_key'] = assoc_key
148             request.session['auth_provider'] = provider
149             return HttpResponseRedirect(reverse('auth_external_register'))
150
151     return HttpResponseRedirect(reverse('auth_signin'))
152
153 def external_register(request):
154     if request.method == 'POST' and 'bnewaccount' in request.POST:
155         form1 = SimpleRegistrationForm(request.POST)
156
157         if form1.is_valid():
158             user_ = User(username=form1.cleaned_data['username'], email=form1.cleaned_data['email'])
159             user_.email_isvalid = request.session.get('auth_validated_email', '') == form1.cleaned_data['email']
160             user_.set_unusable_password()
161
162             if User.objects.all().count() == 0:
163                 user_.is_superuser = True
164                 user_.is_staff = True
165
166             user_.save()
167             UserJoinsAction(user=user_, ip=request.META['REMOTE_ADDR']).save()
168
169             try:
170                 assoc_key = request.session['assoc_key']
171                 auth_provider = request.session['auth_provider']
172             except:
173                 request.session['auth_error'] = _(
174                         "Oops, something went wrong in the middle of this process. Please try again. Note that you need to have cookies enabled for the authentication to work."
175                         )
176                 logging.error("Missing session data when trying to complete user registration: %s" % ", ".join(
177                         ["%s: %s" % (k, v) for k, v in request.META.items()]))
178                 return HttpResponseRedirect(reverse('auth_signin'))
179
180             uassoc = AuthKeyUserAssociation(user=user_, key=assoc_key, provider=auth_provider)
181             uassoc.save()
182
183             del request.session['assoc_key']
184             del request.session['auth_provider']
185
186             return login_and_forward(request, user_, message=_("A welcome email has been sent to your email address. "))
187     else:
188         auth_provider = request.session.get('auth_provider', None)
189         if not auth_provider:
190             request.session['auth_error'] = _(
191                     "Oops, something went wrong in the middle of this process. Please try again.")
192             logging.error("Missing session data when trying to complete user registration: %s" % ", ".join(
193                     ["%s: %s" % (k, v) for k, v in request.META.items()]))
194             return HttpResponseRedirect(reverse('auth_signin'))
195
196         provider_class = AUTH_PROVIDERS[auth_provider].consumer
197
198         # Pass the cookies to the Facebook authentication class get_user_data method. We need them to take the access token.
199         if provider_class.__class__.__name__ == 'FacebookAuthConsumer':
200             user_data = provider_class.get_user_data(request.COOKIES)
201         else:
202             user_data = provider_class.get_user_data(request.session['assoc_key'])
203
204         if not user_data:
205             user_data = request.session.get('auth_consumer_data', {})
206
207         username = user_data.get('username', '')
208         email = user_data.get('email', '')
209
210         if email:
211             request.session['auth_validated_email'] = email
212
213         form1 = SimpleRegistrationForm(initial={
214         'next': '/',
215         'username': username,
216         'email': email,
217         })
218
219     provider_context = AUTH_PROVIDERS[request.session['auth_provider']].context
220
221     return render_to_response('auth/complete.html', {
222     'form1': form1,
223     'provider':provider_context and mark_safe(provider_context.human_name) or _('unknown'),
224     'login_type':provider_context.id,
225     'gravatar_faq_url':reverse('faq') + '#gravatar',
226     }, context_instance=RequestContext(request))
227
228 def request_temp_login(request):
229     if request.method == 'POST':
230         form = TemporaryLoginRequestForm(request.POST)
231
232         if form.is_valid():
233             users = form.user_cache
234
235             for u in users:
236                 if u.is_suspended():
237                     return forward_suspended_user(request, u, False)
238
239             for u in users:
240                 try:
241                     hash = get_object_or_404(ValidationHash, user=u, type='templogin')
242                     if hash.expiration < datetime.datetime.now():
243                         hash.delete()
244                         return request_temp_login(request)
245                 except:
246                     hash = ValidationHash.objects.create_new(u, 'templogin', [u.id])
247
248                 send_template_email([u], "auth/temp_login_email.html", {'temp_login_code': hash})
249
250                 request.user.message_set.create(message=_("An email has been sent with your temporary login key"))
251
252             return HttpResponseRedirect(reverse('index'))
253     else:
254         form = TemporaryLoginRequestForm()
255
256     return render_to_response(
257             'auth/temp_login_request.html', {'form': form},
258             context_instance=RequestContext(request))
259
260 def temp_signin(request, user, code):
261     user = get_object_or_404(User, id=user)
262
263     if (ValidationHash.objects.validate(code, user, 'templogin', [user.id])):
264         
265         # If the user requests temp_signin he must have forgotten his password. So we mark it as unusable.
266         user.set_unusable_password()
267         user.save()
268         
269         return login_and_forward(request, user, reverse('user_authsettings', kwargs={'id': user.id}),
270                                  _(
271                                          "You are logged in with a temporary access key, please take the time to fix your issue with authentication."
272                                          ))
273     else:
274         raise Http404()
275
276 def send_validation_email(request):
277     if not request.user.is_authenticated():
278         return HttpResponseUnauthorized(request)
279     else:
280         # We check if there are some old validation hashes. If there are -- we delete them.
281         try:
282             hash = ValidationHash.objects.get(user=request.user, type='email')
283             hash.delete()
284         except:
285             pass
286
287         # We don't care if there are previous cashes in the database... In every case we have to create a new one
288         hash = ValidationHash.objects.create_new(request.user, 'email', [request.user.email])
289
290         send_template_email([request.user], "auth/mail_validation.html", {'validation_code': hash})
291         request.user.message_set.create(message=_("A message with an email validation link was just sent to your address."))
292         return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
293
294         
295
296 def validate_email(request, user, code):
297     user = get_object_or_404(User, id=user)
298
299     if (ValidationHash.objects.validate(code, user, 'email', [user.email])):
300         EmailValidationAction(user=user, ip=request.META['REMOTE_ADDR']).save()
301         return login_and_forward(request, user, reverse('index'), _("Thank you, your email is now validated."))
302     else:
303         return render_to_response('auth/mail_already_validated.html', { 'user' : user }, RequestContext(request))
304
305 def auth_settings(request, id):
306     user_ = get_object_or_404(User, id=id)
307
308     if not (request.user.is_superuser or request.user == user_):
309         return HttpResponseUnauthorized(request)
310
311     auth_keys = user_.auth_keys.all()
312
313     if request.user.is_superuser or (not user_.has_usable_password()):
314         FormClass = SetPasswordForm
315     else:
316         FormClass = ChangePasswordForm
317
318     if request.POST:
319         form = FormClass(request.POST, user=user_)
320         if form.is_valid():
321             is_new_pass = not user_.has_usable_password()
322             user_.set_password(form.cleaned_data['password1'])
323             user_.save()
324
325             if is_new_pass:
326                 request.user.message_set.create(message=_("New password set"))
327                 if not request.user.is_superuser:
328                     form = ChangePasswordForm(user=user_)
329             else:
330                 request.user.message_set.create(message=_("Your password was changed"))
331
332             return HttpResponseRedirect(reverse('user_authsettings', kwargs={'id': user_.id}))
333     else:
334         form = FormClass(user=user_)
335
336     auth_keys_list = []
337
338     for k in auth_keys:
339         provider = AUTH_PROVIDERS.get(k.provider, None)
340
341         if provider is not None:
342             name =  "%s: %s" % (provider.context.human_name, provider.context.readable_key(k))
343         else:
344             from forum.authentication.base import ConsumerTemplateContext
345             "unknown: %s" % ConsumerTemplateContext.readable_key(k)
346
347         auth_keys_list.append({
348         'name': name,
349         'id': k.id
350         })
351
352     return render_to_response('auth/auth_settings.html', {
353     'view_user': user_,
354     "can_view_private": (user_ == request.user) or request.user.is_superuser,
355     'form': form,
356     'has_password': user_.has_usable_password(),
357     'auth_keys': auth_keys_list,
358     'allow_local_auth': AUTH_PROVIDERS.get('local', None),
359     }, context_instance=RequestContext(request))
360
361 def remove_external_provider(request, id):
362     association = get_object_or_404(AuthKeyUserAssociation, id=id)
363     if not (request.user.is_superuser or request.user == association.user):
364         return HttpResponseUnauthorized(request)
365
366     request.user.message_set.create(message=_("You removed the association with %s") % association.provider)
367     association.delete()
368     return HttpResponseRedirect(reverse('user_authsettings', kwargs={'id': association.user.id}))
369
370 def login_and_forward(request, user, forward=None, message=None):
371     if user.is_suspended():
372         return forward_suspended_user(request, user)
373
374     user.backend = "django.contrib.auth.backends.ModelBackend"
375     login(request, user)
376
377     # Store the login action
378     UserLoginAction(user=user, ip=request.META['REMOTE_ADDR']).save()
379
380     if message is None:
381         message = _("Welcome back %s, you are now logged in") % smart_unicode(user.username)
382
383     request.user.message_set.create(message=message)
384
385     if not forward:
386         forward = request.session.get(ON_SIGNIN_SESSION_ATTR, reverse('index'))
387
388     pending_data = request.session.get(PENDING_SUBMISSION_SESSION_ATTR, None)
389
390     if pending_data and (user.email_isvalid or pending_data['type'] not in settings.REQUIRE_EMAIL_VALIDATION_TO):
391         submission_time = pending_data['time']
392         if submission_time < datetime.datetime.now() - datetime.timedelta(minutes=int(settings.HOLD_PENDING_POSTS_MINUTES)):
393             del request.session[PENDING_SUBMISSION_SESSION_ATTR]
394         elif submission_time < datetime.datetime.now() - datetime.timedelta(minutes=int(settings.WARN_PENDING_POSTS_MINUTES)):
395             user.message_set.create(message=(_("You have a %s pending submission.") % pending_data['data_name']) + " %s, %s, %s" % (
396                 html.hyperlink(reverse('manage_pending_data', kwargs={'action': _('save')}), _("save it")),
397                 html.hyperlink(reverse('manage_pending_data', kwargs={'action': _('review')}), _("review")),
398                 html.hyperlink(reverse('manage_pending_data', kwargs={'action': _('cancel')}), _("cancel"))
399             ))
400         else:
401             return manage_pending_data(request, _('save'), forward)
402
403     return HttpResponseRedirect(forward)
404
405 def forward_suspended_user(request, user, show_private_msg=True):
406     message = _("Sorry, but this account is suspended")
407     if show_private_msg:
408         msg_type = 'privatemsg'
409     else:
410         msg_type = 'publicmsg'
411
412     suspension = user.suspension
413     if suspension:
414         message += (":<br />" + suspension.extra.get(msg_type, ''))
415
416     request.user.message_set.create(message)
417     return HttpResponseRedirect(reverse('index'))
418
419 @decorate.withfn(login_required)
420 def signout(request):
421     logout(request)
422     return HttpResponseRedirect(reverse('index'))