Skip to content

Commit e13ea75

Browse files
committed
Support new user invitations
1 parent ac8843a commit e13ea75

File tree

5 files changed

+42
-11
lines changed

5 files changed

+42
-11
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"guzzlehttp/guzzle": "^5.3",
99
"guzzlehttp/ringphp": "^1.1",
1010
"platformsh/console-form": ">=0.0.24 <2.0",
11-
"platformsh/client": ">=0.33.0 <2.0",
11+
"platformsh/client": ">=0.35.2 <2.0",
1212
"symfony/console": "^3.0 >=3.2",
1313
"symfony/yaml": "^3.0 || ^2.6",
1414
"symfony/finder": "^3.0",

composer.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ api:
149149
# Overridden by {application.env_prefix}AUTO_LOAD_SSH_CERT env var
150150
auto_load_ssh_cert: false
151151

152+
# Whether the API supports project invitations.
153+
invitations: true
154+
152155
# How the CLI detects and configures Git repositories as projects.
153156
detection:
154157
git_remote_name: 'platform'

src/Command/User/UserAddCommand.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
use Platformsh\Cli\Command\CommandBase;
55
use Platformsh\Client\Model\EnvironmentAccess;
6+
use Platformsh\Client\Model\Invitation\AlreadyInvitedException;
7+
use Platformsh\Client\Model\Invitation\Environment;
68
use Platformsh\Client\Model\ProjectAccess;
79
use Symfony\Component\Console\Exception\InvalidArgumentException;
810
use Symfony\Component\Console\Helper\ProgressBar;
@@ -251,20 +253,37 @@ protected function execute(InputInterface $input, OutputInterface $output)
251253
// Ask for confirmation.
252254
if ($existingProjectAccess) {
253255
if (!$questionHelper->confirm('Are you sure you want to make these change(s)?')) {
254-
255256
return 1;
256257
}
257258
} else {
258259
$this->stdErr->writeln('<comment>Adding users can result in additional charges.</comment>');
259260
$this->stdErr->writeln('');
260261
if (!$questionHelper->confirm('Are you sure you want to add this user?')) {
261-
262262
return 1;
263263
}
264264
}
265+
$this->stdErr->writeln('');
266+
267+
// If the user does not already exist on the project, then use the Invitations API.
268+
if (!$existingProjectAccess && $this->config()->getWithDefault('api.invitations', false)) {
269+
$this->stdErr->writeln('Inviting the user to the project...');
270+
$environments = [];
271+
foreach ($desiredEnvironmentRoles as $id => $role) {
272+
$environments[] = new Environment($id, $role);
273+
}
274+
try {
275+
$project->inviteUserByEmail($email, $desiredProjectRole, $environments);
276+
$this->stdErr->writeln('');
277+
$this->stdErr->writeln(sprintf('An invitation has been sent to <info>%s</info>', $email));
278+
} catch (AlreadyInvitedException $e) {
279+
$this->stdErr->writeln('');
280+
$this->stdErr->writeln(sprintf('An invitation has already been sent to <info>%s</info>', $e->getEmail()));
281+
}
282+
283+
return 0;
284+
}
265285

266-
// Make the required modifications on the project level: add the user,
267-
// change their role, or do nothing.
286+
// Make the desired changes at the project level.
268287
if (!$existingProjectAccess) {
269288
$this->stdErr->writeln("Adding the user to the project");
270289
$result = $project->addUser($email, $desiredProjectRole);

src/Service/Api.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ private function getUserAgent()
247247
private function getConnectorOptions() {
248248
$connectorOptions = [];
249249
$connectorOptions['accounts'] = rtrim($this->config->get('api.accounts_api_url'), '/') . '/';
250+
$connectorOptions['api_url'] = $this->config->getWithDefault('api.base_url', '');
250251
$connectorOptions['certifier_url'] = $this->config->get('api.certifier_url');
251252
$connectorOptions['verify'] = !$this->config->get('api.skip_ssl');
252253
$connectorOptions['debug'] = $this->config->get('api.debug') ? STDERR : false;
@@ -531,8 +532,12 @@ public function getProjects($refresh = null)
531532
$this->cache->save($cacheKey, $cachedProjects, $this->config->get('api.projects_ttl'));
532533
} else {
533534
$guzzleClient = $this->getHttpClient();
535+
$apiUrl = $this->config->getWithDefault('api.base_url', '');
534536
foreach ((array) $cached as $id => $data) {
535537
$projects[$id] = new Project($data, $data['_endpoint'], $guzzleClient);
538+
if ($apiUrl) {
539+
$projects[$id]->setApiUrl($apiUrl);
540+
}
536541
}
537542
}
538543

@@ -580,6 +585,10 @@ public function getProject($id, $host = null, $refresh = null)
580585
$baseUrl = $cached['_endpoint'];
581586
unset($cached['_endpoint']);
582587
$project = new Project($cached, $baseUrl, $guzzleClient);
588+
$apiUrl = $this->config->getWithDefault('api.base_url', '');
589+
if ($apiUrl) {
590+
$projects[$id]->setApiUrl($apiUrl);
591+
}
583592
}
584593

585594
return $project;

0 commit comments

Comments
 (0)