Commit 4a2c2a3d authored by miro_dietiker's avatar miro_dietiker

Improve create access logic.

parent ec80ba42
......@@ -78,8 +78,11 @@ class ParagraphAccessControlHandler extends EntityAccessControlHandler implement
* {@inheritdoc}
*/
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
// Allowed when nobody implements.
return AccessResult::allowed();
// Allow paragraph entities to be created in the context of entity forms.
if (\Drupal::requestStack()->getCurrentRequest()->getRequestFormat() === 'html') {
return AccessResult::allowed()->addCacheContexts(['request_format']);
}
return AccessResult::neutral()->addCacheContexts(['request_format']);
}
}
<?php
namespace Drupal\Tests\paragraphs\Kernel;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\Context\CacheContextsManager;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Tests\token\Kernel\KernelTestBase;
use Symfony\Component\HttpFoundation\Request;
/**
* @coversDefaultClass \Drupal\paragraphs\ParagraphAccessControlHandler
* @group paragraphs
*/
class ParagraphsAccessTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = [
'paragraphs',
];
/**
* @covers ::checkCreateAccess
*
* @dataProvider createAccessTestCases
*/
public function testCreateAccess($request_format, $expected_result) {
$request = new Request();
$request->setRequestFormat($request_format);
$this->container->get('request_stack')->push($request);
$result = $this->container->get('entity_type.manager')->getAccessControlHandler('paragraph')->createAccess(NULL, NULL, [], TRUE);
$this->assertEquals($expected_result, $result);
}
/**
* Test cases for ::testCreateAccess.
*/
public function createAccessTestCases() {
$container = new ContainerBuilder();
$cache_contexts_manager = $this->prophesize(CacheContextsManager::class);
$cache_contexts_manager->assertValidTokens()->willReturn(TRUE);
$cache_contexts_manager->reveal();
$container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
return [
'Allowed HTML request format' => [
'html',
AccessResult::allowed()->addCacheContexts(['request_format']),
],
'Forbidden other formats' => [
'json',
AccessResult::neutral()->addCacheContexts(['request_format']),
],
];
}
}
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