Commit 11acd945 authored by Vincent Petry's avatar Vincent Petry Committed by GitHub

Merge pull request #27498 from imujjwal96/master

Send password change notification
parents d1df3f18 c006d0d7
......@@ -234,9 +234,43 @@ class LostController extends Controller {
return $this->error($e->getMessage());
}
try {
$this->sendNotificationMail($userId);
} catch (\Exception $e){
return $this->error($e->getMessage());
}
return $this->success();
}
/**
* @param string $userId
* @throws \Exception
*/
protected function sendNotificationMail($userId) {
$user = $this->userManager->get($userId);
$email = $user->getEMailAddress();
if ($email !== '') {
$tmpl = new \OC_Template('core', 'lostpassword/notify');
$msg = $tmpl->fetchPage();
try {
$message = $this->mailer->createMessage();
$message->setTo([$email => $userId]);
$message->setSubject($this->l10n->t('%s password changed successfully', [$this->defaults->getName()]));
$message->setPlainBody($msg);
$message->setFrom([$this->from => $this->defaults->getName()]);
$this->mailer->send($message);
} catch (\Exception $e) {
throw new \Exception($this->l10n->t(
$e->getMessage()
));
}
}
}
/**
* @param string $user
* @throws \Exception
......
<?php
echo $l->t('Password changed successfully');
......@@ -11,6 +11,7 @@
* @author Robin Appelman <icewind@owncloud.com>
* @author Sam Tuke <mail@samtuke.com>
* @author Thomas Müller <thomas.mueller@tmit.eu>
* @author Ujjwal Bhardwaj <ujjwalb1996@gmail.com>
* @author Yarno Boelens <yarnoboelens@gmail.com>
*
* @copyright Copyright (c) 2017, ownCloud GmbH
......@@ -53,6 +54,9 @@ class Controller {
}
if (!is_null($password) && \OC_User::setPassword($username, $password)) {
\OC::$server->getUserSession()->updateSessionTokenPassword($password);
self::sendNotificationMail($username);
\OC_JSON::success();
} else {
\OC_JSON::error();
......@@ -151,16 +155,45 @@ class Controller {
} elseif (!$result && !$recoveryEnabledForUser) {
\OC_JSON::error(["data" => ["message" => $l->t("Unable to change password" )]]);
} else {
self::sendNotificationMail($username);
\OC_JSON::success(["data" => ["username" => $username]]);
}
}
} else { // if encryption is disabled, proceed
if (!is_null($password) && \OC_User::setPassword($username, $password)) {
self::sendNotificationMail($username);
\OC_JSON::success(['data' => ['username' => $username]]);
} else {
\OC_JSON::error(['data' => ['message' => $l->t('Unable to change password')]]);
}
}
}
private static function sendNotificationMail($username) {
$userObject = \OC::$server->getUserManager()->get($username);
$email = $userObject->getEMailAddress();
$defaults = new \OC_Defaults();
$from = \OCP\Util::getDefaultEmailAddress('lostpassword-noreply');
$mailer = \OC::$server->getMailer();
$lion = \OC::$server->getL10N('lib');
if ($email !== '') {
$tmpl = new \OC_Template('core', 'lostpassword/notify');
$msg = $tmpl->fetchPage();
try {
$message = $mailer->createMessage();
$message->setTo([$email => $username]);
$message->setSubject($lion->t('%s password changed successfully', [$defaults->getName()]));
$message->setPlainBody($msg);
$message->setFrom([$from => $defaults->getName()]);
$mailer->send($message);
} catch (\Exception $e) {
throw new \Exception($lion->t(
'Couldn\'t send reset email. Please contact your administrator.'
));
}
}
}
}
......@@ -479,7 +479,7 @@ class LostControllerTest extends \PHPUnit_Framework_TestCase {
->with('NewPassword')
->will($this->returnValue(true));
$this->userManager
->expects($this->exactly(2))
->expects($this->exactly(3))
->method('get')
->with('ValidTokenUser')
->will($this->returnValue($user));
......@@ -487,6 +487,38 @@ class LostControllerTest extends \PHPUnit_Framework_TestCase {
->expects($this->once())
->method('getTime')
->will($this->returnValue(12348));
$user
->expects($this->once())
->method('getEMailAddress')
->will($this->returnValue('test@example.com'));
$message = $this->getMockBuilder('\OC\Mail\Message')
->disableOriginalConstructor()->getMock();
$message
->expects($this->at(0))
->method('setTo')
->with(['test@example.com' => 'ValidTokenUser']);
$message
->expects($this->at(1))
->method('setSubject')
->with(' password changed successfully');
$message
->expects($this->at(2))
->method('setPlainBody')
->with('Password changed successfully');
$message
->expects($this->at(3))
->method('setFrom')
->with(['lostpassword-noreply@localhost' => null]);
$this->mailer
->expects($this->at(0))
->method('createMessage')
->will($this->returnValue($message));
$this->mailer
->expects($this->at(1))
->method('send')
->with($message);
$response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword', 'ValidTokenUser', 'NewPassword', true);
$expectedResponse = ['status' => 'success'];
......
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