misc: look for portal agent associated to user OU when in Authentic (#29658)

This commit is contained in:
Frédéric Péters 2019-01-11 11:22:14 +01:00
parent 6b6f4dc4b3
commit 7aa49fcf12
6 changed files with 34 additions and 7 deletions

View File

@ -205,6 +205,7 @@ if 'TEMPLATE_DEBUG' in globals():
TEMPLATE_CONTEXT_PROCESSORS = (
'hobo.context_processors.template_vars',
'hobo.context_processors.theme_base',
'hobo.context_processors.portal_agent_url',
) + TEMPLATE_CONTEXT_PROCESSORS
else:
assert len(TEMPLATES)
@ -226,6 +227,7 @@ else:
TEMPLATES[0]['OPTIONS']['context_processors'] = [
'hobo.context_processors.template_vars',
'hobo.context_processors.theme_base',
'hobo.context_processors.portal_agent_url',
] + TEMPLATES[0]['OPTIONS']['context_processors']
# needed by hobo.context_processors.theme_base:

View File

@ -61,6 +61,7 @@ class Command(hobo_deploy.Command):
'secret_key': service_dict.get('secret_key')})
service.title = service_dict['title']
service.secret_key = service_dict.get('secret_key')
service.template_name = service_dict.get('template_name') or ''
service.slug = service_slug
service.use_as_idp_for_self = True
service.last_operational_check_timestamp = now()

View File

@ -147,8 +147,30 @@ def theme_base(request):
'theme_base_filename': template_vars(request).get('theme_base_filename') or 'theme.html'
}
def portal_agent_url(request):
def get_portal_agent_url():
portal_agents = []
if 'authentic2' in settings.INSTALLED_APPS:
portal_agents = [x for x in settings.KNOWN_SERVICES.get('combo', {}).values()
if x.get('is-portal-agent')]
if len(portal_agents) > 1 and request.user and request.user.is_authenticated() and request.user.ou_id:
ou_slug = request.user.ou.slug
for portal_agent in portal_agents:
variables = portal_agent.get('variables') or {}
if variables.get('ou-slug') == ou_slug:
return portal_agent['url']
variables = getattr(settings, 'TEMPLATE_VARS', {})
return variables.get(settings.HOBO_MANAGER_HOMEPAGE_URL_VAR)
return {'manager_homepage_url': get_portal_agent_url}
def hobo_json(request):
# this context processor gives Hobo itself variables that would be defined
# from settings loaders based on hobo.json.
from hobo.multitenant.settings_loaders import TemplateVars
from hobo.deploy.utils import get_hobo_json
# XXX: needs some cache ?
return TemplateVars.get_hobo_json_variables(get_hobo_json())
context = TemplateVars.get_hobo_json_variables(get_hobo_json())
context['manager_homepage_url'] = context.get(settings.HOBO_MANAGER_HOMEPAGE_URL_VAR)
return context

View File

@ -89,6 +89,7 @@ class KnownServices(FileBaseSettingsLoader):
'verif_orig': verif_orig,
'variables': service.get('variables'),
'secondary': service.get('secondary'),
'template_name': service.get('template_name'),
}
if service.get('secondary') and (service.get('variables') and
@ -129,7 +130,7 @@ class TemplateVars(FileBaseSettingsLoader):
continue
variables['%s_url' % service.get('slug').replace('-','_')] = service.get('base_url')
if service.get('service-id') == 'combo':
if service.get('service-id') == 'combo' and not service.get('secondary'):
if service.get('template_name') == 'portal-agent':
variables['portal_agent_url'] = service.get('base_url')
variables['portal_agent_title'] = service.get('title')
@ -149,9 +150,6 @@ class TemplateVars(FileBaseSettingsLoader):
variables.update(service.get('variables') or {})
variables['site_title'] = service.get('title')
if getattr(settings, 'HOBO_MANAGER_HOMEPAGE_URL_VAR', None):
variables['manager_homepage_url'] = \
variables.get(settings.HOBO_MANAGER_HOMEPAGE_URL_VAR)
if getattr(settings, 'HOBO_MANAGER_HOMEPAGE_TITLE_VAR', None):
variables['manager_homepage_title'] = \
variables.get(settings.HOBO_MANAGER_HOMEPAGE_TITLE_VAR)
@ -257,6 +255,8 @@ class SharedThemeSettings(FileBaseSettingsLoader):
continue
if service.get('template_name') != 'portal-user':
continue
if service.get('secondary'):
continue
tenant_settings.THEME_SKELETON_URL = '%s__skeleton__/' % (
service.get('base_url'))
break

View File

@ -8,3 +8,5 @@ INSTALLED_APPS += ('kombu.transport.django', 'hobo.contrib.ozwillo',)
ALLOWED_HOSTS.append('localhost')
TEMPLATES[0]['OPTIONS']['debug'] = True
HOBO_MANAGER_HOMEPAGE_URL_VAR = 'portal_agent_url'

View File

@ -227,7 +227,7 @@ def test_known_services(tenants, settings):
assert hasattr(settings, 'KNOWN_SERVICES')
assert 'authentic' in settings.KNOWN_SERVICES
assert 'other' in settings.KNOWN_SERVICES['authentic']
assert (set(['url', 'backoffice-menu-url', 'title', 'orig', 'verif_orig', 'secret', 'variables', 'secondary'])
assert (set(['url', 'backoffice-menu-url', 'title', 'orig', 'verif_orig', 'secret', 'template_name', 'variables', 'secondary'])
== set(settings.KNOWN_SERVICES['authentic']['other'].keys()))
assert (settings.KNOWN_SERVICES['authentic']['other']['url']
== hobo_json['services'][2]['base_url'])