<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Catalyst::Authentication::Realm::SimpleDB;

use strict;
use warnings;
use Catalyst::Exception;
use base qw/Catalyst::Authentication::Realm/;

sub new {
    my ($class, $realmname, $config, $app) = @_;

    my $newconfig = {
        credential =&gt; {
            class =&gt; 'Password',
            password_type =&gt; 'clear'
        },
        store =&gt; {
            class =&gt; 'DBIx::Class',
            role_relation =&gt; 'roles',
            role_field =&gt; 'role',
            use_userdata_from_session =&gt; '1'
        }
    };

    if (!defined($config-&gt;{'user_model'})) {
	    Catalyst::Exception-&gt;throw("Unable to initialize authentication, no user_model specified in SimpleDB config.");
   	}


    ## load any overrides for the credential
    foreach my $key (qw/ password_type password_field password_hash_type/) {
        if (exists($config-&gt;{$key})) {
            $newconfig-&gt;{credential}{$key} = $config-&gt;{$key};
        }
    }

    ## load any overrides for the store
    foreach my $key (qw/ user_model role_relation role_field role_column use_userdata_from_session/) {
        if (exists($config-&gt;{$key})) {
            $newconfig-&gt;{store}{$key} = $config-&gt;{$key};
        }
    }
    if (exists($newconfig-&gt;{'store'}{'role_column'})) {
        delete $newconfig-&gt;{'store'}{'role_relation'};
        delete $newconfig-&gt;{'store'}{'role_field'};
    }

    return $class-&gt;SUPER::new($realmname, $newconfig, $app);
}

1;
__END__

=head1 NAME

Catalyst::Authentication::Realm::SimpleDB - A simplified Catalyst authentication configurator.

=head1 SYNOPSIS

    use Catalyst qw/
        Authentication
    /;

    __PACKAGE__-&gt;config-&gt;{'Plugin::Authentication'} =
        {
            default =&gt; {
                class      =&gt; 'SimpleDB',
                user_model =&gt; 'MyApp::Schema::Users',
            }
        }

    # later on ...
    $c-&gt;authenticate({ username =&gt; 'myusername',
                       password =&gt; 'mypassword' });

    my $age = $c-&gt;user-&gt;get('age');

    $c-&gt;logout;


=head1 DESCRIPTION

The Catalyst::Authentication::Realm::SimpleDB provides a simple way to configure Catalyst Authentication
when using the most common configuration of a password protected user retrieved from an SQL database.

=head1 CONFIGURATION

The SimpleDB Realm class configures the Catalyst authentication system based on the following:

=over

=item *
Your user data is stored in a table that is accessible via $c-&gt;model($cfg-&gt;{user_model});

=item *
Your passwords are stored in the 'password' field in your users table and are not encrypted.

=item *
Your roles for users are stored in a separate table and are directly
accessible via a DBIx::Class relationship called 'roles' and the text of the
role is stored in a field called 'role' within the role table.

=item *
Your user information is stored in the session once the user is authenticated.

=back

For the above usage, only one configuration option is necessary, 'user_model'.
B&lt;user_model&gt; should contain the B&lt;class name of your user class&gt;. See the
L&lt;/PREPARATION&gt; section for info on how to set up your database for use with
this module.

If your system differs from the above, some minor configuration may be
necessary. The options available are detailed below. These options match the
configuration options used by the underlying credential and store modules.
More information on these options can be found in
L&lt;Catalyst::Authentication::Credential::Password&gt; and
L&lt;Catalyst::Authentication::Store::DBIx::Class&gt;.

=over

=item user_model

Contains the class name (as passed to $c-&gt;model() ) of the DBIx::Class schema
to use as the source for user information.  This config item is B&lt;REQUIRED&gt;.

=item password_field

If your password field is not 'password' set this option to the name of your password field.  Note that if you change this
to, say 'users_password' you will need to use that in the authenticate call:

    $c-&gt;authenticate({ username =&gt; 'bob', users_password =&gt; 'foo' });

=item password_type

