Show More
@@ -1,204 +1,207 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | rhodecode.controllers.login |
|
4 | 4 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
5 | 5 | |
|
6 | 6 | Login controller for rhodeocode |
|
7 | 7 | |
|
8 | 8 | :created_on: Apr 22, 2010 |
|
9 | 9 | :author: marcink |
|
10 | 10 | :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com> |
|
11 | 11 | :license: GPLv3, see COPYING for more details. |
|
12 | 12 | """ |
|
13 | 13 | # This program is free software: you can redistribute it and/or modify |
|
14 | 14 | # it under the terms of the GNU General Public License as published by |
|
15 | 15 | # the Free Software Foundation, either version 3 of the License, or |
|
16 | 16 | # (at your option) any later version. |
|
17 | 17 | # |
|
18 | 18 | # This program is distributed in the hope that it will be useful, |
|
19 | 19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20 | 20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21 | 21 | # GNU General Public License for more details. |
|
22 | 22 | # |
|
23 | 23 | # You should have received a copy of the GNU General Public License |
|
24 | 24 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
25 | 25 | |
|
26 | 26 | import logging |
|
27 | 27 | import formencode |
|
28 | 28 | import datetime |
|
29 | 29 | import urlparse |
|
30 | 30 | |
|
31 | 31 | from formencode import htmlfill |
|
32 | 32 | from webob.exc import HTTPFound |
|
33 | 33 | from pylons.i18n.translation import _ |
|
34 | 34 | from pylons.controllers.util import abort, redirect |
|
35 | 35 | from pylons import request, response, session, tmpl_context as c, url |
|
36 | 36 | |
|
37 | 37 | import rhodecode.lib.helpers as h |
|
38 | 38 | from rhodecode.lib.auth import AuthUser, HasPermissionAnyDecorator |
|
39 | 39 | from rhodecode.lib.base import BaseController, render |
|
40 | 40 | from rhodecode.lib.exceptions import UserCreationError |
|
41 | 41 | from rhodecode.model.db import User |
|
42 | 42 | from rhodecode.model.forms import LoginForm, RegisterForm, PasswordResetForm |
|
43 | 43 | from rhodecode.model.user import UserModel |
|
44 | 44 | from rhodecode.model.meta import Session |
|
45 | 45 | |
|
46 | 46 | |
|
47 | 47 | log = logging.getLogger(__name__) |
|
48 | 48 | |
|
49 | 49 | |
|
50 | 50 | class LoginController(BaseController): |
|
51 | 51 | |
|
52 | 52 | def __before__(self): |
|
53 | 53 | super(LoginController, self).__before__() |
|
54 | 54 | |
|
55 | 55 | def index(self): |
|
56 | 56 | # redirect if already logged in |
|
57 | 57 | c.came_from = request.GET.get('came_from') |
|
58 | 58 | not_default = self.rhodecode_user.username != 'default' |
|
59 | 59 | ip_allowed = self.rhodecode_user.ip_allowed |
|
60 | 60 | if self.rhodecode_user.is_authenticated and not_default and ip_allowed: |
|
61 | 61 | return redirect(url('home')) |
|
62 | 62 | |
|
63 | 63 | if request.POST: |
|
64 | 64 | # import Login Form validator class |
|
65 | 65 | login_form = LoginForm() |
|
66 | 66 | try: |
|
67 | 67 | session.invalidate() |
|
68 | 68 | c.form_result = login_form.to_python(dict(request.POST)) |
|
69 | 69 | # form checks for username/password, now we're authenticated |
|
70 | 70 | username = c.form_result['username'] |
|
71 | 71 | user = User.get_by_username(username, case_insensitive=True) |
|
72 | 72 | auth_user = AuthUser(user.user_id) |
|
73 | 73 | auth_user.set_authenticated() |
|
74 | 74 | cs = auth_user.get_cookie_store() |
|
75 | 75 | session['rhodecode_user'] = cs |
|
76 | 76 | user.update_lastlogin() |
|
77 | 77 | Session().commit() |
|
78 | 78 | |
|
79 | 79 | # If they want to be remembered, update the cookie |
|
80 | 80 | if c.form_result['remember']: |
|
81 | 81 | _year = (datetime.datetime.now() + |
|
82 | 82 | datetime.timedelta(seconds=60 * 60 * 24 * 365)) |
|
83 | 83 | session._set_cookie_expires(_year) |
|
84 | 84 | |
|
85 | 85 | session.save() |
|
86 | 86 | |
|
87 | 87 | log.info('user %s is now authenticated and stored in ' |
|
88 | 88 | 'session, session attrs %s' % (username, cs)) |
|
89 | 89 | |
|
90 | 90 | # dumps session attrs back to cookie |
|
91 | 91 | session._update_cookie_out() |
|
92 | 92 | |
|
93 | 93 | # we set new cookie |
|
94 | 94 | headers = None |
|
95 | 95 | if session.request['set_cookie']: |
|
96 | 96 | # send set-cookie headers back to response to update cookie |
|
97 | 97 | headers = [('Set-Cookie', session.request['cookie_out'])] |
|
98 | 98 | |
|
99 | 99 | allowed_schemes = ['http', 'https'] |
|
100 | 100 | if c.came_from: |
|
101 | 101 | parsed = urlparse.urlparse(c.came_from) |
|
102 | 102 | server_parsed = urlparse.urlparse(url.current()) |
|
103 | 103 | if parsed.scheme and parsed.scheme not in allowed_schemes: |
|
104 | 104 | log.error( |
|
105 | 105 | 'Suspicious URL scheme detected %s for url %s' % |
|
106 | 106 | (parsed.scheme, parsed)) |
|
107 | 107 | c.came_from = url('home') |
|
108 | 108 | elif server_parsed.netloc != parsed.netloc: |
|
109 | 109 | log.error('Suspicious NETLOC detected %s for url %s' |
|
110 | 110 | 'server url is: %s' % |
|
111 | 111 | (parsed.netloc, parsed, server_parsed)) |
|
112 | 112 | c.came_from = url('home') |
|
113 | 113 | raise HTTPFound(location=c.came_from, headers=headers) |
|
114 | 114 | else: |
|
115 | 115 | raise HTTPFound(location=url('home'), headers=headers) |
|
116 | 116 | |
|
117 | 117 | except formencode.Invalid, errors: |
|
118 | defaults = errors.value | |
|
119 | # remove password from filling in form again | |
|
120 | del defaults['password'] | |
|
118 | 121 | return htmlfill.render( |
|
119 | 122 | render('/login.html'), |
|
120 | 123 | defaults=errors.value, |
|
121 | 124 | errors=errors.error_dict or {}, |
|
122 | 125 | prefix_error=False, |
|
123 | 126 | encoding="UTF-8") |
|
124 | 127 | except UserCreationError, e: |
|
125 | 128 | # container auth or other auth functions that create users on |
|
126 | 129 | # the fly can throw this exception signaling that there's issue |
|
127 | 130 | # with user creation, explanation should be provided in |
|
128 | 131 | # Exception itself |
|
129 | 132 | h.flash(e, 'error') |
|
130 | 133 | |
|
131 | 134 | return render('/login.html') |
|
132 | 135 | |
|
133 | 136 | @HasPermissionAnyDecorator('hg.admin', 'hg.register.auto_activate', |
|
134 | 137 | 'hg.register.manual_activate') |
|
135 | 138 | def register(self): |
|
136 | 139 | c.auto_active = 'hg.register.auto_activate' in User.get_default_user()\ |
|
137 | 140 | .AuthUser.permissions['global'] |
|
138 | 141 | |
|
139 | 142 | if request.POST: |
|
140 | 143 | register_form = RegisterForm()() |
|
141 | 144 | try: |
|
142 | 145 | form_result = register_form.to_python(dict(request.POST)) |
|
143 | 146 | form_result['active'] = c.auto_active |
|
144 | 147 | UserModel().create_registration(form_result) |
|
145 | 148 | h.flash(_('You have successfully registered into RhodeCode'), |
|
146 | 149 | category='success') |
|
147 | 150 | Session().commit() |
|
148 | 151 | return redirect(url('login_home')) |
|
149 | 152 | |
|
150 | 153 | except formencode.Invalid, errors: |
|
151 | 154 | return htmlfill.render( |
|
152 | 155 | render('/register.html'), |
|
153 | 156 | defaults=errors.value, |
|
154 | 157 | errors=errors.error_dict or {}, |
|
155 | 158 | prefix_error=False, |
|
156 | 159 | encoding="UTF-8") |
|
157 | 160 | except UserCreationError, e: |
|
158 | 161 | # container auth or other auth functions that create users on |
|
159 | 162 | # the fly can throw this exception signaling that there's issue |
|
160 | 163 | # with user creation, explanation should be provided in |
|
161 | 164 | # Exception itself |
|
162 | 165 | h.flash(e, 'error') |
|
163 | 166 | |
|
164 | 167 | return render('/register.html') |
|
165 | 168 | |
|
166 | 169 | def password_reset(self): |
|
167 | 170 | if request.POST: |
|
168 | 171 | password_reset_form = PasswordResetForm()() |
|
169 | 172 | try: |
|
170 | 173 | form_result = password_reset_form.to_python(dict(request.POST)) |
|
171 | 174 | UserModel().reset_password_link(form_result) |
|
172 | 175 | h.flash(_('Your password reset link was sent'), |
|
173 | 176 | category='success') |
|
174 | 177 | return redirect(url('login_home')) |
|
175 | 178 | |
|
176 | 179 | except formencode.Invalid, errors: |
|
177 | 180 | return htmlfill.render( |
|
178 | 181 | render('/password_reset.html'), |
|
179 | 182 | defaults=errors.value, |
|
180 | 183 | errors=errors.error_dict or {}, |
|
181 | 184 | prefix_error=False, |
|
182 | 185 | encoding="UTF-8") |
|
183 | 186 | |
|
184 | 187 | return render('/password_reset.html') |
|
185 | 188 | |
|
186 | 189 | def password_reset_confirmation(self): |
|
187 | 190 | if request.GET and request.GET.get('key'): |
|
188 | 191 | try: |
|
189 | 192 | user = User.get_by_api_key(request.GET.get('key')) |
|
190 | 193 | data = dict(email=user.email) |
|
191 | 194 | UserModel().reset_password(data) |
|
192 | 195 | h.flash(_('Your password reset was successful, ' |
|
193 | 196 | 'new password has been sent to your email'), |
|
194 | 197 | category='success') |
|
195 | 198 | except Exception, e: |
|
196 | 199 | log.error(e) |
|
197 | 200 | return redirect(url('reset_password')) |
|
198 | 201 | |
|
199 | 202 | return redirect(url('login_home')) |
|
200 | 203 | |
|
201 | 204 | def logout(self): |
|
202 | 205 | session.delete() |
|
203 | 206 | log.info('Logging out and deleting session for user') |
|
204 | 207 | redirect(url('home')) |
General Comments 0
You need to be logged in to leave comments.
Login now