Skip to content
Snippets Groups Projects
TestAccount.pm 3.38 KiB
Newer Older
package IdPAccountManager::TestAccount;

use IdPAccountManager::Data::Testaccount;
use IdPAccountManager::Data::Testaccount::Manager;

use IdPAccountManager::Tools;
use Conf;
require Exporter;
my @ISA = qw(Exporter);
my @EXPORT = qw();

use Carp;

INIT {
  ## Set error mode  to non fatal
  IdPAccountManager::Data::Testaccount::Manager->error_mode('return');  
 }

sub new {
    my ($pkg) = shift;
    my %args = @_;

    my $self = {};

    ## Bless Provider object
    bless $self, $pkg;
    
    ## Object may be created either with a hashref as argument or an IdPAccountManager::Data::Testaccount object
    ## Second case is usefull when fetching a set of IdPAccountManager::Data::Testaccount via IdPAccountManager::Data::Testaccount::Manager
    if (ref($_[0]) eq 'IdPAccountManager::Data::Testaccount') {
        $self->{'persistent'} = $_[0];
    }else {
        $self->{'persistent'} = IdPAccountManager::Data::Testaccount->new(%args);
    }
    my $self = shift;
    my $attribute_name = shift;
    ## User password is not stored in DB
    if ($attribute_name eq 'user_password') {
        return $self->{$attribute_name};
    }else {
        return $self->{'persistent'}->$attribute_name;

    }
}

sub save {
    my $self = shift;
    
    ## If no id is defined, it is a new account
    unless (defined $self->{'persistent'}->id) {
        $self->{'persistent'}->creation_date(time);
        $self->{'persistent'}->expiration_date(time + ($Conf::global{'accounts_validity_period'} * 3600 * 24));
        $self->{'user_password'} = &IdPAccountManager::Tools::generate_password();
        $self->{'persistent'}->user_password_hash(&IdPAccountManager::Tools::sha256_hash($self->{'user_password'}));
    }
    
    unless ($self->{'persistent'}->save()) {
        IdPAccountManager::Tools::do_log('error', "Failed to save Test Account in DB");
        return undef;
    }
}

## Delete a test account
sub delete {
    my $self = shift;
    unless ($self->{'persistent'}->delete()) {
        IdPAccountManager::Tools::do_log('error', "Failed to delete a test account in DB");
        return undef;
    }
}

## Print the content of a test account
sub print {
    my $self = shift;
    my $fd = shift || \*STDOUT;
    
    printf $fd "Account ID=%s; password_hash=%s; sp_entityid=%s; account_profile=%s; creation_date=%s; expiration_date=%s\n",
            $self->get('id'), $self->get('user_password_hash'), $self->get('sp_entityid'), $self->get('account_profile'),
            &POSIX::strftime('%Y:%m:%d', localtime($self->get('creation_date'))), &POSIX::strftime('%Y:%m:%d', localtime($self->get('expiration_date')));

    return 1.
}

## list all test accounts
## Class method
sub list_test_accounts {
    my %args = @_;

    my $persistent_accounts= IdPAccountManager::Data::Testaccount::Manager->get_testaccounts(%args);
    my $accounts;
    foreach my $persistent_account (@{$persistent_accounts}) {
        my $account = new IdPAccountManager::TestAccount($persistent_account);
        push @$accounts, $account;
    }
#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)