If the password is not stored in plaintext you will need to define what format the password is in.  The common options are
B&lt;crypted&gt; and B&lt;hashed&gt;.  Crypted uses the standard unix crypt to encrypt the password.  Hashed uses the L&lt;Digest&gt; modules to
perform password hashing.

=item password_hash_type

If you use a hashed password type - this defines the type of hashing. See L&lt;Catalyst::Authentication::Credential::Password&gt;
for more details on this setting.

=item role_column

If your users roles are stored directly in your user table, set this to the column name that contains your roles.  For
example, if your user table contains a field called 'permissions', the value of role_column would be 'permissions'.
B&lt;NOTE&gt;: If multiple values are stored in the role column, they should be space or pipe delimited.

=item role_relation and role_field

These define an alternate role relationship name and the column that holds the role's name in plain text.  See
L&lt;Catalyst::Authentication::Store::DBIx::Class/CONFIGURATION&gt; for more details on these settings.

=item use_userdata_from_session

This is a simple 1 / 0 setting which determines how a user's data is saved / restored from the session.  If
it is set to 1, the user's complete information (at the time of authentication) is cached between requests.
If it is set to 0, the users information is loaded from the database on each request.

=back


=head1 PREPARATION

This module makes several assumptions about the structure of your database.
Below is an example of a table structure which will function with this module
in it's default configuration. You can use this table structure as-is or add
additional fields as necessary. B&lt;NOTE&gt; that this is the default SimpleDB
configuration only. Your table structure can differ significantly from this
when using the L&lt;DBIx::Class
Store|Catalyst::Authentication::Store::DBIx::Class/&gt; directly.


    --
    -- note that you can add any additional columns you require to the users table.
    --
    CREATE TABLE users (
            id            INTEGER PRIMARY KEY,
            username      TEXT,
            password      TEXT,
    );

    CREATE TABLE roles (
            id   INTEGER PRIMARY KEY,
            role TEXT
    );
    CREATE TABLE user_roles (
            user_id INTEGER,
            role_id INTEGER,
            PRIMARY KEY (user_id, role_id)
    );

Also, after you have loaded this table structure into your DBIx::Class schema,
please be sure that you have a many_to_many DBIx::Class relationship defined
for the users to roles relation. Your schema files should contain something
along these lines:

C&lt;lib/MyApp/Schema/Users.pm&gt;:

    __PACKAGE__-&gt;has_many(map_user_role =&gt; 'MyApp::Schema::UserRoles', 'user_id');
    __PACKAGE__-&gt;many_to_many(roles =&gt; 'map_user_role', 'role');

C&lt;lib/MyApp/Schema/UserRoles.pm&gt;:

    __PACKAGE__-&gt;belongs_to(role =&gt; 'MyApp::Schema::Roles', 'role_id');

=head1 MIGRATION

If and when your application becomes complex enough that you need more features
than SimpleDB gives you access to, you can migrate to a standard Catalyst
Authentication configuration fairly easily.  SimpleDB simply creates a standard
Auth config based on the inputs you give it.  The config SimpleDB creates by default
looks like this:

    MyApp-&gt;config('Plugin::Authentication') = {
        default =&gt; {
            credential =&gt; {
                class =&gt; 'Password',
                password_type =&gt; 'clear'
            },
            store =&gt; {
                class =&gt; 'DBIx::Class',
                role_relation =&gt; 'roles',
                role_field =&gt; 'role',
                use_userdata_from_session =&gt; '1',
                user_model =&gt; $user_model_from_simpledb_config
	        }
	    }
    };


=head1 SEE ALSO

This module relies on a number of other modules to do it's job.  For more information
you can refer to the following:

=over

=item *
L&lt;Catalyst::Manual::Tutorial::Authentication&gt;

=item *
L&lt;Catalyst::Plugin::Authentication&gt;

=item *
L&lt;Catalyst::Authentication::Credential::Password&gt;

=item *
L&lt;Catalyst::Authentication::Store::DBIx::Class&gt;

=item *
L&lt;Catalyst::Plugin::Authorization::Roles&gt;

=back

=cut

</pre></body></html>