Commit 2db96638 authored by catch's avatar catch

Issue #2062151 by dawehner: Create a current user service to ensure that...

Issue #2062151 by dawehner: Create a current user service to ensure that current account is always available.
parent 8c231383
......@@ -603,6 +603,12 @@ services:
tags:
- { name: event_subscriber }
arguments: ['@authentication']
current_user:
class: Drupal\Core\Session\AccountInterface
factory_method: authenticate
factory_service: authentication
arguments: ['@request']
scope: request
asset.css.collection_renderer:
class: Drupal\Core\Asset\CssCollectionRenderer
asset.css.collection_optimizer:
......
......@@ -147,6 +147,15 @@ public static function request() {
return static::$container->get('request');
}
/**
* Gets the current active user.
*
* @return \Drupal\Core\Session\AccountInterface
*/
public static function currentUser() {
return static::$container->get('current_user');
}
/**
* Retrieves the entity manager service.
*
......
......@@ -121,6 +121,16 @@ protected function urlGenerator() {
return $this->container->get('url_generator');
}
/**
* Returns the current user.
*
* @return \Drupal\Core\Session\AccountInterface
* The current user.
*/
protected function currentUser() {
return $this->container->get('current_user');
}
/**
* Translates a string to the current language or to a given language using
* the string translation service.
......
......@@ -286,7 +286,7 @@ function _block_get_renderable_region($list = array()) {
// the regular 'roles define permissions' schema, it brings too many
// chances of having unwanted output get in the cache and later be served
// to other users. We therefore exclude user 1 from block caching.
$not_cacheable = $GLOBALS['user']->id() == 1 ||
$not_cacheable = Drupal::currentUser()->id() == 1 ||
count(Drupal::moduleHandler()->getImplementations('node_grants')) ||
!\Drupal::request()->isMethodSafe();
......
......@@ -69,14 +69,13 @@ protected function checkAccess(EntityInterface $entity, $operation, $langcode, A
}
// Otherwise, check for other access restrictions.
global $user;
// User role access handling.
// If a block has no roles associated, it is displayed for every role.
// For blocks with roles associated, if none of the user's roles matches
// the settings from this block, access is denied.
$visibility = $entity->get('visibility');
if (!empty($visibility['role']['roles']) && !array_intersect(array_filter($visibility['role']['roles']), $user->getRoles())) {
if (!empty($visibility['role']['roles']) && !array_intersect(array_filter($visibility['role']['roles']), $account->getRoles())) {
// No match.
return FALSE;
}
......
......@@ -161,6 +161,24 @@ public function testUrlGeneratorFront() {
$this->assertEqual($this->container->get('url_generator')->generateFromPath('<front>'), $base_path);
}
/**
* Tests the user account on the DIC.
*/
public function testUserAccount() {
$account = $this->drupalCreateUser();
$this->drupalLogin($account);
$second_account = $this->drupalCreateUser();
$this->drupalGet('router_test/test12/' . $second_account->id());
$this->assertText($account->getUsername() . ':' . $second_account->getUsername());
$this->assertEqual($account->id(), $this->loggedInUser->id(), 'Ensure that the user was not changed.');
$this->drupalGet('router_test/test13/' . $second_account->id());
$this->assertText($account->getUsername() . ':' . $second_account->getUsername());
$this->assertEqual($account->id(), $this->loggedInUser->id(), 'Ensure that the user was not changed.');
}
/**
* Checks that an ajax request gets rendered as an Ajax response, by mime.
*
......
......@@ -7,10 +7,39 @@
namespace Drupal\router_test;
use Drupal\Core\Controller\ControllerInterface;
use Drupal\user\UserInterface;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
/**
* Test controllers that are intended to be wrapped in a main controller.
*/
class TestContent {
class TestContent extends ContainerAware implements ControllerInterface {
/**
* The HTTP kernel.
*
* @var \Symfony\Component\HttpKernel\HttpKernelInterface
*/
protected $httpKernel;
/**
* Constructs a TestContent instance.
*/
public function __construct(HttpKernelInterface $http_kernel) {
$this->httpKernel = $http_kernel;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container->get('http_kernel'));
}
/**
* Provides example content for testing route enhancers.
......@@ -30,4 +59,20 @@ public function test11() {
return $account->getUsername();
}
public function testAccount(UserInterface $user) {
$current_user = \Drupal::currentUser();
$this->container->set('current_user', $user);
return $current_user->getUsername() . ':' . $user->getUsername();
}
/**
* Uses a subrequest to determine the content.
*/
public function subrequestTest(UserInterface $user) {
$request = \Drupal::request();
$request = Request::create('/router_test/test13/' . $user->id(), 'GET', $request->query->all(), $request->cookies->all(), array(), $request->server->all());
return $this->httpKernel->handle($request, HttpKernelInterface::SUB_REQUEST);
}
}
......@@ -69,3 +69,18 @@ router_test_11:
_user_is_logged_in: 'TRUE'
defaults:
_content: '\Drupal\router_test\TestContent::test11'
router_test_12:
pattern: '/router_test/test12/{user}'
requirements:
_access: 'TRUE'
defaults:
_content: '\Drupal\router_test\TestContent::subrequestTest'
router_test_13:
pattern: '/router_test/test13/{user}'
requirements:
_access: 'TRUE'
defaults:
_content: '\Drupal\router_test\TestContent::testAccount'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment