lemonldap-ng/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Plugins/AutoSignin.pm

61 lines
1.4 KiB
Perl
Raw Normal View History

2017-10-27 07:17:32 +02:00
package Lemonldap::NG::Portal::Plugins::AutoSignin;
2017-10-27 07:17:30 +02:00
use strict;
use Mouse;
use Safe;
use Lemonldap::NG::Portal::Main::Constants qw(
PE_OK
);
our $VERSION = '2.0.0';
extends 'Lemonldap::NG::Portal::Main::Plugin';
# INTERFACE
use constant beforeAuth => 'check';
# INITIALIZATION
has rules => ( is => 'rw', default => sub { [] } );
sub init {
my ($self) = @_;
2017-10-27 15:11:30 +02:00
if ( my $rules = $self->conf->{autoSigninRules} ) {
2017-10-27 07:17:30 +02:00
my $safe = Safe->new;
2017-10-27 15:11:30 +02:00
foreach my $id ( sort keys %$rules ) {
my $sub = $safe->reval('sub{my($env)=@_;return ('.$rules->{$id}.')}');
2017-10-27 07:17:30 +02:00
if ($@) {
2017-10-27 15:11:30 +02:00
$self->error('Bad Autologin rule "'.$rules->{$id}.': $@');
2017-10-27 07:17:30 +02:00
return 0;
}
2017-10-27 15:11:30 +02:00
$id =~ s/^\s*([\w\-\@]+)\s*/$1/;
push @{ $self->rules }, [ $sub, $id ];
2017-10-27 07:17:30 +02:00
}
}
return 1;
}
# RUNNING METHODS
sub check {
my ( $self, $req ) = @_;
foreach ( @{ $self->rules } ) {
my ( $test, $name ) = @$_;
2017-10-27 15:11:30 +02:00
$self->logger->debug("Autosignin: testing user $name");
2017-10-27 07:17:30 +02:00
if ( $test->( $req->env ) ) {
$req->user($name);
my @steps =
grep { !ref $_ and $_ !~ /^(?:extractFormInfo|authenticate)$/ }
@{ $req->steps };
$req->steps( \@steps );
2017-10-27 15:11:30 +02:00
$self->userLogger->notice("Autosignin for $name");
2017-10-27 07:17:30 +02:00
return PE_OK;
}
}
return PE_OK;
}
1;