Add has2f function (#2391)

This commit is contained in:
Maxime Besson 2020-11-18 00:53:07 +01:00
parent 433d0f1259
commit b18350703d
2 changed files with 34 additions and 1 deletions

View File

@ -9,6 +9,7 @@ use strict;
use Encode;
use MIME::Base64;
use Lemonldap::NG::Common::IPv6;
use JSON::XS;
#use AutoLoader qw(AUTOLOAD);
@ -18,7 +19,7 @@ our $VERSION = '2.0.7';
# Not that only functions, not methods, can be written here
our $functions =
[
qw(&checkLogonHours &date &checkDate &basic &unicode2iso &iso2unicode &groupMatch &isInNet6 &varIsInUri)
qw(&checkLogonHours &date &checkDate &basic &unicode2iso &iso2unicode &groupMatch &isInNet6 &varIsInUri &has2f)
];
## @function boolean checkLogonHours(string logon_hours, string syntax, string time_correction, boolean default_access)
@ -224,4 +225,33 @@ sub varIsInUri {
: $uri =~ /$wanteduri$attribute/o;
}
my $json = JSON::XS->new;
sub has2f {
my ( $session, $type ) = @_;
unless ( $session->{_2fDevices} ) {
return 0;
}
my $_2fDevices;
eval { $_2fDevices = $json->decode( $session->{_2fDevices} ); };
if ( $@ or ref($_2fDevices) ne "ARRAY" ) {
return 0;
}
# Empty array
if ( length @{$_2fDevices} == 0 ) {
return 0;
# Array has one value and we did not specify a type, succeed
}
elsif ( not $type ) {
return 1;
}
else {
my @found = grep { lc( $_->{type} ) eq lc($type) } @{$_2fDevices};
return ( @found ? 1 : 0 );
}
return 0;
}
1;

View File

@ -627,6 +627,9 @@ sub substitute {
# handle inGroup
$expr =~ s/\binGroup\(([^)]*)\)/listMatch(\$s->{'hGroups'},$1,1)/g;
# handle has2f
$expr =~ s/\bhas2f\(([^),]*)\)/has2f(\$s,$1)/g;
return $expr;
}