lemonldap-ng/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Plugins/FavApps.pm
2019-04-14 22:34:58 +02:00

105 lines
2.5 KiB
Perl

package Lemonldap::NG::Portal::Plugins::FavApps;
use strict;
use Mouse;
use JSON qw(from_json to_json);
use Lemonldap::NG::Portal::Main::Constants qw(
PE_BADCREDENTIALS
PE_TOKENEXPIRED
PE_NOTOKEN
PE_MALFORMEDUSER
);
our $VERSION = '2.1.0';
extends 'Lemonldap::NG::Portal::Main::Plugin';
# INITIALIZATION
sub init {
my $self = shift;
$self->addAuthRoute( favapps => 'register', ['POST'] );
$self->addAuthRoute( favapps => 'read', ['GET'] );
return 1;
}
# RUNNING METHOD
sub register {
my ( $self, $req, $apps ) = @_;
my $user = $req->sessionInfo->{ $self->conf->{whatToTrace} };
# Read existing favorite Apps
$self->logger->debug("Looking for favorite Apps...");
my $_favapps;
if ( $req->sessionInfo->{_favApps} ) {
$_favapps = eval {
from_json( $req->sessionInfo->{_favapps}, { allow_nonref => 1 } );
};
if ($@) {
$self->logger->error("Corrupted session (_2fDevices): $@");
return $self->p->sendError( $req, "Corrupted session", 500 );
}
}
else {
$self->logger->debug("No favorite Apps found");
$_favapps = [];
}
push @$_favapps, $apps;
$self->p->updatePersistentSession( $req,
{ _favapps => to_json($_favapps) } );
$self->userLogger->notice("Apps registration of $apps succeeds for $user");
return [
200,
[
'Content-Type' => 'application/json',
'Content-Length' => 12,
],
['{"result":1}']
];
}
sub read {
my ( $self, $req ) = @_;
my $user = $req->sessionInfo->{ $self->conf->{whatToTrace} };
# Read existing favorite Apps
$self->logger->debug("Looking for favorite Apps...");
my $_favapps;
if ( $req->sessionInfo->{_favApps} ) {
$_favapps = eval {
from_json( $req->sessionInfo->{_favapps}, { allow_nonref => 1 } );
};
if ($@) {
$self->logger->error("Corrupted session (_2fDevices): $@");
return $self->p->sendError( $req, "Corrupted session", 500 );
}
}
else {
$self->logger->debug("No favorite Apps found");
$_favapps = [];
}
# Serialize data
my $data = to_json( {
result => 1, apps => $_favapps
}
);
$self->logger->debug(scalar @$_favapps . " Apps found for $user");
return [
200,
[
'Content-Type' => 'application/json',
'Content-Length' => length($data),
],
[$data]
];
}
1;