Working on Choice (#595)

This commit is contained in:
Xavier Guimard 2016-06-29 19:34:36 +00:00
parent daa100ca32
commit 5820e5bf36
3 changed files with 134 additions and 4 deletions

View File

@ -0,0 +1,128 @@
package Lemonldap::NG::Portal::Lib::Choice;
use strict;
use Mouse;
extends 'Lemonldap::NG::Portal::Lib::Wrapper';
our $VERSION = '2.0.0';
has modules => ( is => 'rw', default => sub { {} } );
# INITIALIZATION
# init() must be called by module::init() with a number:
# - 0 for auth
# - 1 for userDB
# - 2 for passwordDB ?
sub init {
my ( $self, $type ) = @_;
foreach my $name ( keys %{ $self->conf->{authChoiceModules} } ) {
my @mods =
split( /[;\|]/, $self->conf->{authChoiceModules}->{$name} );
my $module =
'::' . [ 'Auth', 'UserDB', 'Password' ]->[$type] . '/' . $mods[$type];
if ( $module = $self->loadPlugin($module) ) {
$self->modules->{$name} = $module;
$self->p->lmLog(
[qw(Authentication User Password)]->[$type]
. " module $name selected",
'debug'
);
}
else {
# error() is set by underlying module
return 0;
}
}
return $self;
}
sub checkChoice {
my ( $self, $req ) = @_;
# TODO: find choice or return 0
# then set $req->datas->{_choice} to "$name"
}
package Lemonldap::NG::Portal::Simple;
## @method private Lemonldap::NG::Portal::_Choice _buildAuthLoop()
# Build authentication loop displayed in template
# @return authLoop rarray reference
sub _buildAuthLoop {
my ( $self, $req ) = @_;
my @authLoop;
# Test authentication choices
unless ( ref $self->conf->{authChoiceModules} eq 'HASH' ) {
$self->lmLog( "No authentication choices defined", 'warn' );
return [];
}
foreach ( sort keys %{ $self->conf->{authChoiceModules} } ) {
my $name = $_;
# Ignore 'forcedSAML'
next if $name eq 'forcedSAML';
# Name can have a digit as first character
# for sorting purpose
# Remove it in displayed name
$name =~ s/^(\d*)?(\s*)?//;
# Replace also _ by space for a nice display
$name =~ s/\_/ /g;
# Find modules associated to authChoice
my ( $auth, $userDB, $passwordDB, $url ) =
split( /[;\|]/, $self->conf->{authChoiceModules}->{$_} );
if ( $auth and $userDB and $passwordDB ) {
# Default URL
$url = ( defined $url ? $url .= $ENV{'REQUEST_URI'} : '#' );
$self->lmLog( "Use URL $url", 'debug' );
# Options to store in the loop
my $optionsLoop =
{ name => $name, key => $_, module => $auth, url => $url };
# Get displayType for this module
my $displayType =
&{"Lemonldap::NG::Portal::Auth::${auth}::getDisplayType"};
$self->lmLog( "Display type $displayType for module $auth",
'debug' );
$optionsLoop->{$displayType} = 1;
# If displayType is logo, check if key.png is available
if (
-e $self->getApacheHtdocsPath . "/skins/common/" . $_ . ".png" )
{
$optionsLoop->{logoFile} = $_ . ".png";
}
else { $optionsLoop->{logoFile} = $auth . ".png"; }
# Register item in loop
push @authLoop, $optionsLoop;
$self->lmLog( "Authentication choice $name will be displayed",
'debug' );
}
else {
$req->error("Authentication choice $_ value is invalid");
return 0;
}
}
return \@authLoop;
}
1;

View File

@ -2,7 +2,7 @@
#
# It fakes portal object to catch entry points and load them only if underlying
# auth module is activated
package Lemonldap::NG::Portal::Auth::Wrapper;
package Lemonldap::NG::Portal::Lib::Wrapper;
use strict;
use Mouse;
@ -46,10 +46,12 @@ sub _wrapEntryPoint {
return PE_OK;
}
# loadModule() fakes portal loadModule()
# loadPlugin() fakes portal loadPlugin: it loads module but does not load
# entry points since they will be launched by ^ methods
sub loadPlugin {
my ( $self, $name, $module ) = @_;
my $obj = $self->p->can('loadPlugin')->( $self, $module );
my $obj = $self->p->can('loadModule')->( $self, $module );
( $obj and $obj->init ) or return 0;
return $obj;
}

View File

@ -8,7 +8,7 @@ my $res;
init(
{
logLevel => 'debug',
logLevel => 'error',
useSafeJail => 1,
issuerDBGetActivation => 1,
issuerDBGetPath => '^/test/',