Issue #2375689 by Arla, Wim Leers, dawehner, tim.plunkett, Berdir: BlockBase::blockAccess() should return AccessResult instead of a bool

8.0.x
webchick 2015-04-16 09:45:19 -07:00
parent 3deb640a91
commit 8f07297676
15 changed files with 88 additions and 42 deletions

View File

@ -121,34 +121,28 @@ abstract class BlockBase extends ContextAwarePluginBase implements BlockPluginIn
* {@inheritdoc} * {@inheritdoc}
*/ */
public function access(AccountInterface $account, $return_as_object = FALSE) { public function access(AccountInterface $account, $return_as_object = FALSE) {
// @todo Remove self::blockAccess() and force individual plugins to return $access = $this->blockAccess($account);
// their own AccessResult logic. Until that is done in
// https://www.drupal.org/node/2375689 the access will be set uncacheable.
if ($this->blockAccess($account)) {
$access = AccessResult::allowed();
}
else {
$access = AccessResult::forbidden();
}
$access->setCacheMaxAge(0);
return $return_as_object ? $access : $access->isAllowed(); return $return_as_object ? $access : $access->isAllowed();
} }
/** /**
* Indicates whether the block should be shown. * Indicates whether the block should be shown.
* *
* Blocks with specific access checking should override this method rather
* than access(), in order to avoid repeating the handling of the
* $return_as_object argument.
*
* @param \Drupal\Core\Session\AccountInterface $account * @param \Drupal\Core\Session\AccountInterface $account
* The user session for which to check access. * The user session for which to check access.
* *
* @return bool * @return \Drupal\Core\Access\AccessResult
* TRUE if the block should be shown, or FALSE otherwise. * The access result.
* *
* @see self::access() * @see self::access()
*/ */
protected function blockAccess(AccountInterface $account) { protected function blockAccess(AccountInterface $account) {
// By default, the block is visible. // By default, the block is visible.
return TRUE; return AccessResult::allowed();
} }
/** /**

View File

@ -9,6 +9,7 @@ namespace Drupal\aggregator\Plugin\Block;
use Drupal\aggregator\FeedStorageInterface; use Drupal\aggregator\FeedStorageInterface;
use Drupal\aggregator\ItemStorageInterface; use Drupal\aggregator\ItemStorageInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase; use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\Query\QueryInterface; use Drupal\Core\Entity\Query\QueryInterface;
@ -104,7 +105,7 @@ class AggregatorFeedBlock extends BlockBase implements ContainerFactoryPluginInt
*/ */
protected function blockAccess(AccountInterface $account) { protected function blockAccess(AccountInterface $account) {
// Only grant access to users with the 'access news feeds' permission. // Only grant access to users with the 'access news feeds' permission.
return $account->hasPermission('access news feeds'); return AccessResult::allowedIfHasPermission($account, 'access news feeds');
} }
/** /**

View File

@ -7,6 +7,7 @@
namespace Drupal\block_test\Plugin\Block; namespace Drupal\block_test\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase; use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\Cache;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
@ -62,7 +63,7 @@ class TestAccessBlock extends BlockBase implements ContainerFactoryPluginInterfa
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function blockAccess(AccountInterface $account) { protected function blockAccess(AccountInterface $account) {
return $this->state->get('test_block_access', FALSE); return $this->state->get('test_block_access', FALSE) ? AccessResult::allowed() : AccessResult::forbidden();
} }
/** /**

View File

@ -7,6 +7,7 @@
namespace Drupal\block_test\Plugin\Block; namespace Drupal\block_test\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase; use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
@ -34,7 +35,7 @@ class TestBlockInstantiation extends BlockBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function blockAccess(AccountInterface $account) { protected function blockAccess(AccountInterface $account) {
return $account->hasPermission('access content'); return AccessResult::allowedIfHasPermission($account, 'access content');
} }
/** /**

View File

@ -7,6 +7,7 @@
namespace Drupal\block_content\Plugin\Block; namespace Drupal\block_content\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase; use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockManagerInterface; use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\EntityManagerInterface;
@ -50,6 +51,13 @@ class BlockContentBlock extends BlockBase implements ContainerFactoryPluginInter
*/ */
protected $account; protected $account;
/**
* The block content entity.
*
* @var \Drupal\block_content\BlockContentInterface
*/
protected $blockContent;
/** /**
* Constructs a new BlockContentBlock. * Constructs a new BlockContentBlock.
* *
@ -129,22 +137,46 @@ class BlockContentBlock extends BlockBase implements ContainerFactoryPluginInter
$this->blockManager->clearCachedDefinitions(); $this->blockManager->clearCachedDefinitions();
} }
/**
* {@inheritdoc}
*/
protected function blockAccess(AccountInterface $account) {
if ($this->getEntity()) {
return $this->getEntity()->access('view', $account, TRUE);
}
return AccessResult::forbidden();
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function build() { public function build() {
$uuid = $this->getDerivativeId(); if ($block = $this->getEntity()) {
if ($block = $this->entityManager->loadEntityByUuid('block_content', $uuid)) {
return $this->entityManager->getViewBuilder($block->getEntityTypeId())->view($block, $this->configuration['view_mode']); return $this->entityManager->getViewBuilder($block->getEntityTypeId())->view($block, $this->configuration['view_mode']);
} }
else { else {
return array( return array(
'#markup' => $this->t('Block with uuid %uuid does not exist. <a href="!url">Add custom block</a>.', array( '#markup' => $this->t('Block with uuid %uuid does not exist. <a href="!url">Add custom block</a>.', array(
'%uuid' => $uuid, '%uuid' => $this->getDerivativeId(),
'!url' => $this->urlGenerator->generate('block_content.add_page') '!url' => $this->urlGenerator->generate('block_content.add_page')
)), )),
'#access' => $this->account->hasPermission('administer blocks') '#access' => $this->account->hasPermission('administer blocks')
); );
} }
} }
/**
* Loads the block content entity of the block.
*
* @return \Drupal\block_content\BlockContentInterface|null
* The block content entity.
*/
protected function getEntity() {
$uuid = $this->getDerivativeId();
if (!isset($this->blockContent)) {
$this->blockContent = $this->entityManager->loadEntityByUuid('block_content', $uuid);
}
return $this->blockContent;
}
} }

