Authen-Htpasswd-Trac
view release on metacpan or search on metacpan
lib/Authen/Htpasswd/Trac.pm view on Meta::CPAN
package Authen::Htpasswd::Trac;
use strict;
use warnings;
use Carp;
use DBI;
use SQL::Abstract;
use base qw( Authen::Htpasswd );
__PACKAGE__->mk_accessors($_) for qw( dbh table sql );
our $VERSION = '0.00004';
sub new {
my $class = shift;
my $file = shift;
my $args = shift;
my $self = $class->SUPER::new( $file, $args );
unless ( exists $args->{trac} ) {
croak "trac is empty.";
}
unless ( -f $args->{trac} ) {
croak "could not find trac database.";
}
$self->sql( SQL::Abstract->new );
$self->table( $args->{table} ? $args->{table} : 'permission' );
$self->dbh( $self->_dbh( $args->{trac} ) );
$self;
}
sub _dbh {
my $self = shift;
my $db = shift;
my $connect_info
= [ 'dbi:SQLite:dbname=' . $db, '', '', { AutoCommit => 1 } ];
my $dbh = DBI->connect(@$connect_info)
|| croak "could not connect database($db)";
$self->dbh($dbh);
}
sub find_user_permissions {
my ( $self, $username, $password ) = @_;
my $user = $self->lookup_user($username);
croak "could not find user $username" unless $user;
if ( $user->check_password($password) ) {
return $self->_find_permissions($username);
}
return ();
}
sub _find_permissions {
my $self = shift;
my $username = shift;
my ( $stmt, @binds )
= $self->sql->select( $self->table, 'action',
{ username => $username } );
my $sth = $self->dbh->prepare($stmt);
$sth->execute(@binds);
return map { $_->[0] } @{ $sth->fetchall_arrayref };
}
sub add_permission {
my ( $self, $username, $action ) = @_;
my ( $stmt, @binds )
= $self->sql->insert( $self->table, [ $username, $action ], );
( run in 1.542 second using v1.01-cache-2.11-cpan-54e63673c56 )