The new Authentication tutorial is now available on CPAN. Please use that instead of the example below.
The most up-to-date version is always on SVN. Use pod2html from your Perl's bin directory to convert it to HTML.
If you are implementing authentication and authorization in your Catalyst application, you may be having some difficulty recently. Authentication and Authorization is now implemented differently than is shown in the Catalyst::Manual::Tutorial (5.701003). Until related documentation has been updated, I hope that this will help you apply authentication and authorization to your own application.
This is not a full fledged tutorial and I do not presume to have a high level knowledge of all related components. What I am presenting here, however, is what worked for me. For familiarity, I'm basing this example directly on the MyApp tutorial from the Catalyst::Manual documentation. So, if you are familiar with that then the changes here should be clear and easily carried over into your own custom application as needed.
As always, feel free to request additional help in #catalyst, which is where I received the help I needed to implement this myself. My hope is that my presentation here is adequate enough that you will be able to implement this on your own with little stress. Only a few small changes are needed.
In short, the changes consist of the following:
See the brief list of suggested reading material (cpan docs) at the end of this document.
First, if you follow the Authentication tutorial's example, you have a database like the following.
CREATE TABLE users (id INTEGER PRIMARY KEY, username TEXT, password TEXT, email_address TEXT, first_name TEXT, last_name TEXT, active INTEGER);
You do not need to alter anything in your database.
If you've created your DBIx schema according to the same documentation, it should result in the following classes.
The only thing you will need to change in your User class is to add a second relationship below has_many. This new line will be a many_to_many relationship that says User can have many Roles and they will be identified by the role field in the map_user_roles relation. So, add the following:
__PACKAGE__->many_to_many( roles => 'map_user_role', 'role' );
Nothing needs to change in your Role class.
Nothing needs to be changed in your UserRole class.
Your MyApp.pm should currently include the following:
use Catalyst qw/ . . . Authentication Authentication::Store::DBIC Authentication::Credential::Password
You can change this to read as follows:
use Catalyst qw/ . . . Authentication
If you stored your configuration in YAML (myapp.yml), it probably includes the following:
authentication: dbic: user_class: MyAppDB::User user_field: username password_field: password
You'll need to change it to look like the following. Notice that configuration is no longer stored under
authorization->dbic, but that the authorization details are now placed under
authentication->realms->default->store. Also note that some of the fields have changed slightly. (Do not copy and paste the YAML content as proper indentation is crucial).
authentication: realms: default: credential: class: Password password_field: password password_type: clear store: class: DBIx::Class user_class: DB::User id_field: id role_relation: roles role_field: role
In this new YAML configuration...
refers to MyAppDB::User, up above.
The field your user is identified by in your Users table.
The name of the the many_to_many relationship we defined in MyAppDB::User
The name of the field in the Roles table that identifies the text names of your roles (ie 'admin').