Skip to content

Commit

Permalink
more changes to squash eventually
Browse files Browse the repository at this point in the history
  • Loading branch information
dsmith4-godaddy committed Oct 29, 2024
1 parent cccd52e commit d9647e8
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 25 deletions.
37 changes: 19 additions & 18 deletions src/API/Accounts/AccountsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,25 @@ class AccountsClient extends BaseApiClient
{
protected $apiName = 'accounts';

public function getCollaboratorAccess($accessToken, $accountId)
public function getCollaborators(string $accessToken, string $accountId)
{
return $this->guzzle($this->getBearerTokenMiddleware($accessToken))->get("accounts/{$accountId}/access");
}

public function getCollabRole(string $accessToken, int $accountId, int $collabId, int $appId = 0): int
{
$r = $this->getCollaborators($accessToken, $accountId);
$collabInfo = json_decode($r->getBody()->getContents(), true);
foreach(@$collabInfo['whoCanAccess'] as $tidbit) {
// print_r($tidbit);
// should this output be validated or similar?
if (($tidbit['appId'] == $appId) && ($tidbit['sourceId'] == $collabId)) {
return $tidbit['role'];
}
}
return 0;
}

public function addCollaboratorToAcct(string $accessToken, string $newAcctEmail, string $newAcctName, int $newAcctRole, int $newAcctId)
{
return $this->guzzle($this->getBearerTokenMiddleware($accessToken))
Expand All @@ -25,10 +39,11 @@ public function addCollaboratorToAcct(string $accessToken, string $newAcctEmail,
]);
}

public function removeCollaboratorFromAcct(string $accessToken, string $acctId, string $collabId) {
$role = 6; // this is a temp hack, need to actually get user's role somehow
public function removeCollaboratorFromAcct(string $accessToken, int $acctId, int $collabId, int $appId = 0) {
// $role = 6; // this is a temp hack, need to actually get user's role somehow
$role = $this->getCollabRole($accessToken, $acctId, $collabId, $appId);
return $this->guzzle($this->getBearerTokenMiddleware($accessToken))
->delete("accounts/{$acctId}/collaborators/{$collabId}/{$role}/0");
->delete("accounts/{$acctId}/collaborators/{$collabId}/{$role}/{$appId}");
}

public function removeCollaboratorsForApp(string $accessToken, string $accountId, int $appId)
Expand Down Expand Up @@ -65,20 +80,6 @@ public function removeAccess(string $accessToken, $targetAccountId, $sourceAccou
throw new \Exception('Invalid arguments - must include source & role [& app], or app by itself');
}


public function getApiKeys(string $token, array $accountIds)
{
return $this->guzzle($this->getBearerTokenMiddleware($token))
->get(
'accounts/apikeys',
[
'query' => [
'accountIds' => $accountIds,
],
]
);
}

public function listSshPublicKeys(
string $accessToken,
string $accountId,
Expand Down
5 changes: 3 additions & 2 deletions src/Command/Accounts/AddCollabToAcctCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,22 @@ class AddCollabToAcctCommand extends Command
*/
protected $api;

public function __construct(AuthApi $authApi, AccountsClient $apps, $name = 'account:collabs:add-to-account')
public function __construct(AuthApi $authApi, AccountsClient $apps, $name = 'account:collabs:add')
{
$this->authClient = $authApi;
$this->api = $apps;
parent::__construct($name);
}

// TODO the role should probably be an enum with actually useful text values instead of random ints
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('roleId', InputArgument::REQUIRED, 'Role ID')
->addArgument('name', InputArgument::OPTIONAL, 'Display Name', 0)
;
$this->addOauthOptions();
Expand Down
56 changes: 56 additions & 0 deletions src/Command/Accounts/GetCollabRoleCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

// TODO remove this it was a testing stub :V

namespace Pagely\AtomicClient\Command\Accounts;

use League\CLImate\TerminalObject\Dynamic\Input;
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 GetCollabRoleCommand extends Command
{
use OauthCommandTrait;

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

public function __construct(AuthApi $authApi, AccountsClient $apps, $name = 'account:collabs:get-role')
{
$this->authClient = $authApi;
$this->api = $apps;
parent::__construct($name);
}

public function configure()
{
parent::configure();
$this
->setDescription('Get collaborator role on account or specific app')
->addArgument('accountId', InputArgument::REQUIRED, 'Account ID')
->addArgument('collabId', InputArgument::REQUIRED, 'Collab user ID')
->addArgument('appId', InputArgument::OPTIONAL, 'App ID', 0)
;
$this->addOauthOptions();
}

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->getCollabRole($token, $accountId, $collabId, $appId);
print_r($r);

return 0;
}
}
2 changes: 1 addition & 1 deletion src/Command/Accounts/ListCollabsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
$accountId = $input->getArgument('accountId');
$token = $this->token->token;

$r = $this->api->getCollaboratorAccess($token, $accountId);
$r = $this->api->getCollaborators($token, $accountId);
$output->writeln(json_encode(json_decode($r->getBody()->getContents()), JSON_PRETTY_PRINT));

return 0;
Expand Down
9 changes: 5 additions & 4 deletions src/Command/Accounts/RemoveCollabFromAcctCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class RemoveCollabFromAcctCommand extends Command
*/
protected $api;

public function __construct(AuthApi $authApi, AccountsClient $apps, $name = 'account:collabs:remove-from-acct')
public function __construct(AuthApi $authApi, AccountsClient $apps, $name = 'account:collabs:remove')
{
$this->authClient = $authApi;
$this->api = $apps;
Expand All @@ -30,9 +30,10 @@ public function configure()
{
parent::configure();
$this
->setDescription('Remove collaborator from account (BROKEN)')
->setDescription('Remove collaborator from account')
->addArgument('accountId', InputArgument::REQUIRED, 'Account ID')
->addArgument('collabId', InputArgument::REQUIRED, 'Collab User ID')
->addArgument('appId', InputArgument::OPTIONAL, 'App ID, or 0 for all apps', 0)
;
$this->addOauthOptions();
}
Expand All @@ -41,10 +42,10 @@ public function execute(InputInterface $input, OutputInterface $output): int
{
$accountId = $input->getArgument('accountId');
$collabId = $input->getArgument('collabId');
$appId = $input->getArgument('appId');
$token = $this->token->token;

// this NEEDS that function! trust me!
$r = $this->api->removeCollaboratorFromAcct($token, $accountId, $collabId);
$r = $this->api->removeCollaboratorFromAcct($token, $accountId, $collabId, $appId);
$output->writeln(json_encode(json_decode($r->getBody()->getContents()), JSON_PRETTY_PRINT));

return 0;
Expand Down

0 comments on commit d9647e8

Please sign in to comment.