Commit ee62c967 authored by Clément OUDOT's avatar Clément OUDOT
Browse files

Reset password by mail for DBI backend

parent 19ae0803
......@@ -5,14 +5,32 @@
# DBI password backend class
package Lemonldap::NG::Portal::PasswordDBDBI;
use strict;
use Lemonldap::NG::Portal::Simple;
use Lemonldap::NG::Portal::AuthDBI; #inherits
use base qw(Lemonldap::NG::Portal::_DBI);
use strict;
use base qw(Lemonldap::NG::Portal::_DBI );
#use Lemonldap::NG::Portal::_SMTP; #inherits
our $VERSION = '0.1';
*passwordDBInit = *Lemonldap::NG::Portal::AuthDBI::authInit;
## @apmethod int passwordDBInit()
# Load SMTP functions and call DBI authInit()
# @return Lemonldap::NG::Portal constant
sub passwordDBInit {
my $self = shift;
eval { use base qw(Lemonldap::NG::Portal::_SMTP) };
if ($@) {
$self->lmLog( "Unable to load SMTP functions ($@)", 'error' );
return PE_ERROR;
}
unless ( $self->{dbiPasswordMailCol} )
{
$self->lmLog( "Missing configuration parameters for DBI password reset",
'error' );
return PE_ERROR;
}
return $self->Lemonldap::NG::Portal::AuthDBI::authInit();
}
## @apmethod int modifyPassword()
# Modify the password
......@@ -67,9 +85,54 @@ sub modifyPassword {
sub resetPasswordByMail {
my $self = shift;
# TODO
# Exit method if no mail
return PE_OK unless ( $self->{mail} );
# Find mail in database
my $dbh =
$self->dbh( $self->{dbiAuthChain}, $self->{dbiAuthUser},
$self->{dbiAuthPassword} );
return PE_ERROR unless $dbh;
PE_OK;
my $table = $self->{dbiAuthTable};
my $mail = $self->{mail};
my $mailCol = $self->{dbiPasswordMailCol};
$mail =~ s/'/''/g;
my $sth;
eval {
$sth = $dbh->prepare("SELECT * FROM $table WHERE $mailCol='$mail'");
$sth->execute();
};
if ($@) {
$self->lmLog( "DBI error: $@", 'error' );
return PE_ERROR;
}
unless ( $sth->fetchrow_hashref() ) {
$self->lmLog( "Mail $mail not found", 'notice' );
return PE_BADCREDENTIALS;
}
$self->lmLog( "Reset password request for " . $self->{mail}, 'debug' );
# Generate a complex password
my $password = $self->gen_password( $self->{randomPasswordRegexp} );
$self->lmLog( "Generated password: " . $password, 'debug' );
# Modify password
my $hpassword = $self->hash_password( $password, $self->{dbiAuthPasswordHash} );
my $result = $self->modify_password( $self->{mail}, $hpassword, $self->{dbiPasswordMailCol} );
return PE_ERROR unless $result;
# Send new password by mail
$result = $self->send_password( $password, $self->{mail} );
return PE_ERROR unless $result;
PE_PASSWORD_OK;
}
1;
......@@ -42,21 +42,21 @@ sub getUser {
my $pivot = $self->{userPivot};
my $user = $self->{user};
$user =~ s/'/''/g;
my $sth;
eval {
my $sth = $dbh->prepare("SELECT * FROM $table WHERE $pivot='$user'");
$sth = $dbh->prepare("SELECT * FROM $table WHERE $pivot='$user'");
$sth->execute();
unless ( $self->{entry} = $sth->fetchrow_hashref() ) {
$self->lmLog( "User $user not found", 'notice' );
return PE_BADCREDENTIALS;
}
};
if ($@) {
$self->lmLog( "DBI error: $@", 'error' );
return PE_ERROR;
}
unless ( $self->{entry} = $sth->fetchrow_hashref() ) {
$self->lmLog( "User $user not found", 'notice' );
return PE_BADCREDENTIALS;
}
PE_OK;
}
......
......@@ -114,20 +114,22 @@ sub check_password {
# Modify password with SQL UPDATE
# @param user user
# @param password password
# @param userCol optional user column
# @param passwordCol optional password column
# @return boolean result
sub modify_password {
my $self = shift;
my $user = shift;
my $password = shift;
my $userCol = shift || $self->{dbiAuthLoginCol};
my $passwordCol = shift || $self->{dbiAuthPasswordCol};
my $table = $self->{dbiAuthTable};
my $loginCol = $self->{dbiAuthLoginCol};
my $passwordCol = $self->{dbiAuthPasswordCol};
my $table = $self->{dbiAuthTable};
eval {
my $sth =
$self->{_dbh}->prepare(
"UPDATE $table SET $passwordCol=$password WHERE $loginCol='$user'");
"UPDATE $table SET $passwordCol=$password WHERE $userCol='$user'");
$sth->execute();
};
if ($@) {
......
......@@ -23,6 +23,14 @@ sub gen_password {
return $random->randregex( $regexp );
}
## @method int test_mail()
# @return boolean result
sub test_mail {
my $self = shift;
# TODO check that a mail can be sent
}
## @method int send_password()
# Send password by mail
# @param password password
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment