comparison core/modules/toolbar/src/Controller/ToolbarController.php @ 0:4c8ae668cc8c

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