From dc8a17d608e9aba6fb3c6ae9db17a4d498992162 Mon Sep 17 00:00:00 2001
From: Guillaume Rousse <guillaume.rousse@renater.fr>
Date: Mon, 7 May 2018 14:15:11 +0200
Subject: [PATCH] formmat multi-valued attributes in templates

---
 lib/AccountManager/Tools.pm               | 13 +++++
 templates/accounts/accounts.php.tt2       | 59 ++++++++++++-----------
 templates/web/en/create_accounts.tt2.html |  9 +++-
 3 files changed, 52 insertions(+), 29 deletions(-)

diff --git a/lib/AccountManager/Tools.pm b/lib/AccountManager/Tools.pm
index c5a5960..126b7c4 100644
--- a/lib/AccountManager/Tools.pm
+++ b/lib/AccountManager/Tools.pm
@@ -10,6 +10,7 @@ use List::Util qw(shuffle);
 use List::MoreUtils qw(pairwise);
 use MIME::Base64;
 use Template;
+use Template::Stash;
 
 sub encrypt {
     my ($string, $key) = @_;
@@ -94,6 +95,18 @@ sub generate_secret {
 sub update_ssp_authsources {
     my ($templates_dir, $output, $accounts) = @_;
 
+    # scalar virtual method to return a quoted value
+    $Template::Stash::SCALAR_OPS->{ quote } = sub {
+        my $scalar = shift;
+        return "'" . $scalar . "'";
+    };
+
+    # list virtual method to return a list of quoted values
+    $Template::Stash::LIST_OPS->{ quote } = sub {
+        my $list = shift;
+        return [ map { "'" . $_ . "'" } @$list ];
+    };
+
     my $tt2 = Template->new({
         ENCODING     => 'utf8',
         INCLUDE_PATH => $templates_dir . '/accounts'
diff --git a/templates/accounts/accounts.php.tt2 b/templates/accounts/accounts.php.tt2
index 6b9d15e..568ef2d 100644
--- a/templates/accounts/accounts.php.tt2
+++ b/templates/accounts/accounts.php.tt2
@@ -1,18 +1,3 @@
-[% MACRO enumerate(list) BLOCK -%]
-[% FOREACH item IN list -%]
-'[% item %]'[% IF ! loop.last -%], [% END -%]
-[% END -%]
-[% END -%]
-[% MACRO print_attribute(account, attribute) BLOCK -%]
-[% IF account.$attribute -%]
-        '[% attribute %]' => '[% account.$attribute %]',
-[% END -%]
-[% END -%]
-[% MACRO print_multivalued_attribute(account, attribute) BLOCK -%]
-[% IF account.$attribute -%]
-        '[% attribute %]' => [ [% enumerate(account.$attribute) -%] ],
-[% END -%]
-[% END -%]
 <?php
 // PhP configuration file loaded in simpleSamlPhp authsources.php file
 $validTestAccounts = array (
@@ -20,18 +5,38 @@ $validTestAccounts = array (
 
 [% FOREACH account IN accounts -%]
     'user[% account.id() %]:{SHA256}[% account.password_hash() %]=' => array(
-[% print_attribute(account, 'internal_uid') -%]
-[% print_attribute(account, 'cn') -%]
-[% print_attribute(account, 'displayName') -%]
-[% print_attribute(account, 'givenName') -%]
-[% print_attribute(account, 'sn') -%]
-[% print_attribute(account, 'mail') -%]
-[% print_multivalued_attribute(account, 'eduPersonAffiliation') -%]
-[% print_multivalued_attribute(account, 'eduPersonScopedAffiliation') -%]
-[% print_attribute(account, 'eduPersonPrincipalName') -%]
-[% print_attribute(account, 'schacHomeOrganization') -%]
-[% print_attribute(account, 'schacHomeOrganizationType') -%]
-[% print_attribute(account, 'associatedSP') -%]
+        'internal_uid'               => array([% account.internal_uid().quote %]),
+        'associatedSP'               => array([% account.associatedSP().quote %]),
+    [% IF account.cn() -%]
+        'cn'                         => array([% account.cn().quote %]),
+    [% END -%]
+    [% IF account.sn() -%]
+        'sn'                         => array([% account.sn().quote %]),
+    [% END -%]
+    [% IF account.displayName() -%]
+        'displayName'                => array([% account.displayName().quote %]),
+    [% END -%]
+    [% IF account.givenName() -%]
+        'givenName'                  => array([% account.givenName().quote %]),
+    [% END -%]
+    [% IF account.mail() -%]
+        'mail'                       => array([% account.mail().quote %]),
+    [% END -%]
+    [% IF account.eduPersonAffiliation() -%]
+        'eduPersonAffiliation'       => array([% account.eduPersonAffiliation().quote.join(', ') %]),
+    [% END -%]
+    [% IF account.eduPersonScopedAffiliation() -%]
+        'eduPersonScopedAffiliation' => array([% account.eduPersonScopedAffiliation().quote.join(', ') %]),
+    [% END -%]
+    [% IF account.eduPersonPrincipalName() -%]
+        'eduPersonPrincipalName'     => array([% account.eduPersonPrincipalName().quote %]),
+    [% END -%]
+    [% IF account.schacHomeOrganization() -%]
+        'schacHomeOrganization'      => array([% account.schacHomeOrganization().quote %]),
+    [% END -%]
+    [% IF account.schacHomeOrganizationType() -%]
+        'schacHomeOrganizationType'  => array([% account.schacHomeOrganizationType().quote %]),
+    [% END -%]
     ),
 
 [% END -%]
diff --git a/templates/web/en/create_accounts.tt2.html b/templates/web/en/create_accounts.tt2.html
index 5db2146..39a9603 100644
--- a/templates/web/en/create_accounts.tt2.html
+++ b/templates/web/en/create_accounts.tt2.html
@@ -49,9 +49,14 @@ provider.</p>
         <div>
             <table>
                 <caption>List of user attributes</caption>
-    [% FOREACH attribute IN [ 'cn', 'displayName', 'givenName', 'sn', 'mail', 'schacHomeOrganization', 'schacHomeOrganizationType', 'eduPersonPrincipalName', 'eduPersonAffiliation', 'eduPersonScopedAffiliation' ] -%]
+    [% FOREACH attribute IN [ 'cn', 'displayName', 'givenName', 'sn', 'mail', 'schacHomeOrganization', 'schacHomeOrganizationType', 'eduPersonPrincipalName' ] -%]
                 <tr>
-                    <th>[% attribute %]</th><td> [% account.$attribute %]</td>
+                    <th>[% attribute %]</th><td> [% account.$attribute() %]</td>
+                </tr>
+    [% END %]
+    [% FOREACH attribute IN [ 'eduPersonAffiliation', 'eduPersonScopedAffiliation' ] -%]
+                <tr>
+                    <th>[% attribute %]</th><td> [% account.$attribute().join(', ') %]</td>
                 </tr>
     [% END %]
                 <tr>
-- 
GitLab