From b4a377c9965811624ad580902045b77cec9ae563 Mon Sep 17 00:00:00 2001
From: "renater.salaun" <renater.salaun@047e039d-479c-447e-8a29-aa6bf4a09bab>
Date: Mon, 8 Sep 2014 07:16:06 +0000
Subject: [PATCH] Initial version of the code

git-svn-id: https://svn.geant.net/GEANT/edugain_testidp_account_manager/trunk@2 047e039d-479c-447e-8a29-aa6bf4a09bab
---
 bin/account-manager-client.pl                 | 38 +++++++++++++++++++
 bin/create-database.pl                        | 28 ++++++++++++++
 conf/create-manager-db.sql                    | 12 ++++++
 lib/IdPAccountManager/Data/DB/AutoBase1.pm    | 18 +++++++++
 .../Data/DB/Object/AutoBase2.pm               |  9 +++++
 lib/IdPAccountManager/Data/Testaccount.pm     | 24 ++++++++++++
 .../Data/Testaccount/Manager.pm               | 14 +++++++
 lib/IdPAccountManager/TestAccount.pm          | 36 ++++++++++++++++++
 lib/IdPAccountManager/TestAccount.pm.ORIG     | 36 ++++++++++++++++++
 9 files changed, 215 insertions(+)
 create mode 100755 bin/account-manager-client.pl
 create mode 100755 bin/create-database.pl
 create mode 100644 conf/create-manager-db.sql
 create mode 100644 lib/IdPAccountManager/Data/DB/AutoBase1.pm
 create mode 100644 lib/IdPAccountManager/Data/DB/Object/AutoBase2.pm
 create mode 100644 lib/IdPAccountManager/Data/Testaccount.pm
 create mode 100644 lib/IdPAccountManager/Data/Testaccount/Manager.pm
 create mode 100644 lib/IdPAccountManager/TestAccount.pm
 create mode 100644 lib/IdPAccountManager/TestAccount.pm.ORIG

