root / snf-astakos-app / astakos / im / target / redirect.py @ 09e7393c
History | View | Annotate | Download (5 kB)
1 | aba1e498 | Antony Chazapis | # Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | 64cd4730 | Antony Chazapis | #
|
3 | 64cd4730 | Antony Chazapis | # Redistribution and use in source and binary forms, with or
|
4 | 64cd4730 | Antony Chazapis | # without modification, are permitted provided that the following
|
5 | 64cd4730 | Antony Chazapis | # conditions are met:
|
6 | 64cd4730 | Antony Chazapis | #
|
7 | 64cd4730 | Antony Chazapis | # 1. Redistributions of source code must retain the above
|
8 | 64cd4730 | Antony Chazapis | # copyright notice, this list of conditions and the following
|
9 | 64cd4730 | Antony Chazapis | # disclaimer.
|
10 | 64cd4730 | Antony Chazapis | #
|
11 | 64cd4730 | Antony Chazapis | # 2. Redistributions in binary form must reproduce the above
|
12 | 64cd4730 | Antony Chazapis | # copyright notice, this list of conditions and the following
|
13 | 64cd4730 | Antony Chazapis | # disclaimer in the documentation and/or other materials
|
14 | 64cd4730 | Antony Chazapis | # provided with the distribution.
|
15 | 64cd4730 | Antony Chazapis | #
|
16 | 64cd4730 | Antony Chazapis | # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 | 64cd4730 | Antony Chazapis | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | 64cd4730 | Antony Chazapis | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 | 64cd4730 | Antony Chazapis | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 | 64cd4730 | Antony Chazapis | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 | 64cd4730 | Antony Chazapis | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 | 64cd4730 | Antony Chazapis | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 | 64cd4730 | Antony Chazapis | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 | 64cd4730 | Antony Chazapis | # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | 64cd4730 | Antony Chazapis | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 | 64cd4730 | Antony Chazapis | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 | 64cd4730 | Antony Chazapis | # POSSIBILITY OF SUCH DAMAGE.
|
28 | 64cd4730 | Antony Chazapis | #
|
29 | 64cd4730 | Antony Chazapis | # The views and conclusions contained in the software and
|
30 | 64cd4730 | Antony Chazapis | # documentation are those of the authors and should not be
|
31 | 64cd4730 | Antony Chazapis | # interpreted as representing official policies, either expressed
|
32 | 64cd4730 | Antony Chazapis | # or implied, of GRNET S.A.
|
33 | 64cd4730 | Antony Chazapis | |
34 | 148b0cde | Sofia Papagiannaki | from django.core.urlresolvers import reverse |
35 | 148b0cde | Sofia Papagiannaki | from django.shortcuts import redirect |
36 | a196eb7e | Sofia Papagiannaki | from django.utils.translation import ugettext as _ |
37 | a196eb7e | Sofia Papagiannaki | from django.contrib import messages |
38 | a196eb7e | Sofia Papagiannaki | from django.utils.http import urlencode |
39 | ebd369d0 | Sofia Papagiannaki | from django.contrib.auth import login as auth_login, authenticate, logout |
40 | ebd369d0 | Sofia Papagiannaki | from django.http import HttpResponse, HttpResponseBadRequest |
41 | a196eb7e | Sofia Papagiannaki | |
42 | 148b0cde | Sofia Papagiannaki | from urllib import quote |
43 | ebd369d0 | Sofia Papagiannaki | from urlparse import urlunsplit, urlsplit, urlparse, parse_qsl |
44 | 64cd4730 | Antony Chazapis | |
45 | 860b37f0 | Sofia Papagiannaki | from astakos.im.settings import COOKIE_NAME, COOKIE_DOMAIN |
46 | 09e7393c | Sofia Papagiannaki | from astakos.im.util import set_cookie |
47 | 860b37f0 | Sofia Papagiannaki | |
48 | e015e9e6 | Sofia Papagiannaki | import logging |
49 | e015e9e6 | Sofia Papagiannaki | |
50 | e015e9e6 | Sofia Papagiannaki | logger = logging.getLogger(__name__) |
51 | e015e9e6 | Sofia Papagiannaki | |
52 | 64cd4730 | Antony Chazapis | def login(request): |
53 | a196eb7e | Sofia Papagiannaki | """
|
54 | 09e7393c | Sofia Papagiannaki | If there is no ``next`` request parameter redirects to astakos index page
|
55 | 09e7393c | Sofia Papagiannaki | displaying an error message.
|
56 | 09e7393c | Sofia Papagiannaki | If the request user is authenticated and has signed the approval terms,
|
57 | 09e7393c | Sofia Papagiannaki | redirects to `next` request parameter. If not, redirects to approval terms
|
58 | 09e7393c | Sofia Papagiannaki | in order to return back here after agreeing with the terms.
|
59 | ebd369d0 | Sofia Papagiannaki | Otherwise, redirects to login in order to return back here after successful login.
|
60 | a196eb7e | Sofia Papagiannaki | """
|
61 | ebd369d0 | Sofia Papagiannaki | next = request.GET.get('next')
|
62 | ebd369d0 | Sofia Papagiannaki | if not next: |
63 | ebd369d0 | Sofia Papagiannaki | return HttpResponseBadRequest(_('No next parameter')) |
64 | ebd369d0 | Sofia Papagiannaki | force = request.GET.get('force', None) |
65 | ebd369d0 | Sofia Papagiannaki | response = HttpResponse() |
66 | ebd369d0 | Sofia Papagiannaki | if force == '': |
67 | ebd369d0 | Sofia Papagiannaki | logout(request) |
68 | ebd369d0 | Sofia Papagiannaki | response.delete_cookie(COOKIE_NAME, path='/', domain=COOKIE_DOMAIN)
|
69 | 148b0cde | Sofia Papagiannaki | if request.user.is_authenticated():
|
70 | e510fae4 | Sofia Papagiannaki | # if user has not signed the approval terms
|
71 | e510fae4 | Sofia Papagiannaki | # redirect to approval terms with next the request path
|
72 | 09e7393c | Sofia Papagiannaki | if not request.user.signed_terms(): |
73 | e510fae4 | Sofia Papagiannaki | # first build next parameter
|
74 | e510fae4 | Sofia Papagiannaki | parts = list(urlsplit(request.build_absolute_uri()))
|
75 | e510fae4 | Sofia Papagiannaki | params = dict(parse_qsl(parts[3], keep_blank_values=True)) |
76 | e510fae4 | Sofia Papagiannaki | # delete force parameter
|
77 | e510fae4 | Sofia Papagiannaki | parts[3] = urlencode(params)
|
78 | e510fae4 | Sofia Papagiannaki | next = urlunsplit(parts) |
79 | e510fae4 | Sofia Papagiannaki | |
80 | e510fae4 | Sofia Papagiannaki | # build url location
|
81 | e510fae4 | Sofia Papagiannaki | parts[2] = reverse('latest_terms') |
82 | e510fae4 | Sofia Papagiannaki | params = {'next':next} |
83 | e510fae4 | Sofia Papagiannaki | parts[3] = urlencode(params)
|
84 | e510fae4 | Sofia Papagiannaki | url = urlunsplit(parts) |
85 | e510fae4 | Sofia Papagiannaki | response['Location'] = url
|
86 | e510fae4 | Sofia Papagiannaki | response.status_code = 302
|
87 | e510fae4 | Sofia Papagiannaki | return response
|
88 | 860b37f0 | Sofia Papagiannaki | renew = request.GET.get('renew', None) |
89 | ebd369d0 | Sofia Papagiannaki | if renew == '': |
90 | ebd369d0 | Sofia Papagiannaki | request.user.renew_token() |
91 | ebd369d0 | Sofia Papagiannaki | request.user.save() |
92 | ebd369d0 | Sofia Papagiannaki | |
93 | ebd369d0 | Sofia Papagiannaki | # authenticate before login
|
94 | ebd369d0 | Sofia Papagiannaki | user = authenticate(email=request.user.email, auth_token=request.user.auth_token) |
95 | ebd369d0 | Sofia Papagiannaki | auth_login(request, user) |
96 | ebd369d0 | Sofia Papagiannaki | set_cookie(response, user) |
97 | ebd369d0 | Sofia Papagiannaki | logger.info('Token reset for %s' % request.user.email)
|
98 | ebd369d0 | Sofia Papagiannaki | parts = list(urlsplit(next)) |
99 | ebd369d0 | Sofia Papagiannaki | parts[3] = urlencode({'user': request.user.email, 'token': request.user.auth_token}) |
100 | ebd369d0 | Sofia Papagiannaki | url = urlunsplit(parts) |
101 | ebd369d0 | Sofia Papagiannaki | response['Location'] = url
|
102 | ebd369d0 | Sofia Papagiannaki | response.status_code = 302
|
103 | ebd369d0 | Sofia Papagiannaki | return response
|
104 | 148b0cde | Sofia Papagiannaki | else:
|
105 | e510fae4 | Sofia Papagiannaki | # redirect to login with next the request path
|
106 | ebd369d0 | Sofia Papagiannaki | |
107 | ebd369d0 | Sofia Papagiannaki | # first build next parameter
|
108 | ebd369d0 | Sofia Papagiannaki | parts = list(urlsplit(request.build_absolute_uri()))
|
109 | ebd369d0 | Sofia Papagiannaki | params = dict(parse_qsl(parts[3], keep_blank_values=True)) |
110 | ebd369d0 | Sofia Papagiannaki | # delete force parameter
|
111 | ebd369d0 | Sofia Papagiannaki | if 'force' in params: |
112 | ebd369d0 | Sofia Papagiannaki | del params['force'] |
113 | ebd369d0 | Sofia Papagiannaki | parts[3] = urlencode(params)
|
114 | ebd369d0 | Sofia Papagiannaki | next = urlunsplit(parts) |
115 | ebd369d0 | Sofia Papagiannaki | |
116 | ebd369d0 | Sofia Papagiannaki | # build url location
|
117 | ebd369d0 | Sofia Papagiannaki | parts[2] = reverse('astakos.im.views.index') |
118 | ebd369d0 | Sofia Papagiannaki | params = {'next':next} |
119 | ebd369d0 | Sofia Papagiannaki | parts[3] = urlencode(params)
|
120 | ebd369d0 | Sofia Papagiannaki | url = urlunsplit(parts) |
121 | ebd369d0 | Sofia Papagiannaki | response['Location'] = url
|
122 | ebd369d0 | Sofia Papagiannaki | response.status_code = 302
|
123 | ebd369d0 | Sofia Papagiannaki | return response |