Allow input type select with FindUser (#1976)

This commit is contained in:
Christophe Maudoux 2021-01-23 22:58:25 +01:00
parent 8debc0fd1d
commit 7c3955b2aa
4 changed files with 77 additions and 22 deletions

View File

@ -479,20 +479,17 @@ sub display {
# Display authentication form # Display authentication form
else { else {
my $plugin =
$self->loadedModules->{
"Lemonldap::NG::Portal::Plugins::FindUser"};
my $fields = []; my $fields = [];
if ( $self->conf->{findUser} if ( $plugin
&& $self->conf->{findUser}
&& $self->conf->{impersonationRule} && $self->conf->{impersonationRule}
&& $self->conf->{findUserSearchingAttributes} ) && $self->conf->{findUserSearchingAttributes} )
{ {
$login = $req->data->{findUser}; $login = $req->data->{findUser};
$self->logger->debug( $fields = $plugin->buildForm();
'Building array ref with searching fields...');
@$fields = map { {
key => $_,
value =>
$self->conf->{findUserSearchingAttributes}->{$_}
};
} sort keys %{ $self->conf->{findUserSearchingAttributes} };
} }
# Authentication loop # Authentication loop

View File

@ -88,13 +88,15 @@ sub retreiveFindUserParams {
$self->logger->debug("FindUser: reading parameters..."); $self->logger->debug("FindUser: reading parameters...");
@$searching = map { @$searching = map {
my $param = $req->params($_) // ''; my $key = ( split '#', $_ )[0];
if ( $param =~ /$self->{conf}->{findUserControl}/o ) { my $value = $req->params($key) // '';
$self->logger->debug("Push searching parameter: $_ => $param"); if ( $value =~ /$self->{conf}->{findUserControl}/o ) {
{ key => $_, value => $param }; $self->logger->debug("Push searching parameter: $key => $value");
{ key => $key, value => $value };
} }
else { else {
$self->logger->warn("Parameter $_ has been reject by findUserControl"); $self->logger->warn(
"Parameter $key has been reject by findUserControl");
(); ();
} }
} sort keys %{ $self->conf->{findUserSearchingAttributes} }; } sort keys %{ $self->conf->{findUserSearchingAttributes} };
@ -119,4 +121,48 @@ sub retreiveFindUserParams {
return ( $searching, $excluding ); 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}->{$_} || $_;
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 => $self->conf->{findUserSearchingAttributes}->{$_}
? $self->conf->{findUserSearchingAttributes}->{$_}
: $_
};
}
} sort keys %{ $self->conf->{findUserSearchingAttributes} };
return $fields;
}
1; 1;

View File

@ -8,12 +8,24 @@
<input type="hidden" name="token" value="<TMPL_VAR NAME="TOKEN">" /> <input type="hidden" name="token" value="<TMPL_VAR NAME="TOKEN">" />
</TMPL_IF> </TMPL_IF>
<TMPL_LOOP NAME="FIELDS"> <TMPL_LOOP NAME="FIELDS">
<TMPL_IF NAME="select">
<label for="<TMPL_VAR NAME="key">"><TMPL_VAR NAME="value"></label>
<select id="findUser_<TMPL_VAR NAME="key">" name="<TMPL_VAR NAME="key">">
<TMPL_IF NAME="null">
<option value=""></option>
</TMPL_IF>
<TMPL_LOOP NAME="choices">
<option value="<TMPL_VAR NAME="key">"><TMPL_VAR NAME="value"></option>
</TMPL_LOOP>
</select>
<TMPL_ELSE>
<div class="input-group mb-3"> <div class="input-group mb-3">
<div class="input-group-prepend"> <div class="input-group-prepend">
<span class="input-group-text"><label for="<TMPL_VAR NAME="key">" class="mb-0"><i class="fa fa-user"></i></label></span> <span class="input-group-text"><label for="<TMPL_VAR NAME="key">" class="mb-0"><i class="fa fa-user"></i></label></span>
</div> </div>
<input id="findUser_<TMPL_VAR NAME="key">" name="<TMPL_VAR NAME="key">" type="text" autocomplete="off" class="form-control" placeholder="<TMPL_VAR NAME="value">" /> <input id="findUser_<TMPL_VAR NAME="key">" name="<TMPL_VAR NAME="key">" type="text" autocomplete="off" class="form-control" placeholder="<TMPL_VAR NAME="value">" />
</div> </div>
</TMPL_IF>
</TMPL_LOOP> </TMPL_LOOP>
<button type="submit" class="btn btn-info" > <button type="submit" class="btn btn-info" >
<span class="fa fa-eye"></span> <span class="fa fa-eye"></span>

View File

@ -22,7 +22,7 @@ my $client = LLNG::Manager::Test->new( {
findUserControl => '^[\w*\s]+$', findUserControl => '^[\w*\s]+$',
findUserWildcard => '*', findUserWildcard => '*',
findUserSearchingAttributes => findUserSearchingAttributes =>
{ uid => 'Login', guy => 'Kind', cn => 'Name' }, { uid => undef, guy => 'Kind', cn => 'Name' },
findUserExcludingAttributes => findUserExcludingAttributes =>
{ type => 'mutant', uid => 'rtyler' }, { type => 'mutant', uid => 'rtyler' },
} }
@ -78,7 +78,7 @@ m%<input id="findUser_guy" name="guy" type="text" autocomplete="off" class="form
) or explain( $res->[2]->[0], 'id="findUser_guy"' ); ) or explain( $res->[2]->[0], 'id="findUser_guy"' );
ok( ok(
$res->[2]->[0] =~ $res->[2]->[0] =~
m%<input id="findUser_uid" name="uid" type="text" autocomplete="off" class="form-control" placeholder="Login" />%, m%<input id="findUser_uid" name="uid" type="text" autocomplete="off" class="form-control" placeholder="uid" />%,
'id="findUser_uid"' 'id="findUser_uid"'
) or explain( $res->[2]->[0], 'id="findUser_uid"' ); ) or explain( $res->[2]->[0], 'id="findUser_uid"' );
ok( ok(