« blib/lib/Lemonldap/NG/Portal/Main/Request.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/Main/Plugin.pm »
| 1 | package Lemonldap::NG::Portal::Plugins::AutoSignin; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Mouse; | ||
| 5 | use Safe; | ||
| 6 | use Lemonldap::NG::Portal::Main::Constants qw( | ||
| 7 | PE_OK | ||
| 8 | ); | ||
| 9 | |||
| 10 | our $VERSION = '2.0.8'; | ||
| 11 | |||
| 12 | extends 'Lemonldap::NG::Portal::Main::Plugin'; | ||
| 13 | |||
| 14 | # INTERFACE | ||
| 15 | |||
| 16 | use constant beforeAuth => 'check'; | ||
| 17 | |||
| 18 | # INITIALIZATION | ||
| 19 | |||
| 20 | has rules => ( | ||
| 21 | is => 'rw', | ||
| 22 | isa => 'ArrayRef', | ||
| 23 | default => sub { [] } | ||
| 24 | ); | ||
| 25 | |||
| 26 | sub init { | ||
| 27 | my ($self) = @_; | ||
| 28 | if ( my $rules = $self->conf->{autoSigninRules} ) { | ||
| 29 | my $safe = Safe->new; | ||
| 30 | foreach my $id ( sort keys %$rules ) { | ||
| 31 | my $sub = | ||
| 32 | $safe->reval( | ||
| 33 | 'sub{my($env)=@_;local *_;return (' . $rules->{$id} . ')}' ); | ||
| 34 | if ($@) { | ||
| 35 | $self->logger->error( | ||
| 36 | 'Bad Autologin rule "' . $rules->{$id} . ": $@" ); | ||
| 37 | $self->logger->debug( | ||
| 38 | "Skipping Autologin rule for user \"$id\""); | ||
| 39 | next; | ||
| 40 | } | ||
| 41 | $self->logger->debug("Autologin rule for user \"$id\" appended"); | ||
| 42 | $id =~ s/^\s*([\w\-\@]+)\s*/$1/; | ||
| 43 | push @{ $self->rules }, [ $sub, $id ]; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | return 1; | ||
| 48 | } | ||
| 49 | |||
| 50 | # RUNNING METHODS | ||
| 51 | |||
| 52 | sub check { | ||
| 53 | my ( $self, $req ) = @_; | ||
| 54 | |||
| 55 | foreach ( @{ $self->rules } ) { | ||
| 56 | my ( $test, $name ) = @$_; | ||
| 57 | $self->logger->debug("Autosignin: testing user $name"); | ||
| 58 | if ( $test->( $req->env ) ) { | ||
| 59 | $req->user($name); | ||
| 60 | my @steps = | ||
| 61 | grep { | ||
| 62 | !ref $_ | ||
| 63 | and $_ !~ /^(?:extractFormInfo|authenticate|secondFactor)$/ | ||
| 64 | } @{ $req->steps }; | ||
| 65 | $req->steps( \@steps ); | ||
| 66 | $self->userLogger->notice("Autosignin for $name"); | ||
| 67 | return PE_OK; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | return PE_OK; | ||
| 71 | } | ||
| 72 | |||
| 73 | 1; |