From 8fd61826bc42d13c99ae863d58a587624cf5d3b4 Mon Sep 17 00:00:00 2001
From: Guillaume Rousse <guillaume.rousse@renater.fr>
Date: Thu, 3 Jan 2019 15:29:32 +0100
Subject: [PATCH] transfer presentation complexity from code to templates

---
 lib/AccountManager/App.pm                | 79 ++++++------------------
 lib/AccountManager/Entity.pm             |  6 ++
 lib/AccountManager/L10N/fr.pm            | 16 +++++
 templates/web/edugain/select_sp.tt2.html | 37 ++++++-----
 templates/web/renater/select_sp.tt2.html | 56 ++++++++++++-----
 5 files changed, 105 insertions(+), 89 deletions(-)

diff --git a/lib/AccountManager/App.pm b/lib/AccountManager/App.pm
index 5da75bc..4678a58 100644
--- a/lib/AccountManager/App.pm
+++ b/lib/AccountManager/App.pm
@@ -254,71 +254,32 @@ sub req_select_sp {
     $self->check_authentication(action => 'select_sp')
         if $self->{configuration}->{app}->{login_url};
 
-    my @groups;
-    my @organization_entities;
-
-    foreach my $id (split(/, */, $self->{configuration}->{groups}->{list})) {
-        my $spec = $self->{configuration}->{$id};
-        if ($spec->{type} eq 'metadata') {
-
-            my $entities = AccountManager::Entity->get_entities(
-                db => $self->{db},
-                query => [
-                    type        => { eq   => 'sp' },
-                    federations => { like => "%\"$id\"%"  }
-                ],
-                debug => 1
-            );
-            push @groups, {
-                id       => $id,
-                label    => $spec->{label},
-                type     => 'list',
-                entities => $entities
-            };
-        } elsif ($spec->{type} eq 'organization') {
-            next unless $ENV{HTTP_SHIB_IDENTITY_PROVIDER};
-
-            my $idps = AccountManager::Entity->get_entities(
-                db    => $self->{db},
-                query => [
-                    type     => 'idp',
-                    entityid => $ENV{HTTP_SHIB_IDENTITY_PROVIDER},
-                ]
-            );
-            my $idp = $idps->[0];
-
-            next unless $idp;
-
-            my $entities = AccountManager::Entity->get_entities(
-                db    => $self->{db},
-                query => [
-                    type             => 'sp',
-                    organization_url => $idp->organization_url()
-                ]
-            );
-
-            push @groups, {
-                id       => $id,
-                label    => $spec->{label},
-                type     => 'list',
-                entities => $entities
-            };
-        } elsif ($spec->{type} eq 'link') {
-            push @groups, {
-                id      => $id,
-                label   => $spec->{label},
-                type    => 'link',
-                url     => $spec->{url},
-                message => $spec->{message},
-            };
-        }
+    my $sps = AccountManager::Entity->get_entities(
+        db => $self->{db},
+        query => [
+            type => 'sp',
+        ],
+        sort_by => 'display_name'
+    );
+
+    my $idp;
+    if ($ENV{HTTP_SHIB_IDENTITY_PROVIDER}) {
+        my $idps = AccountManager::Entity->get_entities(
+            db    => $self->{db},
+            query => [
+                type     => 'idp',
+                entityid => $ENV{HTTP_SHIB_IDENTITY_PROVIDER},
+            ]
+        );
+        $idp = $idps->[0];
     }
 
     $self->respond(
         template => 'select_sp.tt2.html',
         data     => {
             action => 'select_sp',
-            groups => \@groups,
+            sps    => $sps,
+            idp    => $idp,
         }
     );
 }
diff --git a/lib/AccountManager/Entity.pm b/lib/AccountManager/Entity.pm
index 7b70996..c5ec588 100644
--- a/lib/AccountManager/Entity.pm
+++ b/lib/AccountManager/Entity.pm
@@ -50,5 +50,11 @@ sub is_contact {
     return any { $email eq lc($_) } $self->contacts();
 }
 
+sub is_member_of {
+    my ($self, $federation) = @_;
+
+    return any { $federation eq $_ } $self->federations();
+}
+
 1;
 
diff --git a/lib/AccountManager/L10N/fr.pm b/lib/AccountManager/L10N/fr.pm
index 3ed7908..4d440b9 100644
--- a/lib/AccountManager/L10N/fr.pm
+++ b/lib/AccountManager/L10N/fr.pm
@@ -48,6 +48,22 @@ msgstr "Ces attributs sont transmis via le protocole SAML, après une authentifi
 msgid "All rights reserved"
 msgstr "Tous droits réservés"
 
+#: templates/web/edugain/select_sp.tt2.html:33
+msgid "All service providers"
+msgstr "Tous les fournisseurs de service"
+
+#: templates/web/renater/select_sp.tt2.html:33
+msgid "All service providers registered in Education-Research federation"
+msgstr "Tous les fournisseurs de service enregistrés dans la fédération Education-Recherche"
+
+#: templates/web/renater/select_sp.tt2.html:55
+msgid "All service providers registered in eduGAIN federation"
+msgstr "Tous les fournisseurs de service enregistrés dans la fédération eduGAIN"
+
+#: templates/web/renater/select_sp.tt2.html:44
+msgid "All service providers registered in test federation"
+msgstr "Tous les fournisseurs de service enregistrés dans la fédération de test"
+
 #. (email)
 #: templates/web/edugain/complete_challenge.tt2.html:14 templates/web/renater/complete_challenge.tt2.html:14
 msgid "An email challenge including a validation token has been emailed to you at %1."
diff --git a/templates/web/edugain/select_sp.tt2.html b/templates/web/edugain/select_sp.tt2.html
index d5e142d..6d8596d 100644
--- a/templates/web/edugain/select_sp.tt2.html
+++ b/templates/web/edugain/select_sp.tt2.html
@@ -15,22 +15,29 @@
             [% lh.maketext("You must be an administrator of that service to continue afterwards.") %]
         </p>
 
-[% FOREACH group IN groups %]
+    [% IF idp %]
         <fieldset>
-            <legend>[% lh.maketext(group.label) %]</legend>
-    [% SWITCH group.type %]
-        [% CASE 'list' %]
-            <select id="[% group.id %]" name="[% group.id %]">
+            <legend>[% lh.maketext("Service providers from my own organization") %]</legend>
+            <select id="self" name="self">
                 <option value=""></option>
-            [% FOREACH entity IN group.entities.sort('display_name') %]
-                <option value="[% entity.entityid %]">[% entity.display_name %]</option>
-            [% END %]
+        [% organization_url == idp.organization_url() %]
+        [% FOREACH sp IN sps %]
+            [% NEXT UNLESS sp.organization_url() == organization_url %]
+                <option value="[% sp.entityid() %]">[% sp.display_name() %]</option>
+        [% END %]
             </select>
-        [% CASE 'link' %]
-            <a class="button" href="[% group.url %]">[% lh.maketext(group.message) %]</a>
+        </fieldset>
+    [% END %]
+
+        <fieldset>
+            <legend>[% lh.maketext("All service providers") %]</legend>
+            <select id="all" name="all">
+                <option value=""></option>
+    [% FOREACH sp IN sps %]
+                <option value="[% sp.entityid() %]">[% sp.display_name() %]</option>
     [% END %]
+            </select>
         </fieldset>
-[% END %]
         <input type="hidden" id="entityid" name="entityid"/>
 
         <div class="callout primary">[% lh.maketext("Those service providers have been extracted from federation metadata.") %]</div>
@@ -184,10 +191,10 @@ $( document ).ready(function() {
         }
     });
 
