6

我有以下代码:

$r->find('user')->via('post')->over(authenticated => 1);

鉴于该路由,我可以通过使用 Mojolicious::Plugin::Authentication 设置的经过身份验证的检查到达用户路由。

我想在该路线上添加另一个“结束”。

$r->find('user')->via('post')->over(authenticated => 1)->over(access => 1);

不过,这似乎覆盖了经过身份验证的“过度”。

我尝试使用以下名称分解路线:

 my $auth = $r->route('/')->over(authenticated => 1)
     ->name('Authenticated Route');

 $access = $auth->route('/user')->over(access => 1)->name('USER_ACCESS');

但这根本不起作用。两个'over'都没有被访问。

我的路由是 /user、/item 之类的东西,使用 MojoX::JSON::RPC::Service 设置。所以,我没有 /user/:id 之类的东西来设置子路由。(不确定是否重要)所有路由都像 /user,用参数发送。

我有这样的条件:

$r->add_condition(
    access => sub {
        # do some stuff
    },
);

那就是 $r->route('/user')->over(access => 1); 中的“访问”

简而言之,路线在使用时可以正常工作:

$r->find('user')->via('post')->over(authenticated => 1);

但我无法添加第二条路线。

那么,在设置这些具有多个条件的路线时,我缺少什么?是否可以将多个条件添加到单个路由/route_name?

4

3 回答 3

2

您可以over在此测试中使用这两个条件:

use Mojolicious::Lite;

# dummy conditions storing their name and argument in the stash
for my $name (qw(foo bar)) {
    app->routes->add_condition($name => sub {
        my ($route, $controller, $to, @args) = @_;
        $controller->stash($name => $args[0]);
    });
}

# simple foo and bar dump action
sub dump {
    my $self = shift;
    $self->render_text(join ' ' => map {$self->stash($_)} qw(foo bar));
}

# traditional route with multiple 'over'
app->routes->get('/frst')->over(foo => 'yo', bar => 'works')->to(cb => \&dump);

# lite route with multiple 'over'
get '/scnd' => (foo => 'hey', bar => 'cool') => \&dump;

# test the lite app above
use Test::More tests => 4;
use Test::Mojo;

my $t = Test::Mojo->new;

# test first route
$t->get_ok('/frst')->content_is('yo works');
$t->get_ok('/scnd')->content_is('hey cool');

__END__
1..4
ok 1 - get /frst
ok 2 - exact match for content
ok 3 - get /scnd
ok 4 - exact match for content

在 perl 5.12.1 上与 Mojolicious 3.38 一起工作正常 - @DavidO 是对的,也许桥梁可以更好地完成这项工作。:)

于 2012-09-05T15:17:43.473 回答
0

就我而言,我使用两种under方法:

$r =  $app->routes;
$guest =  $r->under->to( 'auth#check_level' );
$user  =  $r->under->to( 'auth#check_level', { required_level => 100 } );
$admin =  $r->under->to( 'auth#check_level', { required_level => 200 } );

$guest->get( '/' )->to( 'main#index' );
$user->get( '/user' )->to( 'user#show' );
$super_admin =  $admin->under->to( 'manage#check_level', { super_admin => 100 } );
$super_admin->get( '/delete_everything' )->to( 'system#shutdown' );

在此示例中,当任何路由匹配时,under将调用一些

'/' -> auth#check_level -> main_index
'/user' -> auth#check_level { required_level => 100 } -> 'user#show'
'/delete_everything' -> auth#check_level { required_level => 200 } -> 'manage#check_level', { super_admin => 100 } -> 'system#shutdown'

正如您在 target 之前看到action的那样,您controller将运行另一个名为:auth#check_levelmanage#check_level

在授权用户时,您只需stash->{ required_level }与您设置的每个额外操作进行比较session->{ required_level }

package YourApp::Controller::Manage;

sub check_level {
    my $self =  shift;

    my $user_have =  $self->session->{ required_level };
    my $we_require =  $self->stash->{ required_level };
    # 'system#shutdown' will be called if user has required level
    return 1   if  $user_have >= $we_require; 


    $self->redirect_to( '/you_have_no_access_rights' );
    return 0; #This route will not match. 'system#shutdown' will not be called
}

PS当然我可以使用cb或只是CODEREF与控制器动作“接近相同”:

$r->under({ cb => \&YourApp::Controller::auth::check_level });
$r->under( \&YourApp::Controller::auth::check_level ); # "same"

但我更喜欢->to( 'controller#action' )语法。它看起来好多了

于 2016-12-04T19:03:08.487 回答
0

如果我们使用这种方法呢?

# register condition
$r->add_condition(
    chain => sub {
        my ($route, $controller, $captures, $checkers) = @_; 

        for my $checker (@$checkers) {
            return 0 unless $checker->($route, $controller, $captures);
        }

        return 1;
    },
);

# ...
# example of using
$r->get('/')->over(chain => [\&checker1, \&checker2])->to(cb => \&foo)->name('bar');
于 2017-10-23T23:01:29.500 回答