diff --git a/bin/account-manager-client.pl b/bin/account-manager-client.pl
new file mode 100755
index 0000000..2e564b6
--- /dev/null
+++ b/bin/account-manager-client.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/perl
+
+## 18/07/2014, Olivier Salaün
+## Command-line client for the Test IdP Account Manager
+
+use strict;
+use utf8;
+use lib "/opt/testidp/IdPAccountManager/lib";
+
+use Getopt::Long;
+
+use IdPAccountManager::TestAccount;
+
+my %options;
+unless (&GetOptions(\%options, 'help', 'create_test_account', 'account_profile=s', 'sp_entityid=s')) {
+    die "Unknown options.";
+}
+
+if ($options{'help'}) {
+    printf "$0 --create_test_account --account_profile=<profile_id> --sp_entityid=<entityid>\n";
+}
+
+if ($options{'create_test_account'}) {
+    
+    unless ($options{'account_profile'}) {
+        die "Missing account_profile option";
+    }
+
+    unless ($options{'sp_entityid'}) {
+        die "Missing sp_entityid option";
+    }
+    
+    my $test_account = new IdPAccountManager::TestAccount(account_profile => $options{'account_profile'},
+                                                          sp_entityid => $options{'sp_entityid'});
+    unless (defined $test_account) {
+        die "Failed to create test account";
+    }
+}
diff --git a/bin/create-database.pl b/bin/create-database.pl
new file mode 100755
index 0000000..d4684b1
--- /dev/null
+++ b/bin/create-database.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/perl
+## Initialize Rose::DB code given the DB structure
+
+use Rose::DB;
+use Rose::DB::Object::Loader;
+use Getopt::Long;
+
+my %options;
+unless (&GetOptions(\%options, 'database=s')) {
+    die "Unknown options.";
+}
+
+my $dbname = $options{'database'} || 'idp_account_manager';
+
+$loader = 
+    Rose::DB::Object::Loader->new(
+        db_dsn       => "dbi:mysql:dbname=$dbname;host=localhost",
+        db_username  => 'idpadmin',
+        db_password  => 'PQur;m9#I[',
+        db_options   => { AutoCommit => 1, ChopBlanks => 1 },
+        class_prefix => 'IdPAccountManager::Data',
+        #with_unique_keys => 0,
+        );
+
+$loader->make_modules(with_managers => 1,
+                      module_dir => '/tmp',
+                      #with_relationships => ['one to many','many to one']
+    );
diff --git a/conf/create-manager-db.sql b/conf/create-manager-db.sql
new file mode 100644
index 0000000..1e51272
--- /dev/null
+++ b/conf/create-manager-db.sql
@@ -0,0 +1,12 @@
+## Test IdP Account Manager DB creation script
+
+CREATE TABLE `testaccounts` (
+  `id` bigint(20) NOT NULL auto_increment,
+  `user_id` varchar(50) NOT NULL,
+  `user_password` varchar(50) NOT NULL,
+  `creation_date` int default NULL,
+  `expiration_date` int default NULL,
+  PRIMARY KEY  (`id`),
+  UNIQUE (user_id)
+) ENGINE=InnoDB  DEFAULT CHARSET=utf8 ;
+
diff --git a/lib/IdPAccountManager/Data/DB/AutoBase1.pm b/lib/IdPAccountManager/Data/DB/AutoBase1.pm
new file mode 100644
index 0000000..cef561f
--- /dev/null
+++ b/lib/IdPAccountManager/Data/DB/AutoBase1.pm
@@ -0,0 +1,18 @@
+package IdPAccountManager::Data::DB::AutoBase1;
+
+use strict;
+
+use base 'Rose::DB';
+
+__PACKAGE__->use_private_registry;
+
+__PACKAGE__->register_db
+(
+  connect_options => { AutoCommit => 1, ChopBlanks => 1 },
+  driver          => 'mysql',
+  dsn             => 'dbi:mysql:dbname=idp_account_manager;host=localhost',
+  password        => 'PQur;m9#I[',
+  username        => 'idpadmin',
+);
+
+1;
diff --git a/lib/IdPAccountManager/Data/DB/Object/AutoBase2.pm b/lib/IdPAccountManager/Data/DB/Object/AutoBase2.pm
new file mode 100644
index 0000000..a7d9068
--- /dev/null
+++ b/lib/IdPAccountManager/Data/DB/Object/AutoBase2.pm
@@ -0,0 +1,9 @@
+package IdPAccountManager::Data::DB::Object::AutoBase2;
+
+use base 'Rose::DB::Object';
+
+use IdPAccountManager::Data::DB::AutoBase1;
+
+sub init_db { IdPAccountManager::Data::DB::AutoBase1->new }
+
+1;
diff --git a/lib/IdPAccountManager/Data/Testaccount.pm b/lib/IdPAccountManager/Data/Testaccount.pm
new file mode 100644
index 0000000..c651422
--- /dev/null
+++ b/lib/IdPAccountManager/Data/Testaccount.pm
@@ -0,0 +1,24 @@
+package IdPAccountManager::Data::Testaccount;
+
+use strict;
+
+use base qw(IdPAccountManager::Data::DB::Object::AutoBase2);
+
+__PACKAGE__->meta->setup(
+    table   => 'testaccounts',
+
+    columns => [
+        id              => { type => 'bigserial', not_null => 1 },
+        user_id         => { type => 'varchar', length => 50, not_null => 1 },
+        user_password   => { type => 'varchar', length => 50, not_null => 1 },
+        creation_date   => { type => 'integer' },
+        expiration_date => { type => 'integer' },
+    ],
+
+    primary_key_columns => [ 'id' ],
+
+    unique_key => [ 'user_id' ],
+);
+
+1;
+
diff --git a/lib/IdPAccountManager/Data/Testaccount/Manager.pm b/lib/IdPAccountManager/Data/Testaccount/Manager.pm
new file mode 100644
index 0000000..a2d6b36
--- /dev/null
+++ b/lib/IdPAccountManager/Data/Testaccount/Manager.pm
@@ -0,0 +1,14 @@
+package IdPAccountManager::Data::Testaccount::Manager;
+
+use strict;
+
+use base qw(Rose::DB::Object::Manager);
+
+use IdPAccountManager::Data::Testaccount;
+
+sub object_class { 'IdPAccountManager::Data::Testaccount' }
+
+__PACKAGE__->make_manager_methods('testaccounts');
+
+1;
+
diff --git a/lib/IdPAccountManager/TestAccount.pm b/lib/IdPAccountManager/TestAccount.pm
new file mode 100644
index 0000000..de034a5
--- /dev/null
+++ b/lib/IdPAccountManager/TestAccount.pm
@@ -0,0 +1,36 @@
+package IdPAccountManager::TestAccount;
+
+use Moose;
+use Moose::Util::TypeConstraints;
+
+subtype 'entityid',
+    as 'Str',
+    where { /^(urn:|http(s)?\:\/\/)/ },
+    message { "$_ is not a valide entityid"};
+
+has 'account_profile' => (is => 'ro',
+                          isa => 'Str',
+                          required => 1);
+has 'sp_entityid' => (is => 'rw',
+                      isa => 'entityid',
+                      required => 1,
+                      );
+
+#before 'new' => sub { print "about to call new\n"; };
+
+1; # Magic true value required at end of module
+__END__
+
+=head1 NAME
+
+IdPAccountManager::TestAccount - Manage test user accounts for the Test Identity Provider
+
+=head1 SYNOPSIS
+
+=head1 DESCRIPTION
+
+=head1 SUBROUTINES/METHODS
+
+=head1 AUTHOR
+
+Olivier Salaün (olivier.salaun@renater.fr)
diff --git a/lib/IdPAccountManager/TestAccount.pm.ORIG b/lib/IdPAccountManager/TestAccount.pm.ORIG
new file mode 100644
index 0000000..de034a5
--- /dev/null
+++ b/lib/IdPAccountManager/TestAccount.pm.ORIG
@@ -0,0 +1,36 @@
+package IdPAccountManager::TestAccount;
+
+use Moose;
+use Moose::Util::TypeConstraints;
+
+subtype 'entityid',
+    as 'Str',
+    where { /^(urn:|http(s)?\:\/\/)/ },
+    message { "$_ is not a valide entityid"};
+
+has 'account_profile' => (is => 'ro',
+                          isa => 'Str',
+                          required => 1);
+has 'sp_entityid' => (is => 'rw',
+                      isa => 'entityid',
+                      required => 1,
+                      );
+
+#before 'new' => sub { print "about to call new\n"; };
+
+1; # Magic true value required at end of module
+__END__
+
+=head1 NAME
+
+IdPAccountManager::TestAccount - Manage test user accounts for the Test Identity Provider
+
+=head1 SYNOPSIS
+
+=head1 DESCRIPTION
+
+=head1 SUBROUTINES/METHODS
+
+=head1 AUTHOR
+
+Olivier Salaün (olivier.salaun@renater.fr)
-- 
GitLab