Try.pm 4.92 KB
Newer Older
1 2 3 4 5
package Lemonldap::NG::Handler::PSGI::Try;

use strict;
use Mouse;

Yadd's avatar
Yadd committed
6 7
our $VERSION = '2.0.0';

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
extends 'Lemonldap::NG::Handler::PSGI::Router';

has 'authRoutes' => (
    is      => 'rw',
    isa     => 'HashRef',
    default => sub { { GET => {}, POST => {}, PUT => {}, DELETE => {} } }
);

has 'unAuthRoutes' => (
    is      => 'rw',
    isa     => 'HashRef',
    default => sub { { GET => {}, POST => {}, PUT => {}, DELETE => {} } }
);

sub addRoute {
    die;
}

sub addAuthRoute {
    my $self = shift;
    $self->routes( $self->authRoutes );
Yadd's avatar
Yadd committed
29
    $self->logger->debug('Declaring auth route');
30 31 32 33 34 35
    return $self->SUPER::addRoute(@_);
}

sub addUnauthRoute {
    my $self = shift;
    $self->routes( $self->unAuthRoutes );
Yadd's avatar
Yadd committed
36
    $self->logger->debug('Declaring unauth route');
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    return $self->SUPER::addRoute(@_);
}

sub defaultAuthRoute {
    my $self = shift;
    $self->routes( $self->authRoutes );
    return $self->SUPER::defaultRoute(@_);
}

sub defaultUnauthRoute {
    my $self = shift;
    $self->routes( $self->unAuthRoutes );
    return $self->SUPER::defaultRoute(@_);
}

sub _run {
    my $self = shift;
    $self->rule(1);

    return sub {
        my $req = Lemonldap::NG::Common::PSGI::Request->new( $_[0] );
Yadd's avatar
Yadd committed
58 59
        my $res = $self->_authAndTrace( $req, 1 );
        if ( $res->[0] < 300 ) {
60
            $self->routes( $self->authRoutes );
Yadd's avatar
Yadd committed
61
            $req->userData( $self->api->data );
62 63
        }
        else {
Yadd's avatar
Yadd committed
64
            # Unset headers (handler adds a Location header)
Yadd's avatar
Yadd committed
65 66
            $self->logger->debug(
                "User not authenticated, Try in use, cancel redirection");
Yadd's avatar
Yadd committed
67
            $req->userData( {} );
Yadd's avatar
Yadd committed
68
            $req->respHeaders( [] );
69 70
            $self->routes( $self->unAuthRoutes );
        }
71 72 73 74 75 76 77 78 79 80 81
        $res = $self->handler($req);

        # Insert respHeaders in response only if not already set
        my %hdr1 = @{ $res->[1] };
        my %hdr2 = @{ $req->{respHeaders} };
        foreach ( keys %hdr2 ) {
            unless ( $hdr1{$_} and $hdr2{$_} eq $hdr1{$_} ) {
                push @{ $res->[1] }, ( $_ => $hdr2{$_} );
            }
        }
        return $res;
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    };
}

1;
__END__

=head1 NAME

=encoding utf8

Lemonldap::NG::Handler::PSGI::Try - Special handler for Lemonldap::NG Portal

=head1 SYNOPSIS

  package My::PSGI;
  
  use base Lemonldap::NG::Handler::PSGI::Try;
  
  sub init {
    my ($self,$args) = @_;
    
    # Declare REST routes for authenticated users (could be HTML templates or
    # methods)
    $self->addAuthRoute ( 'index.html', undef, ['GET'] )
         ->addAuthRoute ( books => { ':book' => 'booksMethod' }, ['GET', 'POST'] );
  
    # Default route (ie: PATH_INFO == '/')
    $self->defaultAuthRoute('index.html');
  
    # Same for unauthenticated users
    $self->addUnauthRoute ( 'login.html', undef, ['GET'] )
         ->addUnauthRoute ( 'login', undef, ['POST'] );
    $self->defaultUnauthRoute('login.html');
   
    # Return a boolean. If false, then error message has to be stored in
    # $self->error
    return 1;
  }
  
  sub booksMethod {
    my ( $self, $req, @otherPathInfo ) = @_;

    # Will be called only if authorisated
    my $userId = $self->userId;
    my $book = $req->params('book');
    my $method = $req->method;
    ...
    $self->sendJSONresponse(...);
  }

=head1 DESCRIPTION

Lemonldap::NG::Handler::PSGI::Try is a L<Lemonldap::NG::Handler::PSGI::Router>
package that maintain 2 REST routers: one for authenticated users and one for
unauthenticated users.

=head1 METHODS

Same as L<Lemonldap::NG::Handler::PSGI::Router> (inherits from
L<Lemonldap::NG::Common::PSGI::Router>) except that:

=over

=item addRoute() must be replaced by addAuthRoute() or addUnauthRoute()

=item defaultRoute() must be replaced by defaultAuthRoute() or defaultUnauthRoute()

=back

Yadd's avatar
Yadd committed
151
Note also that user session data are available in $req parameter (first argument
152 153 154 155
received by REST methods):

=over

Yadd's avatar
Yadd committed
156
=item $req->userData() returns a hash reference containing user session data
157 158 159 160 161

=back

=head1 SEE ALSO

Yadd's avatar
Yadd committed
162
See L<Lemonldap::NG::Common::PSGI::Router> for more.
163 164 165 166 167

=head1 AUTHORS

=over

Yadd's avatar
Yadd committed
168
=item LemonLDAP::NG team L<http://lemonldap-ng.org/team>
169 170 171 172 173 174

=back

=head1 BUG REPORT

Use OW2 system to report bug or ask for features:
Yadd's avatar
Yadd committed
175
L<https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues>
176 177 178 179 180 181 182 183

=head1 DOWNLOAD

Lemonldap::NG is available at
L<http://forge.objectweb.org/project/showfiles.php?group_id=274>

=head1 COPYRIGHT AND LICENSE

Yadd's avatar
Yadd committed
184
See COPYING file for details.
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199

This library is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see L<http://www.gnu.org/licenses/>.

=cut