From 26104d2589633229a2fcc420fb3e24a8ab1b45e3 Mon Sep 17 00:00:00 2001
From: Thijs Kinkhorst <thijs@kinkhorst.com>
Date: Sun, 31 Mar 2019 06:43:54 +0000
Subject: [PATCH] Fix preferredidp in old UI disco 'links' style.

The calling code of the template had been changed to accommodate
new twig templates, but slightly changed the content of the
template variables so preferredidp never matched the list of known
idp's. This fixes it in the least invasive way. Could be
refactored because we now loop the idplist twice, but since this
is legacy code that's likely not worth it.

Thanks Stefan Winter from RESTENA for reporting.
---
 templates/selectidp-links.php | 45 +++++++++++++++++++----------------
 1 file changed, 24 insertions(+), 21 deletions(-)

diff --git a/templates/selectidp-links.php b/templates/selectidp-links.php
index 979188291..6c4eabad3 100644
--- a/templates/selectidp-links.php
+++ b/templates/selectidp-links.php
@@ -40,30 +40,33 @@ foreach ($this->data['idplist'] as $idpentry) {
                 $this->t('idpname_'.$idpentry2['entityid'])
             );
         });
-        if (!empty($this->data['preferredidp']) &&
-            array_key_exists($this->data['preferredidp'], $this->data['idplist'])
-        ) {
-            $idpentry = $this->data['idplist'][$this->data['preferredidp']];
-            echo '<div class="preferredidp">';
-            echo '	<img src="/'.$this->data['baseurlpath'].
-                'resources/icons/experience/gtk-about.64x64.png" class="float-r" alt="'.
-                $this->t('icon_prefered_idp').'" />';
 
-            if (array_key_exists('icon', $idpentry) && $idpentry['icon'] !== null) {
-                $iconUrl = \SimpleSAML\Utils\HTTP::resolveURL($idpentry['icon']);
-                echo '<img class="float-l" style="margin: 1em; padding: 3px; border: 1px solid #999" src="'.
-                    htmlspecialchars($iconUrl).'" />';
-            }
-            echo "\n".'	<h3 style="margin-top: 8px">'.
-                htmlspecialchars($this->t('idpname_'.$idpentry['entityid'])).'</h3>';
+        if (!empty($this->data['preferredidp'])) {
+            foreach ($this->data['idplist'] as $idpentry) {
+                if ($idpentry['entityid'] != $this->data['preferredidp']) {
+                    continue;
+                }
+                echo '<div class="preferredidp">';
+                echo '	<img src="/'.$this->data['baseurlpath'].
+                    'resources/icons/experience/gtk-about.64x64.png" class="float-r" alt="'.
+                    $this->t('icon_prefered_idp').'" />';
 
-            if (!empty($idpentry['description'])) {
-                echo '	<p>'.htmlspecialchars($this->t('idpdesc_'.$idpentry['entityid'])).'<br />';
+                if (array_key_exists('icon', $idpentry) && $idpentry['icon'] !== null) {
+                    $iconUrl = \SimpleSAML\Utils\HTTP::resolveURL($idpentry['icon']);
+                    echo '<img class="float-l" style="margin: 1em; padding: 3px; border: 1px solid #999" src="'.
+                        htmlspecialchars($iconUrl).'" />';
+                }
+                echo "\n".'	<h3 style="margin-top: 8px">'.
+                    htmlspecialchars($this->t('idpname_'.$idpentry['entityid'])).'</h3>';
+
+                if (!empty($idpentry['description'])) {
+                    echo '	<p>'.htmlspecialchars($this->t('idpdesc_'.$idpentry['entityid'])).'<br />';
+                }
+                echo('<button id="preferredidp" type="submit" class="btn" name="idp_'.
+                    htmlspecialchars($idpentry['entityid']).'">'.
+                    $this->t('select').'</button></p>');
+                echo '</div>';
             }
-            echo('<button id="preferredidp" type="submit" class="btn" name="idp_'.
-                htmlspecialchars($idpentry['entityid']).'">'.
-                $this->t('select').'</button></p>');
-            echo '</div>';
         }
 
         foreach ($this->data['idplist'] as $idpentry) {
-- 
GitLab