| 1 |
NAME |
| 2 |
Catalyst::Plugin::Authorization::Roles - Role based authorization for |
| 3 |
Catalyst based on Catalyst::Plugin::Authentication |
| 4 |
|
| 5 |
SYNOPSIS |
| 6 |
use Catalyst qw/ |
| 7 |
Authentication |
| 8 |
Authorization::Roles |
| 9 |
/; |
| 10 |
|
| 11 |
sub delete : Local { |
| 12 |
my ( $self, $c ) = @_; |
| 13 |
|
| 14 |
$c->assert_user_roles( qw/admin/ ); # only admins can delete |
| 15 |
|
| 16 |
$c->model("Foo")->delete_it(); |
| 17 |
} |
| 18 |
|
| 19 |
DESCRIPTION |
| 20 |
Role based access control is very simple: every user has a list of |
| 21 |
roles, which that user is allowed to assume, and every restricted part |
| 22 |
of the app makes an assertion about the necessary roles. |
| 23 |
|
| 24 |
With "assert_user_roles", if the user is a member in all of the required |
| 25 |
roles access is granted. Otherwise, access is denied. With |
| 26 |
"assert_any_user_role" it is enough that the user is a member in one |
| 27 |
role. |
| 28 |
|
| 29 |
There are alternative approaches to do this on a per action basis, see |
| 30 |
Catalyst::ActionRole::ACL. |
| 31 |
|
| 32 |
For example, if you have a CRUD application, for every mutating action |
| 33 |
you probably want to check that the user is allowed to edit. To do this, |
| 34 |
create an editor role, and add that role to every user who is allowed to |
| 35 |
edit. |
| 36 |
|
| 37 |
sub edit : Local { |
| 38 |
my ( $self, $c ) = @_; |
| 39 |
$c->assert_user_roles( qw/editor/ ); |
| 40 |
$c->model("TheModel")->make_changes(); |
| 41 |
} |
| 42 |
|
| 43 |
When this plugin checks the roles of a user it will first see if the |
| 44 |
user supports the self check method. |
| 45 |
|
| 46 |
When this is not supported the list of roles is extracted from the user |
| 47 |
using the "roles" method. |
| 48 |
|
| 49 |
When this is supported, the "check_roles" method will be used to |
| 50 |
delegate the role check to the user class. Classes like the one provided |
| 51 |
with iCatalyst::Authentication::Store::DBIx::Class optimize the check |
| 52 |
this way. |
| 53 |
|
| 54 |
METHODS |
| 55 |
assert_user_roles [ $user ], @roles |
| 56 |
Checks that the user (as supplied by the first argument, or, if |
| 57 |
omitted, "$c->user") has the specified roles. |
| 58 |
|
| 59 |
If for any reason ("$c->user" is not defined, the user is missing a |
| 60 |
role, etc) the check fails, an error is thrown. |
| 61 |
|
| 62 |
You can either catch these errors with an eval, or clean them up in |
| 63 |
your "end" action. |
| 64 |
|
| 65 |
check_user_roles [ $user ], @roles |
| 66 |
Takes the same args as "assert_user_roles", and performs the same |
| 67 |
check, but instead of throwing errors returns a boolean value. |
| 68 |
|
| 69 |
assert_any_user_role [ $user ], @roles |
| 70 |
Checks that the user (as supplied by the first argument, or, if |
| 71 |
omitted, "$c->user") has at least one of the specified roles. |
| 72 |
|
| 73 |
Other than that, works like "assert_user_roles". |
| 74 |
|
| 75 |
check_any_user_role [ $user ], @roles |
| 76 |
Takes the same args as "assert_any_user_role", and performs the same |
| 77 |
check, but instead of throwing errors returns a boolean value. |
| 78 |
|
| 79 |
SEE ALSO |
| 80 |
Catalyst::Plugin::Authentication |
| 81 |
Catalyst::ActionRole::ACL |
| 82 |
Catalyst::Manual::Tutorial::06_Authorization |
| 83 |
|
| 84 |
AUTHOR |
| 85 |
Yuval Kogman <[email protected]> |
| 86 |
|
| 87 |
COPYRIGHT & LICENSE |
| 88 |
Copyright (c) 2005-2011 the Catalyst::Plugin::Authorization::Roles |
| 89 |
"AUTHOR" as listed above. |
| 90 |
|
| 91 |
This library is free software; you can redistribute it and/or modify it |
| 92 |
under the same terms as Perl itself. |
| 93 |
|