diff --git a/lib/Http/Middleware/ProvisioningMiddleware.php b/lib/Http/Middleware/ProvisioningMiddleware.php index 73d9760427..b99d2eb3d7 100644 --- a/lib/Http/Middleware/ProvisioningMiddleware.php +++ b/lib/Http/Middleware/ProvisioningMiddleware.php @@ -65,7 +65,12 @@ public function beforeController($controller, $methodName) { // Nothing to update return; } + $config = $this->provisioningManager->getConfig(); + if ($config === null || !$config->isActive()) { + return; + } try { + $this->provisioningManager->provisionSingleUser($config, $user); $this->provisioningManager->updatePassword( $user, $this->credentialStore->getLoginCredentials()->getPassword() diff --git a/tests/Unit/Http/Middleware/ProvisioningMiddlewareTest.php b/tests/Unit/Http/Middleware/ProvisioningMiddlewareTest.php index 1fac600219..3f42fe201e 100644 --- a/tests/Unit/Http/Middleware/ProvisioningMiddlewareTest.php +++ b/tests/Unit/Http/Middleware/ProvisioningMiddlewareTest.php @@ -26,6 +26,7 @@ use ChristophWurst\Nextcloud\Testing\TestCase; use OCA\Mail\Controller\PageController; use OCA\Mail\Http\Middleware\ProvisioningMiddleware; +use OCA\Mail\Service\Provisioning\Config; use OCA\Mail\Service\Provisioning\Manager; use OCP\Authentication\Exceptions\CredentialsUnavailableException; use OCP\Authentication\Exceptions\PasswordUnavailableException; @@ -86,6 +87,13 @@ public function testBeforeControllerNoCredentialsAvailable() { $this->userSession->expects($this->once()) ->method('getUser') ->willReturn($user); + $config = $this->createMock(Config::class); + $this->provisioningManager->expects($this->once()) + ->method('getConfig') + ->willReturn($config); + $config->expects($this->once()) + ->method('isActive') + ->willReturn(true); $this->credentialStore->expects($this->once()) ->method('getLoginCredentials') ->willThrowException($this->createMock(CredentialsUnavailableException::class)); @@ -103,6 +111,13 @@ public function testBeforeControllerNoPasswordAvailable() { $this->userSession->expects($this->once()) ->method('getUser') ->willReturn($user); + $config = $this->createMock(Config::class); + $this->provisioningManager->expects($this->once()) + ->method('getConfig') + ->willReturn($config); + $config->expects($this->once()) + ->method('isActive') + ->willReturn(true); $credentials = $this->createMock(ICredentials::class); $this->credentialStore->expects($this->once()) ->method('getLoginCredentials') @@ -119,11 +134,60 @@ public function testBeforeControllerNoPasswordAvailable() { ); } + public function testBeforeControllerNoConfigAvailable() { + $user = $this->createMock(IUser::class); + $this->userSession->expects($this->once()) + ->method('getUser') + ->willReturn($user); + $this->provisioningManager->expects($this->once()) + ->method('getConfig') + ->willReturn(null); + $this->credentialStore->expects($this->never()) + ->method('getLoginCredentials'); + $this->provisioningManager->expects($this->never()) + ->method('updatePassword'); + + $this->middleware->beforeController( + $this->createMock(PageController::class), + 'index' + ); + } + + public function testBeforeControllerNotActive() { + $user = $this->createMock(IUser::class); + $this->userSession->expects($this->once()) + ->method('getUser') + ->willReturn($user); + $config = $this->createMock(Config::class); + $this->provisioningManager->expects($this->once()) + ->method('getConfig') + ->willReturn($config); + $config->expects($this->once()) + ->method('isActive') + ->willReturn(false); + $this->credentialStore->expects($this->never()) + ->method('getLoginCredentials'); + $this->provisioningManager->expects($this->never()) + ->method('updatePassword'); + + $this->middleware->beforeController( + $this->createMock(PageController::class), + 'index' + ); + } + public function testBeforeController() { $user = $this->createMock(IUser::class); $this->userSession->expects($this->once()) ->method('getUser') ->willReturn($user); + $config = $this->createMock(Config::class); + $this->provisioningManager->expects($this->once()) + ->method('getConfig') + ->willReturn($config); + $config->expects($this->once()) + ->method('isActive') + ->willReturn(true); $credentials = $this->createMock(ICredentials::class); $this->credentialStore->expects($this->once()) ->method('getLoginCredentials')