-[% FOREACH group IN groups %]
-    [% NEXT IF group.type != 'list' %]
-    $( "#[% group.id %]" ).combobox();
-[% END %]
+    [% IF idp %]
+    $("#self").combobox();
+    [% END %]
+    $( "#all" ).combobox();
 
     $.validator.messages.required = "[% lh.maketext("This information is required") %]";
     $("form").validate();
diff --git a/templates/web/renater/select_sp.tt2.html b/templates/web/renater/select_sp.tt2.html
index 57fdfca..edae930 100644
--- a/templates/web/renater/select_sp.tt2.html
+++ b/templates/web/renater/select_sp.tt2.html
@@ -15,22 +15,47 @@
             [% lh.maketext("You must be an administrator of that service to continue afterwards.") %]
         </p>
 
-[% FOREACH group IN groups %]
+    [% IF idp %]
         <fieldset>
-            <legend>[% lh.maketext(group.label) %]</legend>
-    [% SWITCH group.type %]
-        [% CASE 'list' %]
-            <select id="[% group.id %]" name="[% group.id %]">
+            <legend>[% lh.maketext("Service providers from my own organization") %]</legend>
+            <select id="self" name="self">
                 <option value=""></option>
-            [% FOREACH entity IN group.entities.sort('display_name') %]
-                <option value="[% entity.entityid %]">[% entity.display_name %]</option>
-            [% END %]
+        [% organization_url == idp.organization_url() %]
+        [% FOREACH sp IN sps %]
+            [% NEXT UNLESS sp.organization_url() == organization_url %]
+                <option value="[% sp.entityid() %]">[% sp.display_name() %]</option>
+        [% END %]
             </select>
-        [% CASE 'link' %]
-            <a class="button" href="[% group.url %]">[% lh.maketext(group.message) %]</a>
+        </fieldset>
+    [% END %]
+
+        <fieldset>
+            <legend>[% lh.maketext("All service providers registered in Education-Research federation") %]</legend>
+            <select id="renater" name="renater">
+                <option value=""></option>
+    [% FOREACH sp IN sps %]
+        [% NEXT UNLESS sp.is_member_of('renater') %]
+                <option value="[% sp.entityid() %]">[% sp.display_name() %]</option>
     [% END %]
+            </select>
         </fieldset>
-[% END %]
+
+        <fieldset>
+            <legend>[% lh.maketext("All service providers registered in test federation") %]</legend>
+            <select id="test" name="test">
+                <option value=""></option>
+    [% FOREACH sp IN sps %]
+        [% NEXT UNLESS sp.is_member_of('test') %]
+                <option value="[% sp.entityid() %]">[% sp.display_name() %]</option>
+    [% END %]
+            </select>
+        </fieldset>
+
+        <fieldset>
+            <legend>[% lh.maketext("All service providers registered in eduGAIN federation") %]</legend>
+            <a class="button" href="https://access-check.edugain.org">[% lh.maketext("eduGAIN Access Check") %]</a>
+        </fieldset>
+
         <input type="hidden" id="entityid" name="entityid"/>
 
         <div class="callout alert-callout-border primary">[% lh.maketext("Those service providers have been extracted from federation metadata.") %]</div>
@@ -185,10 +210,11 @@ $( document ).ready(function() {
         }
     });
 
-[% FOREACH group IN groups %]
-    [% NEXT IF group.type != 'list' %]
-    $( "#[% group.id %]" ).combobox();
-[% END %]
+    [% IF idp %]
+    $("#self").combobox();
+    [% END %]
+    $("#renater").combobox();
+    $("#test").combobox();
 
     $.validator.messages.required = "[% lh.maketext("This information is required") %]";
     $("form").validate();
-- 
GitLab