Skip to content

Commit

Permalink
Add collab LS and RM commands
Browse files Browse the repository at this point in the history
  • Loading branch information
dsmith4-godaddy committed Oct 25, 2024
1 parent 4b07210 commit bdd4d7e
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 248 deletions.
253 changes: 5 additions & 248 deletions src/API/Accounts/AccountsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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 = [];
Expand All @@ -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
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down
51 changes: 51 additions & 0 deletions src/Command/Accounts/ListAdminsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Pagely\AtomicClient\Command\Accounts;

use Pagely\AtomicClient\API\Accounts\AccountsClient;
use Pagely\AtomicClient\API\AuthApi;
use Pagely\AtomicClient\Command\Command;
use Pagely\AtomicClient\Command\OauthCommandTrait;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

// doesn't work - needs the token to have MOAR SCOPE

class ListAdminsCommand extends Command
{
use OauthCommandTrait;

/**
* @var AccountsClient
*/
protected $api;

public function __construct(AuthApi $authApi, AccountsClient $apps, $name = 'account:admins:ls')
{
$this->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;
}
}
51 changes: 51 additions & 0 deletions src/Command/Accounts/RemoveCollabCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Pagely\AtomicClient\Command\Accounts;

use Pagely\AtomicClient\API\Accounts\AccountsClient;
use Pagely\AtomicClient\API\AuthApi;
use Pagely\AtomicClient\Command\Command;
use Pagely\AtomicClient\Command\OauthCommandTrait;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class RemoveCollabCommand extends Command
{
use OauthCommandTrait;

/**
* @var AccountsClient
*/
protected $api;

public function __construct(AuthApi $authApi, AccountsClient $apps, $name = 'account:collabs:remove')
{
$this->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;
}
}

0 comments on commit bdd4d7e

Please sign in to comment.