/**
  * {@inheritdoc}
  */
 public function log($level, $message, array $context = array())
 {
     // Make sure that even when cron failures prevent hook_cron() from being
     // called, we still send out a heartbeat.
     if (!empty($context['channel']) && $context['channel'] == 'cron' && $message == 'Attempting to re-run cron while it is already running.') {
         Subscription::update();
     }
 }
 /**
  * Menu callback for 'admin/config/system/acquia-agent/refresh-status'.
  */
 public function refresh()
 {
     // Refresh subscription information, so we are sure about our update status.
     // We send a heartbeat here so that all of our status information gets
     // updated locally via the return data.
     Subscription::update();
     // Return to the setting pages (or destination).
     return $this->redirect('system.status');
 }
 /**
  * Access callback for sendModuleData() callback.
  */
 public function access()
 {
     $request = \Drupal::request();
     $data = json_decode($request->getContent(), TRUE);
     // We only do this if we are on SSL
     $via_ssl = $request->isSecure();
     if ($this->config('acquia_connector.settings')->get('spi.ssl_override')) {
         $via_ssl = TRUE;
     }
     if ($this->config('acquia_connector.settings')->get('spi.module_diff_data') && $via_ssl) {
         if (Subscription::hasCredentials() && isset($data['body']['file']) && $this->isValidRequest($data, $data['body']['file'])) {
             return AccessResultAllowed::allowed();
         }
         // Log the request if validation failed and debug is enabled.
         if ($this->config('acquia_connector.settings')->get('debug')) {
             $info = array('data' => $data, 'get' => $request->query->all(), 'server' => $request->server->all(), 'request' => $request->request->all());
             \Drupal::logger('acquia module data')->notice('Site Module Data request: @data', array('@data' => var_export($info, TRUE)));
         }
     }
     return AccessResultForbidden::forbidden();
 }
