Skip to content

Commit

Permalink
add role plugin to base app class
Browse files Browse the repository at this point in the history
  • Loading branch information
preaction committed Jun 18, 2021
1 parent dae1bcb commit c5f5ff2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
30 changes: 27 additions & 3 deletions lib/Yancy.pm
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ web application. Apps that inherit from Yancy get these features out-of-the-box:
=item * User logins (L<Yancy::Plugin::Auth>)
=item * Role-based access controls (L<Yancy::Plugin::Roles>)
=back
If you're familiar with developing Mojolicious applications, you can start
Expand Down Expand Up @@ -179,7 +181,11 @@ sub startup {
# no logins exist
}

# XXX: Add RBAC plugin by default
$app->plugin( Roles => {
schema => 'yancy_login_roles',
userid_field => 'login_id',
role_field => 'role',
} );

# Add default not_found renderer
push @{$app->renderer->classes}, 'Yancy';
Expand All @@ -195,28 +201,46 @@ CREATE TABLE yancy_logins (
login_name VARCHAR(191) NOT NULL UNIQUE,
password VARCHAR(191) NOT NULL
);
CREATE TABLE yancy_login_roles (
login_id BIGINT NOT NULL,
role VARCHAR(191) NOT NULL,
PRIMARY KEY ( login_id, role )
);
-- 1 down
DROP TABLE yancy_logins;
DROP TABLE yancy_login_roles;
@@ migrations.yancy_logins.sqlite.sql
-- 1 up
CREATE TABLE yancy_logins (
login_id ROWID,
login_id INTEGER PRIMARY KEY,
login_name VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL
);
CREATE TABLE yancy_login_roles (
login_id INTEGER NOT NULL,
role VARCHAR(255) NOT NULL,
PRIMARY KEY ( login_id, role )
);
-- 1 down
DROP TABLE yancy_logins;
DROP TABLE yancy_login_roles;
@@ migrations.yancy_logins.pg.sql
-- 1 up
CREATE TABLE yancy_logins (
login_id SERIAL,
login_id SERIAL UNIQUE,
login_name VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL
);
CREATE TABLE yancy_login_roles (
login_id INTEGER NOT NULL,
role VARCHAR(255) NOT NULL,
PRIMARY KEY ( login_id, role )
);
-- 1 down
DROP TABLE yancy_logins;
DROP TABLE yancy_login_roles;
@@ not_found.development.html.ep
% layout 'yancy';
Expand Down
11 changes: 9 additions & 2 deletions t/app.t
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use Test::Mojo;
use FindBin qw( $Bin );
use Mojo::File qw( path );
use lib "".path( $Bin, 'lib' );
use Local::Test qw( init_backend );
use Local::Test qw( init_backend load_fixtures );

my $schema = \%Yancy::Backend::Test::SCHEMA;
$schema->{yancy_logins} = {
Expand All @@ -22,10 +22,17 @@ $schema->{yancy_logins} = {
password => { type => 'string', format => 'password' },
},
};
$schema->{yancy_login_roles} = {
'x-id-field' => [ 'login_id', 'role' ],
properties => {
login_id => { type => 'integer' },
role => { type => 'string' },
},
};

my ( $backend_url, $backend, %items ) = init_backend( $schema );

my $t = Test::Mojo->new( 'Yancy', {
my $t = Test::Mojo->new( Yancy => {
backend => $backend_url,
} );

Expand Down

0 comments on commit c5f5ff2

Please sign in to comment.