Verify that DBI connection is available before using it (#595)

This commit is contained in:
Xavier Guimard 2016-07-20 07:28:20 +00:00
parent 566f33a40f
commit d34be2d07c
4 changed files with 36 additions and 40 deletions

View File

@ -9,13 +9,6 @@ our $VERSION = '2.0.0';
extends 'Lemonldap::NG::Portal::Auth::_WebForm',
'Lemonldap::NG::Portal::Lib::DBI';
# INITIALIZATION
sub init {
my ($self) = @_;
return $self->dbh;
}
# RUNNING METHODS
sub authenticate {

View File

@ -15,28 +15,36 @@ our $VERSION = '2.0.0';
# PROPERTIES
# dbh object: DB connection object
has dbh => (
# _dbh object: DB connection object
has _dbh => (
is => 'rw',
lazy => 1,
builder => sub {
my $conf = $_[0]->{conf};
my $dbh = eval {
#TODO: pb
DBI->connect_cached(
$conf->{dbiAuthChain}, $conf->{dbiAuthUser},
$conf->{dbiAuthPassword}, { RaiseError => 1 }
);
};
if ($@) {
$_[0]->{p}->lmLog( "DBI connection error: $@", 'error' );
return 0;
}
return $dbh;
}
builder => 'dbh',
);
sub dbh {
my $conf = $_[0]->{conf};
$_[0]->{_dbh} = eval {
DBI->connect_cached(
$conf->{dbiAuthChain}, $conf->{dbiAuthUser},
$conf->{dbiAuthPassword}, { RaiseError => 1 }
);
};
if ($@) {
$_[0]->{p}->lmLog( "DBI connection error: $@", 'error' );
return 0;
}
return $_[0]->{_dbh};
}
# INITIALIZATION
# All DBI modules has just to verify that DBI connection is available
sub init {
my ($self) = @_;
return $self->_dbh;
}
# RUNNING METHODS
# Return hashed password for use in SQL statement
@ -105,7 +113,9 @@ sub check_password {
@rows = $sth->fetchrow_array();
};
if ($@) {
$self->lmLog( "DBI error: $@", 'error' );
# If connection isn't available, error is displayed by dbh()
$self->lmLog( "DBI error: $@", 'error' ) if ( $self->_dbh );
return 0;
}

View File

@ -9,11 +9,6 @@ extends 'Lemonldap::NG::Portal::Password::Base',
our $VERSION = '2.0.0';
sub init {
my ($self) = @_;
return $self->dbh;
}
sub confirm {
my ( $self, $req, $pwd ) = @_;
return $self->check_password( $req->user, $pwd );
@ -31,7 +26,10 @@ sub modifyPassword {
. '=?' )->execute( $pwd, $req->user );
};
if ($@) {
$self->lmLog( "DBI password modification error: $@", 'error' );
# If connection isn't available, error is displayed by dbh()
$self->lmLog( "DBI password modification error: $@", 'error' )
if ( $self->_dbh );
return PE_ERROR;
}
else {

View File

@ -34,13 +34,6 @@ has exportedVars => (
}
);
# INITIALIZATION
sub init {
my $self = shift;
return $self->dbh;
}
# RUNNING METHODS
sub getUser {
@ -54,7 +47,9 @@ sub getUser {
$sth->execute($user);
};
if ($@) {
$self->lmLog( "DBI error: $@", 'error' );
# If connection isn't available, error is displayed by dbh()
$self->lmLog( "DBI error: $@", 'error' ) if ( $self->_dbh );
return PE_ERROR;
}
unless ( $req->datas->{entry} = $sth->fetchrow_hashref() ) {