Skip to content
Snippets Groups Projects
TestAccount.pm 6.27 KiB
package IdPAccountManager::TestAccount;

## Copyright (c) GEANT
## This software was developed by RENATER. The research leading to these results has received funding
## from the European Community¹s Seventh Framework Programme (FP7/2007-2013) under grant agreement nº 238875 (GÉANT).

use strict;
use warnings;

use IdPAccountManager::Data::TestAccount;
use IdPAccountManager::Data::TestAccount::Manager;

use IdPAccountManager::Tools;
use Conf;
use POSIX qw(strftime);

use Carp;

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

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

    my $self = {};

    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);
    }

    return $self;
}

sub get {
    my ($self, $attribute) = @_;

    ## User password is not stored in DB
    if ($attribute eq 'user_password') {
        return $self->{$attribute};
    } else {
        return $self->{'persistent'}->$attribute;

    }
}

sub save {
    my ($self) = @_;

    ## 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()) {
        return undef;