Commit f1ef6e89 authored by Yadd's avatar Yadd

Enable complex routes for plugins (#595)

parent ec834145
......@@ -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 {
......
......@@ -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' );
......
......@@ -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;
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