2016-05-29 09:48:06 +02:00
|
|
|
# Notifications plugin.
|
|
|
|
#
|
2016-05-31 13:47:08 +02:00
|
|
|
# Two entry points for notifications:
|
2016-05-29 09:48:06 +02:00
|
|
|
# * a new route "/notifback" for checking accepted notifications
|
|
|
|
# (sub getNotifBack). It launch then autoRedirect() with "mustRedirect"
|
|
|
|
# set to 1 because underlying handler has not seen user as authenticated
|
2018-07-05 22:56:16 +02:00
|
|
|
# so data are not set;
|
2016-05-31 13:47:08 +02:00
|
|
|
# * a callback inserted in process steps after authentication process,
|
|
|
|
# This callback launches checkForNotifications to get notification and
|
|
|
|
# cipher LemonLDAP::NG cookies.
|
2016-05-29 09:48:06 +02:00
|
|
|
|
2016-05-28 10:33:39 +02:00
|
|
|
package Lemonldap::NG::Portal::Plugins::Notifications;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use Mouse;
|
2017-10-19 16:59:19 +02:00
|
|
|
use Lemonldap::NG::Portal::Main::Constants qw(
|
|
|
|
PE_ERROR
|
|
|
|
PE_NOTIFICATION
|
|
|
|
PE_OK
|
|
|
|
);
|
2016-05-28 10:33:39 +02:00
|
|
|
|
|
|
|
our $VERSION = '2.0.0';
|
|
|
|
|
|
|
|
extends 'Lemonldap::NG::Portal::Main::Plugin';
|
|
|
|
|
2016-06-09 20:40:20 +02:00
|
|
|
# INTERFACE
|
|
|
|
|
2016-05-29 09:48:06 +02:00
|
|
|
# Declare additional process steps
|
2018-09-05 09:19:01 +02:00
|
|
|
use constant endAuth => 'checkNotifDuringAuth';
|
2016-05-28 10:33:39 +02:00
|
|
|
|
2016-06-09 20:40:20 +02:00
|
|
|
# For now, notifications are done only during authentication process
|
|
|
|
#sub forAuthUser { 'checkNotifForAuthUser' }
|
|
|
|
|
2017-01-31 20:11:59 +01:00
|
|
|
# PROPERTIES
|
|
|
|
|
|
|
|
has module => ( is => 'rw' );
|
|
|
|
|
2016-06-09 20:40:20 +02:00
|
|
|
# INITIALIZATION
|
|
|
|
|
2016-05-28 10:33:39 +02:00
|
|
|
sub init {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
2016-05-29 09:48:06 +02:00
|
|
|
# Declare new route
|
|
|
|
$self->addUnauthRoute( 'notifback' => 'getNotifBack', ['POST'] );
|
|
|
|
$self->addAuthRoute( 'notifback' => 'getNotifBack', ['POST'] );
|
|
|
|
|
2017-02-19 18:04:49 +01:00
|
|
|
if ( $self->conf->{notificationServer} ) {
|
|
|
|
$self->logger->debug('Notification server enable');
|
|
|
|
$self->addUnauthRoute(
|
|
|
|
'notifications' => 'notificationServer',
|
|
|
|
['POST']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-05-29 09:48:06 +02:00
|
|
|
# Search for configuration options
|
2016-05-28 10:33:39 +02:00
|
|
|
my $type = $self->conf->{notificationStorage};
|
|
|
|
unless ($type) {
|
|
|
|
$self->error('notificationStorage is not defined, aborting');
|
|
|
|
return 0;
|
|
|
|
}
|
2016-05-29 09:48:06 +02:00
|
|
|
|
|
|
|
# Initialize notifications storage object
|
2016-06-02 23:20:36 +02:00
|
|
|
$type = "Lemonldap::NG::Common::Notifications::$type";
|
2016-05-29 09:48:06 +02:00
|
|
|
eval "require $type";
|
2016-05-28 10:33:39 +02:00
|
|
|
if ($@) {
|
2016-06-02 23:20:36 +02:00
|
|
|
$self->error(
|
|
|
|
"Unable to load Lemonldap::NG::Common::Notifications::$type: $@");
|
2016-05-28 10:33:39 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2017-01-31 20:11:59 +01:00
|
|
|
$type->import( $self->conf->{oldNotifFormat} ? 'XML' : 'JSON' );
|
2016-05-28 10:33:39 +02:00
|
|
|
|
|
|
|
# TODO: use conf database?
|
|
|
|
|
2016-06-02 23:20:36 +02:00
|
|
|
my $prms = {
|
|
|
|
%{ $self->conf->{notificationStorageOptions} },
|
|
|
|
p => $self->p,
|
|
|
|
conf => $self->p->conf
|
|
|
|
};
|
2017-01-31 20:11:59 +01:00
|
|
|
|
|
|
|
if ( $self->conf->{oldNotifFormat} ) {
|
|
|
|
$self->module( $self->p->loadModule('::Lib::Notifications::XML') )
|
|
|
|
or return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$self->module( $self->p->loadModule('::Lib::Notifications::JSON') )
|
|
|
|
or return 0;
|
|
|
|
}
|
|
|
|
unless ( eval { $self->module->notifObject( $type->new($prms) ); } ) {
|
2016-05-28 10:33:39 +02:00
|
|
|
$self->error($@);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
1;
|
|
|
|
}
|
|
|
|
|
2016-05-31 13:47:08 +02:00
|
|
|
#sub checkNotifForAuthUser {
|
|
|
|
# my ( $self, $req ) = @_;
|
|
|
|
# if ( my $notif = $self->checkForNotifications($req) ) {
|
|
|
|
#
|
|
|
|
# # Cipher cookies
|
|
|
|
# return PE_NOTIFICATION;
|
|
|
|
# }
|
|
|
|
# else {
|
|
|
|
# return PE_OK;
|
|
|
|
# }
|
|
|
|
#}
|
2016-05-29 09:48:06 +02:00
|
|
|
|
2016-06-09 20:40:20 +02:00
|
|
|
# RUNNING METHODS
|
|
|
|
|
2016-05-31 13:47:08 +02:00
|
|
|
sub checkNotifDuringAuth {
|
2016-05-29 09:48:06 +02:00
|
|
|
my ( $self, $req ) = @_;
|
2017-10-19 16:59:19 +02:00
|
|
|
eval {
|
2018-07-05 22:56:16 +02:00
|
|
|
$req->{data}->{notification} =
|
2017-10-19 16:59:19 +02:00
|
|
|
$self->module->checkForNotifications($req);
|
|
|
|
};
|
|
|
|
if ($@) {
|
|
|
|
$self->logger->error($@);
|
|
|
|
return PE_ERROR;
|
|
|
|
}
|
2018-07-05 22:56:16 +02:00
|
|
|
if ( $req->{data}->{notification} ) {
|
2018-09-05 13:35:50 +02:00
|
|
|
|
|
|
|
# Cipher id
|
|
|
|
$req->id( $self->p->HANDLER->tsv->{cipher}->encrypt( $req->id ) );
|
2017-02-07 23:04:49 +01:00
|
|
|
$self->p->rebuildCookies($req);
|
2016-05-29 09:48:06 +02:00
|
|
|
|
|
|
|
# Restore and cipher cookies
|
2016-05-30 22:20:53 +02:00
|
|
|
return PE_NOTIFICATION;
|
2016-05-29 09:48:06 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return PE_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-28 10:33:39 +02:00
|
|
|
sub getNotifBack {
|
2017-01-31 20:11:59 +01:00
|
|
|
my $self = shift;
|
|
|
|
return $self->module->getNotifBack(@_);
|
2016-05-30 13:32:21 +02:00
|
|
|
}
|
|
|
|
|
2017-02-19 18:04:49 +01:00
|
|
|
sub notificationServer {
|
|
|
|
my ( $self, $req ) = @_;
|
|
|
|
return $self->module->notificationServer($req);
|
|
|
|
}
|
|
|
|
|
2016-05-30 13:32:21 +02:00
|
|
|
1;
|