Mojolicious-Plugin-RoutesAuthDBI

from DBI tables does generate app routes, make authentication and make restrict access (authorization).


Keywords
access, auth, authentication, authorization, dbi, mojolicious, plugin, routes
License
Artistic-1.0-Perl

Documentation

Доброго всем

¡ ¡ ¡ ALL GLORY TO GLORIA ! ! !

Mojolicious::Plugin::RoutesAuthDBI

Plugin makes an auth operations throught the plugin Mojolicious::Plugin::Authentication and OAuth2 by Mojolicious::Plugin::OAuth2.

VERSION

0.881

NAME

Mojolicious::Plugin::RoutesAuthDBI - from DBI tables does generate app routes, make authentication and make restrict access (authorization).

DB DESIGN DIAGRAM

First of all you will see SVG or PNG

SYNOPSIS

$app->plugin('RoutesAuthDBI',
  dbh => $app->dbh,
  auth => {...},
  access => {...},
  admin => {...},
  oauth => {...},
  guest => {...},
  template => {...},
  model_namespace=>...,
);

PLUGIN OPTIONS

One option dbh is mandatory, all other - optional.

dbh

Handler DBI connection where are tables: controllers, actions, routes, logins, profiles, roles, refs and oauth.

dbh => $app->dbh,
# or
dbh => sub { shift->dbh },

auth

Hashref options pass to base plugin Mojolicious::Plugin::Authentication. By default the option:

current_user_fn => 'auth_user',
stash_key => "Mojolicious::Plugin::RoutesAuthDBI__user__",
  

The options:

load_user => \&load_user,
validate_user => \&validate_user,

are imported from package access module. See below.

access

Hashref options for special access module. This module has subs/methods for manage auth and access operations, has appling routes from DBI table. By default plugin will load the builtin module:

access => {
  module => 'Access',
  namespace => 'Mojolicious::Plugin::RoutesAuthDBI',
  ...,
},

You might define your own module by passing options:

access => {
  module => 'Foo',
  namespace => 'Bar::Baz', 
  ...,
},

See Mojolicious::Plugin::RoutesAuthDBI::Access for detail options list.

admin

Hashref options for admin controller for actions on SQL tables routes, roles, profiles, logins. By default the builtin module:

admin => {
  controller => 'Admin',
  namespace => 'Mojolicious::Plugin::RoutesAuthDBI',
  ...,
},

You might define your own controller by passing options:

admin => {
  controller => 'Foo',
  namespace => 'Bar::Baz', 
  ...,
},

See Mojolicious::Plugin::RoutesAuthDBI::Admin for detail options list.

oauth

Hashref options for oauth controller. By default the builtin module:

oauth => {
  controller => 'OAuth',
  namespace => 'Mojolicious::Plugin::RoutesAuthDBI',
  ...,
},

You might define your own controller by passing options:

oauth => {
  controller => 'Foo::Bar::Baz',
  ...,
},

See Mojolicious::Plugin::RoutesAuthDBI::OAuth for detail options list.

guest

Hashref options for guest module. Defaults are:

guest => {
  namespace => 'Mojolicious::Plugin::RoutesAuthDBI',
  module => 'Guest',
  session_key => 'guest_data',
  stash_key => "Mojolicious::Plugin::RoutesAuthDBI__guest__",
  
},

Disable guest module usage:

guest => undef, # or none in config

See Mojolicious::Plugin::RoutesAuthDBI::Guest

model_namespace

Where are your models place. Default to "Mojolicious::Plugin::RoutesAuthDBI::Model".

template

Hashref variables for SQL templates of models dictionaries. Defaults is $Mojolicious::Plugin::RoutesAuthDBI::Schema::defaults. See Mojolicious::Plugin::RoutesAuthDBI::Model::Base.

INSTALL

See Mojolicious::Plugin::RoutesAuthDBI::Install.

REQUIRES CONDITIONS

access

Heart of this plugin! This condition apply for all db routes even if column auth set to 0. It is possible to apply this condition to non db routes also:

  • No access check to route, but authorization by session will ready:

    $r->any('/foo')->...->requires(access=>{auth=>0})->...;
  • Allow if has authentication only:

    $r->any('/foo')->...->requires(access=>{auth=>'only'})->...;
    # same as
    # $r->any('/foo')->...->requires(authenticated => 1)->...; # see Mojolicious::Plugin::Authentication
  • Allow for guest

    $r->any('/foo')->...->requires(access=>{guest=>1})->...;

    To makes guest session:

    $c->access->plugin->guest->store($c, {<...some data...>});

    See Mojolicious::Plugin::RoutesAuthDBI::Guest

  • Route accessible if profile roles assigned to either loadable namespace or controller 'Bar.pm' (which assigned neither namespece on db or assigned to that loadable namespace) or action 'bar' on controller Bar.pm (action record in db table actions):

    $r->any('/bar-bar-any-namespace')->to('bar#bar',)->requires(access=>{auth=>1})->...;
  • Explicit defined namespace route accessible either namespace 'Bar' or 'Bar::Bar.pm' controller or action 'bar' in controller 'Bar::Bar.pm' (which assigned to namespace 'Bar' in table refs):

    $r->any('/bar-bar-bar')->to('bar#bar', namespace=>'Bar')->requires(access=>{auth=>1})->...;
  • Check access by overriden namespace 'BarX': controller and action also with that namespace in db table refs:

    $r->any('/bar-nsX')->to('bar#bar', namespace=>'Bar')->requires(access=>{auth=>1, namespace=>'BarX'})->...;
  • Check access by overriden namespace 'BarX' and controller 'BarX.pm', action record also with that ns & c in db table refs:

    $r->any('/bar-nsX-cX')->to('bar#bar', namespace=>'Bar')->requires(access=>{auth=>1, namespace=>'BarX', controller=>'BarX'})->...;
  • Full override names access:

    $r->any('/bar-nsX-cX-aX')->to('bar#bar', namespace=>'Bar')->requires(access=>{auth=>1, namespace=>'BarX', controller=>'BarX', action=>'barX'})->...;
  • $r->any('/bar-cX-aX')->to('bar#bar',)->requires(access=>{auth=>1, controller=>'BarX', action=>'barX'})->...;
  • Route accessible if profile roles list has defined role (admin):

    $r->any('/bar-role-admin')->to('bar#bar',)->requires(access=>{auth=>1, role=> 'admin'})->...;
  • Pass callback to access condition

    The callback will get parameters: $profile, $route, $c, $captures, $args (this callback ref). Callback must returns true or false for restrict access. Example simple auth access:

    $r->any('/check-auth')->requires(access=>sub {my ($profile, $route, $c, $captures, $args) = @_; return $profile;})->to(cb=>sub {my $c =shift; $c->render(format=>'txt', text=>"Hi @{[$c->auth_user->{names}]}!\n\nYou have access!");});

HELPERS

access

Returns access instance obiect. See Mojolicious::Plugin::RoutesAuthDBI::Access methods.

if ($c->access->access_explicit([1,2,3], [1,2,3])) {
  # yes, accessible
}

METHODS and SUBS

Registration() & access() & <internal>.

Example routing table records

Request
HTTP method(s) (optional)
and the URL (space delim)
                           Contoller    Method          Route Name        Auth
-------------------------  -----------  --------------  ----------------- -----
GET /city/new              City         new_form        city_new_form     1
GET /city/:id              City         show            city_show         1
GET /city/edit/:id         City         edit_form       city_edit_form    1
GET /cities                City         index           city_index        1
POST /city                 City         save            city_save         1
GET /city/delete/:id       City         delete_form     city_delete_form  1
DELETE /city/:id           City         delete          city_delete       1
/                          Home         index           home_index        0
get post /foo/baz          Foo          baz             foo_baz           1

It table will generate the Mojolicious routes:

# GET /city/new 
$r->any('/city/new')->methods('get')->requires(<access>)->to(controller => 'city', action => 'new_form')->name('city_new_form');

# GET /city/123 - show item with id 123
$r->any('/city/:id')->methods('get')->requires(<access>)->to(controller => 'city', action => 'show')->name('city_show');

# GET /city/edit/123 - form to edit an item
$r->any('/city/edit/:id')->methods('get')->requires(<access>)->to(controller => 'city', action => 'edit_form')->name('city_edit_form');

# GET /cities - list of all items
$r->any('/cities')->methods('get')->requires(<access>)->to(controller => 'city', action => 'index')->name('cities_index');

# POST /city - create new item or update the item
$r->any('/city')->methods('post')->to(controller => 'city', action => 'save')->name('city_save');

# GET /city/delete/123 - form to confirm delete an item id=123
$r->any('/city/delete/:id')->methods('get')->requires(<access>)->to(controller => 'city', action => 'delete_form')->name('city_delete_form');

# DELETE /city/123 - delete an item id=123
$r->any('/city/:id')->methods('delete')->requires(<access>)->to(controller => 'city', action => 'delete')->name('city_delete');
    
# without HTTP method and no auth restrict
$r->any('/')->to(controller => 'Home', action => 'index')->name('home_index');
    
# GET or POST /foo/baz 
$r->any('/foo/baz')->methods('GET', 'post')->requires(<access>)->to(controller => 'Foo', action => 'baz')->name('foo_baz');

Warning

If you changed the routes table then kill -HUP or reload app to regenerate routes. Changing assess not require reloading the service.

SEE ALSO

Mojolicious::Plugin::Authentication

Mojolicious::Plugin::Authorization

AUTHOR

Михаил Че (Mikhail Che), <mche[-at-]cpan.org>

BUGS / CONTRIBUTING

Please report any bugs or feature requests at https://github.com/mche/Mojolicious-Plugin-RoutesAuthDBI/issues. Pull requests also welcome.

COPYRIGHT

Copyright 2016+ Mikhail Che.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.