2016-05-19 07:11:50 +02:00
|
|
|
package Lemonldap::NG::Portal::UserDB::DBI;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use Mouse;
|
2021-01-03 19:00:20 +01:00
|
|
|
use Lemonldap::NG::Portal::Main::Constants qw(
|
|
|
|
PE_OK
|
|
|
|
PE_ERROR
|
|
|
|
PE_USERNOTFOUND
|
|
|
|
PE_BADCREDENTIALS
|
|
|
|
);
|
2016-05-19 07:11:50 +02:00
|
|
|
|
|
|
|
extends 'Lemonldap::NG::Portal::Lib::DBI';
|
|
|
|
|
2020-12-22 15:17:23 +01:00
|
|
|
our $VERSION = '2.0.11';
|
2016-05-19 07:11:50 +02:00
|
|
|
|
2016-06-09 20:40:20 +02:00
|
|
|
# PROPERTIES
|
|
|
|
|
2016-05-19 07:11:50 +02:00
|
|
|
has exportedVars => (
|
|
|
|
is => 'rw',
|
2017-03-27 18:51:18 +02:00
|
|
|
lazy => 1,
|
2016-05-19 07:11:50 +02:00
|
|
|
builder => sub {
|
|
|
|
my $conf = $_[0]->{conf};
|
|
|
|
return { %{ $conf->{exportedVars} }, %{ $conf->{dbiExportedVars} } };
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2016-06-09 20:40:20 +02:00
|
|
|
# RUNNING METHODS
|
|
|
|
|
2016-05-19 07:11:50 +02:00
|
|
|
sub getUser {
|
2018-01-22 21:57:48 +01:00
|
|
|
my ( $self, $req, %args ) = @_;
|
2016-05-19 07:11:50 +02:00
|
|
|
my $table = $self->table;
|
2018-01-22 21:57:48 +01:00
|
|
|
my $pivot = $args{useMail} ? $self->mailField : $self->pivot;
|
2016-05-19 07:11:50 +02:00
|
|
|
my $user = $req->{user};
|
|
|
|
my $sth;
|
|
|
|
eval {
|
2020-12-20 17:31:50 +01:00
|
|
|
$sth = $self->dbh->prepare("SELECT * FROM $table WHERE $pivot=?");
|
|
|
|
$sth->execute($user);
|
|
|
|
};
|
|
|
|
if ($@) {
|
|
|
|
|
|
|
|
# If connection isn't available, error is displayed by dbh()
|
|
|
|
$self->logger->error("DBI error: $@") if ( $self->_dbh );
|
|
|
|
eval { $self->p->_authentication->setSecurity($req) };
|
|
|
|
return PE_ERROR;
|
|
|
|
}
|
|
|
|
unless ( $req->data->{dbientry} = $sth->fetchrow_hashref() ) {
|
|
|
|
$self->userLogger->warn("User $user not found");
|
|
|
|
eval { $self->p->_authentication->setSecurity($req) };
|
|
|
|
return PE_BADCREDENTIALS;
|
|
|
|
}
|
2021-01-03 19:00:20 +01:00
|
|
|
|
|
|
|
return PE_OK;
|
2020-12-20 17:31:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub findUser {
|
|
|
|
my ( $self, $req, %args ) = @_;
|
2020-12-22 15:17:23 +01:00
|
|
|
my $plugin =
|
|
|
|
$self->p->loadedModules->{"Lemonldap::NG::Portal::Plugins::FindUser"};
|
2020-12-23 10:32:06 +01:00
|
|
|
my ( $searching, $excluding ) = $plugin->retreiveFindUserParams($req);
|
|
|
|
eval { $self->p->_authentication->setSecurity($req) };
|
2020-12-22 15:17:23 +01:00
|
|
|
return PE_OK unless scalar @$searching;
|
|
|
|
|
2021-01-05 21:55:16 +01:00
|
|
|
my $table = $self->table;
|
|
|
|
my $pivot = $args{useMail} ? $self->mailField : $self->pivot;
|
2020-12-22 15:17:23 +01:00
|
|
|
my @args;
|
2021-01-05 21:55:16 +01:00
|
|
|
my $request = "SELECT $pivot FROM $table WHERE ";
|
|
|
|
my ( $iswc, $sth );
|
|
|
|
my $wildcard = $self->conf->{findUserWildcard};
|
|
|
|
$self->logger->info("DBI UserDB with wildcard ($wildcard)") if $wildcard;
|
2020-12-22 15:17:23 +01:00
|
|
|
foreach (@$searching) {
|
2021-01-07 09:54:00 +01:00
|
|
|
$iswc = $_->{value} =~ s/\Q$wildcard\E+/%/g if $wildcard;
|
2021-01-05 21:55:16 +01:00
|
|
|
$request .= $iswc ? "$_->{key} LIKE ? AND " : "$_->{key} = ? AND ";
|
2020-12-27 23:22:26 +01:00
|
|
|
push @args, $_->{value};
|
2020-12-22 15:17:23 +01:00
|
|
|
}
|
|
|
|
foreach (@$excluding) {
|
2020-12-27 23:22:26 +01:00
|
|
|
$request .= "$_->{key} != ? AND ";
|
|
|
|
push @args, $_->{value};
|
2020-12-22 15:17:23 +01:00
|
|
|
}
|
|
|
|
$request =~ s/AND\s$//;
|
|
|
|
$self->logger->debug("DBI UserDB built condition: $request");
|
2020-12-22 22:51:37 +01:00
|
|
|
$self->logger->debug( "DBI UserDB built args: " . join '|', @args );
|
2021-01-02 22:50:56 +01:00
|
|
|
|
2020-12-20 17:31:50 +01:00
|
|
|
eval {
|
2020-12-22 22:51:37 +01:00
|
|
|
$sth = $self->dbh->prepare($request);
|
2020-12-22 15:17:23 +01:00
|
|
|
$sth->execute(@args);
|
2016-05-19 07:11:50 +02:00
|
|
|
};
|
|
|
|
if ($@) {
|
2016-07-20 09:28:20 +02:00
|
|
|
|
|
|
|
# If connection isn't available, error is displayed by dbh()
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->error("DBI error: $@") if ( $self->_dbh );
|
2016-05-19 07:11:50 +02:00
|
|
|
return PE_ERROR;
|
|
|
|
}
|
2020-12-23 10:32:06 +01:00
|
|
|
|
2020-12-22 22:51:37 +01:00
|
|
|
my $results = $sth->fetchall_arrayref();
|
|
|
|
if ( $results->[0]->[0] ) {
|
2021-01-02 22:50:56 +01:00
|
|
|
my $rank = int( rand( scalar @$results ) );
|
2020-12-22 15:17:23 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
'DBI UserDB number of result(s): ' . scalar @$results );
|
|
|
|
$self->logger->debug("Demo UserDB random rank: $rank");
|
2020-12-23 10:32:06 +01:00
|
|
|
$self->userLogger->info(
|
|
|
|
"FindUser: DBI UserDB returns $results->[$rank]->[0]");
|
2020-12-27 00:45:06 +01:00
|
|
|
$req->data->{findUser} = $results->[$rank]->[0];
|
2021-01-03 19:00:20 +01:00
|
|
|
return PE_OK;
|
2016-05-19 07:11:50 +02:00
|
|
|
}
|
2020-12-22 15:17:23 +01:00
|
|
|
|
2021-01-03 19:00:20 +01:00
|
|
|
return PE_USERNOTFOUND;
|
2016-05-19 07:11:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub setSessionInfo {
|
|
|
|
my ( $self, $req ) = @_;
|
|
|
|
|
|
|
|
# Set _user unless already defined
|
|
|
|
$req->{sessionInfo}->{_user} ||= $req->user;
|
|
|
|
|
|
|
|
foreach my $var ( keys %{ $self->exportedVars } ) {
|
|
|
|
my $attr = $self->exportedVars->{$var};
|
2019-08-06 21:54:41 +02:00
|
|
|
$req->{sessionInfo}->{$var} = $req->data->{dbientry}->{$attr}
|
|
|
|
if ( defined $req->data->{dbientry}->{$attr} );
|
2016-05-19 07:11:50 +02:00
|
|
|
}
|
2021-01-05 21:55:16 +01:00
|
|
|
|
2021-01-03 19:00:20 +01:00
|
|
|
return PE_OK;
|
2016-05-19 07:11:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub setGroups {
|
2021-01-05 21:55:16 +01:00
|
|
|
|
2021-01-03 19:00:20 +01:00
|
|
|
return PE_OK;
|
2016-05-19 07:11:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|