From f1ef6e8938d4b2765971216e6d97666e7096d855 Mon Sep 17 00:00:00 2001 From: Xavier Guimard Date: Sat, 17 Dec 2016 20:19:46 +0000 Subject: [PATCH] Enable complex routes for plugins (#595) --- .../lib/Lemonldap/NG/Common/PSGI/Router.pm | 18 ++++++---- .../lib/Lemonldap/NG/Portal/Main/Issuer.pm | 8 ++--- .../lib/Lemonldap/NG/Portal/Main/Plugin.pm | 33 +++++++++++-------- 3 files changed, 34 insertions(+), 25 deletions(-) diff --git a/lemonldap-ng-common/lib/Lemonldap/NG/Common/PSGI/Router.pm b/lemonldap-ng-common/lib/Lemonldap/NG/Common/PSGI/Router.pm index 4ba8f39e3..babe202c5 100644 --- a/lemonldap-ng-common/lib/Lemonldap/NG/Common/PSGI/Router.pm +++ b/lemonldap-ng-common/lib/Lemonldap/NG/Common/PSGI/Router.pm @@ -19,19 +19,19 @@ has 'defaultRoute' => ( is => 'rw', default => 'index.html' ); # Routes initialization sub addRoute { - my ( $self, $word, $dest, $methods ) = (@_); + my ( $self, $word, $dest, $methods, $transform ) = (@_); $methods ||= [qw(GET POST PUT DELETE)]; foreach my $method (@$methods) { - $self->genRoute( $self->routes->{$method}, $word, $dest ); + $self->genRoute( $self->routes->{$method}, $word, $dest, $transform ); } return $self; } sub genRoute { - my ( $self, $routes, $word, $dest ) = @_; + my ( $self, $routes, $word, $dest, $transform ) = @_; if ( ref $word eq 'ARRAY' ) { foreach my $w (@$word) { - $self->genRoute( $routes, $w, $dest ); + $self->genRoute( $routes, $w, $dest, $transform ); } } else { @@ -42,11 +42,14 @@ sub genRoute { } elsif ( $word =~ m#/# ) { $word =~ s#^(.*?)/##; - return $self->genRoute( $routes->{$1}, $word, $dest ); + return $self->genRoute( $routes->{$1}, $word, $dest, $transform ); } else { $dest ||= $word; } + if ( $transform and ( not ref($dest) or ref($dest) eq 'CODE' ) ) { + $dest = $transform->($dest); + } if ( my $t = ref $dest ) { if ( $t eq 'CODE' ) { $routes->{$word} = $dest; @@ -54,13 +57,14 @@ sub genRoute { elsif ( $t eq 'HASH' ) { $routes->{$word} ||= {}; foreach my $w ( keys %$dest ) { - $self->genRoute( $routes->{$word}, $w, $dest->{$w} ); + $self->genRoute( $routes->{$word}, $w, $dest->{$w}, + $transform ); } } elsif ( $t eq 'ARRAY' ) { $routes->{$word} ||= {}; foreach my $w ( @{$dest} ) { - $self->genRoute( $routes->{$word}, $w ); + $self->genRoute( $routes->{$word}, $w, $transform ); } } else { diff --git a/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Main/Issuer.pm b/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Main/Issuer.pm index 97ae8664c..083fa6b1a 100644 --- a/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Main/Issuer.pm +++ b/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Main/Issuer.pm @@ -34,10 +34,10 @@ sub init { $self->type($type); if ( my $path = $self->conf->{"issuerDB${type}Path"} ) { $path =~ s/^.*?(\w+).*?$/$1/; - $self->addUnauthRoute( $path => '_redirect', ['GET'] ); - $self->addUnauthRoute( $path => '_pRedirect', ['POST'] ); - $self->addAuthRoute( $path => "_forAuthUser", ['GET'] ); - $self->addAuthRoute( $path => "_pForAuthUser", ['POST'] ); + $self->addUnauthRoute( $path => { '*' => '_redirect' }, ['GET'] ); + $self->addUnauthRoute( $path => { '*' => '_pRedirect' }, ['POST'] ); + $self->addAuthRoute( $path => { '*' => "_forAuthUser" }, ['GET'] ); + $self->addAuthRoute( $path => { '*' => "_pForAuthUser" }, ['POST'] ); } else { $self->lmLog( "No path declared for issuer $type. Skipping", 'debug' ); diff --git a/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Main/Plugin.pm b/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Main/Plugin.pm index 25c3a380c..ee3883987 100644 --- a/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Main/Plugin.pm +++ b/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Main/Plugin.pm @@ -11,27 +11,32 @@ extends 'Lemonldap::NG::Common::Module'; sub addAuthRoute { my $self = shift; - return $self->_addRoute('addAuthRoute',@_); + return $self->_addRoute( 'addAuthRoute', @_ ); } sub addUnauthRoute { my $self = shift; - return $self->_addRoute('addUnauthRoute',@_); + return $self->_addRoute( 'addUnauthRoute', @_ ); } sub _addRoute { - my ( $self, $type, $word, $subName, $methods ) = @_; - die 'Only simple route are authorizated here. Uses $self->p->addAuthRoute' - if ( ref $subName ); - $self->can($subName) or die "Unknown sub $subName"; - $self->p->$type( - $word, - sub { - my $p = shift; - return $self->$subName(@_); - }, - $methods - ); + my ( $self, $type, $word, $subName, $methods, $transform ) = @_; + $transform //= sub { + my ($sub) = @_; + if ( ref $sub ) { + return sub { + shift; + return $sub->( $self, @_ ); + } + } + else { + return sub { + shift; + return $self->$sub(@_); + } + } + }; + return $self->p->$type( $word, $subName, $methods, $transform ); } 1;