Notification is displayed (#595)

This commit is contained in:
Xavier Guimard 2016-05-31 11:47:08 +00:00
parent 04846adaa5
commit bdace9151f
3 changed files with 94 additions and 23 deletions

View File

@ -95,7 +95,7 @@ sub display {
# 2.1 A notification has to be done (session is created but hidden and unusable
# until the user has accept the message)
elsif ( my $notif = $req->{notification} ) {
elsif ( my $notif = $req->datas->{notification} ) {
$skinfile = 'notification';
%templateParams = (
AUTH_ERROR_TYPE => $req->error_type,

View File

@ -1,15 +1,13 @@
# Notifications plugin.
#
# Three entry points for notifications:
# Two entry points for notifications:
# * 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 datas are not set;
# * two callbacks inserted in process steps:
# - one inserted after authentication process,
# - one for authenticated users,
# These callbacks launch checkForNotifications to get notification and
# cipher LemonLDAP::NG cookies if any found.
# * a callback inserted in process steps after authentication process,
# This callback launches checkForNotifications to get notification and
# cipher LemonLDAP::NG cookies.
package Lemonldap::NG::Portal::Plugins::Notifications;
@ -71,8 +69,6 @@ has notifField => (
# Declare additional process steps
sub afterDatas { 'checkNotifDuringAuth' }
sub forAuthUser { 'checkNotifForAuthUser' }
# Plugin initialization
sub init {
my ($self) = @_;
@ -111,21 +107,26 @@ sub init {
1;
}
# For now, notifications are done only during authentication process
#sub forAuthUser { 'checkNotifForAuthUser' }
#sub checkNotifForAuthUser {
# my ( $self, $req ) = @_;
# if ( my $notif = $self->checkForNotifications($req) ) {
#
# # Cipher cookies
# return PE_NOTIFICATION;
# }
# else {
# return PE_OK;
# }
#}
sub checkNotifDuringAuth {
my ( $self, $req ) = @_;
if ( my $notif = $self->checkForNotifications($req) ) {
# Cipher cookies
return PE_NOTIFICATION;
}
else {
return PE_OK;
}
}
sub checkNotifForAuthUser {
my ( $self, $req ) = @_;
if ( my $notif = $self->checkForNotifications($req) ) {
if ( $req->{datas}->{notification} = $self->checkForNotifications($req) )
{
$self->rebuildCookies($req);
# Restore and cipher cookies
return PE_NOTIFICATION;
@ -215,6 +216,7 @@ sub checkForNotifications {
return 0 unless $i;
# Returns HTML fragment
$req->id( $self->p->HANDLER->tsv->{cipher}->encrypt( $req->id ) );
return $form;
}
@ -231,7 +233,7 @@ sub getNotifBack {
{
return $self->p->sendError( 'No cookie found', 401 );
}
$id = $self->p->conf->decrypt($id)
$id = $self->p->HANDLER->tsv->{cipher}->decrypt($id)
or return $self->sendError( 'Unable to decrypt', 500 );
# Verify that session exists
@ -286,6 +288,7 @@ sub getNotifBack {
# Else uncipher cookie, restore args and launch autoredirect
# TODO
$self->rebuildCookies($req);
$self->p->do( $req, [] );
}
@ -301,4 +304,14 @@ sub getNotifications {
}
}
sub rebuildCookies {
my ( $self, $req ) = @_;
my @tmp;
for ( my $i = 0 ; $i < @{ $req->{respHeaders} } ; $i += 2 ) {
push @tmp, $req->respHeaders->[0], $req->respHeaders->[1]
unless ( $req->respHeaders eq 'Set-Cookie' );
}
$self->p->buildCookie($req);
}
1;

View File

@ -0,0 +1,58 @@
use Test::More;
use strict;
use IO::String;
require 't/test-lib.pm';
my $res;
my $file = 't/20160530_dwho_dGVzdHJlZg==.xml';
open F, "> $file" or die($!);
print F '<?xml version="1.0" encoding="UTF-8"?>
<root><notification uid="dwho" date="2016-05-30" reference="testref"><root>
<title>Test title</title>
<subtitle>Test subtitle</subtitle>
<text>This is a test text</text>
<check>Accept test</check>
</root></notification></root>';
close F;
init(
{
logLevel => 'error',
useSafeJail => 1,
notifications => 1,
templatesDir => 'site/templates/',
notificationStorage => 'File',
notificationStorageOptions => {
dirName => 't'
},
}
);
# Try yo authenticate
# -------------------
ok(
$res = &client->_post(
'/',
IO::String->new(
'user=dwho&password=dwho&url=aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw=='),
accept => 'text/html',
length => 64,
),
'Auth query'
);
ok( $res->[0] == 200, 'Response is 200' ) or explain( $res->[0], 200 );
my $cookies = getCookies($res);
my $id;
ok( $id = $cookies->{lemonldap}, 'Get cookie' )
or explain( $res, 'Set-Cookie: something' );
count(3);
#print STDERR Dumper($res);
clean_sessions();
unlink $file;
done_testing( count() );