Mocky Basic Authentication Using Under Without Mojolicious :: Lite

I am looking for a simple and simple example of how to use the “under” functionality in the application “Moholic”. All the examples that I find deal with "Mojolicious :: Lite" (which I do not use). For example, I listened to screencast here http://mojocasts.com/e3 , and I think I understand the concept as functionality. But I do not use "Mojolicious :: Lite", so it seems that I can not directly follow the example. I continue to fail, trying to adopt the Lite example for a non-Lite style. (Perhaps this is also due to the fact that I'm still new to the structure)

The corresponding code is as follows:

# Router my $r = $self->routes; # Normal route to controller $r->get('/') ->to('x#a'); $r->get('/y')->to('y#b'); $r->any('/z')->to('z#c'); 

Therefore, all of these routes must be user / password protected. I tried to do something like this:

 $r->under = sub { return 1 if ($auth) }; 

But this does not compile, and I just can’t find an example that matches this style of code ... Can someone give me the correct hint or link here? And please forgive me if this is somewhere in the documents ... they may be complete, but they lack clear examples for simple like-minded people like me :-P

+8
perl mojolicious
source share
3 answers

Similar code for Lite examples is as follows:

 # Router my $r = $self->routes; # This route is public $r->any('/login')->to('login#form'); # this sub does the auth-stuff # you can use stuff like: $self->param('password') # to check user/pw and return true if fine my $auth = $r->under( sub { return 1 } ); # This routes are protected $auth->get ('/') ->to('x#a'); $auth->post('/y')->to('y#b'); $auth->any ('/z')->to('z#c'); 

Hope this helps anyone!

(Solution found here: http://mojolicio.us/perldoc/Mojolicious/Routes/Route#under )

+10
source share

I do it like this: in a full mojo application (not lite):

in startup method

 $self->_add_routes_authorization(); # only users of type 'cashier' will have access to routes starting with /cashier my $cashier_routes = $r->route('/cashier')->over( user_type => 'cashier' ); $cashier_routes->route('/bank')->to('cashier#bank'); # only users of type 'client' will have access to routes starting with /user my $user_routes = $r->route('/user')->over( user_type => 'client' ); $user_routes->get('/orders')->to('user#orders'); 

below in the main application file:

 sub _add_routes_authorization { my $self = shift; $self->routes->add_condition( user_type => sub { my ( $r, $c, $captures, $user_type ) = @_; # Keep the weirdos out! # $self->user is the current logged in user, as a DBIC instance return if ( !defined( $self->user ) || $self->user->user_type()->type() ne $user_type ); # It ok, we know him return 1; } ); return; } 

I hope this helps

+3
source share

I use this script in my application:

 my $guest = $r->under->to( "auth#check_level" ); my $user = $r->under->to( "auth#check_level", { required_level => 100 } ); my $admin = $r->under->to( "auth#check_level", { required_level => 200 } ); $guest->get ( '/login' )->to( 'auth#login' ); $user ->get ( '/users/profile' )->to( 'user#show' ); 

After that, all $r child routes will go under the check_level subroutine:

 sub check_level { my( $self ) = @_; # GRANT If we do not require any access privilege my $rl = $self->stash->{ required_level }; return 1 if !$rl; # GRANT If logged in user has required level OR we raise user level one time my $sl = $self->session->{ user_level }; my $fl = $self->flash( 'user_level' ); return 1 if $sl >= $rl || $fl && $fl >= $rl; # RESTRICT $self->render( 'auth/login', status => 403 ); return 0; } 
0
source share

All Articles