Commit 0af43e4e authored by alexpott's avatar alexpott

Issue #2028585 by ParisLiakos, dawehner, tim.plunkett: Replace...

Issue #2028585 by ParisLiakos, dawehner, tim.plunkett: Replace entity_page_create_access() by an access controller.
parent f36e5232
......@@ -355,6 +355,11 @@ services:
class: Drupal\Core\Entity\EntityAccessCheck
tags:
- { name: access_check }
access_check.entity_create:
class: Drupal\Core\Entity\EntityCreateAccessCheck
arguments: ['@plugin.manager.entity']
tags:
- { name: access_check }
maintenance_mode_subscriber:
class: Drupal\Core\EventSubscriber\MaintenanceModeSubscriber
tags:
......
<?php
/**
* @file
* Contains \Drupal\Core\Entity\EntityCreateAccessCheck.
*/
namespace Drupal\Core\Entity;
use Drupal\Core\Access\AccessCheckInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
/**
* Defines an access checker for entity creation.
*/
class EntityCreateAccessCheck implements AccessCheckInterface {
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManager
*/
protected $entityManager;
/**
* Constructs a EntityCreateAccessCheck object.
*
* @param \Drupal\Core\Entity\EntityManager $entity_manager
* The entity manager.
*/
public function __construct(EntityManager $entity_manager) {
$this->entityManager = $entity_manager;
}
/**
* {@inheritdoc}
*/
public function applies(Route $route) {
return array_key_exists('_entity_create_access', $route->getRequirements());
}
/**
* {@inheritdoc}
*/
public function access(Route $route, Request $request) {
list($entity_type, $bundle) = explode(':', $route->getRequirement('_entity_create_access') . ':');
$definition = $this->entityManager->getDefinition($entity_type);
$values = $this->prepareEntityValues($definition, $request, $bundle);
$entity = $this->entityManager->getStorageController($entity_type)
->create($values);
return $this->entityManager->getAccessController($entity_type)->access($entity, 'create');
}
/**
* Prepare the values passed into the storage controller.
*
* @param array $definition
* The entity type definition.
* @param \Symfony\Component\HttpFoundation\Request $request
* The request object.
* @param string $bundle
* (optional) The bundle to check access for.
*
* @return array
* An array of values to be used when creating the entity.
*/
protected function prepareEntityValues(array $definition, Request $request, $bundle = NULL) {
$values = array();
if ($bundle && isset($definition['entity_keys']['bundle'])) {
$values[$definition['entity_keys']['bundle']] = $bundle;
}
return $values;
}
}
......@@ -255,7 +255,7 @@ public function getRenderController($entity_type) {
* @param string $entity_type
* The entity type for this access controller.
*
* @return \Drupal\Core\Entity\EntityRenderControllerInterface.
* @return \Drupal\Core\Entity\EntityAccessControllerInterface.
* A access controller instance.
*/
public function getAccessController($entity_type) {
......
<?php
/**
* @file
* Contains \Drupal\Tests\Core\Entity\EntityCreateAccessCheckTest.
*/
namespace Drupal\Tests\Core\Entity;
use Drupal\Core\Access\AccessCheckInterface;
use Drupal\Core\Entity\EntityCreateAccessCheck;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
/**
* Tests the entity-create access controller.
*
* @group Entity
*
* @see \Drupal\Core\Entity\EntityCreateAccessCheck
*/
class EntityCreateAccessCheckTest extends UnitTestCase {
/**
* The mocked entity manager.
*
* @var \PHPUnit_Framework_MockObject_MockObject
*/
public $entityManager;
public static function getInfo() {
return array(
'name' => 'Entity create access check test',
'description' => 'Unit test of entity create access checking system.',
'group' => 'Entity'
);
}
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
}
/**
* Tests the method for checking if the access check applies to a route.
*/
public function testApplies() {
$entity_manager = $this->getMockBuilder('Drupal\Core\Entity\EntityManager')
->disableOriginalConstructor()
->getMock();
$applies_check = new EntityCreateAccessCheck($entity_manager);
$route = $this->getMockBuilder('Symfony\Component\Routing\Route')
->disableOriginalConstructor()
->getMock();
$route->expects($this->any())
->method('getRequirements')
->will($this->returnValue(array('_entity_create_access' => '')));
$res = $applies_check->applies($route);
$this->assertEquals(TRUE, $res);
$route = $this->getMockBuilder('Symfony\Component\Routing\Route')
->disableOriginalConstructor()
->getMock();
$route->expects($this->any())
->method('getRequirements')
->will($this->returnValue(array()));
$res = $applies_check->applies($route);
$this->assertEquals(FALSE, $res);
}
/**
* Provides test data for testAccess.
*
* @return array
*/
public function providerTestAccess() {
return array(
array('', 'entity_test', FALSE, AccessCheckInterface::DENY),
array('', 'entity_test',TRUE, AccessCheckInterface::ALLOW),
array('test_entity', 'entity_test:test_entity', TRUE, AccessCheckInterface::ALLOW),
array('test_entity', 'entity_test:test_entity', FALSE, AccessCheckInterface::DENY),
);
}
/**
* Tests the method for checking access to routes.
*
* @dataProvider providerTestAccess
*/
public function testAccess($entity_bundle, $requirement, $access, $expected) {
$entity = $this->getMockBuilder('Drupal\Core\Entity\Entity')
->disableOriginalConstructor()
->getMock();
$entity_manager = $this->getMockBuilder('Drupal\Core\Entity\EntityManager')
->disableOriginalConstructor()
->getMock();
$entity_manager->expects($this->any())
->method('getDefinition')
->with($this->equalTo('entity_test'))
->will($this->returnValue(array('entity_keys' => array('bundle' => 'type'))));
$storage_controller = $this->getMock('Drupal\Core\Entity\EntityStorageControllerInterface');
$values = $entity_bundle ? array('type' => $entity_bundle) : array();
$storage_controller->expects($this->any())
->method('create')
->with($this->equalTo($values))
->will($this->returnValue($entity));
$access_controller = $this->getMock('Drupal\Core\Entity\EntityAccessControllerInterface');
$access_controller->expects($this->once())
->method('access')
->with($entity, 'create')
->will($this->returnValue($access));
$entity_manager->expects($this->any())
->method('getStorageController')
->will($this->returnValue($storage_controller));
$entity_manager->expects($this->any())
->method('getAccessController')
->will($this->returnValue($access_controller));
$applies_check = new EntityCreateAccessCheck($entity_manager);
$route = $this->getMockBuilder('Symfony\Component\Routing\Route')
->disableOriginalConstructor()
->getMock();
$route->expects($this->any())
->method('getRequirement')
->with('_entity_create_access')
->will($this->returnValue($requirement));
$request = new Request();
$this->assertEquals($expected, $applies_check->access($route, $request));
}
}
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