From 74d64942904e533e34e8da77bcd3c457fabace46 Mon Sep 17 00:00:00 2001 From: skjnldsv Date: Fri, 11 Jul 2025 09:17:45 +0200 Subject: fix(provisioning_api): catch failed user creation Signed-off-by: skjnldsv --- apps/provisioning_api/lib/Controller/UsersController.php | 8 +++++++- apps/provisioning_api/tests/Controller/UsersControllerTest.php | 9 ++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/apps/provisioning_api/lib/Controller/UsersController.php b/apps/provisioning_api/lib/Controller/UsersController.php index 51b18306b51..1d1d0e8d2f9 100644 --- a/apps/provisioning_api/lib/Controller/UsersController.php +++ b/apps/provisioning_api/lib/Controller/UsersController.php @@ -542,10 +542,16 @@ class UsersController extends AUserDataOCSController { 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 85ec9e374d6..e838cd16633 100644 --- a/apps/provisioning_api/tests/Controller/UsersControllerTest.php +++ b/apps/provisioning_api/tests/Controller/UsersControllerTest.php @@ -452,7 +452,8 @@ class UsersControllerTest extends TestCase { $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') @@ -517,7 +518,8 @@ class UsersControllerTest extends TestCase { $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') @@ -567,7 +569,8 @@ class UsersControllerTest extends TestCase { $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') -- cgit v1.2.3