From 8727d618174ebf4b0d63ef7a69bf96347e3bf164 Mon Sep 17 00:00:00 2001
From: Guillaume Rousse <guillaume.rousse@renater.fr>
Date: Thu, 9 Nov 2017 17:16:30 +0100
Subject: [PATCH] retrieve accounts list in the caller

---
 bin/account-manager.pl.in           | 8 +++++++-
 lib/IdPAccountManager/Tools.pm      | 6 ++----
 lib/IdPAccountManager/WebRequest.pm | 1 +
 3 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/bin/account-manager.pl.in b/bin/account-manager.pl.in
index b4b8e0f..c170ed2 100755
--- a/bin/account-manager.pl.in
+++ b/bin/account-manager.pl.in
@@ -137,10 +137,16 @@ sub list_accounts {
         }
         printf "%d accounts removed\n", scalar @$accounts;
 
+        $accounts =
+            IdPAccountManager::Data::TestAccount::Manager->get_testaccounts(
+                db => $db
+            );
+
         die "failed to update simpleSAMLphp configuration file\n"
             unless IdPAccountManager::Tools::update_ssp_authsources(
                 $configuration->{templates_dir},
-                $configuration->{idp_accounts_file}}
+                $configuration->{idp_accounts_file},
+                $accounts
             );
 
         printf "Update simpleSamlPhp configuration file...\n";
diff --git a/lib/IdPAccountManager/Tools.pm b/lib/IdPAccountManager/Tools.pm
index d3dea2f..c641f97 100644
--- a/lib/IdPAccountManager/Tools.pm
+++ b/lib/IdPAccountManager/Tools.pm
@@ -8,8 +8,6 @@ use Encode;
 use Template;
 use Template::Stash;
 
-use IdPAccountManager::Data::TestAccount::Manager;
-
 INIT {
     ## a TT2 virtual method to get a variable type
     $Template::Stash::LIST_OPS->{isa} = sub {
@@ -82,14 +80,14 @@ sub generate_password {
 
 ## Updates simpleSamlPhp authsources.php configuration file
 sub update_ssp_authsources {
-    my ($templates_dir, $output) = @_;
+    my ($templates_dir, $output, $accounts) = @_;
 
     my $tt2 = Template->new({
         INCLUDE_PATH => $templates_dir
     });
     my $template = 'accountProfiles/valid-accounts.php.tt2';
     my $data = {
-        accounts => IdPAccountManager::Data::TestAccount::Manager->get_testaccounts(),
+        accounts => $accounts
     };
 
     return $tt2->process($template, $data, $output);
diff --git a/lib/IdPAccountManager/WebRequest.pm b/lib/IdPAccountManager/WebRequest.pm
index abc944a..5580245 100755
--- a/lib/IdPAccountManager/WebRequest.pm
+++ b/lib/IdPAccountManager/WebRequest.pm
@@ -488,6 +488,7 @@ sub req_validate_token {
     unless (IdPAccountManager::Tools::update_ssp_authsources(
             $self->{configuration}->{templates_dir},
             $self->{configuration}->{idp_accounts_file},
+            \@accounts
         )) {
         push @{ $self->{out}->{errors} }, "accounts_creation_failed";
         $self->{logger}->error(
-- 
GitLab