From b8b20d505b5cf93cefa9302b5a078a0411bda41c Mon Sep 17 00:00:00 2001
From: Guillaume Rousse <guillaume.rousse@renater.fr>
Date: Thu, 6 Dec 2018 09:55:24 +0100
Subject: [PATCH] honour federations ordering defined in configuration

---
 lib/AccountManager/App.pm                | 13 ++++++++-----
 templates/web/edugain/select_sp.tt2.html | 13 +++++++++----
 templates/web/renater/select_sp.tt2.html | 14 +++++++-------
 3 files changed, 24 insertions(+), 16 deletions(-)

diff --git a/lib/AccountManager/App.pm b/lib/AccountManager/App.pm
index ac22f70..41d689b 100644
--- a/lib/AccountManager/App.pm
+++ b/lib/AccountManager/App.pm
@@ -257,7 +257,7 @@ sub req_select_sp {
 
     my @federations = split(/, */, $self->{configuration}->{federations}->{list});
 
-    my %groups;
+    my @groups;
     my @self_entities;
     foreach my $federation (@federations) {
         if ($self->{configuration}->{$federation}->{metadata}) {
@@ -275,7 +275,8 @@ sub req_select_sp {
             ) if $EVAL_ERROR;
 
             my $entities = $metadata->parse(type => 'sp');
-            $groups{$federation} = {
+            push @groups, {
+                id       => $federation,
                 label    => $self->{configuration}->{$federation}->{label},
                 entities => [
                     map { {
@@ -310,7 +311,8 @@ sub req_select_sp {
                 }
             }
         } elsif ($self->{configuration}->{$federation}->{link}) {
-            $groups{$federation} = {
+            push @groups, {
+                id    => $federation,
                 label => $self->{configuration}->{$federation}->{label},
                 link  => $self->{configuration}->{$federation}->{link},
             };
@@ -318,7 +320,8 @@ sub req_select_sp {
     }
 
     if (@self_entities) {
-        $groups{_self} = {
+        unshift @groups, {
+            id       => '_self',
             label    => $self->{lh}->maketext("My organization"),
             entities => \@self_entities,
         };
@@ -328,7 +331,7 @@ sub req_select_sp {
         template => 'select_sp.tt2.html',
         data     => {
             action => 'select_sp',
-            groups => \%groups,
+            groups => \@groups,
         }
     );
 }
diff --git a/templates/web/edugain/select_sp.tt2.html b/templates/web/edugain/select_sp.tt2.html
index 1ee1bb3..b6e4eb8 100644
--- a/templates/web/edugain/select_sp.tt2.html
+++ b/templates/web/edugain/select_sp.tt2.html
@@ -15,15 +15,20 @@
             [% lh.maketext("You must be an administrator of that service to continue afterwards.") %]
         </p>
 
-        [% FOREACH group IN groups.keys.sort() %]
+        [% FOREACH group IN groups() %]
         <fieldset>
-            <legend>[% groups.$group.label %]</legend>
-            <select id="[% group %]" name="[% group %]">
+            [% IF group.entities %]
+            <legend>[% group.label %]</legend>
+            <select id="[% group.id %]" name="[% group.id %]">
                 <option value=""></option>
-            [% FOREACH entity IN groups.$group.entities.sort('name') %]
+            [% FOREACH entity IN group.entities.sort('name') %]
                 <option value="[% entity.id %]" data-federation="[% entity.federation %]">[% entity.name %]</option>
             [% END %]
             </select>
+            [% END %]
+            [% IF group.link %]
+            <a class="button" href="[% group.link %]">[% group.label %]</a>
+            [% END %]
         </fieldset>
         [% END %]
         <input type="hidden" id="federation" name="federation"/>
diff --git a/templates/web/renater/select_sp.tt2.html b/templates/web/renater/select_sp.tt2.html
index 0b318ca..0f682a9 100644
--- a/templates/web/renater/select_sp.tt2.html
+++ b/templates/web/renater/select_sp.tt2.html
@@ -15,19 +15,19 @@
             [% lh.maketext("You must be an administrator of that service to continue afterwards.") %]
         </p>
 
-        [% FOREACH group IN groups.keys.sort() %]
+        [% FOREACH group IN groups %]
         <fieldset>
-            [% IF groups.$group.entities %] 
-            <legend>[% groups.$group.label %]</legend>
-            <select id="[% group %]" name="[% group %]">
+            [% IF group.entities %]
+            <legend>[% group.label %]</legend>
+            <select id="[% group.id %]" name="[% group.id %]">
                 <option value=""></option>
-                [% FOREACH entity IN groups.$group.entities.sort('name') %]
+                [% FOREACH entity IN group.entities.sort('name') %]
                 <option value="[% entity.id %]" data-federation="[% entity.federation %]">[% entity.name %]</option>
                 [% END %]
             </select>
             [% END %]
-            [% IF groups.$group.link %]
-            <a class="button" href="[% groups.$group.link %]">[% groups.$group.label %]</a>
+            [% IF group.link %]
+            <a class="button" href="[% group.link %]">[% group.label %]</a>
             [% END %]
         </fieldset>
         [% END %]
-- 
GitLab