From 638a5f0954eba50fd3d0e9002626c5626b7e4ef2 Mon Sep 17 00:00:00 2001
From: "renater.salaun" <renater.salaun@047e039d-479c-447e-8a29-aa6bf4a09bab>
Date: Fri, 3 Oct 2014 11:58:04 +0000
Subject: [PATCH] First version of code to create tokens and service providers
 in DB

git-svn-id: https://svn.geant.net/GEANT/edugain_testidp_account_manager/trunk@17 047e039d-479c-447e-8a29-aa6bf4a09bab
---
 bin/account-manager-client.pl | 58 ++++++++++++++++++++++++++++++++---
 1 file changed, 54 insertions(+), 4 deletions(-)

diff --git a/bin/account-manager-client.pl b/bin/account-manager-client.pl
index c1dccd6..79a2c3c 100755
--- a/bin/account-manager-client.pl
+++ b/bin/account-manager-client.pl
@@ -5,7 +5,7 @@
 
 use strict;
 use utf8;
-use lib "/opt/testidp/IdPAccountManager/lib";
+use lib "/opt/testidp/IdPAccountManager/lib", ;
 use lib "/opt/testidp/IdPAccountManager/conf";
 
 use Getopt::Long;
@@ -14,17 +14,18 @@ use POSIX;
 use IdPAccountManager::TestAccount;
 use IdPAccountManager::SAMLMetadata;
 use IdPAccountManager::ServiceProvider;
+use IdPAccountManager::AuthenticationToken;
 
 my %options;
-unless (&GetOptions(\%options, 'help', 'create_test_account', 'account_profile=s', 'sp_entityid=s', 'list_test_accounts', 'parse_federation_metadata', 'list_service_providers')) {
+unless (&GetOptions(\%options, 'help', 'add_test_account', 'account_profile=s', 'sp_entityid=s', 'list_test_accounts', 'parse_federation_metadata', 'list_service_providers','list_authentication_tokens', 'add_authentication_token','email_address=s')) {
     die "Unknown options.";
 }
 
 if ($options{'help'}) {
-    printf "$0 --create_test_account --account_profile=<profile_id> --sp_entityid=<entityid>\n";
+    printf "$0 --add_test_account --account_profile=<profile_id> --sp_entityid=<entityid>\n";
 }
 
-if ($options{'create_test_account'}) {
+if ($options{'add_test_account'}) {
     
     unless ($options{'account_profile'}) {
         die "Missing account_profile option";
@@ -62,6 +63,10 @@ if ($options{'create_test_account'}) {
     
     my $all = IdPAccountManager::TestAccount::list_test_accounts(%args);
     
+    if ($#{$all} < 0) {
+        printf "No test account in DB\n";
+    }
+    
     foreach my $test_account (@$all) {
         $test_account->print();
     }
@@ -92,10 +97,55 @@ if ($options{'create_test_account'}) {
     
     my $all = IdPAccountManager::ServiceProvider::list_service_providers(%args);
     
+    if ($#{$all} < 0) {
+        printf "No service provider in DB\n";
+    }
+    
     foreach my $service_provider (@$all) {
         $service_provider->print();
     }
      
+}elsif ($options{'list_authentication_tokens'}) {
+    
+    my %args;    
+    
+    my $all = IdPAccountManager::AuthenticationToken::list_authentication_tokens(%args);
+    
+    if ($#{$all} < 0) {
+        printf "No token in DB\n";
+    }
+    
+    foreach my $authentication_token (@$all) {
+        $authentication_token->print();
+    }
+    
+}elsif ($options{'add_authentication_token'}) {
+    
+    unless ($options{'email_address'}) {
+        die "Missing email_address option";
+     }
+
+    unless ($options{'sp_entityid'}) {
+        die "Missing sp_entityid option";
+    }
+    
+    my $authentication_token = new IdPAccountManager::AuthenticationToken();
+    unless (defined $authentication_token) {
+         IdPAccountManager::Tools::do_log('error',"Failed to create token object");
+         exit -1;
+    }
+    unless ($authentication_token->set('email_address' => $options{'email_address'})) {
+        IdPAccountManager::Tools::do_log('error',"Failed to set token value");
+        exit -1;
+    }    
+    
+    unless ($authentication_token->save()) {
+        IdPAccountManager::Tools::do_log('error',"Failed to create token");
+        exit -1;
+    }
+    
+    $authentication_token->print();
+    
 }else {
     die "Missing arguments";
     
-- 
GitLab