2009-12-10 12:30:43 +01:00
|
|
|
## @file
|
|
|
|
# DBI userDB mechanism
|
|
|
|
|
|
|
|
## @class
|
|
|
|
# DBI userDB mechanism class
|
|
|
|
package Lemonldap::NG::Portal::UserDBDBI;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use Lemonldap::NG::Portal::Simple;
|
2011-01-25 17:41:31 +01:00
|
|
|
use Lemonldap::NG::Portal::_DBI; #inherits
|
2009-12-10 12:30:43 +01:00
|
|
|
|
2014-02-18 16:17:09 +01:00
|
|
|
our $VERSION = '1.4.0';
|
2009-12-10 12:30:43 +01:00
|
|
|
|
|
|
|
## @apmethod int userDBInit()
|
|
|
|
# Set default values
|
|
|
|
# @return Lemonldap::NG::Portal constant
|
|
|
|
sub userDBInit {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# DBI access to user is the same as authentication by default
|
|
|
|
$self->{dbiUserChain} ||= $self->{dbiAuthChain};
|
|
|
|
$self->{dbiUserUser} ||= $self->{dbiAuthUser};
|
|
|
|
$self->{dbiUserPassword} ||= $self->{dbiAuthPassword};
|
|
|
|
$self->{dbiUserTable} ||= $self->{dbiAuthTable};
|
|
|
|
$self->{userPivot} ||= $self->{dbiAuthLoginCol};
|
|
|
|
|
|
|
|
PE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
## @apmethod int getUser()
|
|
|
|
# Do nothing
|
|
|
|
# @return Lemonldap::NG::Portal constant
|
|
|
|
sub getUser {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Connect
|
2009-12-11 22:17:06 +01:00
|
|
|
my $dbh =
|
|
|
|
$self->dbh( $self->{dbiUserChain}, $self->{dbiUserUser},
|
|
|
|
$self->{dbiUserPassword} );
|
2009-12-10 12:30:43 +01:00
|
|
|
return PE_ERROR unless $dbh;
|
|
|
|
|
|
|
|
my $table = $self->{dbiUserTable};
|
|
|
|
my $pivot = $self->{userPivot};
|
2009-12-19 09:57:59 +01:00
|
|
|
my $user = $self->{user};
|
2010-01-21 18:38:55 +01:00
|
|
|
|
|
|
|
# If in mailProcess, adapt search criteriums
|
|
|
|
if ( $self->{mail} ) {
|
|
|
|
$pivot = $self->{dbiPasswordMailCol};
|
|
|
|
$user = $self->{mail};
|
|
|
|
}
|
|
|
|
|
2009-12-19 09:57:59 +01:00
|
|
|
$user =~ s/'/''/g;
|
2009-12-30 20:42:17 +01:00
|
|
|
my $sth;
|
2009-12-19 09:57:59 +01:00
|
|
|
|
|
|
|
eval {
|
2010-10-23 10:09:41 +02:00
|
|
|
$sth = $dbh->prepare("SELECT * FROM $table WHERE $pivot=?");
|
|
|
|
$sth->execute($user);
|
2009-12-19 09:57:59 +01:00
|
|
|
};
|
|
|
|
if ($@) {
|
|
|
|
$self->lmLog( "DBI error: $@", 'error' );
|
|
|
|
return PE_ERROR;
|
|
|
|
}
|
2009-12-10 12:30:43 +01:00
|
|
|
|
2009-12-30 20:42:17 +01:00
|
|
|
unless ( $self->{entry} = $sth->fetchrow_hashref() ) {
|
2013-10-13 14:15:01 +02:00
|
|
|
$self->_sub( 'userNotice', "User $user not found" );
|
2009-12-30 20:42:17 +01:00
|
|
|
return PE_BADCREDENTIALS;
|
|
|
|
}
|
2012-09-25 17:22:50 +02:00
|
|
|
|
|
|
|
# In mail process, get user value
|
|
|
|
if ( $self->{mail} ) {
|
|
|
|
$table = $self->{dbiAuthTable};
|
|
|
|
$pivot = $self->{dbiAuthLoginCol};
|
|
|
|
$user = $self->{entry}->{ $self->{userPivot} };
|
|
|
|
eval {
|
|
|
|
$sth = $dbh->prepare("SELECT * FROM $table WHERE $pivot=?");
|
|
|
|
$sth->execute($user);
|
|
|
|
};
|
|
|
|
if ($@) {
|
|
|
|
$self->lmLog( "DBI error: $@", 'error' );
|
|
|
|
return PE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $results;
|
|
|
|
|
|
|
|
unless ( $results = $sth->fetchrow_hashref() ) {
|
2013-10-13 14:15:01 +02:00
|
|
|
$self->_sub( 'userNotice', "User $user not found" );
|
2012-09-25 17:22:50 +02:00
|
|
|
return PE_BADCREDENTIALS;
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->{user} = $results->{$pivot};
|
|
|
|
}
|
|
|
|
|
2009-12-19 09:57:59 +01:00
|
|
|
PE_OK;
|
|
|
|
}
|
2009-12-10 12:30:43 +01:00
|
|
|
|
2009-12-19 09:57:59 +01:00
|
|
|
## @apmethod int setSessionInfo()
|
|
|
|
# Get columns for each exportedVars
|
|
|
|
# @return Lemonldap::NG::Portal constant
|
|
|
|
sub setSessionInfo {
|
|
|
|
my $self = shift;
|
2009-12-10 12:30:43 +01:00
|
|
|
|
2012-09-25 17:22:50 +02:00
|
|
|
# Set _user unless already defined
|
|
|
|
$self->{sessionInfo}->{_user} ||= $self->{user};
|
|
|
|
|
2014-02-19 15:35:59 +01:00
|
|
|
my %vars = ( %{ $self->{exportedVars} }, %{ $self->{dbiExportedVars} } );
|
|
|
|
while ( my ( $var, $attr ) = each %vars ) {
|
2014-02-18 16:17:09 +01:00
|
|
|
$self->{sessionInfo}->{$var} = $self->{entry}->{$attr}
|
|
|
|
if ( defined $self->{entry}->{$attr} );
|
|
|
|
}
|
|
|
|
|
2009-12-10 12:30:43 +01:00
|
|
|
PE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
## @apmethod int setGroups()
|
|
|
|
# Do nothing
|
|
|
|
# @return Lemonldap::NG::Portal constant
|
|
|
|
sub setGroups {
|
|
|
|
PE_OK;
|
|
|
|
}
|
2010-08-23 16:52:53 +02:00
|
|
|
|
|
|
|
## @method boolean setUserDBValue(string key, string value)
|
|
|
|
# Store a value in UserDB
|
|
|
|
# @param key Key in user information
|
|
|
|
# @param value Value to store
|
|
|
|
# @return result
|
|
|
|
sub setUserDBValue {
|
2016-01-02 10:29:05 +01:00
|
|
|
my ( $self, $key, $value ) = @_;
|
2010-08-23 16:52:53 +02:00
|
|
|
|
|
|
|
# Mandatory attributes
|
|
|
|
return 0 unless defined $key;
|
|
|
|
|
|
|
|
# Write in database
|
|
|
|
$self->lmLog( "Replace $key attribute in database with value $value",
|
|
|
|
'debug' );
|
|
|
|
|
|
|
|
# Connect
|
|
|
|
my $dbh =
|
|
|
|
$self->dbh( $self->{dbiUserChain}, $self->{dbiUserUser},
|
|
|
|
$self->{dbiUserPassword} );
|
|
|
|
return 0 unless $dbh;
|
|
|
|
|
|
|
|
my $table = $self->{dbiUserTable};
|
|
|
|
my $pivot = $self->{userPivot};
|
|
|
|
my $user = $self->{user};
|
|
|
|
|
|
|
|
$user =~ s/'/''/g;
|
|
|
|
my $sth;
|
|
|
|
|
|
|
|
eval {
|
2010-10-23 10:09:41 +02:00
|
|
|
$sth = $dbh->prepare("UPDATE $table SET $key = $value WHERE $pivot=?");
|
|
|
|
$sth->execute($user);
|
2010-08-23 16:52:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
# Check result
|
|
|
|
if ($@) {
|
|
|
|
$self->lmLog( "DBI error: $@", 'error' );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-12-10 12:30:43 +01:00
|
|
|
1;
|
|
|
|
|