123 lines
3.8 KiB
Perl
123 lines
3.8 KiB
Perl
package Lemonldap::NG::Portal::Plugins::SingleSession;
|
|
|
|
use strict;
|
|
use Mouse;
|
|
use Lemonldap::NG::Portal::Main::Constants qw(PE_OK);
|
|
|
|
our $VERSION = '2.0.8';
|
|
|
|
extends 'Lemonldap::NG::Portal::Main::Plugin',
|
|
'Lemonldap::NG::Portal::Lib::OtherSessions';
|
|
|
|
use constant endAuth => 'run';
|
|
|
|
has singleIPRule => ( is => 'rw' );
|
|
has singleSessionRule => ( is => 'rw' );
|
|
has singleUserByIPRule => ( is => 'rw' );
|
|
|
|
sub init {
|
|
my ($self) = @_;
|
|
|
|
# Build triggering rules from configuration
|
|
$self->singleIPRule(
|
|
$self->p->buildRule( $self->conf->{singleIP}, 'singleIP' ) );
|
|
return 0 unless $self->singleIPRule;
|
|
|
|
$self->singleSessionRule(
|
|
$self->p->buildRule( $self->conf->{singleSession}, 'singleSession' ) );
|
|
return 0 unless $self->singleSessionRule;
|
|
|
|
$self->singleUserByIPRule(
|
|
$self->p->buildRule( $self->conf->{singleUserByIP}, 'singleUserByIP' )
|
|
);
|
|
return 0 unless $self->singleUserByIPRule;
|
|
|
|
return 1;
|
|
}
|
|
|
|
sub run {
|
|
my ( $self, $req ) = @_;
|
|
my $deleted = [];
|
|
my $otherSessions = [];
|
|
my $linkedSessionId = '';
|
|
my $moduleOptions = $self->conf->{globalStorageOptions} || {};
|
|
$moduleOptions->{backend} = $self->conf->{globalStorage};
|
|
|
|
my $sessions = $self->module->searchOn(
|
|
$moduleOptions,
|
|
$self->conf->{whatToTrace},
|
|
$req->{sessionInfo}->{ $self->conf->{whatToTrace} }
|
|
);
|
|
|
|
if ( $self->conf->{securedCookie} == 2 ) {
|
|
$self->logger->debug("Looking for double sessions...");
|
|
$linkedSessionId = $sessions->{ $req->id }->{_httpSession};
|
|
my $msg =
|
|
$linkedSessionId
|
|
? "Linked session found -> $linkedSessionId / " . $req->id
|
|
: "NO linked session found!";
|
|
$self->logger->debug($msg);
|
|
}
|
|
|
|
foreach my $id ( keys %$sessions ) {
|
|
next if ( $req->id eq $id );
|
|
next if ( $linkedSessionId and $id eq $linkedSessionId );
|
|
my $session = $self->p->getApacheSession($id) or next;
|
|
if (
|
|
$self->singleSessionRule->( $req, $req->sessionInfo )
|
|
or ( $self->singleIPRule->( $req, $req->sessionInfo )
|
|
and $req->{sessionInfo}->{ipAddr} ne $session->data->{ipAddr} )
|
|
)
|
|
{
|
|
push @$deleted, $self->p->_sumUpSession( $session->data );
|
|
$self->p->_deleteSession( $req, $session, 1 );
|
|
}
|
|
else {
|
|
push @$otherSessions, $self->p->_sumUpSession( $session->data );
|
|
}
|
|
}
|
|
|
|
if ( $self->singleUserByIPRule->( $req, $req->sessionInfo ) ) {
|
|
my $sessions =
|
|
$self->module->searchOn( $moduleOptions, 'ipAddr',
|
|
$req->sessionInfo->{ipAddr} );
|
|
foreach my $id ( keys %$sessions ) {
|
|
next if ( $req->id eq $id );
|
|
my $session = $self->p->getApacheSession($id) or next;
|
|
unless ( $req->{sessionInfo}->{ $self->conf->{whatToTrace} } eq
|
|
$session->data->{ $self->conf->{whatToTrace} } )
|
|
{
|
|
push @$deleted, $self->p->_sumUpSession( $session->data );
|
|
$self->p->_deleteSession( $req, $session, 1 );
|
|
}
|
|
}
|
|
}
|
|
$req->info(
|
|
$self->p->mkSessionArray( $req, $deleted, 'sessionsDeleted', 1 ) )
|
|
if ( $self->conf->{notifyDeleted} and @$deleted );
|
|
$req->info(
|
|
$self->p->mkSessionArray( $req, $otherSessions, 'otherSessions', 1 )
|
|
. $self->_mkRemoveOtherLink($req) )
|
|
if ( $self->conf->{notifyOther} and @$otherSessions );
|
|
|
|
return PE_OK;
|
|
}
|
|
|
|
# Build the removeOther link
|
|
# Last part of URL is built trough javascript
|
|
# @return removeOther link in HTML code
|
|
sub _mkRemoveOtherLink {
|
|
my ( $self, $req ) = @_;
|
|
|
|
# TODO: remove this
|
|
return $self->loadTemplate(
|
|
$req,
|
|
'removeOther',
|
|
params => {
|
|
link => $self->conf->{portal} . "?removeOther=1"
|
|
}
|
|
);
|
|
}
|
|
|
|
1;
|