/** * Requests API data and returns aliases * * @return string */ private function getAliases() { $request = new Request(); $user = new User(); $path = 'drush_aliases'; $method = 'GET'; $response = $request->request('users', Session::getValue('user_id'), $path, $method); eval(str_replace('<?php', '', $response['data']->drush_aliases)); $formatted_aliases = substr($response['data']->drush_aliases, 0, -1); $sites_object = new Sites(); $sites = $sites_object->all(); 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 { $formatted_aliases .= PHP_EOL . " \$aliases['{$key}'] = "; $formatted_aliases .= $this->constructAlias($environment); } catch (TerminusException $e) { continue; } } } $formatted_aliases .= PHP_EOL; return $formatted_aliases; }
/** * Find out what user you are logged in as. */ public function whoami() { if (Session::getValue('user_uuid')) { $this->output()->outputValue(Session::getValue('user_uuid'), 'You are authenticated as'); } else { $this->failure('You are not logged in.'); } }
/** * Find out what user you are logged in as. */ public function whoami() { if (Session::getValue('email')) { Terminus::line("You are authenticated as " . Session::getValue('email')); } else { Terminus::line("You are not logged in."); } }
public function testGetValue() { //Extant value $session = Session::getValue('session'); $this->assertTrue(strpos($session, '0ffec038-4410-43d0-a404-46997f672d7a') !== false); //Invalid look-up $invalid = Session::getValue('invalid'); $this->assertFalse($invalid); }
/** * Object constructor * * @param [stdClass] $attributes Attributes of this model * @param [array] $options Options to set as $this->key * @return [User] $this */ public function __construct($attributes, $options = array()) { if (!isset($options['id'])) { $options['id'] = Session::getValue('user_uuid'); } parent::__construct($attributes, $options); $this->workflows = new Workflows(array('owner' => $this)); $this->instruments = new Instruments(array('user' => $this)); $this->setProfile(); }
public function __construct($id = null) { if (null === $id) { $this->id = Session::getValue('user_uuid'); } else { $this->id = $id; } $this->getProfile(); self::$instance = $this; return $this; }
/** * 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!"); } }
public function fetch_user_organizations() { $response = Terminus_Command::paged_request('users/' . Session::getValue('user_uuid') . '/memberships/organizations'); $data = array(); foreach ($response['data'] as $membership) { if ($membership->role != 'unprivileged') { $data[$membership->id] = $membership->organization->profile->name; } } return $data; }
public function __construct($id = null) { if (null === $id) { $this->id = Session::getValue('user_uuid'); } else { $this->id = $id; } $this->workflows = new Workflows(array('owner' => $this, 'owner_type' => 'user')); $this->getProfile(); self::$instance = $this; return $this; }
/** * 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()) { if (!isset($options['id'])) { $options['id'] = Session::getValue('user_uuid'); } parent::__construct($attributes, $options); if (isset($attributes->profile)) { $this->profile = $attributes->profile; } $this->workflows = new Workflows(array('owner' => $this)); $this->instruments = new Instruments(array('user' => $this)); $this->organizations = new UserOrganizationMemberships(array('user' => $this)); }
private function hydrate() { $request = Terminus_Command::request('users', Session::getValue('user_uuid'), 'sites', 'GET', array('hydrated' => true)); $sites = $request['data']; foreach ($sites as $site_id => $site_data) { // we need to skip sites that are in the build process still if (!isset($site_data->information)) { continue; } $site_data->id = $site_id; $this->sites[$site_data->information->name] = $site_data; } return $this; }
/** * Make a request to the Dashbord's internal API. * * @param $realm * Permissions realm for data request: currently "user" or "site" but in the * future this could also be "organization" or another high-level business * object (e.g. "product" for managing your app). Can also be "public" to * simply pull read-only data that is not privileged. * * @param $uuid * The UUID of the item in the realm you want to access. * * @param $method * HTTP method (verb) to use. * * @param $data * A native PHP data structure (int, string, arary or simple object) to be * sent along with the request. Will be encoded as JSON for you. */ public static function request($realm, $uuid, $path = FALSE, $method = 'GET', $options = NULL) { if (!in_array($realm, array('login', 'user', 'public')) and !Terminus::is_test()) { Auth::loggedIn(); } try { $cache = Terminus::get_cache(); // combine session realm uuid and path to get a unique key // @todo need cache "groups" $cachekey = md5(Session::getValue('user_uuid') . $uuid . $realm . $path); $data = $cache->get_data($cachekey); // check the request cache if ("GET" == $method and !Terminus::get_config('nocache') and !getenv('CLI_TEST_MODE') and !empty($data)) { if (Terminus::get_config('debug')) { Logger::debug('CacheKey: ' . $cachekey); } return (array) $data; } // for some methods we'll assume the cache should be invalidated if (in_array($method, array("POST", "PUT", "DELETE"))) { $cache->flush(null, 'session'); } if (!in_array($realm, array('login', 'user'))) { $options['cookies'] = array('X-Pantheon-Session' => Session::getValue('session')); $options['verify'] = false; } $url = Endpoint::get(array('realm' => $realm, 'uuid' => $uuid, 'path' => $path)); if (Terminus::get_config('debug')) { Logger::debug('Request URL: ' . $url); } $resp = Request::send($url, $method, $options); $json = $resp->getBody(TRUE); $data = array('info' => $resp->getInfo(), 'headers' => $resp->getRawHeaders(), 'json' => $json, 'data' => json_decode($json), 'status_code' => $resp->getStatusCode()); $cache->put_data($cachekey, $data); return $data; } catch (Guzzle\Http\Exception\BadResponseException $e) { $response = $e->getResponse(); \Terminus::error("%s", $response->getBody(TRUE)); } catch (Guzzle\Http\Exception\HttpException $e) { $request = $e->getRequest(); //die(Terminus_Command::stripSensitiveData()); $sanitized_request = Terminus_Command::stripSensitiveData((string) $request, Terminus_Command::$_blacklist); \Terminus::error("Request %s had failed: %s", array($sanitized_request, $e->getMessage())); } catch (Exception $e) { \Terminus::error("Unrecognised request failure: %s", $e->getMessage()); } }
/** * Adds site with given site ID to cache * * @param [string] $site_id UUID of site to add to cache * @return [void] */ public function addSiteToCache($site_id) { if (count($this->models) == 0) { $this->rebuildCache(); } else { $site = new Site($this->objectify(array('id' => $site_id)), array('collection' => $this)); $site->fetch(); $cache_membership = $site->info(); if ($org_id) { $org = new Organization(null, array('id' => $org_id)); $cache_membership['membership'] = array('id' => $org_id, 'name' => $org->profile->name, 'type' => 'organization'); } else { $user_id = Session::getValue('user_uuid'); $cache_membership['membership'] = array('id' => $user_id, 'name' => 'Team', 'type' => 'team'); } $this->sites_cache->add($cache_membership); } }
/** * 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.'); } }
/** * Simplified request method for Pantheon API * * @param [string] $path API path (URL) * @param [array] $options Options for the request * [string] method GET is default * [mixed] data Native PHP data structure (e.g. int, string array, or * simple object) to be sent along with the request. Will be encoded as * JSON for you. * @return [array] $data */ public static function simple_request($path, $options = array()) { $req_options = array(); $method = 'get'; if (isset($options['method'])) { $method = $options['method']; } if (isset($options['data'])) { $req_options['body'] = json_encode($options['data']); $req_options['headers'] = array('Content-type' => 'application/json'); } $url = 'https://' . TERMINUS_HOST . '/api/' . $path; if (Session::getValue('session')) { $req_options['cookies'] = array('X-Pantheon-Session' => Session::getValue('session')); $req_options['verify'] = false; } try { $resp = Request::send($url, $method, $req_options); } catch (Guzzle\Http\Exception\BadResponseException $e) { \Terminus::error('Request Failure: %s', $e->getMessage()); return; } $json = $resp->getBody(true); $data = array('info' => $resp->getInfo(), 'headers' => $resp->getRawHeaders(), 'json' => $json, 'data' => json_decode($json), 'status_code' => $resp->getStatusCode()); return $data; }
/** * Returns a user with the current session user id * * @return [user] $session user */ public static function getUser() { $user_uuid = Session::getValue('user_uuid'); $user = new User((object) array('id' => $user_uuid)); return $user; }
/** * Find out what user you are logged in as. */ public function whoami() { if (Session::getValue('email')) { $this->output()->outputValue(Session::getValue('email'), "You are authenticated as"); } else { $this->log()->warning("You are not logged in."); } }
/** * Fetches organizational memberships for user * * @return [array] $data Properties below: * [string] id UUID of membership join * [string] name Name of organization * [string] type Always "organization" */ private function fetchUserOrganizations() { $response = $this->request->pagedRequest('users/' . Session::getValue('user_uuid') . '/memberships/organizations'); $data = array(); foreach ($response['data'] as $membership) { if ($membership->role == 'unprivileged') { // Users with unprivileged role in organizations can't see organization // sites, but must be added to the team continue; } $data[] = array('id' => $membership->id, 'name' => $membership->organization->profile->name, 'type' => 'organization'); } return $data; }
public function teamAddMember($email) { $method = 'POST'; $path = sprintf('team/%s', urlencode($email)); $data = array('invited_by' => Session::getValue('user_uuid')); $options = array('body' => json_encode($data), 'headers' => array('Content-type' => 'application/json')); $response = \Terminus_Command::request('sites', $this->getId(), $path, $method, $options); return $response['data']; }
/** * Simplified request method for Pantheon API * * @param [string] $path API path (URL) * @param [array] $arg_options Options for the request * [string] method GET is default * [mixed] data Native PHP data structure (e.g. int, string * array, or simple object) to be sent along with the request. Will * be encoded as JSON for you. * [boolean] absolute_url True if URL passed is to be treated as absolute * @return [array] $data */ public function simpleRequest($path, $arg_options = array()) { $default_options = array('method' => 'get', 'absolute_url' => false); $options = array_merge($default_options, $arg_options); $url = $path; if (strpos($path, 'http') !== 0 && !$options['absolute_url']) { $url = sprintf('%s://%s:%s/api/%s', TERMINUS_PROTOCOL, TERMINUS_HOST, TERMINUS_PORT, $path); } if (Session::getValue('session')) { $options['cookies'] = array('X-Pantheon-Session' => Session::getValue('session')); } try { Terminus::getLogger()->debug('URL: {url}', compact('url')); $response = $this->send($url, $options['method'], $options); } catch (GuzzleHttp\Exception\BadResponseException $e) { throw new TerminusException('API Request Error: {msg}', array('msg' => $e->getMessage())); } $data = array('data' => json_decode($response->getBody()->getContents()), 'headers' => $response->getHeaders(), 'status_code' => $response->getStatusCode()); return $data; }
/** * OnServer Dev Handler * * @param $value string optional -- git or sftp, connection mode to set * @param $commit string optional -- should be the commit message to use if * committing on server changes */ public function onServerDev($value = null, $commit = null) { $path = sprintf("environments/%s/on-server-development", $this->name); if ($commit) { $path = sprintf("%s/commit", $path); $data = $commit ? array('message' => $commit, 'user' => Session::getValue('user_uuid')) : NULL; $options = array('body' => json_encode($data), 'headers' => array('Content-type' => 'application/json')); $data = \Terminus_Command::request('sites', $this->site->getId(), $path, 'POST', $options); } else { if (null == $value) { $data = \Terminus_Command::request('sites', $this->site->getId(), $path, 'GET'); } else { $enabled = $value == 'sftp' ? true : false; $data = array('enabled' => $enabled); $options = array('body' => json_encode($data), 'headers' => array('Content-type' => 'application/json')); $data = \Terminus_Command::request('sites', $this->site->getId(), $path, 'PUT', $options); } } if (empty($data)) { return false; } return $data['data']; }
/** * Simplified request method for Pantheon API * * @param [string] $path API path (URL) * @param [array] $options Options for the request * [string] method GET is default * [mixed] data Native PHP data structure (e.g. int, string array, or * simple object) to be sent along with the request. Will be encoded as * JSON for you. * @return [array] $data */ public static function simpleRequest($path, $options = array()) { $method = 'get'; if (isset($options['method'])) { $method = $options['method']; unset($options['method']); } $url = sprintf('%s://%s:%s/api/%s', TERMINUS_PROTOCOL, TERMINUS_HOST, TERMINUS_PORT, $path); if (Session::getValue('session')) { $options['cookies'] = array('X-Pantheon-Session' => Session::getValue('session')); } try { Terminus::getLogger()->debug('URL: {url}', compact('url')); $resp = Request::send($url, $method, $options); } catch (Guzzle\Http\Exception\BadResponseException $e) { throw new TerminusException('API Request Error: {msg}', array('msg' => $e->getMessage())); } $json = $resp->getBody(true); $data = array('info' => $resp->getInfo(), 'headers' => $resp->getRawHeaders(), 'json' => $json, 'data' => json_decode($json), 'status_code' => $resp->getStatusCode()); return $data; }
/** * Create a new site * * ## OPTIONS * * [--site=<site>] * : Name of the site to create (machine-readable) * * [--name=<name>] * : (deprecated) use --site instead * * [--label=<label>] * : Label for the site * * [--product=<productid>] * : Specify the upstream product to use * * [--import=<url>] * : A url to import a valid archive * * [--org=<org>] * : UUID of organization into which to add this site * */ public function create($args, $assoc_args) { $options = array(); $options['label'] = Input::string($assoc_args, 'label', "Human readable label for the site"); $suggested_name = Utils\sanitize_name($options['label']); if (array_key_exists('name', $assoc_args)) { // Deprecated but kept for backwards compatibility $options['name'] = $assoc_args['name']; } elseif (array_key_exists('site', $assoc_args)) { $options['name'] = $assoc_args['site']; } else { $options['name'] = Input::string($assoc_args, 'site', "Machine name of the site; used as part of the default URL (if left blank will be {$suggested_name})", $suggested_name); } if ($org_id = Input::orgid($assoc_args, 'org', false)) { $options['organization_id'] = $org_id; } if (!isset($assoc_args['import'])) { $product = Input::product($assoc_args, 'product'); $options['product_id'] = $product['id']; Terminus::line(sprintf("Creating new %s installation ... ", $product['longname'])); } $workflow = Site::create($options); $workflow->wait(); Terminus::success("Pow! You created a new site!"); // Add Site to SitesCache $site_id = $workflow->attributes->final_task->site_id; $site = new Site($site_id); $site->fetch(); $cache_membership = array('id' => $site_id, 'name' => $options['name'], 'service_level' => $site->attributes->service_level, 'framework' => $site->attributes->framework); if ($org_id) { $org = new Organization($org_id); $cache_membership['membership'] = array('id' => $org_id, 'name' => $org->profile->name, 'type' => 'organization'); } else { $user_id = Session::getValue('user_uuid'); $cache_membership['membership'] = array('id' => $user_id, 'name' => 'Team', 'type' => 'team'); } $sites_cache = new Terminus\SitesCache(); $sites_cache->add($cache_membership); if (isset($assoc_args['import'])) { sleep(10); //To stop erroenous site-DNE errors Terminus::launch_self('site', array('import'), array('url' => $assoc_args['import'], 'site' => $options['name'], 'element' => 'all')); } else { Terminus::launch_self('site', array('info'), array('site' => $options['name'])); } return true; }