Skip to content

Commit

Permalink
Fix creation of new user and display the correct error message
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Ng <chrng8@gmail.com>
  • Loading branch information
Pytal committed Aug 22, 2022
1 parent 7c6c26b commit 85b1909
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
15 changes: 12 additions & 3 deletions apps/provisioning_api/lib/Controller/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 !== '') {
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion lib/private/User/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions lib/private/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions lib/public/IUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 3 additions & 0 deletions lib/public/User/Backend/ISetDisplayNameBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 85b1909

Please sign in to comment.