« blib/lib/Lemonldap/NG/Portal/2F/Register/TOTP.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/Main/Init.pm »
| 1 | # Notifications plugin. | ||
| 2 | # | ||
| 3 | # Two entry points for notifications: | ||
| 4 | # * a new route "/notifback" for checking accepted notifications | ||
| 5 | # (sub getNotifBack). It launch then autoRedirect() with "mustRedirect" | ||
| 6 | # set to 1 because underlying handler has not seen user as authenticated | ||
| 7 | # so data are not set; | ||
| 8 | # * a callback inserted in process steps after authentication process, | ||
| 9 | # This callback launches checkForNotifications to get notification and | ||
| 10 | # cipher LemonLDAP::NG cookies. | ||
| 11 | |||
| 12 | package Lemonldap::NG::Portal::Plugins::Notifications; | ||
| 13 | |||
| 14 | use strict; | ||
| 15 | use Mouse; | ||
| 16 | use MIME::Base64; | ||
| 17 | use Lemonldap::NG::Portal::Main::Constants qw( | ||
| 18 | PE_OK | ||
| 19 | PE_ERROR | ||
| 20 | PE_NOTIFICATION | ||
| 21 | ); | ||
| 22 | |||
| 23 | our $VERSION = '2.23.0'; | ||
| 24 | |||
| 25 | extends 'Lemonldap::NG::Portal::Main::Plugin'; | ||
| 26 | |||
| 27 | # INTERFACE | ||
| 28 | |||
| 29 | # Declare additional process steps | ||
| 30 | use constant endAuth => 'checkNotifDuringAuth'; | ||
| 31 | |||
| 32 | # For now, notifications are done only during authentication process | ||
| 33 | #sub forAuthUser { 'checkNotifForAuthUser' } | ||
| 34 | |||
| 35 | # PROPERTIES | ||
| 36 | has module => ( is => 'rw' ); | ||
| 37 | |||
| 38 | # INITIALIZATION | ||
| 39 | sub init { | ||
| 40 | my ($self) = @_; | ||
| 41 | |||
| 42 | # Declare new routes | ||
| 43 | $self->addUnauthRoute( notifback => 'getNotifBack', [ 'POST', 'GET' ] ) | ||
| 44 | ->addAuthRoute( notifback => 'getNotifBack', ['POST'] ); | ||
| 45 | $self->addAuthRouteWithRedirect( | ||
| 46 | mynotifications => { '*' => 'myNotifs' }, | ||
| 47 | ['GET'] | ||
| 48 | ) if $self->conf->{notificationsExplorer}; | ||
| 49 | |||
| 50 | if ( $self->conf->{notificationServer} ) { | ||
| 51 | $self->logger->debug('Notification server enable'); | ||
| 52 | |||
| 53 | $self->addUnauthRoute( | ||
| 54 | notifications => 'notificationServer', | ||
| 55 | ['POST'] | ||
| 56 | ) if ( $self->conf->{notificationServerPOST} // 1 ); | ||
| 57 | |||
| 58 | $self->addUnauthRoute( | ||
| 59 | notifications => { '*' => 'notificationServer' }, | ||
| 60 | ['GET'] | ||
| 61 | ) if ( $self->conf->{notificationServerGET} ); | ||
| 62 | |||
| 63 | $self->addUnauthRoute( | ||
| 64 | notifications => | ||
| 65 | { ':uid' => { ':reference' => 'notificationServer' } }, | ||
| 66 | ['DELETE'] | ||
| 67 | ) if ( $self->conf->{notificationServerDELETE} ); | ||
| 68 | } | ||
| 69 | |||
| 70 | # Search for configuration options | ||
| 71 | my $type = $self->conf->{notificationStorage}; | ||
| 72 | unless ($type) { | ||
| 73 | $self->logger->error('notificationStorage is not defined, aborting'); | ||
| 74 | return 0; | ||
| 75 | } | ||
| 76 | |||
| 77 | # Initialize notifications storage object | ||
| 78 | $type = "Lemonldap::NG::Common::Notifications::$type"; | ||
| 79 | eval "require $type"; | ||
| 80 | if ($@) { | ||
| 81 | $self->logger->error( | ||
| 82 | "Unable to load Lemonldap::NG::Common::Notifications::$type: $@"); | ||
| 83 | return 0; | ||
| 84 | } | ||
| 85 | $type->import( $self->conf->{oldNotifFormat} ? 'XML' : 'JSON' ); | ||
| 86 | |||
| 87 | # TODO: use conf database? | ||
| 88 | my $prms = { | ||
| 89 | %{ $self->conf->{notificationStorageOptions} }, | ||
| 90 | p => $self->p, | ||
| 91 | conf => $self->p->conf | ||
| 92 | }; | ||
| 93 | |||
| 94 | if ( $self->conf->{oldNotifFormat} ) { | ||
| 95 | $self->module( $self->p->loadModule('::Lib::Notifications::XML') ) | ||
| 96 | or return 0; | ||
| 97 | } | ||
| 98 | else { | ||
| 99 | $self->module( $self->p->loadModule('::Lib::Notifications::JSON') ) | ||
| 100 | or return 0; | ||
| 101 | } | ||
| 102 | unless ( eval { $self->module->notifObject( $type->new($prms) ); } ) { | ||
| 103 | $self->logger->error($@); | ||
| 104 | return 0; | ||
| 105 | } | ||
| 106 | |||
| 107 | return 1; | ||
| 108 | } | ||
| 109 | |||
| 110 | #sub checkNotifForAuthUser { | ||
| 111 | # my ( $self, $req ) = @_; | ||
| 112 | # if ( my $notif = $self->checkForNotifications($req) ) { | ||
| 113 | # | ||
| 114 | # # Cipher cookies | ||
| 115 | # return PE_NOTIFICATION; | ||
| 116 | # } | ||
| 117 | # else { | ||
| 118 | # return PE_OK; | ||
| 119 | # } | ||
| 120 | #} | ||
| 121 | |||
| 122 | # RUNNING METHODS | ||
| 123 | |||
| 124 | sub checkNotifDuringAuth { | ||
| 125 | my ( $self, $req ) = @_; | ||
| 126 | eval { | ||
| 127 | $req->{data}->{notification} = | ||
| 128 | $self->module->checkForNotifications($req); | ||
| 129 | }; | ||
| 130 | if ($@) { | ||
| 131 | $self->logger->error($@); | ||
| 132 | return PE_ERROR; | ||
| 133 | } | ||
| 134 | if ( $req->{data}->{notification} ) { | ||
| 135 | |||
| 136 | # Cipher id | ||
| 137 | $req->id( $self->p->HANDLER->tsv->{cipher}->encrypt( $req->id ) ); | ||
| 138 | $self->p->rebuildCookies($req); | ||
| 139 | if ( not $req->pdata->{_url} | ||
| 140 | and not $req->data->{_url} | ||
| 141 | and $req->env->{PATH_INFO} ne '/' ) | ||
| 142 | { | ||
| 143 | $req->data->{_url} = | ||
| 144 | encode_base64( $req->portal . $req->env->{PATH_INFO}, '' ); | ||
| 145 | } | ||
| 146 | |||
| 147 | # Restore and cipher cookies | ||
| 148 | return PE_NOTIFICATION; | ||
| 149 | } | ||
| 150 | else { | ||
| 151 | return PE_OK; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | sub getNotifBack { | ||
| 156 | my $self = shift; | ||
| 157 | return $self->module->getNotifBack(@_); | ||
| 158 | } | ||
| 159 | |||
| 160 | sub notificationServer { | ||
| 161 | my ( $self, $req, @args ) = @_; | ||
| 162 | return $self->module->notificationServer( $req, @args ); | ||
| 163 | } | ||
| 164 | |||
| 165 | sub myNotifs { | ||
| 166 | my ( $self, $req, $ref ) = @_; | ||
| 167 | |||
| 168 | $self->p->importHandlerData($req); | ||
| 169 | if ($ref) { | ||
| 170 | return $self->sendJSONresponse( $req, | ||
| 171 | { error => 'Missing epoch parameter' } ) | ||
| 172 | unless $req->param('epoch'); | ||
| 173 | |||
| 174 | # Retrieve notification reference=$ref with epoch | ||
| 175 | my $notif = $self->_viewNotif( $req, $ref, $req->param('epoch') ); | ||
| 176 | $notif =~ s/"checkbox"/"checkbox" checked disabled/g; | ||
| 177 | |||
| 178 | # Return HTML fragment | ||
| 179 | return $self->sendJSONresponse( $req, | ||
| 180 | { notification => $notif, result => ( $notif ? 1 : 0 ) } ); | ||
| 181 | } | ||
| 182 | |||
| 183 | my $_notifications = $self->retrieveNotifs($req); | ||
| 184 | my $nbr = @$_notifications; | ||
| 185 | my $msg = $nbr ? 'myNotification' : 'noNotification'; | ||
| 186 | $msg .= 's' if ( $nbr > 1 ); | ||
| 187 | |||
| 188 | $self->logger->debug("$nbr accepted notification(s) found"); | ||
| 189 | |||
| 190 | # Build template | ||
| 191 | my $params = { | ||
| 192 | NOTIFICATIONS => $_notifications, | ||
| 193 | MSG => $msg | ||
| 194 | }; | ||
| 195 | return $self->sendJSONresponse( $req, { %$params, result => $nbr } ) | ||
| 196 | if ( $req->wantJSON ); | ||
| 197 | |||
| 198 | # Display template | ||
| 199 | return $self->p->sendHtml( $req, 'notifications', params => $params ); | ||
| 200 | } | ||
| 201 | |||
| 202 | sub retrieveNotifs { | ||
| 203 | my ( $self, $req ) = @_; | ||
| 204 | my $user = $req->userData->{ $self->conf->{whatToTrace} }; | ||
| 205 | return [] unless $user; | ||
| 206 | |||
| 207 | # Retrieve user's accepted notifications | ||
| 208 | $self->logger->debug(qq{Searching for "$user" accepted notification(s)}); | ||
| 209 | my @_notifications = sort { | ||
| 210 | $b->{epoch} <=> $a->{epoch} | ||
| 211 | or $a->{reference} cmp $b->{reference} | ||
| 212 | } ( | ||
| 213 | map { | ||
| 214 | /^notification_(.+)$/ | ||
| 215 | ? { reference => $1, epoch => $req->userData->{$_} } | ||
| 216 | : () | ||
| 217 | } | ||
| 218 | keys %{ $req->userData } | ||
| 219 | ); | ||
| 220 | splice @_notifications, $self->conf->{notificationsMaxRetrieve}; | ||
| 221 | |||
| 222 | return \@_notifications; | ||
| 223 | } | ||
| 224 | |||
| 225 | sub _viewNotif { | ||
| 226 | my ( $self, $req, $ref, $epoch ) = @_; | ||
| 227 | |||
| 228 | $self->logger->debug( | ||
| 229 | "Retrieve notification with reference: \"$ref\" and epoch: \"$epoch\""); | ||
| 230 | my $notif = eval { $self->module->viewNotification( $req, $ref, $epoch ); }; | ||
| 231 | if ($@) { | ||
| 232 | $self->logger->debug("Notification not found"); | ||
| 233 | $self->logger->error($@); | ||
| 234 | return ''; | ||
| 235 | } | ||
| 236 | |||
| 237 | return $notif; | ||
| 238 | } | ||
| 239 | |||
| 240 | sub displayLink { | ||
| 241 | my ( $self, $req ) = @_; | ||
| 242 | my $_notifications = $self->retrieveNotifs($req); | ||
| 243 | |||
| 244 | return ( $self->conf->{notificationsExplorer} && scalar @$_notifications ); | ||
| 245 | } | ||
| 246 | |||
| 247 | 1; |