Proper new Lemonldap::NG::Common::Notification

This commit is contained in:
Sandro Cazzaniga 2012-07-19 14:17:32 +00:00
parent 8cff113ce5
commit d6ab796359

View File

@ -1,8 +1,8 @@
##@file
# Notification system for Lemonldap::NG portal and manager
# Notification system for Lemonldap::NG
##@class
# Notification system for Lemonldap::NG portal and manager
# Notification system for Lemonldap::NG
package Lemonldap::NG::Common::Notification;
use strict;
@ -21,7 +21,7 @@ our ( $msg, $parser );
## @cmethod Lemonldap::NG::Common::Notification new(hashref storage)
# Constructor.
# @param $storage same syntax as Lemonldap::NG::Common::Conf object
# @return Lemonldap::NG::Common::Notification object
# @return Lemonldap::NG::Portal::Notification object
sub new {
my ( $class, $storage ) = splice @_;
my $self = bless {}, $class;
@ -38,7 +38,7 @@ sub new {
$msg = "Error: unknown storage type $type";
return 0;
}
unless ($self->_prereq ) {
unless ( $self->_prereq ) {
return 0;
}
@ -54,7 +54,7 @@ sub new {
# @param $level Level (debug|info|notice|error)
sub lmLog {
my ( $self, $mess, $level ) = splice @_;
$self->{p}->lmLog( "[Notification] $mess", $level);
$self->{p}->lmLog( "[Notification] $mess", $level );
}
## @method string getNotification(Lemonldap::NG::Portal portal)
@ -64,7 +64,7 @@ sub lmLog {
# @return HTML fragment containing form content
sub getNotification {
my ( $self, $portal ) = splice @_;
my (@files, $form );
my ( @files, $form );
# Get user datas,
my $uid = $portal->{notificationField} || $portal->{whatToTrace} || 'uid';
@ -98,7 +98,7 @@ sub getNotification {
# Prepare HTML code
@files = map { $n->{$_} } sort keys %$n;
my $i = 0;
my $i = 0; # Files count
foreach my $file (@files) {
eval {
my $xml = $parser->parse_string($file);
@ -106,8 +106,7 @@ sub getNotification {
# Browse notifications in file
foreach my $notif (
$xml->documentElement->getElementByTagName('notification')
)
$xml->documentElement->getElementsByTagName('notification') )
{
# Get the reference
@ -162,7 +161,7 @@ sub getNotification {
};
if ($@) {
$self->lmLog(
"Bad XML File: a notification for $uid was not done ($@)",
"Bad XML file: a notification for $uid was not done ($@)",
'warn' );
return 0;
}
@ -199,14 +198,14 @@ sub checkNotification {
my ( $self, $portal ) = splice @_, 0, 2;
my ( $refs, $checks );
# First, rebuild environment (cookies, ...)
# First, rebuild environment (cookies,...)
foreach ( $portal->param() ) {
if (/^cookie/) {
my @tmp = split /(?:=|;\s+)/, $portal->param($_);
my %tmp = @tmp;
my $value = $portal->{cipher}->decrypt( $tmp[1] );
unless ( defined($value) ) {
$self->lmLog( "Unable to decrypt cookie", 'warn');
$self->lmLog( "Unable to decrypt cookie", 'warn' );
return 0;
}
push @{ $portal->{cookie} },
@ -256,20 +255,20 @@ sub checkNotification {
# 3. Join results
my $files = {};
if ( $user and $all ) { $files = { %$user, %$all }; }
else { $files = { $user ? $user : $all; }
else { $files = $user ? $user : $all; }
unless ($files) {
$self->lmLog( "Can't find notification $refs->{$ref} for $uid",
'error');
'error' );
next;
}
# Browse found files
foreach my $files ( keys %$files ) {
foreach my $file ( keys %$files ) {
my $xml;
eval { $xml = $parser->parse_string( $files->{$file} ) };
if ($@) {
$self->lmLog( "Bad XML notifications for $uid", 'error' );
$self->lmLog( "Bad XML notification for $uid", 'error' );
next;
}
@ -290,20 +289,23 @@ sub checkNotification {
$self->lmLog(
"$uid has accepted notification $refs->{$ref}",
'notice');
'notice' );
# 1. Register acceptation in persistent session
my $time = time();
my $notifkey = "notification_" . $refs->{$ref};
$portal->updatePersistentSession(
{ $notifkey => $time },
);
$self->lmLog("Notification " . $refs->{$ref}
$self->lmLog(
"Notification "
. $refs->{$ref}
. " registered in persistent session",
'debug'
);
# 2. Delete it if not a wildcard notification
if ( exists $user->{$file} ) {
@ -311,8 +313,12 @@ sub checkNotification {
$self->lmLog(
"Notification " . $refs->{$ref} . " deleted",
'debug' );
}
else {
$self->lmLog("Unable to delete notification $refs->{$ref} for $uid", 'warn');
$self->lmLog(
"Unable to delete notification $refs->{$ref} for $uid",
'error'
);
}
}
}
@ -457,7 +463,6 @@ sub _newNotif {
1;
__END__
=head1 NAME
@ -468,7 +473,7 @@ Lemonldap::NG::Common::Notification - Provides notification messages system.
=head1 SYNOPSIS
use Lemonldap::NG::Common;
use Lemonldap::NG::Portal;
=head1 DESCRIPTION
@ -477,7 +482,6 @@ Lemonldap::NG::Common::Notification.
=head1 SEE ALSO
L<Lemonldap::NG::Portal>,
L<Lemonldap::NG::Manager>,
=head1 AUTHOR
@ -502,3 +506,5 @@ it under the same terms as Perl itself, either Perl version 5.10.0 or,
at your option, any later version of Perl 5 you may have available.
=cut