Skip to content

Commit

Permalink
[#1025] User log in/out fixes including i18n and non standard root url
Browse files Browse the repository at this point in the history
Conflicts:

	ckan/controllers/user.py
	ckan/lib/repoze_patch.py
  • Loading branch information
tobes authored and amercader committed Nov 5, 2013
1 parent 0979bcd commit 9067d8d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 36 deletions.
49 changes: 18 additions & 31 deletions ckan/controllers/user.py
Expand Up @@ -287,11 +287,7 @@ def _save_edit(self, id, context):
return self.edit(id, data_dict, errors, error_summary)

def login(self, error=None):
lang = session.pop('lang', None)
if lang:
session.save()
return h.redirect_to(locale=str(lang), controller='user',
action='login')

if 'error' in request.params:
h.flash_error(request.params['error'])

Expand All @@ -301,7 +297,10 @@ def login(self, error=None):
g.openid_enabled = False

if not c.user:
came_from = request.params.get('came_from', '')
came_from = request.params.get('came_from')
if not came_from:
came_from = h.url_for(controller='user', action='logged_in',
__ckan_no_root=True)
c.login_handler = h.url_for(
self._get_repoze_handler('login_handler_path'),
came_from=came_from)
Expand All @@ -314,14 +313,10 @@ def login(self, error=None):
return render('user/logout_first.html')

def logged_in(self):
# we need to set the language via a redirect
lang = session.pop('lang', None)
session.save()
# redirect if needed
came_from = request.params.get('came_from', '')

# we need to set the language explicitly here or the flash
# messages will not be translated.
i18n.set_lang(lang)
if came_from:
return h.redirect_to(str(came_from))

if c.user:
context = None
Expand All @@ -331,8 +326,6 @@ def logged_in(self):

h.flash_success(_("%s is now logged in") %
user_dict['display_name'])
if came_from:
return h.redirect_to(str(came_from))
return self.me()
else:
err = _('Login failed. Bad username or password.')
Expand All @@ -341,29 +334,23 @@ def logged_in(self):
'with a user account.)')
if h.asbool(config.get('ckan.legacy_templates', 'false')):
h.flash_error(err)
h.redirect_to(locale=lang, controller='user',
h.redirect_to(controller='user',
action='login', came_from=came_from)
else:
return self.login(error=err)

def logout(self):
# save our language in the session so we don't lose it
session['lang'] = request.environ.get('CKAN_LANG')
session.save()
h.redirect_to(self._get_repoze_handler('logout_handler_path'))

def set_lang(self, lang):
# this allows us to set the lang in session. Used for logging
# in/out to prevent being lost when repoze.who redirects things
session['lang'] = str(lang)
session.save()
url = h.url_for(controller='user', action='logged_out_page',
__ckan_no_root=True)
h.redirect_to(self._get_repoze_handler('logout_handler_path') +
'?came_from=' + url)

def logged_out(self):
# we need to get our language info back and the show the correct page
lang = session.get('lang')
c.user = None
session.delete()
h.redirect_to(locale=lang, controller='user', action='logged_out_page')
# redirect if needed
came_from = request.params.get('came_from', '')
if came_from:
return h.redirect_to(str(came_from))
h.redirect_to(controller='user', action='logged_out_page')

def logged_out_page(self):
return render('user/logout.html')
Expand Down
8 changes: 3 additions & 5 deletions ckan/lib/repoze_patch.py
Expand Up @@ -2,12 +2,10 @@
from openid.consumer import consumer
from openid.extensions import sreg, ax

import lib.helpers as h

# #1659 fix - logged_out_url prefixed with mount point
def get_full_path(path, environ):
if path.startswith('/'):
path = h._add_i18n_to_url(path)
path = environ.get('SCRIPT_NAME', '') + path
return path

def identify(self, environ):
Expand All @@ -31,8 +29,8 @@ def identify(self, environ):
for a,v in self.forget(environ,{}):
res.headers.add(a,v)
res.status = 302

res.location = get_full_path(self.logged_out_url, environ)
url = self.logged_out_url + '?came_from=' + environ.get('came_from')
res.location = get_full_path(url, environ)

environ['repoze.who.application'] = res
return {}
Expand Down

0 comments on commit 9067d8d

Please sign in to comment.