2017-01-31 23:10:26 +01:00
|
|
|
package Lemonldap::NG::Portal::Lib::Notifications::JSON;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use Mouse;
|
|
|
|
use JSON qw(from_json);
|
|
|
|
|
|
|
|
our $VERSION = '2.0.0';
|
|
|
|
|
|
|
|
no warnings 'redefine';
|
|
|
|
|
|
|
|
# Lemonldap::NG::Portal::Main::Plugin provides addAuthRoute() and
|
|
|
|
# addUnauthRoute() methods in addition of Lemonldap::NG::Common::Module.
|
|
|
|
extends 'Lemonldap::NG::Portal::Main::Plugin';
|
|
|
|
|
2017-01-31 23:31:25 +01:00
|
|
|
# PROPERTIES
|
|
|
|
|
|
|
|
# Underlying notifications storage object (File, DBI, LDAP,...)
|
|
|
|
has notifObject => ( is => 'rw' );
|
|
|
|
|
2017-01-31 23:10:26 +01:00
|
|
|
# INITIALIZATION
|
|
|
|
|
|
|
|
sub init {
|
|
|
|
1;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Search for notifications and if any, returns HTML fragment.
|
|
|
|
sub checkForNotifications {
|
|
|
|
my ( $self, $req ) = @_;
|
|
|
|
|
|
|
|
# Look for pending notifications in database
|
|
|
|
my $uid = $req->sessionInfo->{ $self->notifObject->notifField };
|
|
|
|
my ( $notifs, $forUser ) = $self->notifObject->getNotifications($uid);
|
|
|
|
my $form;
|
|
|
|
return 0 unless ($notifs);
|
|
|
|
|
|
|
|
# Transform notifications
|
|
|
|
my $i = 0; #Files count
|
|
|
|
foreach my $file ( values %$notifs ) {
|
|
|
|
my $json = from_json($file);
|
|
|
|
my $j = 0; #Notifications count
|
|
|
|
my @res;
|
2017-02-02 07:08:40 +01:00
|
|
|
$json = [$json] unless ( ref $json eq 'ARRAY' );
|
2017-01-31 23:10:26 +01:00
|
|
|
LOOP: foreach my $notif ( @{$json} ) {
|
|
|
|
|
|
|
|
# Get the reference
|
|
|
|
my $reference = $notif->{reference};
|
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Get reference $reference");
|
2017-01-31 23:10:26 +01:00
|
|
|
|
|
|
|
# Check it in session
|
|
|
|
if ( exists $req->{sessionInfo}->{"notification_$reference"} ) {
|
|
|
|
|
|
|
|
# The notification was already accepted
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Notification $reference was already accepted");
|
2017-01-31 23:10:26 +01:00
|
|
|
next LOOP;
|
|
|
|
}
|
|
|
|
push @res, $notif;
|
|
|
|
$j++;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Go to next file if no notification found
|
|
|
|
next unless $j;
|
|
|
|
$i++;
|
2017-02-01 06:30:44 +01:00
|
|
|
$form .= $self->toForm(@res);
|
2017-01-31 23:10:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Stop here if nothing to display
|
|
|
|
return 0 unless $i;
|
|
|
|
|
2017-02-01 06:30:44 +01:00
|
|
|
# Cipher cookie
|
2017-01-31 23:10:26 +01:00
|
|
|
$req->id( $self->p->HANDLER->tsv->{cipher}->encrypt( $req->id ) );
|
2017-02-01 06:30:44 +01:00
|
|
|
|
|
|
|
# Returns HTML fragment
|
2017-01-31 23:10:26 +01:00
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub getNotifBack {
|
|
|
|
my ( $self, $req, $name ) = @_;
|
|
|
|
|
|
|
|
# Look if all notifications have been accepted. If not, redirects to
|
|
|
|
# portal
|
|
|
|
|
|
|
|
# Search for Lemonldap::NG cookie (ciphered)
|
|
|
|
my $id;
|
|
|
|
unless ( $id = $req->cookies->{ $self->{conf}->{cookieName} } ) {
|
|
|
|
return $self->p->sendError( $req, 'No cookie found', 401 );
|
|
|
|
}
|
|
|
|
$id = $self->p->HANDLER->tsv->{cipher}->decrypt($id)
|
|
|
|
or return $self->sendError( $req, 'Unable to decrypt', 500 );
|
|
|
|
|
|
|
|
# Verify that session exists
|
|
|
|
$req->userData( $self->p->HANDLER->retrieveSession($id) )
|
|
|
|
or return $self->sendError( $req, 'Unknown session', 401 );
|
|
|
|
|
|
|
|
# Restore datas
|
|
|
|
$self->p->importHandlerDatas($req);
|
|
|
|
my $uid = $req->sessionInfo->{ $self->notifObject->notifField };
|
|
|
|
|
|
|
|
my ( $notifs, $forUser ) = $self->notifObject->getNotifications($uid);
|
|
|
|
if ($notifs) {
|
|
|
|
|
|
|
|
# Get accepted notifications
|
|
|
|
my ( $refs, $checks ) = ( {}, {} );
|
|
|
|
my $prms = $req->parameters;
|
|
|
|
foreach ( keys %$prms ) {
|
|
|
|
my $v = $prms->{$_};
|
|
|
|
if (s/^reference//) {
|
|
|
|
$refs->{$v} = $_;
|
|
|
|
}
|
|
|
|
elsif ( s/^check// and /^(\d+x\d+)x(\d+)$/ and $v eq 'accepted' ) {
|
|
|
|
push @{ $checks->{$1} }, $2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
my $result = 1;
|
|
|
|
foreach my $fileName ( keys %$notifs ) {
|
|
|
|
my $file = $notifs->{$fileName};
|
|
|
|
my $fileResult = 1;
|
|
|
|
my $json = from_json($file);
|
2017-02-02 13:29:59 +01:00
|
|
|
$json = [$json] unless ( ref $json eq 'ARRAY' );
|
2017-01-31 23:10:26 +01:00
|
|
|
|
|
|
|
# Get pending notifications and verify that they have been accepted
|
|
|
|
foreach my $notif (@$json) {
|
|
|
|
my $reference = $notif->{reference};
|
|
|
|
|
|
|
|
# Check if this pending notification has been seen
|
|
|
|
if ( my $refId = $refs->{$reference} ) {
|
|
|
|
|
|
|
|
# Verity that checkboxes have been checked
|
2017-01-31 23:31:25 +01:00
|
|
|
if ( $notif->{check} ) {
|
|
|
|
if ( my $toCheckCount = @{ $notif->{check} } ) {
|
2017-01-31 23:10:26 +01:00
|
|
|
unless ($checks->{$refId}
|
|
|
|
and $toCheckCount == @{ $checks->{$refId} } )
|
|
|
|
{
|
2017-02-15 15:16:59 +01:00
|
|
|
$self->userLogger->notice(
|
2017-01-31 23:10:26 +01:00
|
|
|
"$uid has not accepted notification $reference"
|
|
|
|
);
|
|
|
|
$result = $fileResult = 0;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
# Current pending notification has not been found in
|
|
|
|
# request
|
|
|
|
$result = $fileResult = 0;
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
'Current pending notification has not been found');
|
2017-01-31 23:10:26 +01:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Register acceptation
|
2017-02-15 15:16:59 +01:00
|
|
|
$self->userLogger->notice(
|
2017-01-31 23:10:26 +01:00
|
|
|
"$uid has accepted notification $reference");
|
|
|
|
$self->p->updatePersistentSession( $req,
|
|
|
|
{ "notification_$reference" => time() } );
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"Notification $reference registered in persistent session"
|
2017-01-31 23:10:26 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Notifications accepted for this file, delete it unless it's a wildcard
|
|
|
|
if ( $fileResult and exists $forUser->{$fileName} ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug("Notification file deleted");
|
2017-01-31 23:10:26 +01:00
|
|
|
$self->notifObject->delete($fileName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unless ($result) {
|
|
|
|
|
|
|
|
# One pending notification has been found and not accepted,
|
|
|
|
# restart process to display pending notifications
|
|
|
|
# TODO: is it a good idea to launch all 'afterDatas' subs ?
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
'Pending notification has been found and not accepted');
|
2017-01-31 23:10:26 +01:00
|
|
|
return $self->p->do( $req, $self->p->afterDatas );
|
|
|
|
}
|
|
|
|
|
|
|
|
# All pending notifications have been accepted, restore cookies and
|
|
|
|
# launch 'controlUrl' to restore "urldc" using do()
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug('All pending notifications have been accepted');
|
2017-02-07 23:04:49 +01:00
|
|
|
$self->p->rebuildCookies($req);
|
2017-01-31 23:10:26 +01:00
|
|
|
return $self->p->do( $req, ['controlUrl'] );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
# No notifications checked here, this entry point must not be called.
|
|
|
|
# Redirecting to portal
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug('No notifications checked');
|
2017-01-31 23:10:26 +01:00
|
|
|
$req->mustRedirect(1);
|
|
|
|
return $self->p->do( $req, [] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub toForm {
|
|
|
|
my ( $self, @notifs ) = @_;
|
|
|
|
my $i = 0;
|
|
|
|
@notifs = map {
|
|
|
|
$i++;
|
|
|
|
if ( $_->{check} ) {
|
|
|
|
my $j = 0;
|
|
|
|
$_->{check} =
|
|
|
|
[ map { $j++; { id => '1x' . $i . 'x' . $j, value => $_ } }
|
|
|
|
@{ $_->{check} } ];
|
|
|
|
}
|
|
|
|
$_->{id} = "1x$i";
|
|
|
|
$_;
|
|
|
|
} @notifs;
|
|
|
|
require HTML::Template;
|
|
|
|
my $file =
|
2017-02-01 06:30:44 +01:00
|
|
|
$self->conf->{templateDir} . '/'
|
|
|
|
. $self->conf->{portalSkin}
|
|
|
|
. '/notifinclude.tpl';
|
2017-02-02 07:08:40 +01:00
|
|
|
$file = $self->conf->{templateDir} . '/common/notifinclude.tpl'
|
2017-02-01 06:30:44 +01:00
|
|
|
unless ( -e $file );
|
|
|
|
my $tpl = HTML::Template->new(
|
|
|
|
filename => $file,
|
|
|
|
die_on_bad_params => 0,
|
|
|
|
die_on_missing_include => 1,
|
|
|
|
cache => 1,
|
|
|
|
global_vars => 0,
|
2017-01-31 23:10:26 +01:00
|
|
|
);
|
2017-02-01 06:30:44 +01:00
|
|
|
$tpl->param( notifications => \@notifs );
|
|
|
|
return $tpl->output;
|
2017-01-31 23:10:26 +01:00
|
|
|
}
|
|
|
|
|
2017-02-19 18:04:49 +01:00
|
|
|
sub notificationServer {
|
|
|
|
my ( $self, $req ) = @_;
|
|
|
|
return $self->p->sendError( $req, 'Only JSON requests here', 400 )
|
|
|
|
unless ( $req->wantJSON );
|
|
|
|
my $res = $self->notifObject->newNotification( $req->content );
|
|
|
|
return $self->p->sendError( $req, 'Bad request', 400 ) unless ($res);
|
|
|
|
return $self->p->sendJSONresponse( $req, { result => $res } );
|
|
|
|
}
|
|
|
|
|
2017-01-31 23:10:26 +01:00
|
|
|
1;
|