| Catalyst-Authentication-Store-DBI documentation | Contained in the Catalyst-Authentication-Store-DBI distribution. |
Catalyst::Authentication::Store::DBI - Storage class for Catalyst Authentication using DBI
use Catalyst qw(Authentication);
__PACKAGE__->config->{'authentication'} = {
'default_realm' => 'default',
'realms' => {
'default' => {
'credential' => {
'class' => 'Password',
'password_field' => 'password',
'password_type' => 'hashed',
'password_hash_type' => 'SHA-1',
},
'store' => {
'class' => 'DBI',
'user_table' => 'login',
'user_key' => 'id',
'user_name' => 'name',
'role_table' => 'authority',
'role_key' => 'id',
'role_name' => 'name',
'user_role_table' => 'competence',
'user_role_user_key' => 'login',
'user_role_role_key' => 'authority',
},
},
},
};
sub login :Global
{
my ($self, $c) = @_;
my $req = $c->request();
# catch login failures
unless ($c->authenticate({
'name' => $req->param('name'),
'password' => $req->param('password'),
})) {
...
}
...
}
sub something :Path
{
my ($self, $c) = @_;
# handle missing role case
unless ($c->check_user_roles('editor')) {
...
}
...
}
This module implements the Catalyst::Authentication API using Catalyst::Model::DBI.
It uses DBI to let your application authenticate users against a database and it provides support for Catalyst::Plugin::Authorization::Roles.
Simon Bertrang, <simon.bertrang@puzzworks.com>
Copyright (c) 2008 PuzzWorks OHG, http://puzzworks.com/
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| Catalyst-Authentication-Store-DBI documentation | Contained in the Catalyst-Authentication-Store-DBI distribution. |
package Catalyst::Authentication::Store::DBI; use strict; use warnings; use Catalyst::Authentication::Store::DBI::User; our $VERSION = '0.01';
# instantiates the store object sub new { my ($class, $config, $app, $realm) = @_; unless (defined($config) && ref($config) eq 'HASH') { Catalyst::Exception->throw(__PACKAGE__ . ' needs a hashref for configuration'); } my $self = {%$config}; bless($self, $class); return $self; }
# locates a user using data contained in the hashref sub find_user { my ($self, $authinfo, $c) = @_; my $sql; my $sth; my %user; unless ($self->{'dbh'}) { $self->{'dbh'} = $c->model('DBI')->dbh(); } my $dbh = $self->{'dbh'}; my @col = map { $_ } sort(keys(%$authinfo)); $sql = 'SELECT * FROM ' . $self->{'user_table'} . ' WHERE ' . join(' AND ', map { $_ . ' = ?' } @col); $sth = $dbh->prepare($sql) or die($dbh->errstr()); $sth->execute(@$authinfo{@col}) or die($dbh->errstr()); $sth->bind_columns(\( @user{ @{ $sth->{'NAME_lc'} } } )) or die($dbh->errstr()); unless ($sth->fetch()) { $sth->finish(); return undef; } $sth->finish(); unless (exists($user{$self->{'user_key'}}) && length($user{$self->{'user_key'}})) { return undef; } return Catalyst::Authentication::Store::DBI::User->new($self, \%user); }
sub for_session { my ($self, $c, $user) = @_; return $user->id(); }
sub from_session { my ($self, $c, $frozen) = @_; my $sql; my $sth; my %user; unless ($self->{'dbh'}) { $self->{'dbh'} = $c->model('DBI')->dbh(); } my $dbh = $self->{'dbh'}; $sql = 'SELECT * FROM ' . $self->{'user_table'} . ' WHERE ' . $self->{'user_key'} . ' = ?'; $sth = $dbh->prepare($sql) or die($dbh->errstr()); $sth->execute($frozen) or die($dbh->errstr()); $sth->bind_columns(\( @user{ @{ $sth->{'NAME_lc'} } } )) or die($dbh->errstr()); unless ($sth->fetch()) { $sth->finish(); return undef; } $sth->finish(); unless (exists($user{$self->{'user_key'}}) && length($user{$self->{'user_key'}})) { return undef; } return Catalyst::Authentication::Store::DBI::User->new($self, \%user); }
sub user_supports { my $self = shift; return; }
1;