Notification JSON/File (#868)
This commit is contained in:
parent
be55c43316
commit
dc2080716d
|
@ -13,8 +13,11 @@ our $VERSION = '2.0.0';
|
|||
|
||||
extends 'Lemonldap::NG::Common::Notifications';
|
||||
|
||||
our $ext = 'json';
|
||||
|
||||
sub import {
|
||||
shift;
|
||||
$ext = 'xml' if ( $_[0] eq 'XML' );
|
||||
return Lemonldap::NG::Common::Notifications->import(@_);
|
||||
}
|
||||
|
||||
|
@ -30,12 +33,8 @@ has table => (
|
|||
|
||||
has fileNameSeparator => ( is => 'rw', default => '_' );
|
||||
|
||||
## @method hashref get(string uid,string ref)
|
||||
# Returns notifications corresponding to the user $uid.
|
||||
# If $ref is set, returns only notification corresponding to this reference.
|
||||
# @param $uid UID
|
||||
# @param $ref Notification reference
|
||||
# @return hashref where keys are filenames and values are XML strings
|
||||
sub get {
|
||||
my ( $self, $uid, $ref ) = @_;
|
||||
return () unless ($uid);
|
||||
|
@ -43,7 +42,7 @@ sub get {
|
|||
my $identifier = &getIdentifier( $self, $uid, $ref );
|
||||
|
||||
opendir D, $self->{dirName};
|
||||
my @notif = grep /^\d{8}${fns}${identifier}\S*\.xml$/, readdir(D);
|
||||
my @notif = grep /^\d{8}${fns}${identifier}\S*\.$ext$/, readdir(D);
|
||||
closedir D;
|
||||
|
||||
my $files;
|
||||
|
@ -67,9 +66,9 @@ sub getAll {
|
|||
opendir D, $self->{dirName};
|
||||
my @notif;
|
||||
my $fns = $self->{fileNameSeparator};
|
||||
@notif = grep /^\S*\.xml$/, readdir(D);
|
||||
@notif = grep /^\S*\.$ext$/, readdir(D);
|
||||
my %h = map {
|
||||
/^(\d{8})${fns}([^\s${fns}]+)${fns}([^\s${fns}]+)(?:${fns}([^\s${fns}]+))?\.xml$/
|
||||
/^(\d{8})${fns}([^\s${fns}]+)${fns}([^\s${fns}]+)(?:${fns}([^\s${fns}]+))?\.$ext$/
|
||||
? (
|
||||
$_ => {
|
||||
date => $1,
|
||||
|
@ -88,7 +87,7 @@ sub getAll {
|
|||
# @param $myref identifier returned by get() or getAll()
|
||||
sub delete {
|
||||
my ( $self, $myref ) = @_;
|
||||
my $new = ( $myref =~ /(.*?)(?:\.xml)$/ )[0] . '.done';
|
||||
my $new = ( $myref =~ /(.*?)(?:\.$ext)$/ )[0] . '.done';
|
||||
return rename( $self->{dirName} . "/$myref", $self->{dirName} . "/$new" );
|
||||
}
|
||||
|
||||
|
@ -101,15 +100,9 @@ sub purge {
|
|||
return unlink( $self->{dirName} . "/$myref" );
|
||||
}
|
||||
|
||||
## @method boolean newNotif(string date, string uid, string ref, string xml)
|
||||
# Insert a new notification
|
||||
# @param date Date
|
||||
# @param uid UID
|
||||
# @param ref Reference of the notification
|
||||
# @param xml XML notification
|
||||
# @return true if succeed
|
||||
sub newNotif {
|
||||
my ( $self, $date, $uid, $ref, $condition, $xml ) = @_;
|
||||
my ( $self, $date, $uid, $ref, $condition, $content ) = @_;
|
||||
my $fns = $self->{fileNameSeparator};
|
||||
$date =~ s/-//g;
|
||||
return ( 0, "Bad date" ) unless ( $date =~ /^\d{8}/ );
|
||||
|
@ -118,14 +111,14 @@ sub newNotif {
|
|||
. "/${date}${fns}${uid}${fns}"
|
||||
. encode_base64( $ref, '' );
|
||||
$filename .= "${fns}" . encode_base64( $condition, '' ) if $condition;
|
||||
$filename .= ".xml";
|
||||
$filename .= ".$ext";
|
||||
|
||||
return ( 0, 'This notification still exists' ) if ( -e $filename );
|
||||
my $old = ( $filename =~ /(.*?)(?:\.xml)$/ )[0] . '.done';
|
||||
my $old = ( $filename =~ /(.*?)(?:\.$ext)$/ )[0] . '.done';
|
||||
return ( 0, 'This notification has been done' ) if ( -e $old );
|
||||
open my $F, ">$filename" or return ( 0, "Unable to create $filename ($!)" );
|
||||
binmode($F);
|
||||
print $F $xml;
|
||||
print $F $content;
|
||||
return ( 0, "Unable to close $filename ($!)" ) unless ( close $F );
|
||||
return 1;
|
||||
}
|
||||
|
|
|
@ -61,19 +61,16 @@ sub checkForNotifications {
|
|||
# Go to next file if no notification found
|
||||
next unless $j;
|
||||
$i++;
|
||||
if ( $req->wantJSON ) {
|
||||
$form = \@res;
|
||||
}
|
||||
else {
|
||||
$form .= $self->toForm(@res);
|
||||
}
|
||||
$form .= $self->toForm(@res);
|
||||
}
|
||||
|
||||
# Stop here if nothing to display
|
||||
return 0 unless $i;
|
||||
|
||||
# Returns HTML fragment
|
||||
# Cipher cookie
|
||||
$req->id( $self->p->HANDLER->tsv->{cipher}->encrypt( $req->id ) );
|
||||
|
||||
# Returns HTML fragment
|
||||
return $form;
|
||||
}
|
||||
|
||||
|
@ -215,24 +212,20 @@ sub toForm {
|
|||
} @notifs;
|
||||
require HTML::Template;
|
||||
my $file =
|
||||
( $self->conf->{notificationTplFile}
|
||||
and -e $self->conf->{notificationTplFile} )
|
||||
? $self->conf->{notificationTplFile}
|
||||
: $self->conf->{templatesDir} . '/common/notification.tpl';
|
||||
return join(
|
||||
'',
|
||||
map {
|
||||
my $tpl = HTML::Template->new(
|
||||
filename => $file,
|
||||
die_on_bad_params => 0,
|
||||
die_on_missing_include => 1,
|
||||
cache => 1,
|
||||
global_vars => 1,
|
||||
);
|
||||
$tpl->param(%$_);
|
||||
$tpl->output;
|
||||
} @notifs
|
||||
$self->conf->{templateDir} . '/'
|
||||
. $self->conf->{portalSkin}
|
||||
. '/notifinclude.tpl';
|
||||
$file = $self->conf->{templatesDir} . '/common/notifinclude.tpl'
|
||||
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,
|
||||
);
|
||||
$tpl->param( notifications => \@notifs );
|
||||
return $tpl->output;
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
<TMPL_LOOP NAME="notifications">
|
||||
<input type="hidden" name="reference<TMPL_VAR NAME="id">" value="<TMPL_VAR NAME="reference">">
|
||||
<TMPL_IF NAME="title">
|
||||
<h2 class="notifText">Test title</h2>
|
||||
|
@ -11,3 +12,4 @@
|
|||
<TMPL_LOOP NAME="check">
|
||||
<p class="notifCheck"><label for="check<TMPL_VAR NAME="id">"><input type="checkbox" name="check<TMPL_VAR NAME="id">" id="<TMPL_VAR NAME="id">" value="accepted"><TMPL_VAR NAME="value"></label></p>
|
||||
</TMPL_LOOP>
|
||||
</TMPL_LOOP>
|
|
@ -4,7 +4,7 @@ use IO::String;
|
|||
|
||||
my $res;
|
||||
my $file = 't/notifications.db';
|
||||
my $mainTests = 5;
|
||||
my $mainTests = 6;
|
||||
eval { unlink $file };
|
||||
require 't/test-lib.pm';
|
||||
|
||||
|
@ -65,6 +65,7 @@ q{INSERT INTO notifications VALUES ('dwho','testref','2016-05-30 00:00:00',?,nul
|
|||
);
|
||||
expectOK($res);
|
||||
my $id = expectCookie($res);
|
||||
ok($res->[2]->[0] =~ /1x1x1/,' Found ref');
|
||||
|
||||
# Verify that cookie is ciphered (session unvalid)
|
||||
ok(
|
||||
|
|
108
lemonldap-ng-portal/t/40-Notifications-JSON-File.t
Normal file
108
lemonldap-ng-portal/t/40-Notifications-JSON-File.t
Normal file
|
@ -0,0 +1,108 @@
|
|||
use Test::More;
|
||||
use strict;
|
||||
use IO::String;
|
||||
|
||||
require 't/test-lib.pm';
|
||||
|
||||
my $res;
|
||||
my $file = 't/20160530_dwho_dGVzdHJlZg==.json';
|
||||
|
||||
open F, "> $file" or die($!);
|
||||
print F '[
|
||||
{
|
||||
"uid": "dwho",
|
||||
"date": "2016-05-30",
|
||||
"reference": "testref",
|
||||
"title": "Test title",
|
||||
"subtitle": "Test subtitle",
|
||||
"text": "This is a test text",
|
||||
"check": ["Accept test"]
|
||||
}
|
||||
]';
|
||||
close F;
|
||||
|
||||
my $client = LLNG::Manager::Test->new(
|
||||
{
|
||||
ini => {
|
||||
logLevel => 'error',
|
||||
useSafeJail => 1,
|
||||
notifications => 1,
|
||||
templatesDir => 'site/templates/',
|
||||
notificationStorage => 'File',
|
||||
notificationStorageOptions => {
|
||||
dirName => 't'
|
||||
},
|
||||
oldNotifFormat => 0,
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
# Try yo authenticate
|
||||
# -------------------
|
||||
ok(
|
||||
$res = $client->_post(
|
||||
'/',
|
||||
IO::String->new(
|
||||
'user=dwho&password=dwho&url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw=='),
|
||||
accept => 'text/html',
|
||||
length => 64,
|
||||
),
|
||||
'Auth query'
|
||||
);
|
||||
count(1);
|
||||
expectOK($res);
|
||||
my $id = expectCookie($res);
|
||||
|
||||
# Verify that cookie is ciphered (session unvalid)
|
||||
ok(
|
||||
$res = $client->_get(
|
||||
'/',
|
||||
query => 'url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw==',
|
||||
cookie => "lemonldap=$id",
|
||||
),
|
||||
'Test cookie received'
|
||||
);
|
||||
count(1);
|
||||
expectReject($res);
|
||||
|
||||
# Try to validate notification without accepting it
|
||||
my $str = 'reference1x1=testref&url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw==';
|
||||
ok(
|
||||
$res = $client->_post(
|
||||
'/notifback',
|
||||
IO::String->new($str),
|
||||
cookie => "lemonldap=$id",
|
||||
accept => 'text/html',
|
||||
length => length($str),
|
||||
),
|
||||
"Don't accept notification"
|
||||
);
|
||||
ok( $res->[0] == 200, "Don't receive redirection" )
|
||||
or explain( [ $res->[0], $res->[1] ], 200 );
|
||||
count(2);
|
||||
|
||||
# Try to validate notification
|
||||
$str =
|
||||
'reference1x1=testref&check1x1x1=accepted&url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw==';
|
||||
ok(
|
||||
$res = $client->_post(
|
||||
'/notifback',
|
||||
IO::String->new($str),
|
||||
cookie => "lemonldap=$id",
|
||||
accept => 'text/html',
|
||||
length => length($str),
|
||||
),
|
||||
"Accept notification"
|
||||
);
|
||||
expectRedirection( $res, qr/./ );
|
||||
$file =~ s/json$/done/;
|
||||
ok( -e $file, 'Notification was deleted' );
|
||||
count(2);
|
||||
|
||||
#print STDERR Dumper($res);
|
||||
|
||||
clean_sessions();
|
||||
|
||||
unlink $file;
|
||||
|
||||
done_testing( count() );
|
Loading…
Reference in New Issue
Block a user