##// END OF EJS Templates
login: Move method to store user in session out of login controller....
johbo -
r28:48f65d03 default
parent child Browse files
Show More
@@ -52,40 +52,41 b' from rhodecode.model.user import UserMod'
52 52 log = logging.getLogger(__name__)
53 53
54 54
55 def _store_user_in_session(username, remember=False):
56 user = User.get_by_username(username, case_insensitive=True)
57 auth_user = AuthUser(user.user_id)
58 auth_user.set_authenticated()
59 cs = auth_user.get_cookie_store()
60 session['rhodecode_user'] = cs
61 user.update_lastlogin()
62 Session().commit()
63
64 # If they want to be remembered, update the cookie
65 if remember:
66 _year = (datetime.datetime.now() +
67 datetime.timedelta(seconds=60 * 60 * 24 * 365))
68 session._set_cookie_expires(_year)
69
70 session.save()
71
72 log.info('user %s is now authenticated and stored in '
73 'session, session attrs %s', username, cs)
74
75 # dumps session attrs back to cookie
76 session._update_cookie_out()
77 # we set new cookie
78 headers = None
79 if session.request['set_cookie']:
80 # send set-cookie headers back to response to update cookie
81 headers = [('Set-Cookie', session.request['cookie_out'])]
82 return headers
83
84
55 85 class LoginController(BaseController):
56 86
57 87 def __before__(self):
58 88 super(LoginController, self).__before__()
59 89
60 def _store_user_in_session(self, username, remember=False):
61 user = User.get_by_username(username, case_insensitive=True)
62 auth_user = AuthUser(user.user_id)
63 auth_user.set_authenticated()
64 cs = auth_user.get_cookie_store()
65 session['rhodecode_user'] = cs
66 user.update_lastlogin()
67 Session().commit()
68
69 # If they want to be remembered, update the cookie
70 if remember:
71 _year = (datetime.datetime.now() +
72 datetime.timedelta(seconds=60 * 60 * 24 * 365))
73 session._set_cookie_expires(_year)
74
75 session.save()
76
77 log.info('user %s is now authenticated and stored in '
78 'session, session attrs %s', username, cs)
79
80 # dumps session attrs back to cookie
81 session._update_cookie_out()
82 # we set new cookie
83 headers = None
84 if session.request['set_cookie']:
85 # send set-cookie headers back to response to update cookie
86 headers = [('Set-Cookie', session.request['cookie_out'])]
87 return headers
88
89 90 def _validate_came_from(self, came_from):
90 91 if not came_from:
91 92 return came_from
@@ -134,7 +135,7 b' class LoginController(BaseController):'
134 135 session.invalidate()
135 136 c.form_result = login_form.to_python(dict(request.POST))
136 137 # form checks for username/password, now we're authenticated
137 headers = self._store_user_in_session(
138 headers = _store_user_in_session(
138 139 username=c.form_result['username'],
139 140 remember=c.form_result['remember'])
140 141 raise self._redirect_to_origin(
@@ -170,7 +171,7 b' class LoginController(BaseController):'
170 171 return render('/login.html')
171 172
172 173 if auth_info:
173 headers = self._store_user_in_session(auth_info.get('username'))
174 headers = _store_user_in_session(auth_info.get('username'))
174 175 raise self._redirect_to_origin(
175 176 location=c.came_from, headers=headers)
176 177 return render('/login.html')
General Comments 0
You need to be logged in to leave comments. Login now