View File

@ -7,6 +7,7 @@
namespace Drupal\forum\Plugin\Block; namespace Drupal\forum\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase; use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
@ -59,7 +60,7 @@ abstract class ForumBlockBase extends BlockBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function blockAccess(AccountInterface $account) { protected function blockAccess(AccountInterface $account) {
return $account->hasPermission('access content'); return AccessResult::allowedIfHasPermission($account, 'access content');
} }
/** /**

View File

@ -7,6 +7,7 @@
namespace Drupal\help\Plugin\Block; namespace Drupal\help\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase; use Drupal\Core\Block\BlockBase;
use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
@ -96,7 +97,12 @@ class HelpBlock extends BlockBase implements ContainerFactoryPluginInterface {
*/ */
protected function blockAccess(AccountInterface $account) { protected function blockAccess(AccountInterface $account) {
$this->help = $this->getActiveHelp($this->request); $this->help = $this->getActiveHelp($this->request);
return (bool) $this->help; if ($this->help) {
return AccessResult::allowed();
}
else {
return AccessResult::forbidden();
}
} }
/** /**

View File

@ -7,6 +7,7 @@
namespace Drupal\language\Plugin\Block; namespace Drupal\language\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase; use Drupal\Core\Block\BlockBase;
use Drupal\Core\Path\PathMatcherInterface; use Drupal\Core\Path\PathMatcherInterface;
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
@ -80,7 +81,8 @@ class LanguageBlock extends BlockBase implements ContainerFactoryPluginInterface
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function blockAccess(AccountInterface $account) { protected function blockAccess(AccountInterface $account) {
return $this->languageManager->isMultilingual(); $access = $this->languageManager->isMultilingual() ? AccessResult::allowed() : AccessResult::forbidden();
return $access->addCacheTags(['config:configurable_language_list']);
} }
/** /**

View File

@ -7,6 +7,7 @@
namespace Drupal\node\Plugin\Block; namespace Drupal\node\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase; use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\Cache;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
@ -36,7 +37,7 @@ class SyndicateBlock extends BlockBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function blockAccess(AccountInterface $account) { protected function blockAccess(AccountInterface $account) {
return $account->hasPermission('access content'); return AccessResult::allowedIfHasPermission($account, 'access content');
} }
/** /**

View File

@ -7,10 +7,10 @@
namespace Drupal\search\Plugin\Block; namespace Drupal\search\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Block\BlockBase; use Drupal\Core\Block\BlockBase;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
/** /**
* Provides a 'Search form' block. * Provides a 'Search form' block.
@ -27,7 +27,7 @@ class SearchBlock extends BlockBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function blockAccess(AccountInterface $account) { protected function blockAccess(AccountInterface $account) {
return $account->hasPermission('search content'); return AccessResult::allowedIfHasPermission($account, 'search content');
} }
/** /**

View File

@ -7,6 +7,7 @@
namespace Drupal\shortcut\Plugin\Block; namespace Drupal\shortcut\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase; use Drupal\Core\Block\BlockBase;
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
@ -34,7 +35,7 @@ class ShortcutsBlock extends BlockBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function blockAccess(AccountInterface $account) { protected function blockAccess(AccountInterface $account) {
return $account->hasPermission('access shortcuts'); return AccessResult::allowedIfHasPermission($account, 'access shortcuts');
} }
} }

View File

@ -7,6 +7,7 @@
namespace Drupal\statistics\Plugin\Block; namespace Drupal\statistics\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase; use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
@ -57,22 +58,23 @@ class StatisticsPopularBlock extends BlockBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function blockAccess(AccountInterface $account) { protected function blockAccess(AccountInterface $account) {
$access = AccessResult::allowedIfHasPermission($account, 'access content');
if ($account->hasPermission('access content')) { if ($account->hasPermission('access content')) {
$daytop = $this->configuration['top_day_num']; $daytop = $this->configuration['top_day_num'];
if (!$daytop || !($result = statistics_title_list('daycount', $daytop)) || !($this->day_list = node_title_list($result, $this->t("Today's:")))) { if (!$daytop || !($result = statistics_title_list('daycount', $daytop)) || !($this->day_list = node_title_list($result, $this->t("Today's:")))) {
return FALSE; return AccessResult::forbidden()->inheritCacheability($access);
} }
$alltimetop = $this->configuration['top_all_num']; $alltimetop = $this->configuration['top_all_num'];
if (!$alltimetop || !($result = statistics_title_list('totalcount', $alltimetop)) || !($this->all_time_list = node_title_list($result, $this->t('All time:')))) { if (!$alltimetop || !($result = statistics_title_list('totalcount', $alltimetop)) || !($this->all_time_list = node_title_list($result, $this->t('All time:')))) {
return FALSE; return AccessResult::forbidden()->inheritCacheability($access);
} }
$lasttop = $this->configuration['top_last_num']; $lasttop = $this->configuration['top_last_num'];
if (!$lasttop || !($result = statistics_title_list('timestamp', $lasttop)) || !($this->last_list = node_title_list($result, $this->t('Last viewed:')))) { if (!$lasttop || !($result = statistics_title_list('timestamp', $lasttop)) || !($this->last_list = node_title_list($result, $this->t('Last viewed:')))) {
return FALSE; return AccessResult::forbidden()->inheritCacheability($access);
} }
return TRUE; return $access;
} }
return FALSE; return AccessResult::forbidden()->inheritCacheability($access);
} }
/** /**

View File

@ -10,7 +10,6 @@ namespace Drupal\form_test\Plugin\Block;
use Drupal\Core\Block\BlockBase; use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormBuilderInterface; use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
@ -63,13 +62,6 @@ class RedirectFormBlock extends BlockBase implements ContainerFactoryPluginInter
); );
} }
/**
* {@inheritdoc}
*/
protected function blockAccess(AccountInterface $account) {
return TRUE;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -7,6 +7,7 @@
namespace Drupal\user\Plugin\Block; namespace Drupal\user\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RedirectDestinationTrait; use Drupal\Core\Routing\RedirectDestinationTrait;
use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Routing\RouteMatchInterface;
@ -76,7 +77,11 @@ class UserLoginBlock extends BlockBase implements ContainerFactoryPluginInterfac
*/ */
protected function blockAccess(AccountInterface $account) { protected function blockAccess(AccountInterface $account) {
$route_name = $this->routeMatch->getRouteName(); $route_name = $this->routeMatch->getRouteName();
return ($account->isAnonymous() && !in_array($route_name, array('user.register', 'user.login', 'user.logout'))); if ($account->isAnonymous() && !in_array($route_name, array('user.register', 'user.login', 'user.logout'))) {
return AccessResult::allowed()
->addCacheContexts(['route', 'user.roles:anonymous']);
}
return AccessResult::forbidden();
} }
/** /**

View File

@ -7,6 +7,7 @@
namespace Drupal\views\Plugin\Block; namespace Drupal\views\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase; use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
@ -93,7 +94,13 @@ abstract class ViewsBlockBase extends BlockBase implements ContainerFactoryPlugi
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function blockAccess(AccountInterface $account) { protected function blockAccess(AccountInterface $account) {
return $this->view->access($this->displayID); if ($this->view->access($this->displayID)) {
$access = AccessResult::allowed();
}
else {
$access = AccessResult::forbidden();
}
return $access;
} }
/** /**