2014-05-04 15:43:38 +02:00
|
|
|
<?php
|
|
|
|
class PasswordResetJob extends AbstractJob
|
|
|
|
{
|
|
|
|
const TOKEN = 'token';
|
|
|
|
const NEW_PASSWORD = 'new-password';
|
|
|
|
|
|
|
|
public function execute()
|
|
|
|
{
|
|
|
|
if (!$this->hasArgument(self::TOKEN))
|
|
|
|
{
|
|
|
|
$user = UserModel::findByNameOrEmail($this->getArgument(self::USER_NAME));
|
|
|
|
|
2014-05-07 09:26:04 +02:00
|
|
|
if (empty($user->getConfirmedEmail()))
|
2014-05-04 15:43:38 +02:00
|
|
|
throw new SimpleException('This user has no e-mail confirmed; password reset cannot proceed');
|
|
|
|
|
2014-05-07 09:26:04 +02:00
|
|
|
UserModel::sendPasswordResetEmail($user);
|
2014-05-04 15:43:38 +02:00
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$tokenText = $this->getArgument(self::TOKEN);
|
|
|
|
$token = TokenModel::findByToken($tokenText);
|
|
|
|
TokenModel::checkValidity($token);
|
|
|
|
|
|
|
|
$alphabet = array_merge(range('A', 'Z'), range('a', 'z'), range('0', '9'));
|
|
|
|
$newPassword = join('', array_map(function($x) use ($alphabet)
|
|
|
|
{
|
|
|
|
return $alphabet[$x];
|
|
|
|
}, array_rand($alphabet, 8)));
|
|
|
|
|
|
|
|
$user = $token->getUser();
|
2014-05-07 00:34:02 +02:00
|
|
|
$user->setPassword($newPassword);
|
2014-05-04 15:43:38 +02:00
|
|
|
$token->used = true;
|
|
|
|
TokenModel::save($token);
|
|
|
|
UserModel::save($user);
|
|
|
|
|
2014-05-04 19:23:09 +02:00
|
|
|
Logger::log('{subject} just reset password', [
|
2014-05-04 15:43:38 +02:00
|
|
|
'subject' => TextHelper::reprUser($user)]);
|
|
|
|
|
|
|
|
$x = new StdClass;
|
|
|
|
$x->user = $user;
|
|
|
|
$x->newPassword = $newPassword;
|
|
|
|
return $x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function sendEmail($user)
|
|
|
|
{
|
|
|
|
$regConfig = getConfig()->registration;
|
|
|
|
|
|
|
|
$mail = new Mail();
|
|
|
|
$mail->body = $regConfig->passwordResetEmailBody;
|
|
|
|
$mail->subject = $regConfig->passwordResetEmailSubject;
|
|
|
|
$mail->senderName = $regConfig->passwordResetEmailSenderName;
|
|
|
|
$mail->senderEmail = $regConfig->passwordResetEmailSenderEmail;
|
2014-05-07 09:26:04 +02:00
|
|
|
$mail->recipientEmail = $user->getConfirmedEmail();
|
2014-05-04 15:43:38 +02:00
|
|
|
|
|
|
|
return Mailer::sendMailWithTokenLink(
|
|
|
|
$user,
|
|
|
|
['UserController', 'passwordResetAction'],
|
|
|
|
$mail);
|
|
|
|
}
|
|
|
|
}
|