From 39e7fc19264939138916c4de8f7143b7cc3c63ea Mon Sep 17 00:00:00 2001
From: Guillaume Rousse <guillaume.rousse@renater.fr>
Date: Thu, 29 Nov 2018 09:57:34 +0100
Subject: [PATCH] extract service URL, and provide a direct access link to user

---
 conf/manager.sql                               |  1 +
 lib/AccountManager/App.pm                      | 11 +++++++++++
 lib/AccountManager/L10N/fr.pm                  |  4 ++--
 lib/AccountManager/Metadata.pm                 |  7 ++++++-
 lib/AccountManager/ServiceProvider.pm          |  1 +
 templates/web/edugain/create_accounts.tt2.html |  4 ++--
 templates/web/renater/create_accounts.tt2.html |  4 ++--
 7 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/conf/manager.sql b/conf/manager.sql
index e2ecaa0..c373dbe 100644
--- a/conf/manager.sql
+++ b/conf/manager.sql
@@ -14,6 +14,7 @@ CREATE TABLE tokens (
 CREATE TABLE services (
     id bigint(20) NOT NULL AUTO_INCREMENT,
     entityid varchar(200) NOT NULL,
+    url varchar(200) DEFAULT NULL,
     displayname varchar(500) DEFAULT NULL,
     contacts varchar(2000) DEFAULT NULL,
     PRIMARY KEY (id),
diff --git a/lib/AccountManager/App.pm b/lib/AccountManager/App.pm
index 5d2eab7..0e5c05d 100644
--- a/lib/AccountManager/App.pm
+++ b/lib/AccountManager/App.pm
@@ -344,6 +344,7 @@ sub req_select_email {
 
         # complete persistent object
         $sp->displayname($entity->{display_name});
+        $sp->url($entity->{url});
         $sp->contacts(uniq map { $_->{EmailAddress} } @{$entity->{contacts}})
             if $entity->{contacts};
 
@@ -570,6 +571,15 @@ sub req_create_accounts {
 
     $self->check_token(token => $token, entityid => $entityid);
 
+    my $sp = AccountManager::ServiceProvider->new(
+        db       => $self->{db},
+        entityid => $entityid,
+    );
+    $self->abort(
+        log  => sprintf("No such SP '%s' in database", $entityid),
+        user => "no_such_entity"
+    ) if !$sp->load(speculative => 1);
+
     ## create test accounts
     my @accounts;
 
@@ -645,6 +655,7 @@ sub req_create_accounts {
             action   => 'create_accounts',
             accounts => \@accounts,
             entityid => $entityid,
+            url      => $sp->url(),
             email    => $email,
             key      => $key,
             token    => $download_token->secret(),
diff --git a/lib/AccountManager/L10N/fr.pm b/lib/AccountManager/L10N/fr.pm
index 2dfedd1..954d944 100644
--- a/lib/AccountManager/L10N/fr.pm
+++ b/lib/AccountManager/L10N/fr.pm
@@ -259,8 +259,8 @@ msgid "Validation token: %1"
 msgstr "Code de validation: %1"
 
 #: templates/web/edugain/create_accounts.tt2.html:10 templates/web/renater/create_accounts.tt2.html:12
-msgid "You can now use these test accounts to login at your service provider."
-msgstr "Vous pouvez maintenant utiliser ces comptes pour vous identifier sur votre fournisseur de service."
+msgid "You can now use these test accounts to login at your <a href=\"%1\">service provider</a>."
+msgstr "Vous pouvez maintenant utiliser ces comptes pour vous identifier sur votre <a href=\"%1\">fournisseur de service</a>."
 
 #: templates/web/edugain/errors.tt2.html:38 templates/web/renater/errors.tt2.html:37
 msgid "You can report the issue to the administrators"
diff --git a/lib/AccountManager/Metadata.pm b/lib/AccountManager/Metadata.pm
index 9f57a28..e83ec95 100644
--- a/lib/AccountManager/Metadata.pm
+++ b/lib/AccountManager/Metadata.pm
@@ -138,7 +138,12 @@ sub parse {
                     my %names =
                         map { $_->getAttribute('xml:lang') => $_->textContent() }
                         @names;
-                    $data->{name} = $names{en} ? $names{en} : $names[0];
+                    $data->{name} = $names{en} ? $names{en} : $names[0]->textContent();
+                    my @urls = $uiinfo->getChildrenByLocalName('InformationURL');
+                    my %urls =
+                        map { $_->getAttribute('xml:lang') => $_->textContent() }
+                        @urls;
+                    $data->{url} = $urls{en} ? $urls{en} : $urls[0]->textContent();
                 }
 
             } elsif ($child->localname() eq 'Extensions') {
diff --git a/lib/AccountManager/ServiceProvider.pm b/lib/AccountManager/ServiceProvider.pm
index 8d83daa..c501112 100644
--- a/lib/AccountManager/ServiceProvider.pm
+++ b/lib/AccountManager/ServiceProvider.pm
@@ -15,6 +15,7 @@ __PACKAGE__->meta->setup(
         id          => { type => 'bigserial', not_null => 1 },
         entityid    => { type => 'varchar', length => 200, not_null => 1 },
         displayname => { type => 'varchar', length => 500 },
+        url         => { type => 'varchar', length => 200 },
         contacts    => { type => 'array' },
     ],
 
diff --git a/templates/web/edugain/create_accounts.tt2.html b/templates/web/edugain/create_accounts.tt2.html
index f828c16..9f582e5 100644
--- a/templates/web/edugain/create_accounts.tt2.html
+++ b/templates/web/edugain/create_accounts.tt2.html
@@ -7,8 +7,8 @@
 
 <h2>[% lh.maketext("Test accounts created") %]</h2>
 <p>
-    [% lh.maketext("You can now use these test accounts to login at your service provider.") %]
-    [% lh.maketext("To do so, select <strong>[_1]</strong> when choosing an identity provider.", idp_displayname) %]
+[% lh.maketext("You can now use these test accounts to login at your <a href=\"[_1]\">service provider</a>.", url) %]
+[% lh.maketext("To do so, select <strong>[_1]</strong> when choosing an identity provider.", idp_displayname) %]
 </p>
 
 <p><a class="button" href="[% app.url %]?action=download_accounts&entityid=[% entityid %]&token=[% token %]&key=[% key %]">[% lh.maketext("Download in CSV format") %]</a></p>
diff --git a/templates/web/renater/create_accounts.tt2.html b/templates/web/renater/create_accounts.tt2.html
index b017cb8..514cfe0 100644
--- a/templates/web/renater/create_accounts.tt2.html
+++ b/templates/web/renater/create_accounts.tt2.html
@@ -9,8 +9,8 @@
 
 <h2>[% lh.maketext("Test accounts created") %]</h2>
 <p>
-    [% lh.maketext("You can now use these test accounts to login at your service provider.") %]
-    [% lh.maketext("To do so, select <strong>[_1]</strong> when choosing an identity provider.", idp_displayname) %]
+[% lh.maketext("You can now use these test accounts to login at your <a href=\"[_1]\">service provider</a>.", url) %]
+[% lh.maketext("To do so, select <strong>[_1]</strong> when choosing an identity provider.", idp_displayname) %]
 </p>
 
 <p><a class="button" href="[% app.url %]?action=download_accounts&entityid=[% entityid %]&token=[% token %]&key=[% key %]">[% lh.maketext("Download in CSV format") %]</a></p>
-- 
GitLab