167 lines
5.0 KiB
Perl
167 lines
5.0 KiB
Perl
package Lemonldap::NG::Portal::Plugins::FindUser;
|
|
|
|
use strict;
|
|
use Mouse;
|
|
use Lemonldap::NG::Portal::Main::Constants qw(
|
|
PE_OK
|
|
PE_NOTOKEN
|
|
PE_TOKENEXPIRED
|
|
PE_FIRSTACCESS
|
|
);
|
|
|
|
our $VERSION = '2.0.11';
|
|
|
|
extends qw(
|
|
Lemonldap::NG::Portal::Main::Plugin
|
|
Lemonldap::NG::Portal::Lib::_tokenRule
|
|
);
|
|
|
|
# INITIALIZATION
|
|
has ott => (
|
|
is => 'rw',
|
|
lazy => 1,
|
|
default => sub {
|
|
my $ott =
|
|
$_[0]->{p}->loadModule('Lemonldap::NG::Portal::Lib::OneTimeToken');
|
|
$ott->timeout( $_[0]->{conf}->{formTimeout} );
|
|
return $ott;
|
|
}
|
|
);
|
|
|
|
sub init {
|
|
my ($self) = @_;
|
|
( my $imp = grep /::Plugins::Impersonation$/, $self->p->enabledPlugins )
|
|
? $self->addUnauthRoute( finduser => 'provideUser', ['POST'] )
|
|
: $self->logger->warn('FindUser plugin enabled without Impersonation');
|
|
$self->logger->warn('FindUser plugin enabled without searching attribute')
|
|
unless keys %{ $self->conf->{findUserSearchingAttributes} };
|
|
|
|
return 1;
|
|
}
|
|
|
|
# RUNNING METHOD
|
|
sub provideUser {
|
|
my ( $self, $req ) = @_;
|
|
my $error;
|
|
|
|
# Check token
|
|
if ( $self->ottRule->( $req, {} ) ) {
|
|
my $token = $req->param('token');
|
|
unless ($token) {
|
|
$self->userLogger->warn('FindUser called without token');
|
|
$error = PE_NOTOKEN;
|
|
}
|
|
else {
|
|
unless ( $self->ott->getToken($token) ) {
|
|
$self->userLogger->warn(
|
|
'FindUser called with an expired/bad token');
|
|
$error = PE_TOKENEXPIRED;
|
|
}
|
|
}
|
|
}
|
|
if ($error) {
|
|
eval { $self->p->_authentication->setSecurity($req) };
|
|
return $self->p->do( $req, [ sub { $error } ] );
|
|
}
|
|
|
|
$req->steps( ['findUser'] );
|
|
$req->data->{findUserChoice} = $self->conf->{authChoiceFindUser};
|
|
if ( $error = $self->p->process($req) ) {
|
|
$self->logger->debug("Process returned error: $error");
|
|
eval { $self->p->_authentication->setSecurity($req) };
|
|
return $self->p->do( $req, [ sub { $error } ] );
|
|
}
|
|
|
|
return $self->sendJSONresponse(
|
|
$req,
|
|
{
|
|
user => ( $req->data->{findUser} ? $req->data->{findUser} : '' ),
|
|
result => 1
|
|
}
|
|
) if $req->wantJSON;
|
|
return $self->p->do( $req, [ sub { PE_FIRSTACCESS } ] );
|
|
}
|
|
|
|
sub retreiveFindUserParams {
|
|
my ( $self, $req ) = @_;
|
|
my ( $searching, $excluding ) = ( [], [] );
|
|
|
|
$self->logger->debug("FindUser: reading parameters...");
|
|
@$searching = map {
|
|
my $key = ( split '#', $_ )[0];
|
|
my $value = $req->params($key) // '';
|
|
if ( $value =~ /$self->{conf}->{findUserControl}/o ) {
|
|
$self->logger->debug("Push searching parameter: $key => $value");
|
|
{ key => $key, value => $value };
|
|
}
|
|
else {
|
|
$self->logger->warn(
|
|
"Parameter $key has been reject by findUserControl");
|
|
();
|
|
}
|
|
} sort keys %{ $self->conf->{findUserSearchingAttributes} };
|
|
|
|
if ( scalar @$searching
|
|
&& keys %{ $self->conf->{findUserExcludingAttributes} } )
|
|
{
|
|
$self->logger->debug("FindUser: reading excluding parameters...");
|
|
@$excluding = map {
|
|
my $key = $_;
|
|
map {
|
|
$self->logger->debug("Push excluding parameter: $key => $_");
|
|
{ # Allow multivalued excluding parameters
|
|
key => $key,
|
|
value => $_
|
|
}
|
|
} split $self->conf->{multiValuesSeparator},
|
|
$self->conf->{findUserExcludingAttributes}->{$_};
|
|
} sort keys %{ $self->conf->{findUserExcludingAttributes} };
|
|
}
|
|
|
|
return ( $searching, $excluding );
|
|
}
|
|
|
|
sub buildForm {
|
|
my $self = shift;
|
|
my $fields = [];
|
|
$self->logger->debug('Building array ref with searching fields...');
|
|
@$fields =
|
|
sort { $a->{select} <=> $b->{select} || $a->{value} cmp $b->{value} }
|
|
map {
|
|
my ( $key, $value, $null ) = split '#', $_;
|
|
my @values = split $self->conf->{multiValuesSeparator},
|
|
$self->conf->{findUserSearchingAttributes}->{$_} || $key;
|
|
my $nbr = scalar @values;
|
|
if ( $nbr > 1 ) {
|
|
if ( $nbr % 2 ) { () }
|
|
else {
|
|
my %hash = @values;
|
|
my $choices = [];
|
|
$nbr /= 2;
|
|
$self->logger->debug(
|
|
"Building $key with type 'select' and $nbr entries...");
|
|
@$choices = map { { key => $_, value => $hash{$_} } }
|
|
sort keys %hash;
|
|
{
|
|
select => 1,
|
|
key => $key,
|
|
null => $null,
|
|
value => $value ? $value : $key,
|
|
choices => $choices
|
|
};
|
|
}
|
|
}
|
|
else {
|
|
{
|
|
select => 0,
|
|
key => $key,
|
|
value => $values[0]
|
|
};
|
|
}
|
|
} sort keys %{ $self->conf->{findUserSearchingAttributes} };
|
|
|
|
return $fields;
|
|
}
|
|
|
|
1;
|