fix: provision api's status codes

Signed-off-by: Hamza Mahjoubi <hamzamahjoubi221@gmail.com>
pull/47846/head
Hamza Mahjoubi 2024-09-09 15:08:15 +07:00
parent 6e8c532932
commit 9be2f060f9
3 changed files with 31 additions and 31 deletions

@ -488,7 +488,7 @@ class UsersController extends AUserData {
$group = $this->groupManager->get($groupid); $group = $this->groupManager->get($groupid);
// Check if group exists // Check if group exists
if ($group === null) { if ($group === null) {
throw new OCSException($this->l10n->t('Sub-admin group does not exist'), 102); throw new OCSException($this->l10n->t('Sub-admin group does not exist'), 109);
} }
// Check if trying to make subadmin of admin group // Check if trying to make subadmin of admin group
if ($group->getGID() === 'admin') { if ($group->getGID() === 'admin') {
@ -508,7 +508,7 @@ class UsersController extends AUserData {
} }
if ($password === '') { if ($password === '') {
if ($email === '') { if ($email === '') {
throw new OCSException($this->l10n->t('To send a password link to the user an email address is required.'), 108); throw new OCSException($this->l10n->t('An email address is required, to send a password link to the user.'), 108);
} }
$passwordEvent = new GenerateSecurePasswordEvent(); $passwordEvent = new GenerateSecurePasswordEvent();
@ -1011,7 +1011,7 @@ class UsersController extends AUserData {
} }
// Check if permitted to edit this field // Check if permitted to edit this field
if (!in_array($key, $permittedFields)) { if (!in_array($key, $permittedFields)) {
throw new OCSException('', 103); throw new OCSException('', 113);
} }
// Process the edit // Process the edit
switch ($key) { switch ($key) {
@ -1032,14 +1032,14 @@ class UsersController extends AUserData {
$quota = \OCP\Util::computerFileSize($quota); $quota = \OCP\Util::computerFileSize($quota);
} }
if ($quota === false) { if ($quota === false) {
throw new OCSException($this->l10n->t('Invalid quota value: %1$s', [$value]), 102); throw new OCSException($this->l10n->t('Invalid quota value: %1$s', [$value]), 101);
} }
if ($quota === -1) { if ($quota === -1) {
$quota = 'none'; $quota = 'none';
} else { } else {
$maxQuota = (int)$this->config->getAppValue('files', 'max_quota', '-1'); $maxQuota = (int)$this->config->getAppValue('files', 'max_quota', '-1');
if ($maxQuota !== -1 && $quota > $maxQuota) { if ($maxQuota !== -1 && $quota > $maxQuota) {
throw new OCSException($this->l10n->t('Invalid quota value. %1$s is exceeding the maximum quota', [$value]), 102); throw new OCSException($this->l10n->t('Invalid quota value. %1$s is exceeding the maximum quota', [$value]), 101);
} }
$quota = \OCP\Util::humanFileSize($quota); $quota = \OCP\Util::humanFileSize($quota);
} }
@ -1048,7 +1048,7 @@ class UsersController extends AUserData {
if ($quota === 'none') { if ($quota === 'none') {
$allowUnlimitedQuota = $this->config->getAppValue('files', 'allow_unlimited_quota', '1') === '1'; $allowUnlimitedQuota = $this->config->getAppValue('files', 'allow_unlimited_quota', '1') === '1';
if (!$allowUnlimitedQuota) { if (!$allowUnlimitedQuota) {
throw new OCSException($this->l10n->t('Unlimited quota is forbidden on this instance'), 102); throw new OCSException($this->l10n->t('Unlimited quota is forbidden on this instance'), 101);
} }
} }
$targetUser->setQuota($quota); $targetUser->setQuota($quota);
@ -1059,33 +1059,33 @@ class UsersController extends AUserData {
case self::USER_FIELD_PASSWORD: case self::USER_FIELD_PASSWORD:
try { try {
if (strlen($value) > IUserManager::MAX_PASSWORD_LENGTH) { if (strlen($value) > IUserManager::MAX_PASSWORD_LENGTH) {
throw new OCSException($this->l10n->t('Invalid password value'), 102); throw new OCSException($this->l10n->t('Invalid password value'), 101);
} }
if (!$targetUser->canChangePassword()) { if (!$targetUser->canChangePassword()) {
throw new OCSException($this->l10n->t('Setting the password is not supported by the users backend'), 103); throw new OCSException($this->l10n->t('Setting the password is not supported by the users backend'), 112);
} }
$targetUser->setPassword($value); $targetUser->setPassword($value);
} catch (HintException $e) { // password policy error } catch (HintException $e) { // password policy error
throw new OCSException($e->getMessage(), 103); throw new OCSException($e->getMessage(), 107);
} }
break; break;
case self::USER_FIELD_LANGUAGE: case self::USER_FIELD_LANGUAGE:
$languagesCodes = $this->l10nFactory->findAvailableLanguages(); $languagesCodes = $this->l10nFactory->findAvailableLanguages();
if (!in_array($value, $languagesCodes, true) && $value !== 'en') { if (!in_array($value, $languagesCodes, true) && $value !== 'en') {
throw new OCSException($this->l10n->t('Invalid language'), 102); throw new OCSException($this->l10n->t('Invalid language'), 101);
} }
$this->config->setUserValue($targetUser->getUID(), 'core', 'lang', $value); $this->config->setUserValue($targetUser->getUID(), 'core', 'lang', $value);
break; break;
case self::USER_FIELD_LOCALE: case self::USER_FIELD_LOCALE:
if (!$this->l10nFactory->localeExists($value)) { if (!$this->l10nFactory->localeExists($value)) {
throw new OCSException($this->l10n->t('Invalid locale'), 102); throw new OCSException($this->l10n->t('Invalid locale'), 101);
} }
$this->config->setUserValue($targetUser->getUID(), 'core', 'locale', $value); $this->config->setUserValue($targetUser->getUID(), 'core', 'locale', $value);
break; break;
case self::USER_FIELD_FIRST_DAY_OF_WEEK: case self::USER_FIELD_FIRST_DAY_OF_WEEK:
$intValue = (int)$value; $intValue = (int)$value;
if ($intValue < -1 || $intValue > 6) { if ($intValue < -1 || $intValue > 6) {
throw new OCSException($this->l10n->t('Invalid first day of week'), 102); throw new OCSException($this->l10n->t('Invalid first day of week'), 101);
} }
if ($intValue === -1) { if ($intValue === -1) {
$this->config->deleteUserValue($targetUser->getUID(), 'core', AUserData::USER_FIELD_FIRST_DAY_OF_WEEK); $this->config->deleteUserValue($targetUser->getUID(), 'core', AUserData::USER_FIELD_FIRST_DAY_OF_WEEK);
@ -1110,14 +1110,14 @@ class UsersController extends AUserData {
} }
} }
if (!$success) { if (!$success) {
throw new OCSException('', 102); throw new OCSException('', 101);
} }
break; break;
case IAccountManager::PROPERTY_EMAIL: case IAccountManager::PROPERTY_EMAIL:
if (filter_var($value, FILTER_VALIDATE_EMAIL) || $value === '') { if (filter_var($value, FILTER_VALIDATE_EMAIL) || $value === '') {
$targetUser->setEMailAddress($value); $targetUser->setEMailAddress($value);
} else { } else {
throw new OCSException('', 102); throw new OCSException('', 101);
} }
break; break;
case IAccountManager::COLLECTION_EMAIL: case IAccountManager::COLLECTION_EMAIL:
@ -1126,13 +1126,13 @@ class UsersController extends AUserData {
$mailCollection = $userAccount->getPropertyCollection(IAccountManager::COLLECTION_EMAIL); $mailCollection = $userAccount->getPropertyCollection(IAccountManager::COLLECTION_EMAIL);
if ($mailCollection->getPropertyByValue($value)) { if ($mailCollection->getPropertyByValue($value)) {
throw new OCSException('', 102); throw new OCSException('', 101);
} }
$mailCollection->addPropertyWithDefaults($value); $mailCollection->addPropertyWithDefaults($value);
$this->accountManager->updateAccount($userAccount); $this->accountManager->updateAccount($userAccount);
} else { } else {
throw new OCSException('', 102); throw new OCSException('', 101);
} }
break; break;
case IAccountManager::PROPERTY_PHONE: case IAccountManager::PROPERTY_PHONE:
@ -1156,7 +1156,7 @@ class UsersController extends AUserData {
$this->knownUserService->deleteByContactUserId($targetUser->getUID()); $this->knownUserService->deleteByContactUserId($targetUser->getUID());
} }
} catch (InvalidArgumentException $e) { } catch (InvalidArgumentException $e) {
throw new OCSException('Invalid ' . $e->getMessage(), 102); throw new OCSException('Invalid ' . $e->getMessage(), 101);
} }
} }
} catch (PropertyDoesNotExistException $e) { } catch (PropertyDoesNotExistException $e) {
@ -1165,7 +1165,7 @@ class UsersController extends AUserData {
try { try {
$this->accountManager->updateAccount($userAccount); $this->accountManager->updateAccount($userAccount);
} catch (InvalidArgumentException $e) { } catch (InvalidArgumentException $e) {
throw new OCSException('Invalid ' . $e->getMessage(), 102); throw new OCSException('Invalid ' . $e->getMessage(), 101);
} }
break; break;
case IAccountManager::PROPERTY_PROFILE_ENABLED: case IAccountManager::PROPERTY_PROFILE_ENABLED:
@ -1203,12 +1203,12 @@ class UsersController extends AUserData {
$userProperty->setScope($value); $userProperty->setScope($value);
$this->accountManager->updateAccount($userAccount); $this->accountManager->updateAccount($userAccount);
} catch (InvalidArgumentException $e) { } catch (InvalidArgumentException $e) {
throw new OCSException('Invalid ' . $e->getMessage(), 102); throw new OCSException('Invalid ' . $e->getMessage(), 101);
} }
} }
break; break;
default: default:
throw new OCSException('', 103); throw new OCSException('', 113);
} }
return new DataResponse(); return new DataResponse();
} }

