From 6f4f79c13566e174c858ca173327f927593a6ad9 Mon Sep 17 00:00:00 2001 From: vbochkarev Date: Fri, 13 Feb 2026 13:25:25 +0300 Subject: [PATCH] added methods to activate/deactivate users --- examples/users_actions.php | 8 ++++ src/AmoCRM/EntitiesServices/Users.php | 68 +++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/examples/users_actions.php b/examples/users_actions.php index 8fc9fb57..f440bc40 100644 --- a/examples/users_actions.php +++ b/examples/users_actions.php @@ -117,6 +117,14 @@ function (AccessTokenInterface $accessToken, string $baseDomain) { die; } +// Деактивация добавленных пользователей +try { + $usersService->deactivate($usersCollection); +} catch (AmoCRMApiException $e) { + printError($e); + die(); +} + //Получим всех пользователей аккаунта try { $usersCollection = $usersService->get(); diff --git a/src/AmoCRM/EntitiesServices/Users.php b/src/AmoCRM/EntitiesServices/Users.php index 286e2b44..7642d71e 100644 --- a/src/AmoCRM/EntitiesServices/Users.php +++ b/src/AmoCRM/EntitiesServices/Users.php @@ -3,6 +3,12 @@ namespace AmoCRM\EntitiesServices; use AmoCRM\Collections\UsersCollection; +use AmoCRM\Exceptions\AmoCRMApiConnectExceptionException; +use AmoCRM\Exceptions\AmoCRMApiException; +use AmoCRM\Exceptions\AmoCRMApiHttpClientException; +use AmoCRM\Exceptions\AmoCRMApiNoContentException; +use AmoCRM\Exceptions\AmoCRMApiTooManyRedirectsException; +use AmoCRM\Exceptions\AmoCRMoAuthApiException; use AmoCRM\Exceptions\NotAvailableForActionException; use AmoCRM\Filters\BaseEntityFilter; use AmoCRM\Helpers\EntityTypesInterface; @@ -15,6 +21,7 @@ use AmoCRM\Models\Rights\RightModel; use AmoCRM\Models\RoleModel; use AmoCRM\Models\UserModel; +use InvalidArgumentException; /** * Class Users @@ -100,6 +107,67 @@ protected function processAction(BaseApiCollection $collection, array $response) return $collection; } + /** + * @throws AmoCRMApiException + * @throws AmoCRMApiNoContentException + * @throws AmoCRMApiHttpClientException + * @throws AmoCRMoAuthApiException + * @throws AmoCRMApiConnectExceptionException + * @throws AmoCRMApiTooManyRedirectsException + */ + public function activate(UsersCollection $users): bool + { + try { + $this->request->post(sprintf('%s/activate', $this->getMethod()), $this->prepareUserIdsPayload($users)); + } catch (AmoCRMApiNoContentException $exception) { + return true; + } + + return false; + } + + /** + * @throws AmoCRMApiException + * @throws AmoCRMApiNoContentException + * @throws AmoCRMApiHttpClientException + * @throws AmoCRMoAuthApiException + * @throws AmoCRMApiConnectExceptionException + * @throws AmoCRMApiTooManyRedirectsException + */ + public function deactivate(UsersCollection $users): bool + { + try { + $this->request->post(sprintf('%s/deactivate', $this->getMethod()), $this->prepareUserIdsPayload($users)); + } catch (AmoCRMApiNoContentException $exception) { + return true; + } + + return false; + } + + protected function prepareUserIdsPayload(UsersCollection $users): array + { + if ($users->isEmpty()) { + throw new InvalidArgumentException('Collection is empty'); + } + + if ($users->count() > 10) { + throw new InvalidArgumentException('Maximum 10 users per request'); + } + + $payload = []; + foreach ($users as $user) { + /** @var UserModel $user */ + if (!$id = $user->getId()) { + throw new InvalidArgumentException('User without ID in collection'); + } + + $payload[] = ['id' => $id]; + } + + return $payload; + } + /** * @param BaseApiCollection $collection *