CGI-Builder-Auth

 view release on metacpan or  search on metacpan

lib/CGI/Builder/Auth/GroupAdmin/SQL.pm  view on Meta::CPAN

# $Id: SQL.pm,v 1.1.1.1 2004/06/28 19:24:28 veselosky Exp $
package CGI::Builder::Auth::GroupAdmin::SQL;
use strict;
use DBI;
use Carp ();
use vars qw(@ISA $VERSION);
@ISA = qw(CGI::Builder::Auth::GroupAdmin);
$VERSION = (qw$Revision: 1.1.1.1 $)[1];

my %Default = (
	       HOST => "",                  #server hostname
               PORT => "",                  #server port
	       DB => "",                    #database name
	       USER => "", 	            #database login name	    
	       AUTH => "",                  #database login password
	       DRIVER => "mSQL",            #driver for DBI
	       GROUPTABLE => "",             #table with field names below
	       NAMEFIELD => "user",         #field for the name
	       GROUPFIELD => "group",       #field for the group
	       );

sub new {
    my($class) = shift;
    my $self = bless { %Default, @_ } => $class;
    $self->_check(qw(DRIVER DB GROUPTABLE)); 
    if(!defined $self->{'DBH'}) { $self->db($self->{DB}) }
    else { $self->{'_DBH'} = $self->{'DBH'}; };
    return $self;
}

sub DESTROY {
    my($self) = @_;
    #Don't disconnect if you didn't make it.
    $self->{'_DBH'}->disconnect if(!defined $self->{'DBH'});
}

sub db {
    my($self,$db) = @_;
    my $old = $self->{DB};
    return $old unless $db;
    $self->{DB} = $db; 

    if(defined $self->{'_DBH'}) {
	$self->{'_DBH'}->disconnect;
    }

    my $source = sprintf("dbi:%s:%s",@{$self}{qw(DRIVER DB)});
    $source .= ":$self->{HOST}" if $self->{HOST};
    $source .= ":$self->{PORT}" if $self->{HOST} and $self->{PORT};
    $self->{'_DBH'} = DBI->connect($source,@{$self}{qw(USER AUTH)} ) 
	|| Carp::croak($DBI::errstr);
    return $old;
}

package CGI::Builder::Auth::GroupAdmin::SQL::_generic;
use vars qw(@ISA);
@ISA = qw(CGI::Builder::Auth::GroupAdmin::SQL);

sub create { return shift->add(@_); }


sub add {
	my $self = shift;
    my($groupname,$username) = @_;
    my $statement;
    #This function is sometimes called to create a group.  So it has to be able to accept only a group name, and no username.     #It will then create a placeholder that has no username, but a groupname.
    #return(0, "add_group: no user name!") unless $username;  
    if(defined $username) {
    return(0, "add_group: no group!") unless $groupname;
    return(0, "user '$username' already exists in group '$groupname'") 
	if $self->exists($groupname,$username);

    $statement = 
	$self->{GROUPTABLE} ne $self->{USERTABLE} ?
	    sprintf("INSERT into %s (%s,%s)\n VALUES (?,?)\n",
		    @{$self}{ qw(GROUPTABLE NAMEFIELD GROUPFIELD) })
		:
            sprintf("UPDATE %s\n SET %s=?\n WHERE %s=?\n",
		    $self->{GROUPTABLE},$self->{NAMEFIELD},
		    $self->{GROUPFIELD});
			    
    #print STDERR $statement if $self->debug;
    }
    else {

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.079 second using v1.00-cache-1.14-grep-28634ff-cpan-ac32402124b )