Catalyst-Plugin-Authorization-ACL
view release on metacpan or search on metacpan
lib/Catalyst/Plugin/Authorization/ACL/Engine.pm view on Meta::CPAN
package Catalyst::Plugin::Authorization::ACL::Engine;
BEGIN {
$Catalyst::Plugin::Authorization::ACL::Engine::AUTHORITY = 'cpan:RKITOVER';
}
$Catalyst::Plugin::Authorization::ACL::Engine::VERSION = '0.16';
use namespace::autoclean;
use Moose;
extends qw/Moose::Object Exporter/;
# I heart stevan
use Class::Throwable;
use Tree::Simple;
use Tree::Simple::Visitor::FindByPath;
use Tree::Simple::Visitor::GetAllDescendents;
use Carp qw/croak/;
use List::Util 'first';
has app => (is => 'rw');
has actions => (is => 'ro', isa => 'HashRef', default => sub { {} });
has _app_actions_tree => (is => 'ro', isa => 'Tree::Simple', lazy_build => 1);
our $DENIED = bless {}, __PACKAGE__ . "::Denied";
our $ALLOWED = bless {}, __PACKAGE__ . "::Allowed";
our @EXPORT_OK = qw/$DENIED $ALLOWED/;
sub BUILDARGS {
my ($self, $c) = @_;
return +{ app => $c };
}
sub _build__app_actions_tree {
my $self = shift;
my $root = Tree::Simple->new('/', Tree::Simple->ROOT);
my $app = $self->app;
my @actions = grep defined, map {
my $controller = $_;
map $controller->action_for($_->name), $controller->get_action_methods
} grep $_->isa('Catalyst::Controller'), values %{ $app->components };
for my $action (@actions) {
my @path = split '/', $action->reverse;
my $name = pop @path;
if (@path) {
my $by_path = Tree::Simple::Visitor::FindByPath->new;
$by_path->setSearchPath(@path);
$root->accept($by_path);
if (my $namespace_node = $by_path->getResult) {
$namespace_node->addChild(Tree::Simple->new($action));
next;
}
}
my $node = $root;
for my $el (@path) {
if (my $found = first { $_->getNodeValue eq $el }
@{ $node->getAllChildren }) {
$node = $found;
}
else {
$node = Tree::Simple->new($el, $node);
}
}
$node->addChild(Tree::Simple->new($action));
}
return $root;
}
sub add_deny {
my ( $self, $spec, $condition ) = @_;
my $test = $self->fudge_condition($condition);
$self->add_rule(
( run in 0.565 second using v1.01-cache-2.11-cpan-39bf76dae61 )