CGI-Builder-Auth
view release on metacpan or search on metacpan
lib/CGI/Builder/Auth/UserAdmin/SQL.pm view on Meta::CPAN
}
package CGI::Builder::Auth::UserAdmin::SQL::_generic;
use vars qw(@ISA);
@ISA = qw(CGI::Builder::Auth::UserAdmin::SQL);
sub add {
my($self, $username, $passwd, $other) = @_;
return(0, "add_user: no user name!") unless $username;
return(0, "add_user: no password!") unless $passwd;
return(0, "user '$username' already exists!")
if $self->exists($username);
my(%f) = ($self->{NAMEFIELD}=>$username,
$self->{PASSWORDFIELD}=>$self->encrypt($passwd));
if ($other) {
Carp::croak('Specify other fields as a hash ref for SQL databases')
unless ref($other) eq 'HASH';
foreach (keys %{$other}) {
$f{$_} = $other->{$_};
}
}
my $statement =
sprintf("INSERT into %s (%s)\n VALUES (%s)\n",
$self->{USERTABLE},
join(',',keys %f),
join(',', map {$self->{'_DBH'}->quote($f{$_})} keys %f));
#This _is_string this is silly. It should be handled in the DBI::quote function.
#Further, if you really want to do that, the fast way is to use Scalar::Util::Numeric
#join(',', map {$self->_is_string($_,$f{$_}) ? $self->{'_DBH'}->quote($f{$_}) : $f{$_} } keys %f));
print STDERR $statement if $self->debug;
$self->{'_DBH'}->do($statement) || Carp::croak($DBI::errstr);
1;
}
sub exists {
my($self, $username) = @_;
my $statement =
sprintf("SELECT %s from %s WHERE %s=%s\n",
@{$self}{qw(PASSWORDFIELD USERTABLE NAMEFIELD)}, $self->{'_DBH'}->quote($username));
print STDERR $statement if $self->debug;
my $sth = $self->{'_DBH'}->prepare($statement);
Carp::carp("Cannot prepare sth ($DBI::err): $DBI::errstr")
unless $sth;
$sth->execute || Carp::croak($DBI::errstr);
my(@row) = $sth->fetchrow;
$sth->finish;
return $row[0];
}
sub delete {
my($self, $username) = @_;
my $statement =
sprintf("DELETE from %s where %s=%s\n",
@{$self}{qw(USERTABLE NAMEFIELD)}, $self->{'_DBH'}->quote($username));
print STDERR $statement if $self->debug;
$self->{'_DBH'}->do($statement) || Carp::croak($DBI::errstr);
}
sub update {
my($self, $username, $passwd,$other) = @_;
return 0 unless $self->exists($username);
my(%f);
if ($other) {
Carp::croak('Specify other fields as a hash ref for SQL databases')
unless ref($other) eq 'HASH';
foreach (keys %{$other}) {
$f{$_} = $other->{$_};
}
}
$f{$self->{PASSWORDFIELD}}=$self->encrypt($passwd) if $passwd;
my $statement =
sprintf("UPDATE %s SET %s\n WHERE %s = '%s'\n",
$self->{USERTABLE},
join(',', map {$_ . "=" . $self->{_DBH}->quote($f{$_}) } keys %f),
#join(',', map {$_ . "=" . ($self->_is_string($_,$f{$_}) ? "'$f{$_}'" : $f{$_}) } keys %f),
$self->{NAMEFIELD}, $username);
print STDERR $statement if $self->debug;
$self->{'_DBH'}->do($statement) || Carp::croak($DBI::errstr);
}
sub list {
my($self) = @_;
my $statement =
sprintf("SELECT %s from %s\n",
@{$self}{qw(NAMEFIELD USERTABLE)});
print STDERR $statement if $self->debug;
my $sth = $self->{'_DBH'}->prepare($statement);
Carp::carp("Cannot prepare sth ($DBI::err): $DBI::errstr")
unless $sth;
$sth->execute || Carp::croak($DBI::errstr);
my($user,@list);
while($user = $sth->fetchrow) {
push(@list, $user);
}
$sth->finish;
return @list;
}
sub fetch {
my($self,$username,@fields) = @_;
return(0, "fetch: no user name!") unless $username;
return(0, "fetch: user '$username' doesn't exist")
unless $self->exists($username);
my (@f);
foreach (@fields) {
push(@f,ref($_) ? @$_ : $_);
}
push (@f,'*') unless @f;
my $statement =
sprintf("SELECT %s FROM %s WHERE %s = %s",
join(',',@f),
@{$self}{qw/USERTABLE NAMEFIELD/},
$self->{'_DBH'}->quote($username));
print STDERR $statement if $self->debug;
my $sth = $self->{'_DBH'}->prepare($statement);
Carp::carp("Cannot prepare sth ($DBI::err): $DBI::errstr")
( run in 0.735 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )