| 1 | package Lemonldap::NG::Common::Notifications::XML; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Mouse; | ||
| 5 | use XML::LibXML; | ||
| 6 | |||
| 7 | our $VERSION = '2.0.10'; | ||
| 8 | |||
| 9 | # XML parser | ||
| 10 | has parser => ( | ||
| 11 | is => 'rw', | ||
| 12 | builder => sub { | ||
| 13 | return XML::LibXML->new( load_ext_dtd => 0, expand_entities => 0 ); | ||
| 14 | } | ||
| 15 | ); | ||
| 16 | |||
| 17 | # Check XML data and insert new notifications. | ||
| 18 | # @param $xml XML string containing notification | ||
| 19 | # @return number of notifications done | ||
| 20 | sub newNotification { | ||
| 21 | my ( $self, $xml, $defaultCond ) = @_; | ||
| 22 | $defaultCond ||= ''; | ||
| 23 | eval { $xml = $self->parser->parse_string($xml) }; | ||
| 24 | if ( my $err = $@ ) { | ||
| 25 | eval { $self->logger->error("Unable to read XML file : $err") }; | ||
| 26 | return 0; | ||
| 27 | } | ||
| 28 | |||
| 29 | my @notifs; | ||
| 30 | my ( $version, $encoding ) = ( $xml->version(), $xml->encoding() ); | ||
| 31 | |||
| 32 | foreach | ||
| 33 | my $notif ( $xml->documentElement->getElementsByTagName('notification') ) | ||
| 34 | { | ||
| 35 | my @data = (); | ||
| 36 | $notif->{reference} =~ s/_/-/g; # Remove underscores (#2135) | ||
| 37 | |||
| 38 | # Mandatory information | ||
| 39 | foreach (qw(date uid reference)) { | ||
| 40 | my $tmp; | ||
| 41 | unless ( $tmp = $notif->getAttribute($_) ) { | ||
| 42 | $self->logger->error("Attribute $_ is missing"); | ||
| 43 | return 0; | ||
| 44 | } | ||
| 45 | if ( $self->get( $notif->{uid}, $notif->{reference} ) ) { | ||
| 46 | my $err = "A notification already exists with reference " | ||
| 47 | . $notif->{reference}; | ||
| 48 | $self->logger->error("$err"); | ||
| 49 | return 0; | ||
| 50 | } | ||
| 51 | |||
| 52 | # Prevent to store time. Keep date only | ||
| 53 | $tmp =~ s/^(\d{4}-\d{2}-\d{2}).*$/$1/; | ||
| 54 | push @data, $tmp; | ||
| 55 | } | ||
| 56 | |||
| 57 | # Other information | ||
| 58 | foreach (qw(condition)) { | ||
| 59 | my $tmp; | ||
| 60 | if ( $tmp = $notif->getAttribute($_) ) { | ||
| 61 | push @data, $tmp; | ||
| 62 | } | ||
| 63 | else { | ||
| 64 | $self->logger->info( | ||
| 65 | "Set defaultCondition ($defaultCond) for notification " | ||
| 66 | . $notif->{reference} ); | ||
| 67 | push @data, $defaultCond; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | my $result = XML::LibXML::Document->new( $version, $encoding ); | ||
| 72 | my $root = XML::LibXML::Element->new('root'); | ||
| 73 | $root->appendChild($notif); | ||
| 74 | $result->setDocumentElement($root); | ||
| 75 | $result = $result->serialize; | ||
| 76 | utf8::encode($result); | ||
| 77 | push @notifs, [ @data, $result ]; | ||
| 78 | } | ||
| 79 | my $count; | ||
| 80 | foreach (@notifs) { | ||
| 81 | $count++; | ||
| 82 | my ( $r, $err ) = $self->newNotif(@$_); | ||
| 83 | die "$err" unless ($r); | ||
| 84 | } | ||
| 85 | return $count; | ||
| 86 | } | ||
| 87 | |||
| 88 | ## Delete notifications for the connected user | ||
| 89 | ## @param $uid of the user | ||
| 90 | ## @param $myref notification's reference | ||
| 91 | ## @return number of deleted notifications | ||
| 92 | sub deleteNotification { | ||
| 93 | my ( $self, $uid, $myref ) = @_; | ||
| 94 | my @data; | ||
| 95 | |||
| 96 | # Check input parameters | ||
| 97 | unless ( $uid and $myref ) { | ||
| 98 | $self->userLogger->error( | ||
| 99 | "SOAP service deleteNotification called without all parameters"); | ||
| 100 | return 0; | ||
| 101 | } | ||
| 102 | |||
| 103 | $self->logger->debug( | ||
| 104 | "SOAP service deleteNotification called for uid $uid and reference $myref" | ||
| 105 | ); | ||
| 106 | |||
| 107 | # Get notifications | ||
| 108 | my $user = $self->get($uid); | ||
| 109 | |||
| 110 | # Return 0 if no files were found | ||
| 111 | return 0 unless ($user); | ||
| 112 | |||
| 113 | # Counting | ||
| 114 | my $count = 0; | ||
| 115 | |||
| 116 | foreach my $ref ( keys %$user ) { | ||
| 117 | my $xml = $self->parser->parse_string( $user->{$ref} ); | ||
| 118 | |||
| 119 | # Browse notification in file | ||
| 120 | foreach my $notif ( | ||
| 121 | $xml->documentElement->getElementsByTagName('notification') ) | ||
| 122 | { | ||
| 123 | |||
| 124 | # Get notification's data | ||
| 125 | if ( $notif->getAttribute('reference') eq $myref ) { | ||
| 126 | push @data, $ref; | ||
| 127 | } | ||
| 128 | |||
| 129 | # Delete the notification (really) | ||
| 130 | foreach (@data) { | ||
| 131 | if ( $self->purge( $_, 1 ) ) { | ||
| 132 | $self->logger->debug("Notification $_ was removed."); | ||
| 133 | $count++; | ||
| 134 | } | ||
| 135 | } | ||
| 136 | } | ||
| 137 | } | ||
| 138 | return $count; | ||
| 139 | } | ||
| 140 | |||
| 141 | 1; |