From bdd4d7e0206ca20546b482991b0f0b0ca9bcae7d Mon Sep 17 00:00:00 2001 From: David Smith Date: Fri, 25 Oct 2024 13:55:44 -0500 Subject: [PATCH] Add collab LS and RM commands --- src/API/Accounts/AccountsClient.php | 253 +------------------ src/Command/Accounts/ListAdminsCommand.php | 51 ++++ src/Command/Accounts/RemoveCollabCommand.php | 51 ++++ 3 files changed, 107 insertions(+), 248 deletions(-) create mode 100644 src/Command/Accounts/ListAdminsCommand.php create mode 100644 src/Command/Accounts/RemoveCollabCommand.php diff --git a/src/API/Accounts/AccountsClient.php b/src/API/Accounts/AccountsClient.php index 5828a36..2fde634 100644 --- a/src/API/Accounts/AccountsClient.php +++ b/src/API/Accounts/AccountsClient.php @@ -30,35 +30,6 @@ public function addSshKey(string $accessToken, int $accountId, string $sshKey) // everything from here down was copied over from mgmt, need to pare it to only the required bits - public function listAccounts($accessToken, string $search = null, bool $includeNonSubs = false, $supportId = null) - { - $options = [ - 'query' => [], - ]; - if ($search) { - $options['query']['search'] = $search; - } - if ($includeNonSubs) { - $options['query']['includeNonSubs'] = 1; - } - if (!empty($supportId)) { - $options['query']['supportId'] = $supportId; - } - - return $this->guzzle($this->getBearerTokenMiddleware($accessToken))->get('accounts', $options); - } - - public function listAccountsByIds($accessToken, array $accountIds, bool $minimal = false) - { - $options = [ - 'query' => [ - 'accountIds' => $accountIds, - 'minimal' => $minimal ? 'true' : 'false', - ], - ]; - - return $this->guzzle($this->getBearerTokenMiddleware($accessToken))->get('accounts', $options); - } // definitely in use :) public function listAccountCollaborators($accessToken, $accountId, string $access = 'from') @@ -74,49 +45,14 @@ public function listAccountCollaborators($accessToken, $accountId, string $acces ); } - public function getByPoolId($accessToken, $poolId) - { - $options = [ - 'query' => [ - 'poolId' => $poolId, - ], - ]; - - return $this->guzzle($this->getBearerTokenMiddleware($accessToken))->get('accounts', $options); - } - - public function getByBillingCustomerId(string $accessToken, string $id) - { - return $this->guzzle($this->getBearerTokenMiddleware($accessToken)) - ->get("accounts/by-billing/{$id}"); - } - - public function getByUsername(string $accessToken, string $username) + // ditto + public function removeCollaboratorsForApp($accessToken, $accountId, $appId) { return $this->guzzle($this->getBearerTokenMiddleware($accessToken)) - ->get('accounts/by-username?username=' . urlencode($username)); - } - - public function getCapacity($accessToken, array $ids) - { - $options = [ - 'json' => [ - 'accountIds' => $ids, - ], - ]; - - return $this->guzzle($this->getBearerTokenMiddleware($accessToken))->post('accounts/capacity', $options); - } - - public function getAccount($accessToken, $id, $minimal = false) - { - $query = []; - if ($minimal) { - $query['query']['minimal'] = 'true'; - } - return $this->guzzle($this->getBearerTokenMiddleware($accessToken))->get("accounts/{$id}", $query); + ->delete("accounts/{$accountId}/collaborators/apps/{$appId}"); } + // doesn't work - the client token doesn't have privs public function getAdmins($accessToken, $supportId = null) { $query = []; @@ -128,43 +64,7 @@ public function getAdmins($accessToken, $supportId = null) return $this->guzzle($this->getBearerTokenMiddleware($accessToken))->get('accounts/admins', $query); } - public function getAdmin($accessToken, $id) - { - return $this->guzzle($this->getBearerTokenMiddleware($accessToken))->get("accounts/admins/{$id}"); - } - - public function getGroupPubKeys($accessToken, $groupName) - { - return $this->guzzle($this->getBearerTokenMiddleware($accessToken))->get("accounts/group/{$groupName}/keys"); - } - - public function getCollaboratorAccess($accessToken, $accountId) - { - return $this->guzzle($this->getBearerTokenMiddleware($accessToken))->get("accounts/{$accountId}/access"); - } - - public function removeCollaboratorsForApp($accessToken, $accountId, $appId) - { - return $this->guzzle($this->getBearerTokenMiddleware($accessToken)) - ->delete("accounts/{$accountId}/collaborators/apps/{$appId}"); - } - - public function updateAccount($accessToken, $accountId, array $data) - { - return $this->guzzle($this->getBearerTokenMiddleware($accessToken)) - ->patch("accounts/{$accountId}", [ - 'json' => $data, - ]); - } - - public function updateAdmin($accessToken, $id, array $data) - { - return $this->guzzle($this->getBearerTokenMiddleware($accessToken)) - ->patch("accounts/admins/{$id}", [ - 'json' => $data, - ]); - } - + // fun! /** * @param string $accessToken * @param int|string $targetAccountId @@ -191,81 +91,6 @@ public function removeAccess(string $accessToken, $targetAccountId, $sourceAccou throw new \Exception('Invalid arguments - must include source & role [& app], or app by itself'); } - public function get2fa($accessToken, $accountId, $phone, $userType = 'account') - { - if ($userType === 'account') { - return $this->guzzle($this->getBearerTokenMiddleware($accessToken)) - ->get("accounts/{$accountId}/2fa", [ - 'query' => ['phone' => $phone], - ]); - } - return $this->guzzle($this->getBearerTokenMiddleware($accessToken)) - ->get("accounts/admins/{$accountId}/2fa", [ - 'query' => ['phone' => $phone], - ]); - } - - public function getAccountOrAdminBySupportId($accessToken, $supportId) - { - try { - $response = $this->getAdmins($accessToken, $supportId); - } catch (ClientException $e) { - if ($e->getCode() !== 404) { - throw $e; - } - } - if (empty($response)) { - $response = $this->listAccounts($accessToken, null, false, $supportId); - } - - return $response; - } - - /** - * @param array $data required: `confirm`, optional: `sendEmail`, `name`, `reason` - * @return \Psr\Http\Message\ResponseInterface - */ - public function cancelAccount($accessToken, $accountId, array $data) - { - return $this->guzzle($this->getBearerTokenMiddleware($accessToken)) - ->post("accounts/{$accountId}/cancel-by-admin", [ - 'json' => $data, - ]); - } - - public function detachAccount($accessToken, $accountId) - { - return $this->guzzle($this->getBearerTokenMiddleware($accessToken)) - ->post("accounts/{$accountId}/detach"); - } - - - public function createAdmin( - string $token, - string $username, - string $name, - string $email, - string $phone, - string $phoneCountry = '1', - ?string $password = null - ) { - $json = [ - 'name' => $name, - 'username' => $username, - 'email' => $email, - 'phone' => $phone, - 'phoneCountry' => $phoneCountry, - ]; - - if ($password) { - $json['password'] = $password; - } - - return $this->guzzle($this->getBearerTokenMiddleware($token)) - ->post('accounts/admins', [ - 'json' => $json, - ]); - } public function getApiKeys(string $token, array $accountIds) { @@ -307,74 +132,6 @@ public function setCellId(string $token, int $accountId, string $cellId) ]); } - public function enable(string $accessToken, int $accountId, string $reason, string $name) - { - return $this->guzzle($this->getBearerTokenMiddleware($accessToken)) - ->post("accounts/{$accountId}/enable", [ - 'json' => [ - 'reason' => $reason, - 'name' => $name, - ], - ]); - } - public function disable(string $accessToken, int $accountId, string $reason, string $name) - { - return $this->guzzle($this->getBearerTokenMiddleware($accessToken)) - ->post("accounts/{$accountId}/disable", [ - 'json' => [ - 'reason' => $reason, - 'name' => $name, - ], - ]); - } - - public function storeSignupInfo(string $accessToken, string $email, string $password) - { - return $this->guzzle($this->getBearerTokenMiddleware($accessToken)) - ->post('accounts/signup-info', [ - 'json' => [ - 'email' => $email, - 'password' => $password, - ], - ]); - } - - public function passwordReset(string $email, bool $admin = false) - { - $path = $admin ? 'accounts/admins/password-reset' : 'accounts/password-reset'; - return $this->guzzle()->post($path, [ - 'json' => [ - 'email' => $email, - ], - ]); - } - - public function passwordResetFinish(string $password, string $token, bool $admin = false) - { - $path = $admin ? 'accounts/admins/password-reset' : 'accounts/password-reset'; - return $this->guzzle()->patch($path, [ - 'json' => [ - 'token' => $token, - 'password' => $password, - ], - ]); - } - - public function listPreProvisionedAccountsForCell(string $token, string $cellId) - { - return $this->guzzle($this->getBearerTokenMiddleware($token)) - ->get("accounts/pre-provisioned/cellId/{$cellId}"); - } - - public function getIsGDPR($accessToken, $accountId) - { - return $this->guzzle($this->getBearerTokenMiddleware($accessToken))->get("accounts/{$accountId}/is-gdpr"); - } - - public function getSecretAnswer(string $token, int $accountId) - { - return $this->guzzle($this->getBearerTokenMiddleware($token))->get("accounts/{$accountId}/secret-answer"); - } public function authenticateUser(string $accessToken, string $username, string $password) { diff --git a/src/Command/Accounts/ListAdminsCommand.php b/src/Command/Accounts/ListAdminsCommand.php new file mode 100644 index 0000000..2a8748d --- /dev/null +++ b/src/Command/Accounts/ListAdminsCommand.php @@ -0,0 +1,51 @@ +authClient = $authApi; + $this->api = $apps; + parent::__construct($name); + } + + public function configure() + { + parent::configure(); + $this + ->setDescription('List admins on account') + ->addArgument('accountId', InputArgument::REQUIRED, 'Account ID') + ; + $this->addOauthOptions(); + } + + public function execute(InputInterface $input, OutputInterface $output): int + { + $accountId = $input->getArgument('accountId'); + $token = $this->token->token; + + $r = $this->api->getAdmins($token, $accountId); + $output->writeln(json_encode(json_decode($r->getBody()->getContents()), JSON_PRETTY_PRINT)); + + return 0; + } +} diff --git a/src/Command/Accounts/RemoveCollabCommand.php b/src/Command/Accounts/RemoveCollabCommand.php new file mode 100644 index 0000000..161b090 --- /dev/null +++ b/src/Command/Accounts/RemoveCollabCommand.php @@ -0,0 +1,51 @@ +authClient = $authApi; + $this->api = $apps; + parent::__construct($name); + } + + public function configure() + { + parent::configure(); + $this + ->setDescription('Remove collaborator from account') + ->addArgument('accountId', InputArgument::REQUIRED, 'Account ID') + ->addArgument('appId', InputArgument::REQUIRED, 'App ID') + ; + $this->addOauthOptions(); + } + + public function execute(InputInterface $input, OutputInterface $output): int + { + $accountId = $input->getArgument('accountId'); + $appId = $input->getArgument('appId'); + $token = $this->token->token; + + $r = $this->api->removeAccess($token, $accountId, $appId); + $output->writeln(json_encode(json_decode($r->getBody()->getContents()), JSON_PRETTY_PRINT)); + + return 0; + } +}