blib/lib/Lemonldap/NG/Manager/Notifications.pm

filter: Statementstmt 81.1% 172 / 212 Branchbran 62.7% 54 / 86 Conditioncond 38.4% 5 / 13 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 95.2% 20 / 21 Podpod 0.0% 0 / 10 totaltotal 72.0% 252 / 350 CC 56 SCAR 24.9
CC
56
Cov
74%
newNotification
20.9
notifications
20.0
updateNotification
11.4
CRAP 12.1
« blib/lib/Lemonldap/NG/Manager/Conf.pm ↑ Summary blib/lib/Lemonldap/NG/Manager/Api/History.pm »
1 package Lemonldap::NG::Manager::Notifications;
2  
3 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 28 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 28 ▶use JSON qw(from_json to_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 28 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 28 ▶use MIME::Base64 qw(decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 28 ▶use Lemonldap::NG::Common::Conf::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 28 ▶use Lemonldap::NG::Common::PSGI::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 28 ▶use Lemonldap::NG::Common::Conf::ReConstants;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 28 require Lemonldap::NG::Common::Notifications;
14  
15 28 extends qw(
16 Lemonldap::NG::Manager::Plugin
17 Lemonldap::NG::Common::PSGI::Router
18 Lemonldap::NG::Common::Conf::AccessLib
19 );
20  
21 28 our $VERSION = '2.23.0';
22  
23 28 has notifAccess => ( is => 'rw' );
24 28 has notifFormat => ( is => 'rw' );
25  
26 #############################
27 # I. INITIALIZATION METHODS #
28 #############################
29  
30 28 ▶use constant defaultRoute => 'notifications.html';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 28 ▶use constant icon => 'bell';
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 45 ▶sub init {
Subroutine init: called CC=5 SCAR=16.7
Pod undocumented
34 45 my ( $self, $conf ) = @_;
35  
36 45 ▶ if ( $conf->{oldNotifFormat} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{'oldNotifFormat'}) { } 0 45 45
37 0 Lemonldap::NG::Common::Notifications->import('XML');
38 0 $self->notifFormat('XML');
39 }
40 else {
41 45 Lemonldap::NG::Common::Notifications->import('JSON');
42 45 $self->notifFormat('JSON');
43 }
44  
45 45 ▶ unless ( $self->setNotifAccess($conf) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->setNotifAccess($conf)) 3 42 45
46 3 $self->addRoute( 'notifications.html', 'notEnabled', ['GET'] );
47 3 $self->addRoute( notifications => 'notEnabled', ['GET'] );
48 3 ▶ return ( $self->error ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$self->error ? : 0 3 3
49 }
50 42 ▶ $self->{multiValuesSeparator} ||= '; ';
Condition50%1/2
$self->{"multiValuesSeparator"} ||= "; "
l!l
042
MC/DC0%0/1
$self->{"multiValuesSeparator"} ||= "; "
$self->{"multiValuesSeparator"}
Truth table50%1/2
A
A= $self->{"multiValuesSeparator"}
Aresult
11
00
51  
52 # HTML template
53 42 $self->addRoute( 'notifications.html', undef, ['GET'] )
54  
55 # READ
56 ->addRoute(
57 'notifications' =>
58 { actives => 'activeNotifications', done => 'doneNotifications' },
59 ['GET']
60 )
61  
62 # Create new notification
63 ->addRoute(
64 'notifications' => { actives => 'newNotification' },
65 ['POST']
66 )
67  
68 # Update a notification (mark as done)
69 ->addRoute(
70 notifications =>
71 { actives => { ':notificationId' => 'updateNotification' } },
72 ['PUT']
73 )
74  
75 # Delete a notification
76 ->addRoute(
77 notifications =>
78 { done => { ':notificationId' => 'deleteDoneNotification' } },
79 ['DELETE']
80 );
81 42 return 1;
82 }
83  
84 45 ▶sub setNotifAccess {
Subroutine setNotifAccess: called CC=7 SCAR=21.8
Pod undocumented
85 45 my ( $self, $conf ) = @_;
86  
87 # TODO: refresh system
88 $self->{$_} //= $conf->{$_}
89 45 ▶ foreach (
Condition66%2/3
$self->{$_} //= $conf->{$_}
l!l&&r!l&&!r
01791
MC/DC50%1/2
$self->{$_} //= $conf->{$_}
$self->{$_} $conf->{$_}
Truth table66%2/3
A //= B
A= $self->{$_}B= $conf->{$_}
ABresult
1X1
011
000
90 qw/portal notification notificationStorage notificationStorageOptions/);
91  
92 45 ▶ unless ( $self->{notification} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"notification"}) 3 42 45
93 3 return 0;
94 }
95  
96 # TODO: old parameters (with table)
97 42 ▶ unless ( $self->{notificationStorage} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"notificationStorage"}) 0 42 42
98 0 $self->handlerAbort( notifications =>
99 'notificationStorage is not defined in configuration' );
100 0 return 0;
101 }
102 42 my $type =
103 "Lemonldap::NG::Common::Notifications::$self->{notificationStorage}";
104 42 $type =~ s/(?:C|R)DBI$/DBI/;
105 42 eval "require $type";
106 42 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 42 42
107 0 $self->handlerAbort( notifications => "Unable to load $type: $@" );
108 0 return 0;
109 }
110  
111 # Force table name
112 42 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $self->notifAccess($type->new({%{$self->{"notificationStorageOptions"};}, "p", $self, "conf", $self})) } }) 0 42 42
113 eval {
114 $self->notifAccess(
115 $type->new( {
116 42 %{ $self->{notificationStorageOptions} },
117 p => $self,
118 conf => $self
119 }
120 )
121 );
122 }
123 )
124 {
125 0 $self->handlerAbort( notifications => $@ );
126 0 return 0;
127 }
128 42 return $self->notifAccess();
129 }
130  
131 #######################
132 # II. DISPLAY METHODS #
133 #######################
134  
135 0 ▶sub notEnabled {
Subroutine notEnabled: not called CC=1 SCAR=6.9
Pod undocumented
136 0 my ( $self, $req ) = @_;
137 0 return $self->sendError( $req,
138 'Notifications are not enabled in your configuration', 400 );
139 }
140  
141 14 ▶sub activeNotifications {
Subroutine activeNotifications: called CC=1 SCAR=0.0
Pod undocumented
142 14 my ( $self, $req, $notif ) = @_;
143 14 return $self->notifications( $req, $notif, 'actives' );
144 }
145  
146 16 ▶sub doneNotifications {
Subroutine doneNotifications: called CC=1 SCAR=0.0
Pod undocumented
147 16 my ( $self, $req, $notif ) = @_;
148 16 return $self->notifications( $req, $notif, 'done' );
149 }
150  
151 30 ▶sub notifications {
Subroutine notifications: called CC=18 SCAR=30.0
Pod undocumented
152 30 my ( $self, $req, $notif, $type ) = @_;
153 30 ▶ my $sub = { actives => 'getAll', done => 'getDone' }->{$type}
Branch: 1 branch
BranchTrueFalseTotal
my $sub = {"actives", "getAll", "done", "getDone"}->{$type} or die "Unknown type $type" 30 0 30
154 or die "Unknown type $type";
155  
156 # Case 1: a notification is required
157 30 ▶ if ($notif) {
Branch: 1 branch
BranchTrueFalseTotal
if ($notif) 4 26 30
158 4 my $params = $req->parameters();
159 return $self->notification( $req, $notif, $type, $params->{uid},
160 4 $params->{reference} );
161 }
162  
163 # Case 2: list
164 26 my $params = $req->parameters();
165 26 my ( $notifs, $res );
166  
167 26 $notifs = $self->notifAccess->$sub();
168 26 my $total = ( keys %$notifs );
169  
170 # Restrict to wanted values
171 26 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (my(%filters) = map({/^(?:(?:group|order)By)$/ ? () : ($_, $params->{$_});} keys %$params)) 14 12 26
172 my %filters =
173 30 ▶ map { /^(?:(?:group|order)By)$/ ? () : ( $_ => $params->{$_} ); }
Branch: 1 branch
BranchTrueFalseTotal
/^(?:(?:group|order)By)$/ ? : 16 14 30
174 keys %$params
175 )
176 {
177 14 ▶ while ( my ( $field, $value ) = each %filters ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %filters) 14 14 28
178 14 $value =~ s/\*/\.\*/g;
179 14 $value = qr/^$value$/;
180 14 foreach my $k ( keys %$notifs ) {
181 delete $notifs->{$k}
182 unless ( $notifs->{$k}->{$field}
183 17 ▶ && $notifs->{$k}->{$field} =~ $value );
Branch: 1 branch
BranchTrueFalseTotal
unless $notifs->{$k}{$field} and $notifs->{$k}{$field} =~ /$value/ 0 17 17
Condition33%1/3
$notifs->{$k}{$field} and $notifs->{$k}{$field} =~ /$value/
!ll&&!rl&&r
0017
MC/DC0%0/2
$notifs->{$k}{$field} and $notifs->{$k}{$field} =~ /$value/
$notifs->{$k}{$field} $notifs->{$k}{$field} =~ /$value/
Truth table33%1/3
A and B
A= $notifs->{$k}{$field}B= $notifs->{$k}{$field} =~ /$value/
ABresult
0X0
100
111
184 }
185 }
186 }
187  
188 26 ▶ if ( my $groupBy = $req->params('groupBy') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $groupBy = $req->params('groupBy')) { } 16 10 26
189 16 my ( $length, $start, $r ) = ( 0, 0 );
190 16 ▶ if ( $groupBy =~ /^substr\((\w+)(?:,(\d+)(?:,(\d+))?)?\)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($groupBy =~ /^substr\((\w+)(?:,(\d+)(?:,(\d+))?)?\)$/) 8 8 16
191 8 ( $groupBy, $length, $start ) = ( $1, $2, $3 );
192 8 ▶ $start ||= 0;
Condition50%1/2
$start ||= 0
l!l
08
MC/DC0%0/1
$start ||= 0
$start
Truth table50%1/2
A
A= $start
Aresult
11
00
193 8 ▶ $length = 1 if ( $length < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $length < 1 0 8 8
194 }
195 16 foreach my $k ( keys %$notifs ) {
196 my $s =
197 $length
198 ? substr( $notifs->{$k}->{$groupBy}, $start, $length )
199 16 ▶ : $notifs->{$k}->{$groupBy};
Branch: 1 branch
BranchTrueFalseTotal
$length ? : 6 10 16
200 16 $r->{$s}++;
201 }
202 16 my $count = 0;
203 $res = [
204 16 map { $count += $r->{$_}; { value => $_, count => $r->{$_} } }
205 sort keys %$r
206 ];
207 16 return $self->sendJSONresponse(
208 $req,
209 {
210 result => 1,
211 count => $count,
212 values => $res,
213 total => $total
214 }
215 );
216 }
217 else {
218 my @r = map {
219 10 my $r = { notification => $_ };
220 12 foreach my $k (qw(uid date condition)) {
221 36 $r->{$k} = $notifs->{$_}->{$k};
222 }
223 12 $r->{reference} = $notifs->{$_}->{ref};
224 12 $r;
225 } keys %$notifs;
226  
227 10 ▶ if ( my $orderBy = $req->params('orderBy') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $orderBy = $req->params("orderBy")) 0 10 10
228 0 my @fields = split /,/, $orderBy;
229 0 ▶ while ( my $f = pop @fields ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = pop @fields) 0 0 0
230 0 @r = sort { $a->{$f} cmp $b->{$f} } @r;
231 }
232 }
233 10 return $self->sendJSONresponse(
234 $req,
235 {
236 result => 1,
237 count => scalar(@r),
238 values => \@r,
239 total => $total
240 }
241 );
242 }
243 }
244  
245 4 ▶sub notification {
Subroutine notification: called CC=8 SCAR=23.4
Pod undocumented
246 4 my ( $self, $req, $id, $type ) = @_;
247 4 my $backend = $self->{notificationStorage};
248 4 $self->logger->debug("Notification storage backend: $backend");
249 4 $self->logger->debug("Notification id: $id");
250  
251 4 ▶ if ( $type eq 'actives' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'actives') { } 2 2 4
252 2 my ( $uid, $ref ) = ( $id =~ /([^_]+?)_(.+)/ );
253 2 my $n = $self->notifAccess->get( $uid, $ref );
254 2 ▶ unless ($n) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n) 0 2 2
255 0 $self->logger->notice(
256 "Active notification $ref not found for user $uid");
257 0 return $self->sendJSONresponse(
258 $req,
259 {
260 result => 0,
261 error => "Active notification $ref not found for user $uid"
262 }
263 );
264 }
265 2 $self->logger->debug("Active notification $ref found for user $uid");
266 2 return $self->sendJSONresponse( $req,
267 { result => 1, count => 1, notifications => [ values %$n ] } );
268 }
269 else {
270 2 ▶ my ( $date, $uid, $ref ) =
Branch: 1 branch
BranchTrueFalseTotal
$backend eq 'File' ? : 1 1 2
271 $backend eq 'File'
272 ? ( $id =~ /([^_]+?)_(.+?)_(.+?)\.done/ )
273 : ( $id =~ /([^_]+?)_(.+?)_(.+)/ );
274 2 ▶ $ref = decode_base64($ref) if ( $backend eq 'File' );
Branch: 1 branch
BranchTrueFalseTotal
if $backend eq "File" 1 1 2
275 2 my $n = $self->notifAccess->getAccepted( $uid, $ref );
276 2 ▶ unless ($n) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n) 0 2 2
277 0 ▶ my $msg =
Branch: 1 branch
BranchTrueFalseTotal
$ref && $uid ? : 0 0 0
Condition0%0/3
$ref and $uid
!ll&&!rl&&r
000
MC/DC0%0/2
$ref and $uid
$ref $uid
Truth table0%0/3
A and B
A= $refB= $uid
ABresult
0X0
100
111
278 $ref && $uid
279 ? "Done notification $ref not found for user $uid"
280 : 'Done notification not found';
281 0 $self->logger->notice($msg);
282 0 return $self->sendJSONresponse(
283 $req,
284 {
285 result => 0,
286 error => $msg
287 }
288 );
289 }
290 2 $self->logger->debug("Done notification $ref found for user $uid");
291 2 return $self->sendJSONresponse(
292 $req,
293 {
294 result => 1,
295 count => 1,
296 done => $id,
297 notifications => [ values %$n ]
298 }
299 );
300 }
301 }
302  
303 9 ▶sub newNotification {
Subroutine newNotification: called CC=13 SCAR=30.4
Pod undocumented
304 9 my ( $self, $req, @other ) = @_;
305 9 ▶ return $self->sendError( $req,
Branch: 1 branch
BranchTrueFalseTotal
if @other 0 9 9
306 'There is no subkey for "newNotification"', 200 )
307 if (@other);
308  
309 9 my $json = $req->jsonBodyToObj;
310 9 ▶ unless ( defined($json) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $json) 0 9 9
311 0 return $self->sendError( $req, undef, 200 );
312 }
313  
314 9 $json->{reference} =~ s/_/-/g; # Remove underscores (#2135)
315  
316 9 foreach my $r (qw(uid reference xml)) {
317 return $self->sendError( $req, "Missing $r", 200 )
318 27 ▶ unless ( $json->{$r} );
Branch: 1 branch
BranchTrueFalseTotal
unless $json->{$r} 0 27 27
319 }
320  
321 # Set default date value
322 9 my $dDate = strftime( "%Y-%m-%d", localtime() );
323 9 ▶ if ( $json->{date} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json->{'date'}) { } 9 0 9
324 9 $self->logger->debug(
325 "Posted data : uid = $json->{uid} - Ref = $json->{reference} - Date = $json->{date}"
326 );
327 }
328 else {
329 0 $self->logger->debug(
330 "Posted data : uid = $json->{uid} - Ref = $json->{reference} - Date = ???"
331 );
332 0 $json->{date} = $dDate;
333 }
334  
335 # Check if posted date > today
336 9 ▶ unless ( $json->{date} ge $dDate ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json->{"date"} ge $dDate) 0 9 9
337 0 $self->logger->debug("Posted Date < today");
338 0 $json->{date} = $dDate;
339 }
340 9 $self->logger->debug("Notification Date = $json->{date}");
341  
342 9 ▶ unless ( $json->{date} =~ /^\d{4}-\d{2}-\d{2}$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json->{"date"} =~ /^\d{4}-\d{2}-\d{2}$/) 2 7 9
343 2 $self->logger->error("Malformed date");
344 2 return $self->sendError( $req, "Malformed date", 200 );
345 }
346  
347 7 my $newNotif;
348 7 ▶ if ( $self->notifFormat eq 'XML' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->notifFormat eq 'XML') { } 0 7 7
349 0 utf8::decode( $json->{xml} );
350 $newNotif = qq#<?xml version='1.0' encoding='UTF-8' standalone='no'?>
351 <root><notification #
352 . join(
353 ' ',
354 map {
355 0 ▶ if ( my $t = $json->{$_} ) { $t =~ s/"/'/g; qq#$_="$t"# }
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $json->{$_}) { } 0 0 0
356 0 else { () }
357 } (qw(uid date reference condition))
358 ) . ">$json->{xml}</notification></root>";
359 }
360 else {
361 7 eval {
362 7 my $tmp = from_json( $json->{xml}, { allow_nonref => 1 } );
363 7 $json->{$_} = $tmp->{$_} foreach ( keys %$tmp );
364 7 delete $json->{xml};
365 };
366 7 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 7 7
367 0 $self->logger->error("Malformed notification $@");
368 0 return $self->sendError( $req, "Malformed notification: $@", 200 );
369 }
370 7 $newNotif = to_json($json);
371 }
372  
373 7 ▶ unless ( eval { $self->notifAccess->newNotification($newNotif) } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $self->notifAccess->newNotification($newNotif) } }) { } 3 4 7
374 4 $self->logger->error("Notification not created: $@");
375 4 return $self->sendError( $req, "Notification not created: $@", 200 );
376 }
377 else {
378 3 return $self->sendJSONresponse( $req, { result => 1 } );
379 }
380 }
381  
382 2 ▶sub updateNotification {
Subroutine updateNotification: called CC=8 SCAR=24.3
Pod undocumented
383 2 my ( $self, $req ) = @_;
384  
385 2 my $json = $req->jsonBodyToObj;
386 2 ▶ unless ( defined($json) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $json) 0 2 2
387 0 return $self->sendError( $req, undef, 200 );
388 }
389  
390 # For now, only "mark as done" is proposed
391 2 ▶ unless ( $json->{done} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json->{"done"}) 0 2 2
392 0 return $self->sendError( $req, 'Only "done=1" is accepted for now',
393 200 );
394 }
395 2 ▶ my $id = $req->params('notificationId') or die;
Branch: 1 branch
BranchTrueFalseTotal
my $id = $req->params("notificationId") or die 2 0 2
396 2 my ( $uid, $ref ) = ( $id =~ /([^_]+?)_(.+)/ );
397 2 my ( $n, $res );
398 2 ▶ unless ( $n = $self->notifAccess->get( $uid, $ref ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n = $self->notifAccess->get($uid, $ref)) 0 2 2
399 0 $self->logger->notice("Notification $ref not found for user $uid");
400 0 return $self->sendError( $req,
401 "Notification $ref not found for user $uid" );
402 }
403  
404 # Delete notifications
405 2 my $status = 1;
406 2 foreach ( keys %$n ) {
407 2 ▶ $status = 0 unless ( $self->notifAccess->delete($_) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->notifAccess->delete($_) 0 2 2
408 }
409  
410 2 ▶ unless ($status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status) { } 2 0 2
411 0 $self->logger->error("Notification $ref for user $uid not deleted");
412 0 return $self->sendError( $req,
413 "Notification $ref for user $uid not deleted" );
414 }
415  
416 else {
417 2 $self->logger->info("Notification $ref deleted for user $uid");
418 2 return $self->sendJSONresponse( $req, { result => 1 } );
419 }
420 }
421  
422 2 ▶sub deleteDoneNotification {
Subroutine deleteDoneNotification: called CC=3 SCAR=11.5
Pod undocumented
423 2 my ( $self, $req ) = @_;
424 2 my $res;
425  
426 # Purge notification
427 2 ▶ my $id = $req->params('notificationId') or die;
Branch: 1 branch
BranchTrueFalseTotal
my $id = $req->params("notificationId") or die 2 0 2
428 2 my ( $uid, $ref, $date ) = ( $id =~ /([^_]+?)_([^_]+?)_(.+)/ );
429 2 my $identifier = $self->notifAccess->getIdentifier( $uid, $ref, $date );
430 2 ▶ unless ( eval { $self->notifAccess->purge($identifier) } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $self->notifAccess->purge($identifier) } }) 0 2 2
431 0 $self->logger->warn("Notification $identifier not purged ($@)");
432 0 return $self->sendError( $req,
433 "Notification $identifier not purged ($@)", 400 );
434 }
435  
436 2 $self->logger->info("Notification $identifier purged");
437 2 return $self->sendJSONresponse( $req, { result => 1 } );
438 }
439  
440 28 1;
« blib/lib/Lemonldap/NG/Manager/Conf.pm ↑ Summary blib/lib/Lemonldap/NG/Manager/Api/History.pm »