diff --git a/CHANGELOG.md b/CHANGELOG.md index 016b9a394..4507b83ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ All notable changes to this project starting with the 0.6.0 release will be docu ## MASTER ### Changed - WP-CLI function `import` has been removed from the command blacklist. [See the documentation for more information.](https://github.com/pantheon-systems/documentation/blob/master/source/docs/guides/create-a-wordpress-site-from-the-commandline-with-terminus-and-wp-cli.md) (#979) +- When running `terminus auth login` and more than one machine token is present, Terminus will now tell you how to use them instead of giving the same error message received when no tokens are present. (#987) ### Fixed - Fixed unidentified index email warning which appeared when logging in via saved machine token by email. (#983) diff --git a/php/Terminus/Commands/AuthCommand.php b/php/Terminus/Commands/AuthCommand.php index 18915c623..2d7107fdb 100755 --- a/php/Terminus/Commands/AuthCommand.php +++ b/php/Terminus/Commands/AuthCommand.php @@ -31,7 +31,8 @@ class AuthCommand extends TerminusCommand { * : dump call information when logging in. */ public function login($args, $assoc_args) { - $auth = new Auth(); + $auth = new Auth(); + $tokens = $auth->getAllSavedTokenEmails(); if (!empty($args)) { $email = array_shift($args); } @@ -68,28 +69,35 @@ public function login($args, $assoc_args) { ); $auth->logInViaMachineToken(['email' => $_SERVER['TERMINUS_USER']]); $this->log()->info('Logging in via machine token'); - } elseif (!isset($email) - && $only_token = $auth->getOnlySavedToken() - ) { + } elseif (!isset($email) && (count($tokens) === 1)) { // Try to log in using a machine token, if there is only one saved token. + $email = array_shift($tokens); + $auth->logInViaMachineToken(compact('email')); $this->log()->info( 'Found a machine token for "{email}".', - ['email' => $only_token['email'],] + compact('email') ); - $auth->logInViaMachineToken($only_token); + $auth->logInViaMachineToken(compact('email')); $this->log()->info('Logging in via machine token'); } else if (isset($email) && isset($assoc_args['password'])) { + // Log in via username and password, if present. $password = $assoc_args['password']; $auth->logInViaUsernameAndPassword( $email, $assoc_args['password'] ); } else { - $this->log()->info( - "Please visit the Dashboard to generate a machine token:\n{url}", - ['url' => $auth->getMachineTokenCreationUrl()] - ); - exit(1); + $message = "visit the dashboard to generate a machine token:\n{url}"; + $context = ['url' => $auth->getMachineTokenCreationUrl()]; + if (count($tokens) > 1) { + $msg = "Tokens were saved for the following email addresses:\n"; + $msg .= "{tokens}\n You may log in via `terminus auth login `"; + $message = "$msg, or you may $message"; + $context['tokens'] = implode("\n", $tokens); + } else { + $message = "Please $message"; + } + $this->failure($message, $context); } if (!isset($email)) { $user = Session::getUser(); diff --git a/php/Terminus/Commands/TerminusCommand.php b/php/Terminus/Commands/TerminusCommand.php index ee922908f..38701cb51 100755 --- a/php/Terminus/Commands/TerminusCommand.php +++ b/php/Terminus/Commands/TerminusCommand.php @@ -105,10 +105,12 @@ public function output() { * @throws TerminusException */ public function ensureLogin() { - $auth = new Auth(); + $auth = new Auth(); + $tokens = $auth->getAllSavedTokenEmails(); if (!$auth->loggedIn()) { - if ($token = $auth->getOnlySavedToken()) { - $auth->logInViaMachineToken($token); + if (count($tokens) === 1) { + $email = array_shift($token); + $auth->logInViaMachineToken(compact('email')); } else if (isset($_SERVER['TERMINUS_MACHINE_TOKEN']) && $token = $_SERVER['TERMINUS_MACHINE_TOKEN'] ) { diff --git a/php/Terminus/Models/Auth.php b/php/Terminus/Models/Auth.php index 09a2f2685..6a424192f 100755 --- a/php/Terminus/Models/Auth.php +++ b/php/Terminus/Models/Auth.php @@ -28,6 +28,16 @@ public function __construct($attributes = null, array $options = array()) { parent::__construct($attributes, $options); } + /** + * Gets all email addresses for which there are saved machine tokens + * + * @return string[] + */ + public function getAllSavedTokenEmails() { + $emails = $this->tokens_cache->getAllSavedTokenEmails(); + return $emails; + } + /** * Generates the URL string for where to create a machine token * @@ -44,20 +54,6 @@ public function getMachineTokenCreationUrl() { return $url; } - /** - * Gets the only saved token or returns false - * - * @return bool|string - */ - public function getOnlySavedToken() { - $emails = $this->tokens_cache->getAllSavedTokenEmails(); - if (count($emails) == 1) { - $email = array_shift($emails); - return $this->tokens_cache->findByEmail($email); - } - return false; - } - /** * Checks to see if the current user is logged in * diff --git a/tests/unit_tests/models/test-auth.php b/tests/unit_tests/models/test-auth.php index a6379a016..5b1f48b88 100644 --- a/tests/unit_tests/models/test-auth.php +++ b/tests/unit_tests/models/test-auth.php @@ -23,24 +23,21 @@ public function testConstruct() { $this->assertTrue(strpos(get_class($this->auth), 'Auth') !== false); } + public function testGetAllSavedTokenEmails() { + $tokens_cache = new TokensCache(); + $tokens_dir = $tokens_cache->getCacheDir(); + $token_count = count(scandir($tokens_dir)) - 2; + $tokens = $this->auth->getAllSavedTokenEmails(); + $this->assertEquals(count($tokens), $token_count); + $this->assertInternalType('array', $tokens); + } + public function testGetMachineTokenCreationUrl() { $url = $this->auth->getMachineTokenCreationUrl(); $this->assertInternalType('string', $url); $this->assertInternalType('integer', strpos($url, 'machine-token/create')); } - public function testGetOnlySavedToken() { - $tokens_cache = new TokensCache(); - $tokens_dir = $tokens_cache->getCacheDir(); - $token_count = count(scandir($tokens_dir)) - 2; - $only_token = $this->auth->getOnlySavedToken(); - if ($token_count != 1) { - $this->assertFalse($only_token); - } else { - $this->assertInternalType('array', $only_token); - } - } - public function testLoggedIn() { $this->assertTrue($this->auth->loggedIn()); }