« blib/lib/Lemonldap/NG/Common/Conf/Backends/Patroni.pm
↑ Summary
blib/lib/Lemonldap/NG/Common/Notifications/File.pm »
| 1 | package Lemonldap::NG::Common::Notifications; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Mouse; | ||
| 5 | use JSON qw(to_json); | ||
| 6 | |||
| 7 | our $VERSION = '2.21.0'; | ||
| 8 | |||
| 9 | extends 'Lemonldap::NG::Common::Module'; | ||
| 10 | |||
| 11 | sub import { | ||
| 12 | if ( $_[1] eq 'XML' ) { | ||
| 13 | extends 'Lemonldap::NG::Common::Notifications::XML', | ||
| 14 | 'Lemonldap::NG::Common::Module'; | ||
| 15 | } | ||
| 16 | else { | ||
| 17 | extends 'Lemonldap::NG::Common::Notifications::JSON', | ||
| 18 | 'Lemonldap::NG::Common::Module'; | ||
| 19 | } | ||
| 20 | } | ||
| 21 | |||
| 22 | has extension => ( | ||
| 23 | is => 'rw', | ||
| 24 | default => 'json' | ||
| 25 | ); | ||
| 26 | |||
| 27 | has notifField => ( | ||
| 28 | is => 'rw', | ||
| 29 | builder => sub { | ||
| 30 | my $uid = | ||
| 31 | $_[0]->conf->{notificationField} | ||
| 32 | || $_[0]->conf->{whatToTrace} | ||
| 33 | || 'uid'; | ||
| 34 | $uid =~ s/^\$//; | ||
| 35 | return $uid; | ||
| 36 | } | ||
| 37 | ); | ||
| 38 | |||
| 39 | sub BUILD { | ||
| 40 | my $self = shift; | ||
| 41 | $self->extension('xml') if $self->conf->{oldNotifFormat}; | ||
| 42 | $self->logger->debug( 'Use extension "' | ||
| 43 | . $self->extension | ||
| 44 | . '" to store notification files' ); | ||
| 45 | } | ||
| 46 | |||
| 47 | sub getNotifications { | ||
| 48 | my ( $self, $uid ) = @_; | ||
| 49 | my $forAll = $self->get( $self->conf->{notificationWildcard} ); | ||
| 50 | if ( $uid and $uid =~ /^_all(Pending|Existing)_$/ ) { | ||
| 51 | $self->logger->info("Retrieve all $1 notifications"); | ||
| 52 | my $all = ( $1 eq 'Pending' ? $self->getAll() : $self->getExisting() ); | ||
| 53 | $all = { map { $_ => to_json( $all->{$_} ) } keys %$all }; | ||
| 54 | return ( $forAll ? { %$all, %$forAll } : $all ); | ||
| 55 | } | ||
| 56 | my $forUser = $self->get($uid); | ||
| 57 | if ( $forUser and $forAll ) { | ||
| 58 | return ( { %$forUser, %$forAll }, $forUser ); | ||
| 59 | } | ||
| 60 | else { | ||
| 61 | return ( ( $forUser ? $forUser : $forAll ), $forUser ); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | sub getAcceptedNotifs { | ||
| 66 | my ( $self, $uid, $ref ) = @_; | ||
| 67 | my $forAll = | ||
| 68 | $self->getAccepted( $self->conf->{notificationWildcard}, $ref ); | ||
| 69 | my $forUser = $self->getAccepted( $uid, $ref ); | ||
| 70 | if ( $forUser and $forAll ) { | ||
| 71 | return { %$forUser, %$forAll }; | ||
| 72 | } | ||
| 73 | else { | ||
| 74 | return ( ( $forUser ? $forUser : $forAll ), $forUser ); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | 1; |