diff --git a/apps/provisioning_api/lib/Controller/UsersController.php b/apps/provisioning_api/lib/Controller/UsersController.php index ba9c260224ce5..9cf232d46d0ef 100644 --- a/apps/provisioning_api/lib/Controller/UsersController.php +++ b/apps/provisioning_api/lib/Controller/UsersController.php @@ -536,10 +536,16 @@ public function addUser( throw new OCSException($this->l10n->t('Required email address was not provided'), 110); } + // Create the user try { $newUser = $this->userManager->createUser($userid, $password); - $this->logger->info('Successful addUser call with userid: ' . $userid, ['app' => 'ocs_api']); + if (!$newUser instanceof IUser) { + // If the user is not an instance of IUser, it means the user creation failed + $this->logger->error('Failed addUser attempt: User creation failed.', ['app' => 'ocs_api']); + throw new OCSException($this->l10n->t('User creation failed'), 111); + } + $this->logger->info('Successful addUser call with userid: ' . $userid, ['app' => 'ocs_api']); foreach ($groups as $group) { $this->groupManager->get($group)->addUser($newUser); $this->logger->info('Added userid ' . $userid . ' to group ' . $group, ['app' => 'ocs_api']); diff --git a/apps/provisioning_api/tests/Controller/UsersControllerTest.php b/apps/provisioning_api/tests/Controller/UsersControllerTest.php index 8240e968abb69..05553d6b3e9aa 100644 --- a/apps/provisioning_api/tests/Controller/UsersControllerTest.php +++ b/apps/provisioning_api/tests/Controller/UsersControllerTest.php @@ -466,7 +466,8 @@ public function testAddUserSuccessful() { $this->userManager ->expects($this->once()) ->method('createUser') - ->with('NewUser', 'PasswordOfTheNewUser'); + ->with('NewUser', 'PasswordOfTheNewUser') + ->willReturn($this->createMock(IUser::class)); $this->logger ->expects($this->once()) ->method('info') @@ -529,7 +530,8 @@ public function testAddUserSuccessfulWithDisplayName() { $this->userManager ->expects($this->once()) ->method('createUser') - ->with('NewUser', 'PasswordOfTheNewUser'); + ->with('NewUser', 'PasswordOfTheNewUser') + ->willReturn($this->createMock(IUser::class)); $this->logger ->expects($this->once()) ->method('info') @@ -579,7 +581,8 @@ public function testAddUserSuccessfulGenerateUserID() { $this->userManager ->expects($this->once()) ->method('createUser') - ->with($this->anything(), 'PasswordOfTheNewUser'); + ->with($this->anything(), 'PasswordOfTheNewUser') + ->willReturn($this->createMock(IUser::class)); $this->logger ->expects($this->once()) ->method('info')