From d59585974e2f3f51598da31f6e1dd0684affa77b Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Fri, 19 Aug 2022 20:02:57 +0000 Subject: [PATCH] Fix creation of new user and display the correct error message Signed-off-by: Christopher Ng --- .../lib/Controller/UsersController.php | 15 ++++++++++++--- lib/private/User/Database.php | 4 +++- lib/private/User/User.php | 3 +++ lib/public/IUser.php | 3 +++ .../User/Backend/ISetDisplayNameBackend.php | 3 +++ 5 files changed, 24 insertions(+), 4 deletions(-) diff --git a/apps/provisioning_api/lib/Controller/UsersController.php b/apps/provisioning_api/lib/Controller/UsersController.php index 839ac404c947b..37bbdc9e1c4d0 100644 --- a/apps/provisioning_api/lib/Controller/UsersController.php +++ b/apps/provisioning_api/lib/Controller/UsersController.php @@ -426,7 +426,14 @@ public function addUser( } if ($displayName !== '') { - $this->editUser($userid, self::USER_FIELD_DISPLAYNAME, $displayName); + try { + $this->editUser($userid, self::USER_FIELD_DISPLAYNAME, $displayName); + } catch (OCSException $e) { + if ($newUser instanceof IUser) { + $newUser->delete(); + } + throw $e; + } } if ($quota !== '') { @@ -837,8 +844,10 @@ public function editUser(string $userId, string $key, string $value): DataRespon switch ($key) { case self::USER_FIELD_DISPLAYNAME: case IAccountManager::PROPERTY_DISPLAYNAME: - if (!$targetUser->setDisplayName($value)) { - throw new OCSException('Invalid displayname', 102); + try { + $targetUser->setDisplayName($value); + } catch (InvalidArgumentException $e) { + throw new OCSException($e->getMessage(), 101); } break; case self::USER_FIELD_QUOTA: diff --git a/lib/private/User/Database.php b/lib/private/User/Database.php index 0b38f04bfe3c0..f106c2e8b6dae 100644 --- a/lib/private/User/Database.php +++ b/lib/private/User/Database.php @@ -212,11 +212,13 @@ public function setPassword(string $uid, string $password): bool { * @param string $displayName The new display name * @return bool * + * @throws \InvalidArgumentException + * * Change the display name of a user */ public function setDisplayName(string $uid, string $displayName): bool { if (mb_strlen($displayName) > 64) { - return false; + throw new \InvalidArgumentException('Invalid displayname'); } $this->fixDI(); diff --git a/lib/private/User/User.php b/lib/private/User/User.php index 7f7d6273e306b..c24a5fe1c6b59 100644 --- a/lib/private/User/User.php +++ b/lib/private/User/User.php @@ -154,6 +154,9 @@ public function getDisplayName() { * * @param string $displayName * @return bool + * + * @since 25.0.0 Throw InvalidArgumentException + * @throws \InvalidArgumentException */ public function setDisplayName($displayName) { $displayName = trim($displayName); diff --git a/lib/public/IUser.php b/lib/public/IUser.php index daf993df6cd2d..bb7bdf3304aa8 100644 --- a/lib/public/IUser.php +++ b/lib/public/IUser.php @@ -58,6 +58,9 @@ public function getDisplayName(); * @param string $displayName * @return bool * @since 8.0.0 + * + * @since 25.0.0 Throw InvalidArgumentException + * @throws \InvalidArgumentException */ public function setDisplayName($displayName); diff --git a/lib/public/User/Backend/ISetDisplayNameBackend.php b/lib/public/User/Backend/ISetDisplayNameBackend.php index 922d356bfd738..db62223ad52fa 100644 --- a/lib/public/User/Backend/ISetDisplayNameBackend.php +++ b/lib/public/User/Backend/ISetDisplayNameBackend.php @@ -36,6 +36,9 @@ interface ISetDisplayNameBackend { * @param string $uid The username * @param string $displayName The new display name * @return bool + * + * @since 25.0.0 Throw InvalidArgumentException + * @throws \InvalidArgumentException */ public function setDisplayName(string $uid, string $displayName): bool; }