diff --git a/core/lib/Drupal/Core/Routing/RouteMatch.php b/core/lib/Drupal/Core/Routing/RouteMatch.php index a5e5203..0a66610 100644 --- a/core/lib/Drupal/Core/Routing/RouteMatch.php +++ b/core/lib/Drupal/Core/Routing/RouteMatch.php @@ -66,13 +66,10 @@ class RouteMatch implements RouteMatchInterface { * The parameters array. * @param array $raw_parameters * The raw $parameters array. - * @param \Symfony\Component\HttpFoundation\Request $request - * A request object. */ - public function __construct($route_name, Route $route, array $parameters = array(), array $raw_parameters = array(), Request $request = NULL) { + public function __construct($route_name, Route $route, array $parameters = array(), array $raw_parameters = array()) { $this->routeName = $route_name; $this->route = $route; - $this->request = $request; // Pre-filter parameters. $route_params = $this->getParameterNames(); @@ -103,8 +100,7 @@ public static function createFromRequest(Request $request) { $request->attributes->get(RouteObjectInterface::ROUTE_NAME), $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT), $request->attributes->all(), - $raw_variables, - $request); + $raw_variables); } else { return new NullRouteMatch(); @@ -157,22 +153,24 @@ public function getRawParameters() { * {@inheritdoc} */ public function createUrlObject(array $options = array()) { - return new Url($this->getRouteName(), $this->getRawParameters(), $options); + return new Url($this->getRouteName(), $this->getRawParameters()->all(), $options); } /** * {@inheritdoc} */ public function access(AccountInterface $account) { - $this->getAccessManager()->checkNamedRoute($this->getRouteName(), $this->getRawParameters(), $account, $this->request); + return $this->getAccessManager()->checkNamedRoute($this->getRouteName(), $this->getRawParameters()->all(), $account); } /** + * Gets the access manager. + * * @return \Drupal\Core\Access\AccessManagerInterface */ protected function getAccessManager() { if (!isset($this->accessManager)) { - $this->accessManager = \Drupal::service('access.manager'); + $this->accessManager = \Drupal::service('access_manager'); } return $this->accessManager; } diff --git a/core/lib/Drupal/Core/Url.php b/core/lib/Drupal/Core/Url.php index a148089..c8ba106 100644 --- a/core/lib/Drupal/Core/Url.php +++ b/core/lib/Drupal/Core/Url.php @@ -429,7 +429,7 @@ public function getInternalPath() { * Returns TRUE if the user has access to the url, otherwise FALSE. */ public function access(AccountInterface $account) { - $this->getAccessManager()->checkNamedRoute($this->getRouteName(), $this->getRouteParameters(), $account); + return $this->getAccessManager()->checkNamedRoute($this->getRouteName(), $this->getRouteParameters(), $account); } /** @@ -437,7 +437,7 @@ public function access(AccountInterface $account) { */ protected function getAccessManager() { if (!isset($this->accessManager)) { - $this->accessManager = \Drupal::service('access.manager'); + $this->accessManager = \Drupal::service('access_manager'); } return $this->accessManager; } diff --git a/core/modules/system/src/Tests/Routing/UrlIntegrationTest.php b/core/modules/system/src/Tests/Routing/UrlIntegrationTest.php index 113b7cf..66239c9 100644 --- a/core/modules/system/src/Tests/Routing/UrlIntegrationTest.php +++ b/core/modules/system/src/Tests/Routing/UrlIntegrationTest.php @@ -7,7 +7,62 @@ namespace Drupal\system\Tests\Routing; -class UrlIntegrationTest { +use Drupal\Core\Url; +use Drupal\simpletest\KernelTestBase; +use Drupal\user\Entity\Role; +use Drupal\user\Entity\User; -} +/** + * Tests the URL object integration into the access system. + * + * @group Url + */ +class UrlIntegrationTest extends KernelTestBase { + + /** + * Modules to enable. + * + * @var array + */ + public static $modules = array('user', 'router_test', 'system'); + + /** + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); + + $this->installSchema('system', ['router']); + } + + /** + * Ensures that the access() method on \Drupal\Core\Url objects works. + */ + public function testAccess() { + \Drupal::service('router.builder')->rebuild(); + /** @var \Drupal\user\RoleInterface $role_with_access */ + $role_with_access = Role::create(['id' => 'role_with_access']); + $role_with_access->grantPermission('administer users'); + $role_with_access->save(); + /** @var \Drupal\user\RoleInterface $role_without_access */ + $role_without_access = Role::create(['id' => 'role_without_access']); + $role_without_access->save(); + + $user_with_access = User::create(['roles' => ['role_with_access']]); + $user_without_access = User::create(['roles' => ['role_without_access']]); + + $url_always_access = new Url('router_test.1'); + $this->assertTrue($url_always_access->access($user_with_access)); + $this->assertTrue($url_always_access->access($user_without_access)); + + $url_none_access = new Url('router_test.15'); + $this->assertFalse($url_none_access->access($user_with_access)); + $this->assertFalse($url_none_access->access($user_without_access)); + + $url_access = new Url('router_test.16'); + $this->assertTrue($url_access->access($user_with_access)); + $this->assertFalse($url_access->access($user_without_access)); + } + +} diff --git a/core/modules/system/tests/modules/router_test_directory/router_test.routing.yml b/core/modules/system/tests/modules/router_test_directory/router_test.routing.yml index 7405d88..5b02a3b 100644 --- a/core/modules/system/tests/modules/router_test_directory/router_test.routing.yml +++ b/core/modules/system/tests/modules/router_test_directory/router_test.routing.yml @@ -93,6 +93,20 @@ router_test.14: defaults: _controller: '\Drupal\router_test\TestControllers::test9' +router_test.15: + path: '/router_test/test15' + defaults: + _controller: '\Drupal\router_test\TestControllers::test1' + requirements: + _access: 'FALSE' + +router_test.16: + path: '/router_test/test16' + defaults: + _controller: '\Drupal\router_test\TestControllers::test1' + requirements: + _permission: 'administer users' + router_test.hierarchy_parent: path: '/menu-test/parent' defaults: diff --git a/core/modules/user/user.module b/core/modules/user/user.module index 8516792..92bc995 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -1101,6 +1101,10 @@ function user_user_role_insert(RoleInterface $role) { } $add_id = 'user_add_role_action.' . $role->id(); + + if (!\Drupal::entityManager()->hasDefinition('action')) { + return; + } if (!entity_load('action', $add_id)) { $action = entity_create('action', array( 'id' => $add_id, diff --git a/core/tests/Drupal/Tests/Core/Routing/RouteMatchTest.php b/core/tests/Drupal/Tests/Core/Routing/RouteMatchTest.php index 6aab6f4..6289409 100644 --- a/core/tests/Drupal/Tests/Core/Routing/RouteMatchTest.php +++ b/core/tests/Drupal/Tests/Core/Routing/RouteMatchTest.php @@ -65,4 +65,42 @@ public function testRouteMatchFromRequest() { $this->assertSame(array('foo' => '1'), $route_match->getRawParameters()->all()); } + /** + * Tests the access() method. + * + * @covers ::access + * @covers ::getAccessManager + * @covers ::setAccessManager + */ + public function testAccess() { + foreach ([TRUE, FALSE] as $access) { + $account = $this->getMock('Drupal\Core\Session\AccountInterface'); + + $access_manager = $this->getMock('Drupal\Core\Access\AccessManagerInterface'); + $access_manager->expects($this->once()) + ->method('checkNamedRoute') + ->with('entity.node.canonical', ['node' => 3], $account) + ->willReturn($access); + + $route_match = new RouteMatch('entity.node.canonical', new Route('node/{node}'), ['node' => (object) ['nid' => 3]], ['node' => 3]); + $route_match->setAccessManager($access_manager); + + $this->assertEquals($access, $route_match->access($account)); + } + } + + /** + * Tests the createUrlObject() method. + * + * @covers ::createUrlObject + */ + public function testCreateUrlObject() { + $route_match = new RouteMatch('entity.node.canonical', new Route('node/{node}'), ['node' => (object) ['nid' => 3]], ['node' => 3]); + + $url = $route_match->createUrlObject(['foo' => 'bar']); + $this->assertEquals('entity.node.canonical', $url->getRouteName()); + $this->assertEquals(['node' => 3], $url->getRouteParameters());; + $this->assertEquals(['foo' => 'bar'], $url->getOptions()); + } + } diff --git a/core/tests/Drupal/Tests/Core/UrlTest.php b/core/tests/Drupal/Tests/Core/UrlTest.php index 324f9e9..87d1c7a 100644 --- a/core/tests/Drupal/Tests/Core/UrlTest.php +++ b/core/tests/Drupal/Tests/Core/UrlTest.php @@ -319,4 +319,28 @@ public function testGetOptions($urls) { } } + /** + * Tests the access() method. + * + * @covers ::access + * @covers ::getAccessManager + * @covers ::setAccessManager + */ + public function testAccess() { + foreach ([FALSE, TRUE] as $access) { + $account = $this->getMock('Drupal\Core\Session\AccountInterface'); + + $access_manager = $this->getMock('Drupal\Core\Access\AccessManagerInterface'); + $access_manager->expects($this->once()) + ->method('checkNamedRoute') + ->with('entity.node.canonical', ['node' => 3], $account) + ->willReturn($access); + + $url = new Url('entity.node.canonical', ['node' => 3]); + $url->setAccessManager($access_manager); + + $this->assertEquals($access, $url->access($account)); + } + } + }