diff --git a/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Plugins/RESTServer.pm b/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Plugins/RESTServer.pm index d8f03ed8f..9149b55b9 100644 --- a/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Plugins/RESTServer.pm +++ b/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Plugins/RESTServer.pm @@ -29,7 +29,9 @@ # n° # * GET /config// : get conf key value # * GET /config/?full : get the full configuration -# where is the session type ("global" for SSO session) +# where is the session type ("global" for SSO session or "persistent") +# * GET /error// : get message from .json +# Return all error messages if no specified # # - Authorizations for connected users (always): # * GET /mysession/?whoami : get "my" uid @@ -50,7 +52,7 @@ use Mouse; use JSON qw(from_json to_json); use MIME::Base64; -our $VERSION = '2.0.5'; +our $VERSION = '2.0.6'; extends 'Lemonldap::NG::Portal::Main::Plugin'; @@ -124,11 +126,14 @@ sub init { ] }, ['GET'], - ); - $self->addUnauthRoute( + ) + + ->addUnauthRoute( config => { ':cfgNum' => { '*' => 'getKey' } }, ['GET'] - ); + ) + + ->addUnauthRoute( error => { '*' => 'getError' }, ['GET'] ); } if ( $self->conf->{restSessionServer} ) { push @parents, 'Lemonldap::NG::Common::Session::REST'; @@ -144,44 +149,48 @@ sub init { ) }, ['GET'] - ); - $self->addUnauthRoute( + ) + + ->addUnauthRoute( sessions => { ':sessionType' => 'newSession' }, ['POST'] - ); + ) - # Methods written below - $self->addUnauthRoute( + # Methods written below + ->addUnauthRoute( sessions => { ':sessionType' => 'updateSession' }, ['PUT'] - ); - $self->addUnauthRoute( + ) + + ->addUnauthRoute( sessions => { ':sessionType' => 'delSession' }, ['DELETE'] - ); + ) - $self->addAuthRoute( + ->addAuthRoute( session => { my => { ':sessionType' => 'getMyKey' } }, [ 'GET', 'POST' ] - ); + ); } # Methods always available $self->addAuthRoute( mysession => { '*' => 'mysession' }, [ 'GET', 'POST' ] - ); - $self->addAuthRoute( + ) + + ->addAuthRoute( mysession => { ':sessionType' => { ':key' => 'delKeyInMySession', '*' => 'delMySession' } }, ['DELETE'] - ); - $self->addAuthRoute( + ) + + ->addAuthRoute( mysession => { ':sessionType' => 'updateMySession' }, ['PUT'] - ); + ); extends @parents if ($add); $self->setTypes( $self->conf ) if ( $self->conf->{restSessionServer} ); return 1; @@ -523,4 +532,47 @@ sub delKeyInMySession { { result => 1, count => $res, modifiedKeys => $dkey } ); } +sub getError { + my ( $self, $req, $lang, $errNum ) = @_; + my $json; + my $langsDir = + $self->conf->{templateDir} + . '/../htdocs' + . $self->conf->{staticPrefix} + . '/languages'; + $lang ||= 'en'; + + $self->logger->debug("GET error: $errNum message from lang: $lang") + if ($errNum); + $self->logger->debug("GET ALL error messages from lang: $lang"); + + if ( open my $file, "<", $langsDir . "/$lang.json" ) { + local $/ = undef; + $json = from_json(<$file>); + } + else { + $self->logger->error("Unable to read $langsDir/$lang.json"); + return $self->p->sendJSONresponse( $req, + { result => 0, error => "Unable to read $lang.json" } ); + } + + %$json = map { $_ =~ /^PE\d+$/ ? ( $_ => $json->{$_} ) : () } + keys %$json + unless ($errNum); + + return $self->p->sendJSONresponse( + $req, + { + result => 1, + lang => $lang, + errorNum => $errNum ? $errNum : 'all', + errorMsg => ( + $errNum + ? $json->{"PE$errNum"} + : $json + ) + } + ); +} + 1;