annotate core/modules/toolbar/src/Controller/ToolbarController.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\toolbar\Controller;
Chris@0 4
Chris@0 5 use Drupal\Component\Utility\Crypt;
Chris@0 6 use Drupal\Core\Access\AccessResult;
Chris@0 7 use Drupal\Core\Ajax\AjaxResponse;
Chris@0 8 use Drupal\Core\Controller\ControllerBase;
Chris@0 9 use Drupal\toolbar\Ajax\SetSubtreesCommand;
Chris@0 10
Chris@0 11 /**
Chris@0 12 * Defines a controller for the toolbar module.
Chris@0 13 */
Chris@0 14 class ToolbarController extends ControllerBase {
Chris@0 15
Chris@0 16 /**
Chris@0 17 * Returns an AJAX response to render the toolbar subtrees.
Chris@0 18 *
Chris@0 19 * @return \Drupal\Core\Ajax\AjaxResponse
Chris@0 20 */
Chris@0 21 public function subtreesAjax() {
Chris@0 22 list($subtrees, $cacheability) = toolbar_get_rendered_subtrees();
Chris@0 23 $response = new AjaxResponse();
Chris@0 24 $response->addCommand(new SetSubtreesCommand($subtrees));
Chris@0 25
Chris@0 26 // The Expires HTTP header is the heart of the client-side HTTP caching. The
Chris@0 27 // additional server-side page cache only takes effect when the client
Chris@0 28 // accesses the callback URL again (e.g., after clearing the browser cache
Chris@0 29 // or when force-reloading a Drupal page).
Chris@0 30 $max_age = 365 * 24 * 60 * 60;
Chris@0 31 $response->setPrivate();
Chris@0 32 $response->setMaxAge($max_age);
Chris@0 33
Chris@0 34 $expires = new \DateTime();
Chris@0 35 $expires->setTimestamp(REQUEST_TIME + $max_age);
Chris@0 36 $response->setExpires($expires);
Chris@0 37
Chris@0 38 return $response;
Chris@0 39 }
Chris@0 40
Chris@0 41 /**
Chris@0 42 * Checks access for the subtree controller.
Chris@0 43 *
Chris@0 44 * @param string $hash
Chris@0 45 * The hash of the toolbar subtrees.
Chris@0 46 *
Chris@0 47 * @return \Drupal\Core\Access\AccessResultInterface
Chris@0 48 * The access result.
Chris@0 49 */
Chris@0 50 public function checkSubTreeAccess($hash) {
Chris@0 51 $expected_hash = _toolbar_get_subtrees_hash()[0];
Chris@0 52 return AccessResult::allowedIf($this->currentUser()->hasPermission('access toolbar') && Crypt::hashEquals($expected_hash, $hash))->cachePerPermissions();
Chris@0 53 }
Chris@0 54
Chris@0 55 }