Tree::Authz - inheritance-based authorization scheme
0.02_1
Re-organised to return objects (blessed into the new class Tree::Authz::Role), instead of strings, which are now referred to as roles rather than groups in the documentation. Some method names changed to reflect the terminology.
Tree::Authz::Role
roles
groups
use Tree::Authz; my $roles = { superuser => [ qw( spymasters politicians ) ], spymasters => [ qw( spies moles ) ], spies => [ 'informants' ], informants => [ 'base' ], moles => [ 'base' ], politicians => [ 'citizens' ], citizens => [ 'base' ], }; my $authz = Tree::Authz->setup_hierarchy( $roles, 'SpyLand' ); my $superuser = $authz->role( 'superuser' ); my $spies = $authz->role( 'spies' ); my $citizens = $authz->role( 'citizens' ); my $base = $authz->role( 'base' ); $spies ->setup_permissions( [ qw( read_secrets wear_disguise ) ] ); $citizens->setup_permissions( 'vote' ); $base ->setup_permissions( 'breathe' ); foreach my $role ( $superuser, $spies, $citizens, $base ) { foreach my $ability ( qw( unspecified_ability spy spies read_secrets wear_disguise vote breathe can ) ) { if ( $role->can( $ability ) ) { print "$role can '$ability'\n"; } else { print "$role cannot '$ability'\n"; } } } # prints: superuser can 'unspecified_ability' # superpowers! superuser can 'spy' superuser can 'spies' superuser can 'read_secrets' superuser can 'wear_disguise' superuser can 'vote' superuser can 'breathe' superuser can 'can' spies cannot 'unspecified_ability' spies can 'spy' spies can 'spies' spies can 'read_secrets' spies can 'wear_disguise' spies can 'vote' spies can 'breathe' spies can 'can' citizens cannot 'unspecified_ability' citizens cannot 'spy' citizens cannot 'spies' citizens cannot 'read_secrets' citizens cannot 'wear_disguise' citizens can 'vote' citizens can 'breathe' citizens can 'can' base cannot 'unspecified_ability' base cannot 'spy' base cannot 'spies' base cannot 'read_secrets' base cannot 'wear_disguise' base cannot 'vote' base cannot 'breathe' # ! base cannot 'can' # !! # storing code on the nodes (roles) of the tree $spies->setup_abilities( read_secret => $coderef ); print $spies->read_secret( '/path/to/secret/file' ); $spies->setup_plugins( 'My::Spies::Skills' ); $spies->fly( $jet ); # My::Spies::Skills::fly
Class for inheritable, role-based permissions system (Role Based Access Control - RBAC).
Custom methods can be placed on role objects. Authorization can be performed either by checking whether the role name matches the required name, or by testing (via can) whether the role can perform the method required.
can
Two role are specified by default. At the top, superusers can do anything ($superuser->can( $action ) always returns a coderef). At the bottom, the base role can do nothing ($base->can( $action ) always returns undef).
$superuser->can( $action )
$base->can( $action )
All roles are automatically capable of authorizing actions named for the singular and plural of the role name.
I'm planning to implement some of the main features and terminology described in this document, which describes a standard for Role Based Access Control:
http://csrc.nist.gov/rbac/rbacSTD-ACM.pdf
Thanks to Kingsley Kerce for providing the link.
This class is a static class - all methods are class methods.
Some methods return Tree::Authz::Role subclass objects.
This class is designed to work in environments where multiple applications run within the same process (i.e. websites under mod_perl). If the optional namespace parameter is supplied to setup_hierarchy, the roles are isolated to the specified namespace. All methods should be called through the class name returned from setup_hierarchy.
mod_perl
setup_hierarchy
If your program is not operating in such an environment (e.g. CGI scripts), then you can completely ignore this parameter, and call class methods either through Tree::Authz, or through the string returned from setup_hierarchy (which, funnily enough, will be 'Tree::Authz').
Tree::Authz
Factory method, returns a Tree::Authz::Role subclass object.
Sets up two permitted actions on the group - the singular and plural of the group name. This might be too cute, and could change to just the group name in a near future release. Opinions welcome.
DEPRECATED.
Use role instead.
role
Returns true if the specified group exists anywhere within the hierarchy.
Use role_exists instead.
role_exists
Method not implemented yet.
Give me a nudge if this would be useful.
Returns true if the specified role exists anywhere in the hierarchy underneath the current or specified role.
Returns an array or arrayref of all the role names in the hierarchy, sorted by name.
Use list_roles instead.
list_roles
Get a simple printout of the structure of your hierarchy.
This method requires Devel::Symdump.
require
If you find yourself parsing the output and using it somehow in your code, let me know, and I'll find a Better Way to provide the data. This method is just intended for quick and dirty printouts and could change at any time.
Class method.
$groups has:
keys - group names values - arrayrefs of subgroup name(s)
Sets up a hierarchy of Perl classes representing the group structure.
The hierarchy will be contained within the $namespace top level if supplied. This makes it easy to set up several independent hierarchies to use within the same process, e.g. for different websites under mod_perl.
Returns a class name through which group objects can be retrieved and other class methods called. This will be 'Tree::Authz' if no namespace is specified.
If called with a $namespace argument, then all loaded packages within the $namespace::Tree::Authz symbol table hierarchy are removed (using Symbol::delete_package from the symbol table. This is experimental and may lead to bugs, the jury is still out. The purpose of this is to allow re-initialisation of the setup within a long-running process such as mod_perl. It could also allow dynamic updates to the hierarchy.
$namespace::Tree::Authz
Tree::Authz can be used independently of a persistence mechanism via setup_hierarchy. However, if you want to manipulate the hierarchy at runtime, a persistence mechanism is required. The implementation is left up to you, but the API is defined. The persistence API should be implemented by the object passed to setup_from_database.
setup_from_database
$database should be an object that responds to the persistence API defined below. The object is stored as class data and is available via the _database method.
_database
The following methods are passed on to the database object, after checking whether any changes would result in a recursive inheritance pattern, in which case they return false. The database methods should return true on success.
Returns a hashref. Keys are role names, values are arrayrefs of subroles.
setup_from_database calls this method on the database object, then passes the data on to setup_hierarchy.
Adds a new role to the scheme.
$parent is required, so no new top-level roles can be inserted. It's up to you to decide whether to raise an error or just return if $parent is omitted.
$children can be a role name or an arrayref of role names. Defaults to 'base' if omitted. It might be worth checking if these roles already exist.
'base'
At the moment I am assuming no multiple inheritance, but things are shaping up to look like there's no great difficulty about allowing it. If allowed, this method should check if $new_role already exists. If it does, ignore any $children (probably raise a warning), add <$new_role> to the sub-roles list of $parent, and return without trying to insert $new_role into the database (because it already exists).
Removes the role from the database, including finding and removing any occurrences of $role in the sub-role lists of other roles.
Returns the list of subroles for the role that was removed, in case you want to put them somewhere else.
Makes $role a sub-role of $to, and deletes it from the sub-roles list of its current parent.
Adds a subrole to a role. Must remove 'base' from the subroles list if present.
Removes a subrole from a role. If the resulting list of subroles would be empty, must insert 'base'.
Class method version of Tree::Authz::Role::setup_permissions.
Tree::Authz::Role::setup_permissions
Use setup_permissions_on_role instead.
setup_permissions_on_role
Class method version of Tree::Authz::Role::setup_abilities.
Tree::Authz::Role::setup_abilities
Use setup_abilities_on_role instead.
setup_abilities_on_role
Class method version of Tree::Authz::Role::setup_plugins.
Tree::Authz::Role::setup_plugins
Deprecated version of setup_plugins_on_role.
setup_plugins_on_role
The deprecation policy is:
1) DEPRECATED methods issue a warning (via carp) and then call the new method. They will be documented next to the replacement method.
carp
2) OBSOLETE methods will croak. These will be documented in a separate section.
3) Removed methods will be documented in a separate section, in the first version they no longer exist in.
Main changes in 0.02
- changed terminology to refer to I<roles> instead of I<groups>. Deprecated all methods with I<role> in their name. These methods now issue a warning via C<carp>, and will be removed in a future release. - added a new class to represent a role - L<Tree::Authz::Role|Tree::Authz::Role>. L<Tree::Authz|Tree::Authz> is now a static class (all its methods are class methods). The objects it returns from some methods are subclasses of L<Tree::Authz::Role|Tree::Authz::Role>.
Roles are now represented by their own class. This should make it easier to add constraints and other RBAC features.
More methods for returning meta information, e.g. immediate subroles of a role, all subroles of a role, list available actions of a role and its subroles.
Might be nice to register users with roles.
Make role objects be singletons - not necessary if the only data they carry is their own name.
Under mod_perl, all setup of hierarchies and permissions must be completed during server startup, before the startup process forks off Apache children. It would be nice to have some way of communicating updates to other processes. Alternatively, you could run the full startup sequence every time you need to access a Tree::Authz role, but that seems sub-optimal.
Lingua::EN::Inflect::Number, Class::Data::Inheritable.
Optional - Devel::Symdump.
Sub::Override for the test suite.
Please report all bugs via the CPAN Request Tracker at http://rt.cpan.org/NoAuth/Bugs.html?Dist=Tree-Authz.
Copyright 2004 by David Baird.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
David Baird, cpan@riverside-cms.co.uk
cpan@riverside-cms.co.uk
DBIx::UserDB, Data::ACL.
To install Tree::Authz, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Tree::Authz
CPAN shell
perl -MCPAN -e shell install Tree::Authz
For more information on module installation, please visit the detailed CPAN module installation guide.