Commit 9f49d0b1 authored by Xavier Guimard's avatar Xavier Guimard

REST in progress (#970)

parent 92244ef4
......@@ -9,6 +9,16 @@ our $VERSION = '2.0.0';
extends 'Lemonldap::NG::Common::Module';
sub sendError {
my $self = shift;
return $self->p->sendError(@_);
}
sub sendJSONresponse {
my $self = shift;
return $self->p->sendJSONresponse(@_);
}
sub addAuthRoute {
my $self = shift;
return $self->_addRoute( 'addAuthRoute', @_ );
......
......@@ -40,10 +40,8 @@ sub enabledPlugins {
if ( $self->conf->{soapSessionServer}
or $self->conf->{soapConfigServer} );
# Check if REST is enabled
push @res, '::Plugins::RESTServer'
if ( $self->conf->{restSessionServer}
or $self->conf->{soapSessionServer} );
# Add REST (check is done by it)
push @res, '::Plugins::RESTServer';
# Check if notification is enabled
push @res, '::Plugins::Notifications' if ( $self->conf->{notifications} );
......
......@@ -76,15 +76,16 @@ sub init {
# Methods written below
$self->addUnauthRoute(
sessions =>
{ ':sessionType' => { ':sessionId' => 'updateSession' } },
sessions => { ':sessionType' => 'updateSession' },
['PUT']
);
$self->addUnauthRoute(
sessions => { ':sessionType' => { ':sessionId' => 'delSession' } },
sessions => { ':sessionType' => 'delSession' },
['DELETE']
);
}
# Methods always available
$self->addAuthRoute(
mysession => { '*' => 'mysession' },
[ 'GET', 'POST' ]
......@@ -97,12 +98,63 @@ sub init {
return 1;
}
sub updateSession {
sub newSession {
my ( $self, $req ) = @_;
my $mod = $self->getMod($req)
or return $self->p->sendError( $req, undef, 400 );
my $infos = $req->jsonBodyToObj
or return $self->p->sendError( $req, undef, 400 );
$infos->{_utime} = time();
my $session = $self->getApacheSession($mod);
return $self->p->sendError( $req, 'Unable to create session', 500 )
unless ($session);
$session->update($infos);
$self->lmLog(
'REST request to create a session for '
. $session->data->{ $self->conf->{whatToTrace} },
'debug'
);
return $self->p->sendJSONresponse( $req, { result => 1 } );
}
sub updateSession {
my ( $self, $req, $id ) = @_;
my $mod = $self->getMod($req)
or return $self->p->sendError( $req, undef, 400 );
return $self->p->sendError( $req, 'ID is required', 400 ) unless ($id);
# Get session
my $session = $self->getApacheSession( $mod, $id )
or return $self->p->sendError( $req, 'Session id does not exists', 400 );
# Get new info
my $infos = $req->jsonBodyToObj
or return $self->p->sendError( $req, undef, 400 );
# Store them
$self->lmLog( "REST request to update session $id", 'debug' );
$session->update($infos);
return $self->p->sendJSONresponse( $req, { result => 1 } );
}
sub delSession {
my ( $self, $req ) = @_;
my ( $self, $req, $id ) = @_;
my $mod = $self->getMod($req)
or return $self->p->sendError( $req, undef, 400 );
return $self->p->sendError( $req, 'ID is required', 400 ) unless ($id);
# Get session
my $session = $self->getApacheSession( $mod, $id )
or return $self->p->sendError( $req, 'Session id does not exists', 400 );
# Delete it
$self->lmLog( "REST request to delete session $id", 'debug' );
my $res = $self->p->_deleteSession($session);
$self->lmLog( " Result is $res", 'debug' );
return $self->p->sendJSONresponse( $req, { result => $res } );
}
sub mysession {
......@@ -130,7 +182,7 @@ sub mysession {
# Now check for authorization
my $res = $self->p->HANDLER->grant( $req->userDatas, $uri, undef, $host );
$self->lmLog( "Result is $res", 'debug' );
$self->lmLog( " Result is $res", 'debug' );
return $self->p->sendJSONresponse( $req, { result => $res } );
}
......@@ -141,4 +193,4 @@ sub getMyKey {
return $self->session( $req, $req->id, $key );
}
1;
1
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment