=encoding utf8 Доброго всем ¡ ¡ ¡ ALL GLORY TO GLORIA ! ! ! =head1 NAME Mojolicious::Plugin::RoutesAuthDBI - Generate routes from sql-table, make authentication and make restrict access (authorization) to route with users/roles tables. Plugin makes an auth operations throught the plugin L on which is based. =head1 SYNOPSIS # at sub startup # after config $app->plugin('RoutesAuthDBI', dbh => $app->dbh, auth => {...}, admin => {...},); =head2 OPTIONS =over 4 =item * B - DBI connection where are tables: routes, users, roles, refs. =item * B - hashref options pass to base plugin L. By default the option: current_user_fn => 'auth_user', The options: load_user => \&load_user, validate_user => \&validate_user, are imported from package admin controller. See below. =item * B - hashref options for special admin controller. This controller has subs and methods for manage auth and access operations, has appling routes from sql-table. By default plugin will load the builtin controller: admin => { controller => 'Admin', namespace => 'Mojolicious::Plugin::RoutesAuthDBI', ..., }, You might define your own controller by passing options: admin => { controller => 'Foo', namespace => 'Bar::Baz', ..., }, See L for detail options list. =back =head1 INSTALL See L. =head3 Warning If you changed the routes table then kill -HUP or reload app to regenerate routes. =head1 DESCRIPTION =head2 SQL schema Refs between three tables in order: route -> role -> user =over 4 =item * Pg sequence for column id on all tables create sequence ID; =item * Table of routes create table routes( id integer default nextval('ID'::regclass) not null primary key, ts timestamp without time zone default now() not null, request character varying null, namespace character varying null, controller character varying not null, action character varying null, name character varying not null, descr text, auth bit(1), disable bit(1), order_by int ); =item * Table of users create table users( id int default nextval('ID'::regclass) not null primary key, ts timestamp without time zone default now() not null, login varchar not null unique, pass varchar not null ); =item * Table of users roles create table roles( id int default nextval('ID'::regclass) not null primary key, ts timestamp without time zone default now() not null, name varchar not null unique ); =item * Table of references between routes, users and roles create table refs( id int default nextval('ID'::regclass) not null primary key, ts timestamp without time zone default now() not null, id1 int not null, id2 int not null, unique(id1, id2) ); create index on refs (id2); where: id1 - primary id of reference, id2 - secondary id of reference =back =head2 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 L: # GET /city/new $r->route('/city/new')->via('get')->over()->to(controller => 'city', action => 'new_form')->name('city_new_form'); # GET /city/123 - show item with id 123 $r->route('/city/:id')->via('get')->over()->to(controller => 'city', action => 'show')->name('city_show'); # GET /city/edit/123 - form to edit an item $r->route('/city/edit/:id')->via('get')->over()->to(controller => 'city', action => 'edit_form')->name('city_edit_form'); # GET /cities - list of all items $r->route('/cities')->via('get')->over()->to(controller => 'city', action => 'index')->name('cities_index'); # POST /city - create new item or update the item $r->route('/city')->via('post')->to(controller => 'city', action => 'save')->name('city_save'); # GET /city/delete/123 - form to confirm delete an item id=123 $r->route('/city/delete/:id')->via('get')->over()->to(controller => 'city', action => 'delete_form')->name('city_delete_form'); # DELETE /city/123 - delete an item id=123 $r->route('/city/:id')->via('delete')->over()->to(controller => 'city', action => 'delete')->name('city_delete'); # without HTTP method and no auth restrict $r->route('/')->to(controller => 'Home', action => 'index')->name('home_index'); # GET or POST /foo/baz $r->route('/foo/baz')->via('GET', 'post')->over()->to(controller => 'Foo', action => 'baz')->name('foo_baz'); =head1 SEE ALSO L, L =head1 AUTHOR Михаил Че (Mikhail Che), C<< >> =head1 BUGS / CONTRIBUTING Please report any bugs or feature requests at L. Pull requests also welcome. =head1 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. =cut