From cccd52ec8a6de3018454b80a093b079699a98bfd Mon Sep 17 00:00:00 2001 From: David Smith Date: Tue, 29 Oct 2024 15:12:39 -0500 Subject: [PATCH] Add collab command mostly --- src/API/Accounts/AccountsClient.php | 20 ++++++- .../Accounts/AddCollabToAcctCommand.php | 57 +++++++++++++++++++ ...nd.php => RemoveCollabFromAcctCommand.php} | 11 ++-- 3 files changed, 80 insertions(+), 8 deletions(-) create mode 100644 src/Command/Accounts/AddCollabToAcctCommand.php rename src/Command/Accounts/{RemoveCollabCommand.php => RemoveCollabFromAcctCommand.php} (75%) diff --git a/src/API/Accounts/AccountsClient.php b/src/API/Accounts/AccountsClient.php index caf1d62..f824af0 100644 --- a/src/API/Accounts/AccountsClient.php +++ b/src/API/Accounts/AccountsClient.php @@ -13,7 +13,25 @@ public function getCollaboratorAccess($accessToken, $accountId) return $this->guzzle($this->getBearerTokenMiddleware($accessToken))->get("accounts/{$accountId}/access"); } - public function removeCollaboratorsForApp($accessToken, $accountId, $appId) + public function addCollaboratorToAcct(string $accessToken, string $newAcctEmail, string $newAcctName, int $newAcctRole, int $newAcctId) + { + return $this->guzzle($this->getBearerTokenMiddleware($accessToken)) + ->post("accounts/{$newAcctId}/collaborators", ['json' => [ + 'email' => $newAcctEmail, + 'name' => $newAcctName, + 'role' => $newAcctRole, + 'appId' => 0 + ], + ]); + } + + public function removeCollaboratorFromAcct(string $accessToken, string $acctId, string $collabId) { + $role = 6; // this is a temp hack, need to actually get user's role somehow + return $this->guzzle($this->getBearerTokenMiddleware($accessToken)) + ->delete("accounts/{$acctId}/collaborators/{$collabId}/{$role}/0"); + } + + public function removeCollaboratorsForApp(string $accessToken, string $accountId, int $appId) { return $this->guzzle($this->getBearerTokenMiddleware($accessToken)) ->delete("accounts/{$accountId}/collaborators/apps/{$appId}"); diff --git a/src/Command/Accounts/AddCollabToAcctCommand.php b/src/Command/Accounts/AddCollabToAcctCommand.php new file mode 100644 index 0000000..206916c --- /dev/null +++ b/src/Command/Accounts/AddCollabToAcctCommand.php @@ -0,0 +1,57 @@ +authClient = $authApi; + $this->api = $apps; + parent::__construct($name); + } + + public function configure() + { + parent::configure(); + $this + ->setDescription('Add collaborator to account') + ->addArgument('email', InputArgument::REQUIRED, 'Email address') + ->addArgument('accountId', InputArgument::REQUIRED, 'Account ID') + ->addArgument('roleId', InputArgument::REQUIRED, 'Role ID') + ->addArgument('name', InputArgument::OPTIONAL, 'Display Name', 0) + ; + $this->addOauthOptions(); + } + + public function execute(InputInterface $input, OutputInterface $output): int + { + $newAcctEmail = $input->getArgument('email'); + $newAcctName = $input->getArgument('name'); + if ($newAcctName === 0) { $newAcctName = $input->getArgument('email'); } + $newAcctAppId = $input->getArgument('accountId'); + $newAcctRole = $input->getArgument('roleId'); + $token = $this->token->token; + + $r = $this->api->addCollaboratorToAcct($token, + $newAcctEmail, $newAcctName, $newAcctRole, $newAcctAppId); + $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/RemoveCollabFromAcctCommand.php similarity index 75% rename from src/Command/Accounts/RemoveCollabCommand.php rename to src/Command/Accounts/RemoveCollabFromAcctCommand.php index 6aeff03..8cfb7d6 100644 --- a/src/Command/Accounts/RemoveCollabCommand.php +++ b/src/Command/Accounts/RemoveCollabFromAcctCommand.php @@ -10,7 +10,7 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -class RemoveCollabCommand extends Command +class RemoveCollabFromAcctCommand extends Command { use OauthCommandTrait; @@ -19,7 +19,7 @@ class RemoveCollabCommand extends Command */ protected $api; - public function __construct(AuthApi $authApi, AccountsClient $apps, $name = 'account:collabs:remove') + public function __construct(AuthApi $authApi, AccountsClient $apps, $name = 'account:collabs:remove-from-acct') { $this->authClient = $authApi; $this->api = $apps; @@ -33,21 +33,18 @@ public function configure() ->setDescription('Remove collaborator from account (BROKEN)') ->addArgument('accountId', InputArgument::REQUIRED, 'Account ID') ->addArgument('collabId', InputArgument::REQUIRED, 'Collab User ID') - ->addArgument('appId', InputArgument::OPTIONAL, 'App ID', 0) ; $this->addOauthOptions(); } - // TODO this needs to look up the provided info and get the user's current role, so we can feed that - // to removeAccess. For now treat it as broken. public function execute(InputInterface $input, OutputInterface $output): int { $accountId = $input->getArgument('accountId'); $collabId = $input->getArgument('collabId'); - $appId = $input->getArgument('appId'); $token = $this->token->token; - $r = $this->api->removeAccess($token, $accountId, $collabId, 8, $appId); + // this NEEDS that function! trust me! + $r = $this->api->removeCollaboratorFromAcct($token, $accountId, $collabId); $output->writeln(json_encode(json_decode($r->getBody()->getContents()), JSON_PRETTY_PRINT)); return 0;