From bdace9151fb5e166062414710048ffe95b728545 Mon Sep 17 00:00:00 2001 From: Xavier Guimard Date: Tue, 31 May 2016 11:47:08 +0000 Subject: [PATCH] Notification is displayed (#595) --- .../lib/Lemonldap/NG/Portal/Main/Display.pm | 2 +- .../NG/Portal/Plugins/Notifications.pm | 57 +++++++++++------- lemonldap-ng-portal/t/10-Notifications.t | 58 +++++++++++++++++++ 3 files changed, 94 insertions(+), 23 deletions(-) create mode 100644 lemonldap-ng-portal/t/10-Notifications.t diff --git a/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Main/Display.pm b/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Main/Display.pm index f10f0f6b9..38eee3e0a 100644 --- a/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Main/Display.pm +++ b/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Main/Display.pm @@ -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, diff --git a/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Plugins/Notifications.pm b/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Plugins/Notifications.pm index b60fda7d7..df3c5f777 100644 --- a/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Plugins/Notifications.pm +++ b/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Plugins/Notifications.pm @@ -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; diff --git a/lemonldap-ng-portal/t/10-Notifications.t b/lemonldap-ng-portal/t/10-Notifications.t new file mode 100644 index 000000000..ea547eb68 --- /dev/null +++ b/lemonldap-ng-portal/t/10-Notifications.t @@ -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 ' + +Test title +Test subtitle +This is a test text +Accept test +'; +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() );