Yancy :: Plugin :: Auth :: Role :: RequireUser (source, CPAN)

CONTENTS

NAME

Yancy::Plugin::Auth::Role::RequireUser - Add authorization based on user attributes

VERSION

version 1.081

SYNOPSIS

use Mojolicious::Lite;
plugin Yancy => ...;

# Require any user
my $require_user = app->yancy->auth->require_user;
my $user = app->routes->under( '/user', $require_user );

# Require a user with the `is_admin` field set to true
my $require_admin = app->yancy->auth->require_user( { is_admin => 1 } );
my $admin = app->routes->under( '/admin', $require_admin );

DESCRIPTION

Note: This module is EXPERIMENTAL and its API may change before Yancy v2.000 is released.

This plugin adds a simple authorization method to your site. All default Yancy auth plugins use this role to provide the yancy.auth.require_user helper.

METHODS

require_user

my $subref = $c->yancy->auth->require_user( \%match );

Build a callback to validate there is a logged-in user, and optionally that the current user has certain fields set. \%match is optional and is a SQL::Abstract where clause matched with "match" in Yancy::Util.

# Ensure the user is logged-in
my $user_cb = $app->yancy->auth->require_user;
my $user_only = $app->routes->under( $user_cb );

# Ensure the user's "is_admin" field is set to 1
my $admin_cb = $app->yancy->auth->require_user( { is_admin => 1 } );
my $admin_only = $app->routes->under( $admin_cb );

SEE ALSO

Yancy::Plugin::Auth

AUTHOR

Doug Bell <preaction@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2021 by Doug Bell.

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