I have the following code in my Mojolicious application module. When I run this using
Test::Mojo $t->post_ok(‘hosts’, encode_json( $json_string ) )->status_is(200), the application works fine. When I use the Firefox rest client I never get passed the
add_condition route modifier ‘access’. I dumped the result of $can (in the add_condition block) and it’s ‘1’. So, in my mind, that should just continue on to the controller (as it does when I run the test). When using the FF rest client this
returns 404 because nothing happens after the access condition returns 1.
Any idea why the application would operate differently in these two situations?
$self->plugin(
'json_rpc_dispatcher',
services => {
'/auth' => Package::Controller::Auth->new,
'/user' => Package::Controller::User->new,
'/hosts' => Package::Controller::Hosts->new,
}
);
$self->app->routes->add_condition(
access => sub {
my ($rt, $cntrlr, $stuff) = @_;
my $sess = $cntrlr->stash('mojo.session');
my $path = $cntrlr->req->url->path->parts;
my $can = $self->can_access({
route => $path->[0],
args => $sess,
});
$can && return 1;
$cntrlr->render_json({
error => 'Not Authorized',
ok => 0,
});
}
);
$self->hook( before_dispatch => sub {
my $this = shift;
my $params = $this->tx->req->json;
if( !$this->session('auth') ) {
my $login = $self->login({
user => $params->{params}{user},
password => $params->{params}{password},
});
return { error => 'Invalid Username or password' } if $login->{error};
my $access = $self->has_access(
$params->{params}{user}, $params->{params}{password}
);
return { error => 'Invalid credentials' } unless keys %{ $access };
$this->session( auth => { dn => $params->{params}{user}, perms => $access } );
}
else {
$r->find('auth' )->over('access');
$r->find('user' )->over('access');
$r->find('hosts')->over('access');
}
} ); # hook
Sorry. The problem had nothing to do with the application or Mojolicious in anyway (or even perl for that matter). Hostname wasn’t resolving correctly.
Well, I don’t know where the problem is really. Starting the server using
morbo script/app daemon -l my.host.name.domain.com wasn’t working.
When I used
morbo script/app daemon -l 192.168.1.11 (where 192.168.1.11 is the ip of my.host.name.domain.com). Things began to operate as expected.
And now neither of those work. 🙁