diff --git a/src/app/Backends/LDAP.php b/src/app/Backends/LDAP.php --- a/src/app/Backends/LDAP.php +++ b/src/app/Backends/LDAP.php @@ -230,7 +230,11 @@ 'nsroledn' => [] ]; - if (!self::getUserEntry($ldap, $user->email, $dn) && $dn) { + if (!self::getUserEntry($ldap, $user->email, $dn)) { + if (empty($dn)) { + self::throwException($ldap, "Failed to create user {$user->email} in LDAP"); + } + self::setUserAttributes($user, $entry); $result = $ldap->add_entry($dn, $entry); diff --git a/src/tests/Feature/Backends/LDAPTest.php b/src/tests/Feature/Backends/LDAPTest.php --- a/src/tests/Feature/Backends/LDAPTest.php +++ b/src/tests/Feature/Backends/LDAPTest.php @@ -222,6 +222,24 @@ } /** + * Test handling errors on user creation + * + * @group ldap + */ + public function testCreateUserException(): void + { + $this->expectException(\Exception::class); + $this->expectExceptionMessageMatches('/Failed to create user/'); + + $user = new User([ + 'email' => 'test-non-existing-ldap@non-existing.org', + 'status' => User::STATUS_ACTIVE, + ]); + + LDAP::createUser($user); + } + + /** * Test handling update of a non-existing domain * * @group ldap