From dd24302f2d92b41fb3c64ac9988b9412ea081917 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Fri, 11 Jan 2019 11:22:14 +0100
Subject: [PATCH] misc: look for portal agent associated to user OU when in
 Authentic (#29658)

---
 debian/debian_config_common.py                |  2 ++
 .../hobo/management/commands/hobo_deploy.py   |  1 +
 hobo/context_processors.py                    | 19 +++++++++++++++++++
 hobo/multitenant/settings_loaders.py          |  6 ++----
 4 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/debian/debian_config_common.py b/debian/debian_config_common.py
index 68f86e3..a09508d 100644
--- a/debian/debian_config_common.py
+++ b/debian/debian_config_common.py
@@ -207,6 +207,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)
@@ -228,6 +229,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:
diff --git a/hobo/agent/hobo/management/commands/hobo_deploy.py b/hobo/agent/hobo/management/commands/hobo_deploy.py
index ceddae2..1b39bb0 100644
--- a/hobo/agent/hobo/management/commands/hobo_deploy.py
+++ b/hobo/agent/hobo/management/commands/hobo_deploy.py
@@ -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()
diff --git a/hobo/context_processors.py b/hobo/context_processors.py
index 4b4c60b..c070b83 100644
--- a/hobo/context_processors.py
+++ b/hobo/context_processors.py
@@ -143,6 +143,25 @@ 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):
     from hobo.multitenant.settings_loaders import TemplateVars
     from hobo.deploy.utils import get_hobo_json
diff --git a/hobo/multitenant/settings_loaders.py b/hobo/multitenant/settings_loaders.py
index fc21960..da3fa29 100644
--- a/hobo/multitenant/settings_loaders.py
+++ b/hobo/multitenant/settings_loaders.py
@@ -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)
-- 
2.20.1

