Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions settings/Controller/ChangePasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public function __construct($appName,
* @return JSONResponse
*/
public function changePersonalPassword($oldpassword = '', $newpassword = null) {
/** @var IUser $user */
$user = $this->userManager->checkPassword($this->userId, $oldpassword);
if ($user === false) {
return new JSONResponse([
Expand All @@ -101,10 +102,19 @@ public function changePersonalPassword($oldpassword = '', $newpassword = null) {
]);
}

/** @var IUser $user */
if ($newpassword === null || $user->setPassword($newpassword) === false) {
try {
if ($newpassword === null || $user->setPassword($newpassword) === false) {
return new JSONResponse([
'status' => 'error'
]);
}
// password policy app throws exception
} catch(HintException $e) {
return new JSONResponse([
'status' => 'error'
'status' => 'error',
'data' => [
'message' => $e->getHint(),
],
]);
}

Expand Down Expand Up @@ -216,7 +226,17 @@ public function changeUserPassword($username = null, $password = null, $recovery
]
]);
} else { // now we know that everything is fine regarding the recovery password, let's try to change the password
$result = $targetUser->setPassword($password, $recoveryPassword);
try {
$result = $targetUser->setPassword($password, $recoveryPassword);
// password policy app throws exception
} catch(HintException $e) {
return new JSONResponse([
'status' => 'error',
'data' => [
'message' => $e->getHint(),
],
]);
}
if (!$result && $recoveryEnabledForUser) {
return new JSONResponse([
'status' => 'error',
Expand Down
25 changes: 25 additions & 0 deletions tests/Core/Controller/ChangePasswordControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/
namespace Tests\Core\Controller;

use OC\HintException;
use OC\Settings\Controller\ChangePasswordController;
use OC\User\Session;
use OCP\App\IAppManager;
Expand Down Expand Up @@ -94,6 +95,30 @@ public function testChangePersonalPasswordWrongPassword() {
$this->assertEquals($expects, $res->getData());
}

public function testChangePersonalPasswordCommonPassword() {
$user = $this->getMockBuilder('OCP\IUser')->getMock();
$this->userManager->expects($this->once())
->method('checkPassword')
->with($this->userId, 'old')
->willReturn($user);

$user->expects($this->once())
->method('setPassword')
->with('new')
->will($this->throwException(new HintException('Common password')));

$expects = [
'status' => 'error',
'data' => [
'message' => 'Common password',
],
];

$res = $this->controller->changePersonalPassword('old', 'new');

$this->assertEquals($expects, $res->getData());
}

public function testChangePersonalPasswordNoNewPassword() {
$user = $this->getMockBuilder('OCP\IUser')->getMock();
$this->userManager->expects($this->once())
Expand Down