The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Mojolicious::Plugin::Check - Mojolicious plugin for controller level conditions.

DESCRIPTION

This module provide delayed to around_action hook execution for conditions to use with database, models and over controller level checks. You do not have to use add_condition directly for this because: conditions check route/headers not business logic, you can`t save something in stash, etc.

SYNOPSIS

    # Add plugin in startup
    $self->plugin('Check');

    # Good example:
    $r->add_condition(integer   => sub {...});
    $app->add_checker(user        => sub {...});
    $r->get('/user/:id')->over(
        integer => 'id',    # good, simple integer check
        user    => 'id',    # good, delay check
    )->to('foo#bar');

    # Bad example:
    $r->add_condition(integer   => sub {...});
    $r->add_condition(user      => sub {...});
    $r->get('/user/:id')->over(
        integer => 'id',    # good, simple integer check
        user    => 'id',    # bad, too early for DB, Model, Controller etc.
    )->to('foo#bar');

METHODS

add_checker

Same as add_condition, but delay execution to around_action hook level.

    # Simple "true" checker example
    $app->add_checker('true' => sub {
        my ($route, $c, $captures, $pattern) = @_;
        return $captures->{$pattern} ? 1 : 0;
    });

    # You can use database and save objects in stash to use in controllers
    $app->add_checker('user_exists' => sub {
        my ($route, $c, $captures, $pattern) = @_;
        my $id = $captures->{$pattern};
        my $db = $c->pg->db;
        $c->stash->{user} = $db->query('...', $id);
        return $c->stash->{user} ? 1 : 0;
    });

    # The user is guaranteed to have or render not_found page.
    $r->get('/user/:id')->over(user_exists => 'id')->to(cb => sub{
        my ($c) = @_;
        my $user = $c->stash('user');
        ...
    });

Return values for sub:

true

Check pass.

false or empty

Check fail. Render "Page not found" automatically.

undef

Check fail. You should render something manually.

    # Example "true" for forbidden status:
    $self->add_checker('true' => sub {
        my ($route, $c, $captures, $pattern) = @_;
        unless( $captures->{$pattern} ){
            $c->render(text => 'Forbidden', status => 403);
            return undef;
        }
        return 1;
    });

AUTHORS

Dmitry E. Oboukhov <unera@debian.org>, Roman V. Nikolaev <rshadow@rambler.ru>

COPYRIGHT

Copyright (C) 2017 Dmitry E. Oboukhov <unera@debian.org> Copyright (C) 2017 Roman V. Nikolaev <rshadow@rambler.ru>

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.