113 lines
2.9 KiB
Perl
113 lines
2.9 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 ) = @_;
|
|
my $user = $req->userData->{ $self->conf->{whatToTrace} };
|
|
my $app = $req->param('app');
|
|
my $logo = $req->param('logo') || '';
|
|
my $desc = $req->param('desc') || '';
|
|
my $title = $req->param('title') || '';
|
|
|
|
# TODO -> test app
|
|
$self->logger->debug("Favorite application received : $app");
|
|
|
|
# Read existing favorite Apps
|
|
$self->logger->debug("Looking for $user favorite Apps...");
|
|
my $_favApps;
|
|
$self->logger->debug("*****" . Data::Dumper::Dumper($req));
|
|
if ( $req->userData->{_favApps} ) {
|
|
$_favApps = eval {
|
|
from_json( $req->userData->{_favApps}, { allow_nonref => 1 } );
|
|
};
|
|
if ($@) {
|
|
$self->logger->error("Corrupted session (_favApps): $@");
|
|
return $self->p->sendError( $req, "Corrupted session", 500 );
|
|
}
|
|
}
|
|
else {
|
|
$self->logger->debug("No favorite Apps found for $user");
|
|
$_favApps = [];
|
|
}
|
|
|
|
push @$_favApps, { app => $app, logo => $logo, desc => $desc, title => $title };
|
|
$self->p->updatePersistentSession( $req,
|
|
{ _favApps => to_json($_favApps) } );
|
|
$self->userLogger->notice("Favorite apps registration of $app 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;
|