/** * Object constructor * * @param array $options Options to set as $this->key */ public function __construct($options = array()) { parent::__construct($options); if (!isset($this->user)) { $this->user = Session::getUser(); } }
/** * Delete a machine token from your account * * ## OPTIONS * [--machine-token-id=<id>] * : UUID or name of the site you want to delete * * [--force] * : to skip the confirmations */ public function delete($args, $assoc_args) { $user = Session::getUser(); $id = $assoc_args['machine-token-id']; if (empty($id)) { $this->failure('You must specify a machine token id to delete.'); } // Find the token $machine_token = $user->machine_tokens->get($assoc_args['machine-token-id']); if (empty($machine_token)) { $this->failure('There are no machine tokens with the id {id}.', array('id' => $id)); } $name = $machine_token->get('device_name'); if (!isset($assoc_args['force']) && !Terminus::getConfig('yes')) { //If the force option isn't used, we'll ask you some annoying questions Input::confirm(array('message' => 'Are you sure you want to delete %s?', 'context' => $name)); } $this->log()->info('Deleting {name} ...', array('name' => $name)); $response = $machine_token->delete(); if ($response['status_code'] == 200) { $this->log()->info('Deleted {name}!', array('name' => $name)); } else { $this->failure('There was an problem deleting the machine token.'); } }
/** * Print and save drush aliases * * ## OPTIONS * * [--print] * : print aliases to screen * * [--location=<location>] * : Specify the the full path, including the filename, to the alias file * you wish to create. Without this option a default of * '~/.drush/pantheon.aliases.drushrc.php' will be used. */ public function aliases($args, $assoc_args) { $user = Session::getUser(); $print = $this->input()->optional(array('key' => 'print', 'choices' => $assoc_args, 'default' => false)); $location = $this->input()->optional(array('key' => 'location', 'choices' => $assoc_args, 'default' => getenv('HOME') . '/.drush/pantheon.aliases.drushrc.php')); if (is_dir($location)) { $message = 'Please provide a full path with filename,'; $message .= ' e.g. {location}/pantheon.aliases.drushrc.php'; $this->failure($message, compact('location')); } $file_exists = file_exists($location); // Create the directory if it doesn't yet exist $dirname = dirname($location); if (!is_dir($dirname)) { mkdir($dirname, 0700, true); } $content = $user->getAliases(); $h = fopen($location, 'w+'); fwrite($h, $content); fclose($h); chmod($location, 0700); $message = 'Pantheon aliases created'; if ($file_exists) { $message = 'Pantheon aliases updated'; } if (strpos($content, 'array') === false) { $message .= ', although you have no sites'; } $this->log()->info($message); if ($print) { $aliases = str_replace(array('<?php', '?>'), '', $content); $this->output()->outputDump($aliases); } }
/** * Show a list of your instruments on Pantheon * * @subcommand list */ public function all($args, $assoc_args) { $user = Session::getUser(); $instruments = $user->instruments->all(); $data = array(); foreach ($instruments as $id => $instrument) { $data[] = array('label' => $instrument->get('label'), 'id' => $instrument->get('id')); } $this->output()->outputRecordList($data); }
/** * Instantiate a console within Terminus * * ## OPTIONS * * [--site=<site>] * : name of site to load * * @subcommand console */ public function console($args, $assoc_args) { $user = Session::getUser(); if (isset($assoc_args['site'])) { $sitename = $assoc_args['site']; $site_id = $this->sitesCache->findId($sitename); $site = new Site($site_id); } eval(\Psy\sh()); }
/** * Object constructor * * @param object $attributes Attributes of this model * @param array $options Options to set as $this->key */ public function __construct($attributes = null, array $options = array()) { parent::__construct($attributes, $options); if (!isset($this->user)) { $this->user = Session::getUser(); } $this->site_memberships = new OrganizationSiteMemberships(array('organization' => $this, 'owner' => $this, 'owner_type' => 'organization')); $this->user_memberships = new OrganizationUserMemberships(array('organization' => $this, 'owner' => $this, 'owner_type' => 'organization')); $this->workflows = new Workflows(array('owner' => $this, 'owner_type' => 'organization')); }
/** * Show a list of your organizations on Pantheon * * @subcommand list */ public function all($args, $assoc_args) { $user = Session::getUser(); $data = array(); $organizations = $user->getOrganizations(); foreach ($organizations as $id => $org) { $org_data = $org->get('organization'); $data[] = array('name' => $org_data->profile->name, 'id' => $org->get('id')); } $this->output()->outputRecordList($data); }
/** * Say hello */ public function hello($args, $assoc_args) { if (Session::getValue('user_uuid')) { $user = Session::getUser(); $user->fetch(); $data = $user->serialize(); $this->log()->info("Hello, {name}!", array('name' => $data['firstname'])); } else { $this->log()->info("Hello, Anonymous!"); } }
/** * Delete a machine token from your account * * ## OPTIONS * [--machine-token-id=<id>] * : UUID or name of the site you want to delete * * [--force] * : to skip the confirmations */ public function delete($args, $assoc_args) { $user = Session::getUser(); $id = $assoc_args['machine-token-id']; if (empty($id)) { $this->failure('You must specify a machine token id to delete.'); } // Find the token $machine_token = $user->machine_tokens->get($assoc_args['machine-token-id']); if (empty($machine_token)) { $this->failure('There are no machine tokens with the id {id}.', array('id' => $id)); } $name = $machine_token->get('device_name'); $this->input()->confirm(['message' => 'Are you sure you want to delete %s?', 'context' => $name, 'args' => $assoc_args]); $this->log()->info('Deleting {name} ...', array('name' => $name)); $response = $machine_token->delete(); if ($response['status_code'] == 200) { $this->log()->info('Deleted {name}!', array('name' => $name)); } else { $this->failure('There was an problem deleting the machine token.'); } }
/** * Instantiates the collection, sets param members as properties * * @param array $options To be set to $this->key * @return Sites */ public function __construct(array $options = array()) { parent::__construct($options); $this->sites_cache = new SitesCache(); $this->user = Session::getUser(); }
/** * Returns an array listing organizaitions applicable to user * * @param array $arg_options Elements as follow: * bool allow_none True to allow the "none" option * @return array A list of organizations */ private function orgList(array $arg_options = []) { $default_options = ['allow_none' => true]; $options = array_merge($default_options, $arg_options); $org_list = []; if ($options['allow_none']) { $org_list = ['-' => 'None']; } $user = Session::getUser(); $organizations = $user->organizations->all(); foreach ($organizations as $id => $org) { $org_data = $org->get('organization'); $org_list[$org->get('id')] = $org_data->profile->name; } return $org_list; }
/** * Execute the login based on a machine token * * @param string[] $args Elements as follow: * string token Machine token to initiate login with * string email Email address to locate token with * @return bool True if login succeeded * @throws TerminusException */ public function logInViaMachineToken($args) { if (isset($args['token'])) { $token = $args['token']; } elseif (isset($args['email'])) { $token = $this->tokens_cache->findByEmail($args['email'])['token']; if (!$token) { throw new TerminusException('No machine token for "{email}" found.', compact('email'), 1); } $this->logger->info('Found a machine token for "{email}".', ['email' => $args['email']]); } $options = array('headers' => array('Content-type' => 'application/json'), 'form_params' => array('machine_token' => $token, 'client' => 'terminus')); $this->logger->info('Logging in via machine token'); $response = $this->request->request('authorize', '', '', 'POST', $options); if (!$response || !isset($response['status_code']) || $response['status_code'] != '200') { throw new TerminusException('The provided machine token is not valid.', [], 1); } $data = $response['data']; $this->setInstanceData($response['data']); $user = Session::getUser(); $user->fetch(); $user_data = $user->serialize(); $this->logger->info('Logged in as {email}.', ['email' => $user_data['email']]); if (isset($args['token'])) { $this->tokens_cache->add(['email' => $user_data['email'], 'token' => $token]); } return true; }
/** * Checks to ensure user can access the given organization * * @param string $org_id Organization name or UUID * @return bool True if this organization is accessible */ private function isOrgAccessible($org_id) { $user = Session::getUser(); $org = $user->organizations->get($org_id); $is_ok = is_object($org); return $is_ok; }
/** * Find out what user you are logged in as. */ public function whoami() { if (Session::getValue('user_uuid')) { $user = Session::getUser(); $user->fetch(); $data = $user->serialize(); $this->output()->outputRecord($data); } else { $this->failure('You are not logged in.'); } }
/** * Instantiate a console within Terminus * * ## OPTIONS * * [--site=<site>] * : name of site to load * * @subcommand console */ public function console($args, $assoc_args) { $user = Session::getUser(); if (isset($assoc_args['site'])) { $site = $this->sites->get($this->input()->siteName(array('args' => $assoc_args))); } eval(\Psy\sh()); }
/** * Requests API data and returns aliases * * @param array $arg_options Elements as follow: * boolean org_only Set to true for only organizational aliases * boolean team_only Set to true for only team aliases * @return string */ private function getAliases(array $arg_options = []) { $default_options = ['org_only' => false, 'team_only' => false]; $options = array_merge($default_options, $arg_options); $user = Session::getUser(); $alias_string = $user->getAliases(); if ($options['team_only']) { return $alias_string; } eval(str_replace('<?php', '', $alias_string)); $team_aliases = substr($alias_string, 0, -1); $sites_object = new Sites(); $sites = $sites_object->fetch()->all(); $org_aliases = ''; foreach ($sites as $site) { $environments = $site->environments->all(); foreach ($environments as $environment) { $key = $site->get('name') . '.' . $environment->get('id'); if (isset($aliases[$key])) { break; } try { $org_aliases .= PHP_EOL . " \$aliases['{$key}'] = "; $org_aliases .= $this->constructAlias($environment); } catch (TerminusException $e) { continue; } } } if ($options['org_only']) { $org_aliases .= PHP_EOL; return $org_aliases; } $all_aliases = $alias_string . $org_aliases . PHP_EOL; return $all_aliases; }
public function testGetUser() { $user = Session::getUser(); $this->assertEquals($user->get('id'), '0ffec038-4410-43d0-a404-46997f672d7a'); }