-
Notifications
You must be signed in to change notification settings - Fork 3
Working to add functionality for better account collab mgmt #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
9effe62
Add AccountsApi.php, copied straight over from mgmt-client
dsmith4-godaddy 5c71811
Let's try putting this new code in the "right" place ...
dsmith4-godaddy 1a4b7fa
account:collabs:ls command
dsmith4-godaddy 4b07210
Doc update: correct PHP version requirement
dsmith4-godaddy bdd4d7e
Add collab LS and RM commands
dsmith4-godaddy 1fcee84
apparently the collab rm api is "weird"
dsmith4-godaddy 5916ff4
Let y'all know this command is busted :D
dsmith4-godaddy cccd52e
Add collab command mostly
dsmith4-godaddy d9647e8
more changes to squash eventually
dsmith4-godaddy 607456d
remove an old testing stub cmd
dsmith4-godaddy c623fb8
finally ready for someone other than me to review :D
dsmith4-godaddy 93c7a2f
Update CI test to a more current PHP ver
dsmith4-godaddy bd2121e
add memory to ci
dsmith4-godaddy 676f9a1
Resolve phpstan warnings
dsmith4-godaddy 25df591
Update src/Command/Accounts/AddCollabToAcctCommand.php
dsmith4-godaddy 4e71b9a
Revert function rename
dsmith4-godaddy a6c1b50
Better option handling for collab add cmd
dsmith4-godaddy 1ea5502
Little cosmetic cleanups
dsmith4-godaddy cb1cbe5
suppress a phpstan warning about my overly paranoid code
dsmith4-godaddy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,66 @@ | ||
| <?php | ||
|
|
||
|
|
||
| namespace Pagely\AtomicClient\API\Accounts; | ||
|
|
||
|
|
||
| use Pagely\AtomicClient\API\BaseApiClient; | ||
|
|
||
| class AccountsClient extends BaseApiClient | ||
| { | ||
| protected $apiName = 'accounts'; | ||
|
|
||
| public function addSshKey(string $accessToken, int $accountId, string $sshKey) | ||
| public function getCollaborators(string $accessToken, int $accountId) | ||
| { | ||
| return $this->guzzle($this->getBearerTokenMiddleware($accessToken))->get("accounts/{$accountId}/access"); | ||
| } | ||
|
|
||
| // this whole function feels super hacky but I don't think there's an API way to get this info | ||
| private 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) { | ||
| if (($tidbit['appId'] == $appId) && ($tidbit['sourceId'] == $collabId)) { | ||
| return $tidbit['role']; | ||
| } | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| public function addCollaboratorToAcct(string $accessToken, string $newAcctEmail, string $newAcctName, int $newAcctId, int $newAcctRole, int $newAppId) | ||
| { | ||
| return $this->guzzle($this->getBearerTokenMiddleware($accessToken)) | ||
| ->post("accounts/{$newAcctId}/collaborators", ['json' => [ | ||
| 'email' => $newAcctEmail, | ||
| 'name' => $newAcctName, | ||
| 'role' => $newAcctRole, | ||
| 'appId' => $newAppId | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
| public function removeCollaboratorFromAcct(string $accessToken, int $acctId, int $collabId, int $appId = 0) | ||
| { | ||
| $role = $this->getCollabRole($accessToken, $acctId, $collabId, $appId); | ||
| return $this->guzzle($this->getBearerTokenMiddleware($accessToken)) | ||
| ->delete("accounts/{$acctId}/collaborators/{$collabId}/{$role}/{$appId}"); | ||
| } | ||
|
|
||
| // this is named createSshPublicKey in upstream mgmt-client | ||
| public function addSshKey( | ||
| string $accessToken, | ||
| int $accountId, | ||
| string $key, | ||
| ?string $orchestration = null, | ||
| ?string $sshUsername = null, | ||
| ) { | ||
| return $this->guzzle($this->getBearerTokenMiddleware($accessToken)) | ||
| ->post("accounts/{$accountId}/ssh/keys", [ | ||
| 'json' => [ | ||
| 'key' => $sshKey | ||
| ], | ||
| 'json' => array_filter(compact( | ||
| 'key', | ||
| 'orchestration', | ||
| 'sshUsername', | ||
| )), | ||
| ]); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| <?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\Input\InputOption; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
|
|
||
| class AddCollabToAcctCommand extends Command | ||
| { | ||
| use OauthCommandTrait; | ||
|
|
||
| /** | ||
| * @var AccountsClient | ||
| */ | ||
| protected $api; | ||
|
|
||
| public function __construct(AuthApi $authApi, AccountsClient $accounts, $name = 'account:collabs:add') | ||
| { | ||
| $this->authClient = $authApi; | ||
| $this->api = $accounts; | ||
| parent::__construct($name); | ||
| } | ||
|
|
||
| public function configure() | ||
| { | ||
| parent::configure(); | ||
| $this | ||
| ->setDescription('Add collaborator to account or app') | ||
| ->addArgument('email', InputArgument::REQUIRED, 'Email address') | ||
| ->addArgument('accountId', InputArgument::REQUIRED, 'Account ID') | ||
| ->addArgument('roleId', InputArgument::REQUIRED, 'Role') | ||
| ->addOption('app', null, InputOption::VALUE_OPTIONAL, 'App ID (acct-level if omitted)', 0) | ||
| ->addOption('displayname', null, InputOption::VALUE_OPTIONAL, 'Display Name', 0) | ||
| ; | ||
| $this->addOauthOptions(); | ||
| } | ||
|
|
||
| public function execute(InputInterface $input, OutputInterface $output): int | ||
| { | ||
| $newAcctEmail = $input->getArgument('email'); | ||
| $newAcctName = $input->getArgument('displayname'); | ||
| if ($newAcctName === 0) { $newAcctName = $input->getArgument('email'); } | ||
| $newAcctId = $input->getArgument('accountId'); | ||
| $newAcctRole = $this->roleToInt($input->getArgument('roleId')); | ||
| if ($newAcctRole === false) { | ||
| $output->writeln ("Invalid role, must be one of 'app-only-minimal', 'app-only', 'billing', 'tech', 'sub-admin', 'super-admin', 'owner'"); | ||
| return Command::FAILURE; | ||
dsmith4-godaddy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| $newAppId = $input->getArgument('app'); | ||
| $token = $this->token->token; | ||
|
|
||
| $r = $this->api->addCollaboratorToAcct($token, | ||
| $newAcctEmail, $newAcctName, $newAcctId, $newAcctRole, $newAppId); | ||
| $output->writeln(json_encode(json_decode($r->getBody()->getContents()), JSON_PRETTY_PRINT)); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| private function roleToInt(string $role) | ||
| { | ||
| $role = strtolower($role); | ||
| switch($role) { | ||
| case "app-only-minimal": | ||
| case "apponlyminimal": | ||
| case "1": | ||
| return 1; | ||
| case "app-only": | ||
| case "apponly": | ||
| case "2": | ||
| return 2; | ||
| case "billing": | ||
| case "4": | ||
| return 4; | ||
| case "tech": | ||
| case "6": | ||
| return 6; | ||
| case "sub-admin": | ||
| case "subadmin": | ||
| case "8": | ||
| return 8; | ||
| case "super-admin": | ||
| case "superadmin": | ||
| case "9": | ||
| return 9; | ||
| case "owner": | ||
| case "10": | ||
| return 10; | ||
| default: | ||
| return false; | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| <?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 ListCollabsCommand extends Command | ||
| { | ||
| use OauthCommandTrait; | ||
|
|
||
| /** | ||
| * @var AccountsClient | ||
| */ | ||
| protected $api; | ||
|
|
||
| public function __construct(AuthApi $authApi, AccountsClient $apps, $name = 'account:collabs:ls') | ||
| { | ||
| $this->authClient = $authApi; | ||
| $this->api = $apps; | ||
| parent::__construct($name); | ||
| } | ||
|
|
||
| public function configure() | ||
| { | ||
| parent::configure(); | ||
| $this | ||
| ->setDescription('List collaborators 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->getCollaborators($token, $accountId); | ||
| $output->writeln(json_encode(json_decode($r->getBody()->getContents()), JSON_PRETTY_PRINT)); | ||
|
|
||
| return 0; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| <?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 RemoveCollabFromAcctCommand 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 or app') | ||
| ->addArgument('accountId', InputArgument::REQUIRED, 'Account ID') | ||
| ->addArgument('collabId', InputArgument::REQUIRED, 'Collab User ID') | ||
| ->addArgument('appId', InputArgument::OPTIONAL, 'App ID (acct-level if omitted)', 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->removeCollaboratorFromAcct($token, $accountId, $collabId, $appId); | ||
| $output->writeln(json_encode(json_decode($r->getBody()->getContents()), JSON_PRETTY_PRINT)); | ||
|
|
||
| return 0; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.