Esempio n. 4
0
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $storage = $form_state->getStorage();
     if (isset($storage['choose']) && isset($storage['response']['subscription'][$form_state->getValue('subscription')])) {
         $config = $this->config('acquia_connector.settings');
         $sub = $storage['response']['subscription'][$form_state->getValue('subscription')];
         $config->set('key', $sub['key'])->set('identifier', $sub['identifier'])->set('subscription_name', $sub['name'])->save();
     } else {
         $this->automaticStartSubmit($form_state);
     }
     // Don't set message or redirect if multistep.
     if (!$form_state->getErrors($form_state) && empty($storage['rebuild'])) {
         // Check subscription and send a heartbeat to Acquia Network via XML-RPC.
         // Our status gets updated locally via the return data.
         $subscription = Subscription::update();
         // Redirect to the path without the suffix.
         if ($subscription) {
             $form_state->setRedirect('acquia_connector.settings');
         }
         if ($subscription['active']) {
             drupal_set_message($this->t('<h3>Connection successful!</h3>You are now connected to the Acquia Subscription.'));
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $config = $this->config('acquia_connector.settings');
     $config->set('key', $form_state->getValue('acquia_key'))->set('identifier', $form_state->getValue('acquia_identifier'))->set('subscription_name', $form_state->getValue('subscription'))->save();
     // Check subscription and send a heartbeat to Acquia Network via XML-RPC.
     // Our status gets updated locally via the return data.
     $subscription = Subscription::update();
     // Redirect to the path without the suffix.
     $form_state->setRedirect('acquia_connector.settings');
     drupal_flush_all_caches();
     if ($subscription['active']) {
         drupal_set_message($this->t('<h3>Connection successful!</h3>You are now connected to the Acquia Subscription.'));
     }
 }
 /**
  * Test Connector subscription methods.
  */
 public function testAcquiaConnectorSubscription()
 {
     // Starts as inactive.
     $is_active = Subscription::isActive();
     $this->assertFalse($is_active, 'Subscription is not currently active.');
     // Confirm HTTP request count is 0 because without credentials no request
     // should have been made.
     $this->assertIdentical(\Drupal::state()->get('acquia_connector_test_request_count', 0), 0);
     $check_subscription = Subscription::update();
     \Drupal::state()->resetCache();
     $this->assertFalse($check_subscription, 'Subscription is currently false.');
     // Confirm HTTP request count is still 0.
     $this->assertIdentical(\Drupal::state()->get('acquia_connector_test_request_count', 0), 0);
     // Fail a connection.
     $random_id = $this->randomString();
     $edit_fields = array('acquia_identifier' => $random_id, 'acquia_key' => $this->randomString());
     $submit_button = 'Connect';
     $this->drupalPostForm($this->credentials_path, $edit_fields, $submit_button);
     // Confirm HTTP request count is 1.
     $this->assertIdentical(\Drupal::state()->get('acquia_connector_test_request_count', 0), 1, 'Made 1 HTTP request in attempt to connect subscription.');
     $is_active = Subscription::isActive();
     $this->assertFalse($is_active, 'Subscription is not active after failed attempt to connect.');
     $this->assertIdentical(\Drupal::state()->get('acquia_connector_test_request_count', 0), 1, 'Still have made only 1 HTTP request');
     $check_subscription = Subscription::update();
     \Drupal::state()->resetCache();
     $this->assertFalse($check_subscription, 'Subscription is false after failed attempt to connect.');
     $this->assertIdentical(\Drupal::state()->get('acquia_connector_test_request_count', 0), 1, 'Still have made only 1 HTTP request');
     // Test default from acquia_agent_settings().
     $stored = \Drupal::config('acquia_connector.settings');
     $current_subscription = $stored->get('subscription_data');
     // Not identical since acquia_agent_has_credentials() causes stored to be
     // deleted.
     $this->assertNotIdentical($check_subscription, $current_subscription, 'Stored subscription data not same before connected subscription.');
     $this->assertTrue($current_subscription['active'] === FALSE, 'Default is inactive.');
     // Reset HTTP request counter;
     \Drupal::state()->set('acquia_connector_test_request_count', 0);
     // Connect.
     $edit_fields = array('acquia_identifier' => $this->acqtest_id, 'acquia_key' => $this->acqtest_key);
     $this->drupalPostForm($this->credentials_path, $edit_fields, $submit_button);
     // HTTP requests should now be 3 (acquia.agent.subscription.name and
     //acquia.agent.subscription and acquia.agent.validate.
     $this->assertIdentical(\Drupal::state()->get('acquia_connector_test_request_count', 0), 3, '3 HTTP requests were made during first connection.');
     $is_active = Subscription::isActive();
     $this->assertTrue($is_active, 'Subscription is active after successful connection.');
     $check_subscription = Subscription::update();
     \Drupal::state()->resetCache();
     $this->assertTrue(is_array($check_subscription), 'Subscription is array after successful connection.');
     // Now stored subscription data should match.
     $stored = \Drupal::config('acquia_connector.settings');
     $this->assertIdentical(\Drupal::state()->get('acquia_connector_test_request_count', 0), 4, '1 additional HTTP request made via acquia_agent_check_subscription().');
     $this->drupalGet($this->base_url);
     $this->drupalGet('admin');
     $this->assertIdentical(\Drupal::state()->get('acquia_connector_test_request_count', 0), 4, 'No extra requests made during visits to other pages.');
     // Reset HTTP request counter;
     \Drupal::state()->set('acquia_connector_test_request_count', 0);
     // Connect on expired subscription.
     $edit_fields = array('acquia_identifier' => $this->acqtest_expired_id, 'acquia_key' => $this->acqtest_expired_key);
     $this->drupalPostForm($this->credentials_path, $edit_fields, $submit_button);
     $this->assertIdentical(\Drupal::state()->get('acquia_connector_test_request_count', 0), 3, '3 HTTP requests were made during expired connection attempt.');
     $is_active = Subscription::isActive();
     $this->assertFalse($is_active, 'Subscription is not active after connection with expired subscription.');
     $this->assertIdentical(\Drupal::state()->get('acquia_connector_test_request_count', 0), 3, 'No additional HTTP requests made via acquia_agent_subscription_is_active().');
     $this->drupalGet($this->base_url);
     $this->drupalGet('admin');
     $this->assertIdentical(\Drupal::state()->get('acquia_connector_test_request_count', 0), 3, 'No HTTP requests made during visits to other pages.');
     // Stored subscription data will now be the expired integer.
     $check_subscription = Subscription::update();
     \Drupal::state()->resetCache();
     $this->assertIdentical($check_subscription, 1200, 'Subscription is expired after connection with expired subscription.');
     $this->assertIdentical(\Drupal::state()->get('acquia_connector_test_request_count', 0), 4, '1 additional request made via acquia_agent_check_subscription().');
     $stored = \Drupal::config('acquia_connector.settings');
     $current_subscription = $stored->get('subscription_data');
     $this->assertIdentical($check_subscription, $current_subscription, 'Stored expected subscription data.');
     // Reset HTTP request counter;
     \Drupal::state()->set('acquia_connector_test_request_count', 0);
     // Connect on subscription that will trigger a 503 response..
     $edit_fields = array('acquia_identifier' => $this->acqtest_503_id, 'acquia_key' => $this->acqtest_503_key);
     $this->drupalPostForm($this->credentials_path, $edit_fields, $submit_button);
     $is_active = Subscription::isActive();
     $this->assertTrue($is_active, 'Subscription is active after successful connection.');
     // Make another request which will trigger 503 server error.
     $check_subscription = Subscription::update();
     \Drupal::state()->resetCache();
     // Hold onto subcription data for comparison.
     $stored = \Drupal::config('acquia_connector.settings');
     $this->assertNotIdentical($check_subscription, '503', 'Subscription is not storing 503.');
     $this->assertTrue(is_array($check_subscription), 'Storing subscription array data.');
     $this->assertIdentical(\Drupal::state()->get('acquia_connector_test_request_count', 0), 4, 'Have made 4 HTTP requests so far.');
 }
 /**
  * Refresh subscription information.
  * @param \Symfony\Component\HttpKernel\Event\FilterControllerEvent $event
  */
 public function onKernelController(FilterControllerEvent $event)
 {
     if ($event->getRequest()->attributes->get('_route') != 'update.manual_status') {
         return;
     }
     $controller = $event->getController();
     /*
      * $controller passed can be either a class or a Closure.
      * This is not usual in Symfony but it may happen.
      * If it is a class, it comes in array format
      */
     if (!is_array($controller)) {
         return;
     }
     if ($controller[0] instanceof \Drupal\update\Controller\UpdateController) {
         // Refresh subscription information, so we are sure about our update status.
         // We send a heartbeat here so that all of our status information gets
         // updated locally via the return data.
         Subscription::update();
     }
 }