Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions examples/users_actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ function (AccessTokenInterface $accessToken, string $baseDomain) {
die;
}

// Деактивация добавленных пользователей
try {
$usersService->deactivate($usersCollection);
} catch (AmoCRMApiException $e) {
printError($e);
die();
}

//Получим всех пользователей аккаунта
try {
$usersCollection = $usersService->get();
Expand Down
68 changes: 68 additions & 0 deletions src/AmoCRM/EntitiesServices/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,6 +21,7 @@
use AmoCRM\Models\Rights\RightModel;
use AmoCRM\Models\RoleModel;
use AmoCRM\Models\UserModel;
use InvalidArgumentException;

/**
* Class Users
Expand Down Expand Up @@ -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
*
Expand Down