lemonldap-ng/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/UserDB/DBI.pm

130 lines
3.6 KiB
Perl
Raw Normal View History

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',
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 {
my ( $self, $req, %args ) = @_;
2016-05-19 07:11:50 +02:00
my $table = $self->table;
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;
my $table = $self->table;
my $pivot = $args{useMail} ? $self->mailField : $self->pivot;
2020-12-22 15:17:23 +01:00
my @args;
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;
$request .= $iswc ? "$_->{key} LIKE ? AND " : "$_->{key} = ? AND ";
push @args, $_->{value};
2020-12-22 15:17:23 +01:00
}
foreach (@$excluding) {
$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");
$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 {
$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 ($@) {
# 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
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};
$req->{sessionInfo}->{$var} = $req->data->{dbientry}->{$attr}
if ( defined $req->data->{dbientry}->{$attr} );
2016-05-19 07:11:50 +02:00
}
2021-01-03 19:00:20 +01:00
return PE_OK;
2016-05-19 07:11:50 +02:00
}
sub setGroups {
2021-01-03 19:00:20 +01:00
return PE_OK;
2016-05-19 07:11:50 +02:00
}
1;