Apache2-AuthAny
view release on metacpan or search on metacpan
lib/Apache2/AuthAny/DB.pm view on Meta::CPAN
my ($UID) = @_;
my $getRoleSql = 'select role from userRoleChoice WHERE UID = ?';
$self->useDB();
return $dbHandle->selectcol_arrayref($getRoleSql, undef, $UID);
}
sub getUserIdentities {
my $self = shift;
my ($UID) = @_;
my $sql = 'SELECT * FROM userIdent WHERE UID = ?';
$self->useDB();
return $dbHandle->selectall_arrayref($sql, { Slice => {} }, $UID);
}
sub getUserTiers {
my $self = shift;
my ($UID) = @_;
my $getTierSql = 'select tier from userTier WHERE UID = ?';
$self->useDB();
return $dbHandle->selectall_arrayref($getTierSql, { Slice => {} }, $UID);
}
sub addUser {
my $self = shift;
my %user = @_;
my @valid_cols = qw[username firstName lastName active];
my @cols;
my @passed_values;
my @values;
foreach my $col (@valid_cols) {
if (exists $user{$col}) {
push @cols, $col;
push @passed_values, $user{$col};
push @values, '?';
}
}
push @cols, 'created';
push @values, 'now()';
my $col_list = join(",", @cols);
my $value_list = join(",", @values);
my $sql = "INSERT INTO user ($col_list) VALUES ($value_list)";
$self->useDB();
if ( $dbHandle->do($sql, undef, @passed_values) ) {
my $UID = $dbHandle->last_insert_id(undef, undef, undef, undef);
return $UID;
} else {
warn $dbHandle->errstr;
return undef;
}
}
sub updateUser {
my $self = shift;
my %user = @_;
my $existingUser = $self->getUserByUsername($user{username}) || {};
my $UID = $existingUser->{UID};
unless ($UID) {
warn "User, '$user{username}' does not exists\n";
return;
}
my @valid_cols = qw[firstName lastName active];
my @sets;
my @passed_values;
foreach my $col (@valid_cols) {
if (exists $user{$col}) {
push @sets, "$col = ?";
push @passed_values, $user{$col};
}
}
my $set_list = join(",", @sets);
my $sql = "UPDATE user SET $set_list WHERE UID = ?";
$self->useDB();
if ( $dbHandle->do($sql, undef, @passed_values, $UID) ) {
return $UID;
} else {
warn $dbHandle->errstr;
return undef;
}
}
sub addUserIdent {
my $self = shift;
my ($UID, $authId, $authProvider) = @_;
# if (
# $UID !~ /^(\d+)$/
# || $authId !~ /^(.+)$/
# || $authProvider !~ /^(uw|basic|openid|protectnet|google|ldap)$/) {
# warn "bad input, '@_'";
# return;
# }
# Make sure there is a user with $UID
# This would not be necessary if we were using tables with foreign keys
unless ($self->getUserByUID($UID)) {
warn "UID, '$UID' not found in user table";
return;
}
# make sure $authId and $authProvider do not already exist
# A composite index in the DB should assure this, however we are using MyISAM
# TODO: check that authId/authProvider do not exists
my $sql = 'INSERT INTO userIdent (UID, authId, authProvider) VALUES (?, ?, ?)';
$self->useDB();
if ($dbHandle->do($sql, undef, $UID, $authId, $authProvider)) {
lib/Apache2/AuthAny/DB.pm view on Meta::CPAN
my $pCookie = $pid->{PID};
unless ($pCookie) {
warn "Missing pid. Got input @_";
return 0;
}
my $logout_key = md5_hex(time . rand);
my $sql = "UPDATE userAACookie SET state = ?, logoutKey = ?
WHERE PID = ?";
$self->useDB();
if ($dbHandle->do($sql, undef, 'logged_out', $logout_key, $pCookie)) {
$pid->{state} = 'logged_out';
return 1;
} else {
warn $dbHandle->errstr;
return 0;
}
}
sub statePCookie {
my $self = shift;
my ($pid, $state) = @_;
my $pCookie = $pid->{PID};
unless ($pCookie && $state) {
warn "Missing pid or state. Got input @_";
return 0;
}
my $sql = "UPDATE userAACookie SET state = ?
WHERE PID = ?";
$self->useDB();
if ($dbHandle->do($sql, undef, $state, $pCookie)) {
$pid->{state} = $state;
return 1;
} else {
warn $dbHandle->errstr;
return 0;
}
}
sub insertPCookie {
my $self = shift;
my ($pCookie, $sCookie, $logout_key) = @_;
unless ($pCookie && $sCookie && $logout_key) {
warn "Missing cookies or logout_key. Got input @_";
return 0;
}
my $sql = "INSERT INTO userAACookie (PID, SID, logoutKey, last, created)
VALUES (?, ?, ?, ?, now())";
$self->useDB();
if ($dbHandle->do($sql, undef, $pCookie, $sCookie, $logout_key, time)) {
return 1;
} else {
warn $dbHandle->errstr;
return 0;
}
}
sub updatePCookieLastAccess {
my $self = shift;
my ($pCookie) = @_;
unless ($pCookie) {
warn "Missing pid";
return 0;
}
my $sql = "UPDATE userAACookie
SET last = ?
WHERE pid = ?";
$self->useDB();
if ($dbHandle->do($sql, undef, time, $pCookie) eq 1) {
return 1;
} else {
warn "Could not update DB with PID, '$pCookie'" . $dbHandle->errstr;
return 0;
}
}
sub updatePCookieLogoutKey {
my $self = shift;
my ($pCookie) = @_;
unless ($pCookie) {
warn "Missing pid";
return 0;
}
my $sql = "UPDATE userAACookie
SET logoutKey = ?
WHERE pid = ?";
$self->useDB();
my $logout_key = md5_hex(time . rand);
if ($dbHandle->do($sql, undef, $logout_key, $pCookie)) {
return 1;
} else {
warn $dbHandle->errstr;
return 0;
}
}
sub cleanupCookies {
my $self = shift;
$self->useDB();
my $sql = qq[DELETE FROM userAACookie WHERE authId IS NULL and now() - created > 300];
my $rc = $dbHandle->do($sql);
if ($rc) {
return $rc;
} else {
warn $dbHandle->errstr;
return 0;
}
}
1;
( run in 1.358 second using v1.01-cache-2.11-cpan-9581c071862 )