diff --git a/apps/settings/lib/Controller/ChangePasswordController.php b/apps/settings/lib/Controller/ChangePasswordController.php index c374b3ff8bf5e..63238771de59b 100644 --- a/apps/settings/lib/Controller/ChangePasswordController.php +++ b/apps/settings/lib/Controller/ChangePasswordController.php @@ -113,8 +113,9 @@ public function __construct(string $appName, * @BruteForceProtection(action=changePersonalPassword) */ public function changePersonalPassword(string $oldpassword = '', string $newpassword = null): JSONResponse { + $loginName = $this->userSession->getLoginName(); /** @var IUser $user */ - $user = $this->userManager->checkPassword($this->userId, $oldpassword); + $user = $this->userManager->checkPassword($loginName, $oldpassword); if ($user === false) { $response = new JSONResponse([ 'status' => 'error', diff --git a/tests/Core/Controller/ChangePasswordControllerTest.php b/tests/Core/Controller/ChangePasswordControllerTest.php index a55b0bc232e0c..3e0cd1b64b11d 100644 --- a/tests/Core/Controller/ChangePasswordControllerTest.php +++ b/tests/Core/Controller/ChangePasswordControllerTest.php @@ -36,6 +36,8 @@ class ChangePasswordControllerTest extends \Test\TestCase { /** @var string */ private $userId = 'currentUser'; + /** @var string */ + private $loginName = 'ua1337'; /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */ private $userManager; /** @var Session|\PHPUnit_Framework_MockObject_MockObject */ @@ -75,9 +77,13 @@ protected function setUp(): void { } public function testChangePersonalPasswordWrongPassword() { + $this->userSession->expects($this->once()) + ->method('getLoginName') + ->willReturn($this->loginName); + $this->userManager->expects($this->once()) ->method('checkPassword') - ->with($this->userId, 'old') + ->with($this->loginName, 'old') ->willReturn(false); $expects = new JSONResponse([ @@ -93,10 +99,14 @@ public function testChangePersonalPasswordWrongPassword() { } public function testChangePersonalPasswordCommonPassword() { + $this->userSession->expects($this->once()) + ->method('getLoginName') + ->willReturn($this->loginName); + $user = $this->getMockBuilder(IUser::class)->getMock(); $this->userManager->expects($this->once()) ->method('checkPassword') - ->with($this->userId, 'old') + ->with($this->loginName, 'old') ->willReturn($user); $user->expects($this->once()) @@ -116,10 +126,14 @@ public function testChangePersonalPasswordCommonPassword() { } public function testChangePersonalPasswordNoNewPassword() { + $this->userSession->expects($this->once()) + ->method('getLoginName') + ->willReturn($this->loginName); + $user = $this->getMockBuilder(IUser::class)->getMock(); $this->userManager->expects($this->once()) ->method('checkPassword') - ->with($this->userId, 'old') + ->with($this->loginName, 'old') ->willReturn($user); $expects = [ @@ -132,10 +146,14 @@ public function testChangePersonalPasswordNoNewPassword() { } public function testChangePersonalPasswordCantSetPassword() { + $this->userSession->expects($this->once()) + ->method('getLoginName') + ->willReturn($this->loginName); + $user = $this->getMockBuilder(IUser::class)->getMock(); $this->userManager->expects($this->once()) ->method('checkPassword') - ->with($this->userId, 'old') + ->with($this->loginName, 'old') ->willReturn($user); $user->expects($this->once()) @@ -152,10 +170,14 @@ public function testChangePersonalPasswordCantSetPassword() { } public function testChangePersonalPassword() { + $this->userSession->expects($this->once()) + ->method('getLoginName') + ->willReturn($this->loginName); + $user = $this->getMockBuilder(IUser::class)->getMock(); $this->userManager->expects($this->once()) ->method('checkPassword') - ->with($this->userId, 'old') + ->with($this->loginName, 'old') ->willReturn($user); $user->expects($this->once())