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

159 lines
4.0 KiB
Perl
Raw Normal View History

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
# 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;
use MIME::Base64;
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.6';
2016-05-28 10:33:39 +02:00
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
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' }
# 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', 'GET' ] );
2016-05-29 09:48:06 +02:00
$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(
2019-07-22 15:39:59 +02:00
notifications => 'notificationServer',
['POST']
);
$self->addUnauthRoute(
notifications => { '*' => 'notificationServer' },
['GET']
);
$self->addUnauthRoute(
notifications =>
{ ':uid' => { ':reference' => 'notificationServer' } },
['DELETE']
2017-02-19 18:04:49 +01:00
);
}
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;
}
$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
};
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 ) = @_;
eval {
$req->{data}->{notification} =
$self->module->checkForNotifications($req);
};
if ($@) {
$self->logger->error($@);
return PE_ERROR;
}
if ( $req->{data}->{notification} ) {
2018-09-05 13:35:50 +02:00
# Cipher id
$req->id( $self->p->HANDLER->tsv->{cipher}->encrypt( $req->id ) );
$self->p->rebuildCookies($req);
if ( not $req->data->{_url} and $req->env->{PATH_INFO} ne '/' ) {
$req->data->{_url} =
encode_base64( $self->conf->{portal} . $req->env->{PATH_INFO},
'' );
}
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 {
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 {
2019-07-22 15:39:59 +02:00
my ( $self, $req, @args ) = @_;
return $self->module->notificationServer($req, @args);
2017-02-19 18:04:49 +01:00
}
2016-05-30 13:32:21 +02:00
1;