/** * {@inheritdoc} */ public function switchTo(AccountInterface $account) { // Prevent session information from being saved and push previous account. if (!isset($this->originalSessionSaving)) { // Ensure that only the first session saving status is saved. $this->originalSessionSaving = $this->writeSafeHandler->isSessionWritable(); } $this->writeSafeHandler->setSessionWritable(FALSE); array_push($this->accountStack, $this->currentUser->getAccount()); $this->currentUser->setAccount($account); return $this; }
/** * {@inheritdoc} */ public function access($storage_type, $id, $op, AccountInterface $account = NULL) { if ($account === NULL) { $account = $this->currentUser->getAccount(); } return $this->getStorage($storage_type)->access($id, $op, $account); }
/** * {@inheritdoc} */ public function run() { // Allow execution to continue even if the request gets cancelled. @ignore_user_abort(TRUE); // Prevent session information from being saved while cron is running. $original_session_saving = $this->sessionManager->isEnabled(); $this->sessionManager->disable(); // Force the current user to anonymous to ensure consistent permissions on // cron runs. $original_user = $this->currentUser->getAccount(); $this->currentUser->setAccount(new AnonymousUserSession()); // Try to allocate enough time to run all the hook_cron implementations. drupal_set_time_limit(240); $return = FALSE; // Try to acquire cron lock. if (!$this->lock->acquire('cron', 900.0)) { // Cron is still running normally. $this->logger->warning('Attempting to re-run cron while it is already running.'); } else { $this->invokeCronHandlers(); $this->setCronLastTime(); // Release cron lock. $this->lock->release('cron'); // Return TRUE so other functions can check if it did run successfully $return = TRUE; } // Process cron queues. $this->processQueues(); // Restore the user. $this->currentUser->setAccount($original_user); if ($original_session_saving) { $this->sessionManager->enable(); } return $return; }
/** * The controller for the meteor.whoami route. * * @return \Symfony\Component\HttpFoundation\Response * A JSON response. */ public function whoami() { $account = $this->accountProxy->getAccount(); $uid = $account->id(); $name = $account->getAccountName(); $display_name = $account->getDisplayName(); $roles = $this->accountProxy->getRoles(); $result = $this->serializer->serialize(['uid' => $uid, 'name' => $name, 'displayName' => $display_name, 'roles' => $roles], 'json'); $response = new Response($result, Response::HTTP_OK); $response->headers->set('Content-type', 'application/json'); return $response; }
/** * {@inheritdoc} */ public function preprocessIndexItems(array &$items) { // Change the current user to our dummy implementation to ensure we are // using the configured roles. $original_user = $this->currentUser->getAccount(); // @todo Why not just use \Drupal\Core\Session\UserSession directly here? $this->currentUser->setAccount(new UserSession(array('roles' => $this->configuration['roles']))); // Count of items that don't have a view mode. $unset_view_modes = 0; // Annoyingly, this doc comment is needed for PHPStorm. See // http://youtrack.jetbrains.com/issue/WI-23586 /** @var \Drupal\search_api\Item\ItemInterface $item */ foreach ($items as $item) { if (!($field = $item->getField('rendered_item'))) { continue; } $datasource_id = $item->getDatasourceId(); $datasource = $item->getDatasource(); $bundle = $datasource->getItemBundle($item->getOriginalObject()); if (empty($this->configuration['view_mode'][$datasource_id][$bundle])) { if (!isset($this->configuration['view_mode'][$datasource_id][$bundle])) { ++$unset_view_modes; } continue; } else { $view_mode = (string) $this->configuration['view_mode'][$datasource_id][$bundle]; } $build = $datasource->viewItem($item->getOriginalObject(), $view_mode); $value = (string) $this->getRenderer()->renderPlain($build); if ($value) { $field->addValue($value); } } if ($unset_view_modes > 0) { $context = array( '%index' => $this->index->label(), '%processor' => $this->label(), '@count' => $unset_view_modes, ); $this->getLogger()->warning('Warning: While indexing items on search index %index, @count item(s) did not have a view mode configured for the %processor processor.', $context); } // Restore the original user. $this->currentUser->setAccount($original_user); }
/** * {@inheritdoc} */ public function preprocessIndexItems(array &$items) { // Change the current user to our dummy implementation to ensure we are // using the configured roles. $original_user = $this->currentUser->getAccount(); // @todo Why not just use \Drupal\Core\Session\UserSession directly here? $this->currentUser->setAccount(new UserSession(array('roles' => $this->configuration['roles']))); // Annoyingly, this doc comment is needed for PHPStorm. See // http://youtrack.jetbrains.com/issue/WI-23586 /** @var \Drupal\search_api\Item\ItemInterface $item */ foreach ($items as $item) { if (empty($this->configuration['view_mode'][$item->getDatasourceId()])) { continue; } if (!($field = $item->getField('rendered_item'))) { continue; } $build = $item->getDatasource()->viewItem($item->getOriginalObject(), $this->configuration['view_mode'][$item->getDatasourceId()]); $field->addValue($this->getRenderer()->renderPlain($build)); } // Restore the original user. $this->currentUser->setAccount($original_user); }