misc: add context-processor user_urls (#61192)
It introduces three template's variables: - login_url - logout_url - registration_url - account_url Each one is adapted to return to the current page.
This commit is contained in:
parent
c88ab84425
commit
13d74f4169
|
@ -241,6 +241,7 @@ if 'TEMPLATE_DEBUG' in globals():
|
|||
'hobo.context_processors.template_vars',
|
||||
'hobo.context_processors.theme_base',
|
||||
'hobo.context_processors.portal_agent_url',
|
||||
'hobo.context_processors.user_urls',
|
||||
) + TEMPLATE_CONTEXT_PROCESSORS
|
||||
else:
|
||||
assert len(TEMPLATES)
|
||||
|
@ -262,6 +263,7 @@ else:
|
|||
'hobo.context_processors.template_vars',
|
||||
'hobo.context_processors.theme_base',
|
||||
'hobo.context_processors.portal_agent_url',
|
||||
'hobo.context_processors.user_urls',
|
||||
] + TEMPLATES[0]['OPTIONS']['context_processors']
|
||||
|
||||
# needed by hobo.context_processors.theme_base:
|
||||
|
|
|
@ -10,6 +10,7 @@ from django.conf import settings
|
|||
from django.core.cache import cache
|
||||
from django.template import Template
|
||||
from django.utils.encoding import smart_bytes
|
||||
from django.utils.http import urlencode
|
||||
from django.utils.six.moves.urllib import parse as urlparse
|
||||
|
||||
from hobo.scrutiny.wsgi.middleware import VersionMiddleware
|
||||
|
@ -187,3 +188,21 @@ def hobo_json(request):
|
|||
context = TemplateVars.get_hobo_json_variables(get_hobo_json())
|
||||
context['manager_homepage_url'] = context.get(settings.HOBO_MANAGER_HOMEPAGE_URL_VAR)
|
||||
return context
|
||||
|
||||
|
||||
def user_urls(request):
|
||||
template_vars = getattr(settings, 'TEMPLATE_VARS', {})
|
||||
full_path = request.get_full_path()
|
||||
query_string = urlencode({'next': full_path})
|
||||
context = {
|
||||
'login_url': '/login/?' + query_string,
|
||||
'logout_url': '/logout/?' + query_string,
|
||||
}
|
||||
absolute_uri = request.build_absolute_uri()
|
||||
if 'idp_account_url' in template_vars:
|
||||
context['account_url'] = template_vars['idp_account_url'] + '?' + urlencode({'next': absolute_uri})
|
||||
if 'idp_registration_url' in template_vars:
|
||||
context['registration_url'] = (
|
||||
template_vars['idp_registration_url'] + '?' + urlencode({'next': absolute_uri})
|
||||
)
|
||||
return context
|
||||
|
|
|
@ -165,28 +165,6 @@ class TemplateVars(FileBaseSettingsLoader):
|
|||
if 'portal-agent' in service.get('template_name', ''):
|
||||
variables['is_portal_agent'] = True
|
||||
|
||||
if 'idp_registration_url' in variables:
|
||||
params = {}
|
||||
if 'portal_user_url' in variables:
|
||||
params['next'] = variables['portal_user_url']
|
||||
if 'portal_user_slug' in variables:
|
||||
# if we are in a secondary hobo, adapt the ou slug and
|
||||
# portal_user_slug to match the service slug in the Authentic
|
||||
# of the primary hobo
|
||||
if authentic_service.get('secondary'):
|
||||
ou_slug = variables['ou-slug']
|
||||
service_slug = '_%s_%s' % (ou_slug, variables['portal_user_slug'])
|
||||
params['service'] = '%s %s' % (ou_slug, service_slug)
|
||||
else:
|
||||
# we should provider the default slug to have a full
|
||||
# service reference, but it could change so for now we
|
||||
# expect authentic to search first in the default ou
|
||||
params['service'] = variables['portal_user_slug']
|
||||
if params:
|
||||
variables['idp_registration_url'] += '?%s' % urlencode(params)
|
||||
variables['idp_service_params'] = urlencode({k: v for k, v in params.items() if k != 'next'})
|
||||
variables['idp_service_and_next_params'] = urlencode(params)
|
||||
|
||||
if getattr(settings, 'HOBO_MANAGER_HOMEPAGE_TITLE_VAR', None):
|
||||
variables['manager_homepage_title'] = variables.get(settings.HOBO_MANAGER_HOMEPAGE_TITLE_VAR)
|
||||
return variables
|
||||
|
|
|
@ -6,7 +6,7 @@ from django.core.cache import cache
|
|||
from django.test import override_settings
|
||||
from httmock import HTTMock, urlmatch
|
||||
|
||||
from hobo.context_processors import theme_base
|
||||
from hobo.context_processors import theme_base, user_urls
|
||||
|
||||
|
||||
def test_theme_base(settings, rf):
|
||||
|
@ -66,3 +66,18 @@ def test_theme_base(settings, rf):
|
|||
context = theme_base(rf.get('/page1/page2/'))
|
||||
check(context, TEMPLATE_PAGE2)
|
||||
assert len(seen_urls) == 0
|
||||
|
||||
|
||||
def test_user_urls(settings, rf):
|
||||
settings.TEMPLATE_VARS = {
|
||||
'idp_registration_url': 'https://idp/register/',
|
||||
'idp_account_url': 'https://idp/accounts/',
|
||||
}
|
||||
|
||||
request = rf.get('/page/')
|
||||
assert user_urls(request) == {
|
||||
'login_url': '/login/?next=%2Fpage%2F',
|
||||
'logout_url': '/logout/?next=%2Fpage%2F',
|
||||
'registration_url': 'https://idp/register/?next=http%3A%2F%2Ftestserver%2Fpage%2F',
|
||||
'account_url': 'https://idp/accounts/?next=http%3A%2F%2Ftestserver%2Fpage%2F',
|
||||
}
|
||||
|
|
|
@ -173,45 +173,3 @@ def test_sms_update_settings_from_path(tmpdir):
|
|||
loader.update_settings_from_path(tenant_settings, path)
|
||||
assert tenant_settings.SMS_URL == 'https://example.com/send/'
|
||||
assert tenant_settings.SMS_SENDER == 'Sender'
|
||||
|
||||
|
||||
def test_get_hobo_json_variables(tmpdir):
|
||||
from django.http.request import QueryDict
|
||||
|
||||
a = Authentic(title='bar', slug='bar', base_url='http://bar.example.net')
|
||||
a.save()
|
||||
c = Combo(title='combo', slug='portal', base_url='http://portal.example.net', template_name='portal-user')
|
||||
c.save()
|
||||
|
||||
loader = TemplateVars()
|
||||
env = get_hobo_json()
|
||||
|
||||
variables = loader.get_hobo_json_variables(env)
|
||||
|
||||
url, query = variables['idp_registration_url'].split('?')
|
||||
assert url == 'http://bar.example.net/accounts/register/'
|
||||
assert QueryDict(query).dict() == {'next': 'http://portal.example.net/', 'service': 'portal'}
|
||||
|
||||
|
||||
def test_get_hobo_json_variables_secondary(tmpdir):
|
||||
from django.http.request import QueryDict
|
||||
|
||||
a = Authentic(title='bar', slug='bar', base_url='http://bar.example.net', secondary=True)
|
||||
a.save()
|
||||
c = Combo(title='combo', slug='portal', base_url='http://portal.example.net', template_name='portal-user')
|
||||
c.save()
|
||||
# simulate hobo.json from a secondary hobo
|
||||
v = Variable(name='ou-slug', auto=True, service_pk=None, value='hobo-othercol')
|
||||
v.save()
|
||||
|
||||
loader = TemplateVars()
|
||||
env = get_hobo_json()
|
||||
|
||||
variables = loader.get_hobo_json_variables(env)
|
||||
|
||||
url, query = variables['idp_registration_url'].split('?')
|
||||
assert url == 'http://bar.example.net/accounts/register/'
|
||||
assert QueryDict(query).dict() == {
|
||||
'next': 'http://portal.example.net/',
|
||||
'service': 'hobo-othercol _hobo-othercol_portal',
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue