Mojolicious-Plugin-RoutesAuthDBI

 view release on metacpan or  search on metacpan

README.pod  view on Meta::CPAN

package Mojolicious::Plugin::RoutesAuthDBI;
use Mojo::Base 'Mojolicious::Plugin::Authentication';
use Mojolicious::Plugin::RoutesAuthDBI::Util qw(load_class);
use Mojo::Util qw(hmac_sha1_sum);
use Hash::Merge qw( merge );
use Scalar::Util 'weaken';

use constant  PKG => __PACKAGE__;

has [qw(app dbh conf)];

has default => sub {
  my $self = shift;
  require Mojolicious::Plugin::RoutesAuthDBI::Schema;
  {
  auth => {
    stash_key => PKG."__user__",
    current_user_fn => 'auth_user',# helper
    load_user => \&load_user,
    validate_user => \&validate_user,
  },
  
  access => {
    namespace => PKG,
    module => 'Access',
    fail_auth_cb => sub {
      shift->render(status => 401, format=>'txt', text=>"Please sign in.\n");
    },
    fail_access_cb => sub {
      shift->render(status => 403, format=>'txt', text=>"You don`t have access on this route (url, action).\n");
    },
    import => [qw(load_user validate_user)],
  },
  
  admin => {
    namespace => PKG,
    controller => 'Admin',
    prefix => lc($self->conf->{admin}{controller} || 'admin'),
    trust => hmac_sha1_sum('admin', $self->app->secrets->[0]),
    role_admin => 'administrators',
  },
  
  oauth => {
    namespace => PKG,
    controller => 'OAuth',
    fail_auth_cb => sub {shift->render(format=>'txt', text=>"@_")},
  },
  
  template => $Mojolicious::Plugin::RoutesAuthDBI::Schema::defaults,
  
  model_namespace => PKG.'::Model',
  
  guest => {# from Mojolicious::Plugin::Authentication
    #~ autoload_user => 1,
    session_key => 'guest_data',
    stash_key => PKG."__guest__",
    #~ current_user_fn => 'current_guest',# helper
    #~ load_user => \&load_guest,
    #~ validate_user => not need
    #~ fail_render => not need
    #######end Mojolicious::Plugin::Authentication conf#######
    namespace => PKG,
    module => 'Guest',
    #~ import => [qw(load_guest)],
  },
  
  log=>{
    namespace => PKG,
    module => 'Log',
    disabled=>0,
  },
  };
};# end defaults

has merge_conf => sub {#hashref
  my $self = shift;
  merge($self->conf, $self->default);
};

has access => sub {# object
  my $self = shift;
  weaken $self;
  my $conf = $self->merge_conf->{'access'};
  @{$self->merge_conf->{template}{tables}}{keys %{$conf->{tables}}} = values %{$conf->{tables}}
    if $conf->{tables};
  my $class = load_class($conf);
  $class->import( @{ $conf->{import} });
  $class->new(app=>$self->app, plugin=>$self,);
}, weak => 1;

has admin => sub {# object
  my $self = shift;
  my $conf = $self->merge_conf->{'admin'};
  @{$self->merge_conf->{template}{tables}}{keys %{$conf->{tables}}} = values %{$conf->{tables}}
    if $conf->{tables};
  load_class($conf)->init(%$conf, app=>$self->app, plugin=>$self,);
}, weak => 1;

has oauth => sub {
  my $self = shift;
  my $conf = $self->merge_conf->{'oauth'};
  @{$self->merge_conf->{template}{tables}}{keys %{$conf->{tables}}} = values %{$conf->{tables}}
    if $conf->{tables};
  load_class($conf)->init(%$conf, app=>$self->app, plugin=>$self, model=>$self->model($conf->{controller}),);
}, weak => 1;

has guest => sub {# object
  my $self = shift;
  my $conf = $self->merge_conf->{'guest'};
  @{$self->merge_conf->{template}{tables}}{keys %{$conf->{tables}}} = values %{$conf->{tables}}
    if $conf->{tables};
  
  $self->merge_conf->{template}{tables}{guests} = $conf->{table}
    if $conf->{table};
  
  my $class = load_class($conf);
  $class->new( %$conf, app=>$self->app, plugin=>$self, model=>$self->model($conf->{module}), );
}, weak => 1;

README.pod  view on Meta::CPAN


=encoding utf8

=head1 Доброго всем

¡ ¡ ¡ ALL GLORY TO GLORIA ! ! !

=head1 Mojolicious::Plugin::RoutesAuthDBI

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

=head1 VERSION

0.881

=head1 NAME

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

=head1 DB DESIGN DIAGRAM

First of all you will see L<SVG|https://github.com/mche/Mojolicious-Plugin-RoutesAuthDBI/blob/master/Diagram.svg>  or L<PNG|http://i.imgur.com/CwqiB4f.png>

=head1 SYNOPSIS

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


=head2 PLUGIN OPTIONS

One option C<dbh> is mandatory, all other - optional.

=head3 dbh

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

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

=head3 auth

Hashref options pass to base plugin L<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.

=head3 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 L<Mojolicious::Plugin::RoutesAuthDBI::Access> for detail options list.

=head3 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 L<Mojolicious::Plugin::RoutesAuthDBI::Admin> for detail options list.

=head3 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',



( run in 0.703 second using v1.01-cache-2.11-cpan-39bf76dae61 )