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

174 lines
4.4 KiB
Perl
Raw Normal View History

2016-03-30 07:47:38 +02:00
## @file
# Demo userDB mechanism
## @class
# Demo userDB mechanism class
package Lemonldap::NG::Portal::UserDB::Demo;
use strict;
2016-04-03 18:27:22 +02:00
use Mouse;
2021-01-03 19:00:20 +01:00
use Lemonldap::NG::Portal::Main::Constants qw(
PE_OK
PE_USERNOTFOUND
PE_BADCREDENTIALS
);
2016-03-30 07:47:38 +02:00
2016-06-02 23:20:36 +02:00
extends 'Lemonldap::NG::Common::Module';
2016-04-03 18:27:22 +02:00
2021-04-01 23:07:58 +02:00
our $VERSION = '2.0.12';
2016-03-30 07:47:38 +02:00
2017-01-29 10:11:27 +01:00
# Sample accounts from Doctor Who characters
2017-03-01 18:35:12 +01:00
our %demoAccounts = (
'dwho' => {
uid => 'dwho',
cn => 'Doctor Who',
mail => 'dwho@badwolf.org',
},
2017-03-01 18:35:12 +01:00
'rtyler' => {
uid => 'rtyler',
cn => 'Rose Tyler',
mail => 'rtyler@badwolf.org',
2017-03-01 18:35:12 +01:00
},
'msmith' => {
uid => 'msmith',
cn => 'Mickey Smith',
mail => 'msmith@badwolf.org',
2017-03-01 18:35:12 +01:00
},
2017-01-29 10:11:27 +01:00
);
2017-01-14 20:31:48 +01:00
2019-12-28 14:08:34 +01:00
our %demoGroups = (
'timelords' => [qw(dwho)],
'earthlings' => [qw(msmith rtyler)],
'users' => [qw(dwho msmith rtyler)],
);
2017-01-14 20:31:48 +01:00
# INITIALIZATION
sub init {
2021-04-01 23:07:58 +02:00
return 1;
2016-03-30 07:47:38 +02:00
}
2017-01-30 22:00:54 +01:00
# RUNNING METHODS
2016-06-09 20:40:20 +02:00
2016-03-30 07:47:38 +02:00
## @apmethod int getUser()
# Check known accounts
# @return Lemonldap::NG::Portal constant
sub getUser {
2018-01-23 22:41:40 +01:00
my ( $self, $req, %args ) = @_;
2016-03-30 07:47:38 +02:00
2018-01-23 22:41:40 +01:00
if ( $args{useMail} ) {
my ($user) = grep { $demoAccounts{$_}->{mail} eq $req->{user} }
keys %demoAccounts;
if ($user) {
$req->{user} = $user;
return PE_OK;
}
2016-03-30 07:47:38 +02:00
}
2018-01-23 22:41:40 +01:00
else {
return PE_OK
if ( defined $demoAccounts{ $req->user } );
}
2016-03-30 07:47:38 +02:00
eval { $self->p->_authentication->setSecurity($req) };
2021-01-03 19:00:20 +01:00
return PE_BADCREDENTIALS;
2016-03-30 07:47:38 +02:00
}
2020-12-20 17:31:50 +01:00
## @apmethod int findUser()
# Search for accounts
# @return Lemonldap::NG::Portal constant
sub findUser {
my ( $self, $req, %args ) = @_;
my $plugin =
$self->p->loadedModules->{"Lemonldap::NG::Portal::Plugins::FindUser"};
2020-12-20 23:44:26 +01:00
my ( $searching, $excluding ) = $plugin->retreiveFindUserParams($req);
2020-12-23 10:32:06 +01:00
eval { $self->p->_authentication->setSecurity($req) };
2020-12-22 15:17:23 +01:00
return PE_OK unless scalar @$searching;
my $iswc;
my $cond = '';
my $wildcard = $self->conf->{findUserWildcard};
$self->logger->info("Demo UserDB with wildcard ($wildcard)") if $wildcard;
foreach (@$searching) {
if ($wildcard) {
2021-01-07 09:54:00 +01:00
$iswc = $_->{value} =~ s/\Q$wildcard\E+//g;
$cond .=
$iswc
? '($' . $_->{key} . " =~ /\Q$_->{value}\E/) && "
: '$' . $_->{key} . " eq '$_->{value}' && ";
}
else {
$cond .= '$' . $_->{key} . " eq '$_->{value}' && ";
}
}
$cond .= '$' . $_->{key} . " ne '$_->{value}' && " foreach (@$excluding);
2020-12-20 17:31:50 +01:00
$cond =~ s/&&\s$//;
$self->logger->debug("Demo UserDB built condition: $cond");
2021-01-02 22:50:56 +01:00
2020-12-20 17:31:50 +01:00
my @results = map {
my $uid = $demoAccounts{$_}->{uid};
2020-12-20 17:31:50 +01:00
my $cn = $demoAccounts{$_}->{cn};
my $mail = $demoAccounts{$_}->{mail};
2022-02-16 17:43:29 +01:00
my $guy = $demoAccounts{$_}->{guy} // 'good';
my $type = $demoAccounts{$_}->{type} // 'character';
2020-12-20 17:31:50 +01:00
eval "($cond)"
? $_
: ();
} keys %demoAccounts;
2020-12-20 23:03:53 +01:00
$self->logger->debug(
'Demo UserDB number of result(s): ' . scalar @results );
2020-12-23 22:38:58 +01:00
if ( scalar @results ) {
2021-01-02 22:50:56 +01:00
my $rank = int( rand( scalar @results ) );
2020-12-23 22:38:58 +01:00
$self->logger->debug("Demo UserDB random rank: $rank");
$self->userLogger->info(
"FindUser: Demo UserDB returns $results[$rank]");
2020-12-27 00:45:06 +01:00
$req->data->{findUser} = $results[$rank];
2021-01-03 19:00:20 +01:00
return PE_OK;
2020-12-23 22:38:58 +01:00
}
2021-01-03 19:00:20 +01:00
return PE_USERNOTFOUND;
2020-12-20 17:31:50 +01:00
}
2016-03-30 07:47:38 +02:00
## @apmethod int setSessionInfo()
# Get sample data
# @return Lemonldap::NG::Portal constant
sub setSessionInfo {
my ( $self, $req ) = @_;
2022-02-16 17:43:29 +01:00
my %vars = (
%{ $self->conf->{exportedVars} },
%{ $self->conf->{demoExportedVars} }
);
2016-03-30 07:47:38 +02:00
while ( my ( $k, $v ) = each %vars ) {
$req->{sessionInfo}->{$k} = $demoAccounts{ $req->{user} }->{$v};
2016-03-30 07:47:38 +02:00
}
2021-01-03 19:00:20 +01:00
return PE_OK;
2016-03-30 07:47:38 +02:00
}
## @apmethod int setGroups()
# Do nothing
# @return Lemonldap::NG::Portal constant
sub setGroups {
2019-12-28 14:08:34 +01:00
my ( $self, $req ) = @_;
2020-05-24 00:04:33 +02:00
my $user = $req->user;
2022-02-16 17:43:29 +01:00
my $groups = $req->sessionInfo->{groups} || '';
2019-12-28 14:08:34 +01:00
my $hGroups = $req->sessionInfo->{hGroups} || {};
for my $grp ( keys %demoGroups ) {
2022-04-30 09:52:50 +02:00
if ( grep { $user && $user eq $_ } @{ $demoGroups{$grp} } ) {
2020-06-06 23:27:37 +02:00
$hGroups->{$grp} = { 'name' => $grp };
2019-12-28 14:08:34 +01:00
$groups =
($groups)
? $groups . $self->conf->{multiValuesSeparator} . $grp
: $grp;
}
}
$req->sessionInfo->{groups} = $groups;
$req->sessionInfo->{hGroups} = $hGroups;
2020-12-27 00:45:06 +01:00
2021-01-03 19:00:20 +01:00
return PE_OK;
2016-03-30 07:47:38 +02:00
}
1;