@ -1623,7 +1623,7 @@ class UsersControllerTest extends TestCase {
->with($userAccount); ->with($userAccount);
$this->expectException(OCSException::class); $this->expectException(OCSException::class);
$this->expectExceptionCode(102); $this->expectExceptionCode(101);
$this->api->editUser('UserToEdit', 'additional_mail', 'demo@nextcloud.com')->getData(); $this->api->editUser('UserToEdit', 'additional_mail', 'demo@nextcloud.com')->getData();
} }
@ -1682,13 +1682,13 @@ class UsersControllerTest extends TestCase {
->with($userAccount); ->with($userAccount);
$this->expectException(OCSException::class); $this->expectException(OCSException::class);
$this->expectExceptionCode(102); $this->expectExceptionCode(101);
$this->api->editUser('UserToEdit', 'additional_mail', 'demo1@nextcloud.com')->getData(); $this->api->editUser('UserToEdit', 'additional_mail', 'demo1@nextcloud.com')->getData();
} }
public function testEditUserRegularUserSelfEditChangeEmailInvalid(): void { public function testEditUserRegularUserSelfEditChangeEmailInvalid(): void {
$this->expectException(\OCP\AppFramework\OCS\OCSException::class); $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
$this->expectExceptionCode(102); $this->expectExceptionCode(101);
$loggedInUser = $this->getMockBuilder(IUser::class) $loggedInUser = $this->getMockBuilder(IUser::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
@ -1922,7 +1922,7 @@ class UsersControllerTest extends TestCase {
public function testEditUserRegularUserSelfEditChangeQuota(): void { public function testEditUserRegularUserSelfEditChangeQuota(): void {
$this->expectException(\OCP\AppFramework\OCS\OCSException::class); $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
$this->expectExceptionCode(103); $this->expectExceptionCode(113);
$loggedInUser = $this->getMockBuilder(IUser::class) $loggedInUser = $this->getMockBuilder(IUser::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
@ -2009,7 +2009,7 @@ class UsersControllerTest extends TestCase {
public function testEditUserAdminUserSelfEditChangeInvalidQuota(): void { public function testEditUserAdminUserSelfEditChangeInvalidQuota(): void {
$this->expectException(\OCP\AppFramework\OCS\OCSException::class); $this->expectException(\OCP\AppFramework\OCS\OCSException::class);
$this->expectExceptionMessage('Invalid quota value: ABC'); $this->expectExceptionMessage('Invalid quota value: ABC');
$this->expectExceptionCode(102); $this->expectExceptionCode(101);
$loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock(); $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
$loggedInUser $loggedInUser

@ -214,7 +214,7 @@ Feature: provisioning
And sending "PUT" to "/cloud/users/brand-new-user" with And sending "PUT" to "/cloud/users/brand-new-user" with
| key | additional_mail | | key | additional_mail |
| value | no-reply@nextcloud.com | | value | no-reply@nextcloud.com |
And the OCS status code should be "102" And the OCS status code should be "101"
And the HTTP status code should be "200" And the HTTP status code should be "200"
And sending "PUT" to "/cloud/users/brand-new-user" with And sending "PUT" to "/cloud/users/brand-new-user" with
| key | additional_mail | | key | additional_mail |
@ -225,7 +225,7 @@ Feature: provisioning
And sending "PUT" to "/cloud/users/brand-new-user" with And sending "PUT" to "/cloud/users/brand-new-user" with
| key | additional_mail | | key | additional_mail |
| value | no.reply2@nextcloud.com | | value | no.reply2@nextcloud.com |
And the OCS status code should be "102" And the OCS status code should be "101"
And the HTTP status code should be "200" And the HTTP status code should be "200"
Then user "brand-new-user" has Then user "brand-new-user" has
| id | brand-new-user | | id | brand-new-user |
@ -270,17 +270,17 @@ Feature: provisioning
When sending "PUT" to "/cloud/users/brand-new-user" with When sending "PUT" to "/cloud/users/brand-new-user" with
| key | phoneScope | | key | phoneScope |
| value | invalid | | value | invalid |
Then the OCS status code should be "102" Then the OCS status code should be "101"
And the HTTP status code should be "200" And the HTTP status code should be "200"
When sending "PUT" to "/cloud/users/brand-new-user" with When sending "PUT" to "/cloud/users/brand-new-user" with
| key | displaynameScope | | key | displaynameScope |
| value | v2-private | | value | v2-private |
Then the OCS status code should be "102" Then the OCS status code should be "101"
And the HTTP status code should be "200" And the HTTP status code should be "200"
When sending "PUT" to "/cloud/users/brand-new-user" with When sending "PUT" to "/cloud/users/brand-new-user" with
| key | emailScope | | key | emailScope |
| value | v2-private | | value | v2-private |
Then the OCS status code should be "102" Then the OCS status code should be "101"
And the HTTP status code should be "200" And the HTTP status code should be "200"
Scenario: Edit a user account multi-value property scopes with invalid or unsupported value Scenario: Edit a user account multi-value property scopes with invalid or unsupported value
@ -326,7 +326,7 @@ Feature: provisioning
When sending "PUT" to "/cloud/users/brand-new-user" with When sending "PUT" to "/cloud/users/brand-new-user" with
| key | phoneScope | | key | phoneScope |
| value | v2-private | | value | v2-private |
Then the OCS status code should be "103" Then the OCS status code should be "113"
And the HTTP status code should be "200" And the HTTP status code should be "200"
Scenario: Search by phone number Scenario: Search by phone number