From 49ac630080a714e8821f5b4d19a44cf8059d97d5 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Wed, 23 Oct 2013 11:19:00 -0700
Subject: [PATCH] Issue #1951386 by benjy, theduke, andypost, tim.plunkett:
 Fixed Extend BlockPluginInterface::access to allow passing in an account.

---
 .../aggregator/Plugin/Block/AggregatorCategoryBlock.php  | 7 ++++---
 .../aggregator/Plugin/Block/AggregatorFeedBlock.php      | 7 ++++---
 .../block/lib/Drupal/block/BlockAccessController.php     | 2 +-
 core/modules/block/lib/Drupal/block/BlockBase.php        | 3 ++-
 .../block/lib/Drupal/block/BlockPluginInterface.php      | 6 +++++-
 .../block_test/Plugin/Block/TestBlockInstantiation.php   | 5 +++--
 .../Drupal/comment/Plugin/Block/RecentCommentsBlock.php  | 7 ++++---
 .../lib/Drupal/forum/Plugin/Block/ForumBlockBase.php     | 7 ++++---
 .../lib/Drupal/language/Plugin/Block/LanguageBlock.php   | 5 +++--
 .../lib/Drupal/node/Plugin/Block/RecentContentBlock.php  | 7 ++++---
 .../node/lib/Drupal/node/Plugin/Block/SyndicateBlock.php | 7 ++++---
 .../lib/Drupal/search/Plugin/Block/SearchBlock.php       | 7 ++++---
 .../statistics/Plugin/Block/StatisticsPopularBlock.php   | 9 +++++----
 .../lib/Drupal/system/Plugin/Block/SystemHelpBlock.php   | 5 +++--
 .../lib/Drupal/system/Plugin/Block/SystemMenuBlock.php   | 7 ++++---
 .../user/lib/Drupal/user/Plugin/Block/UserLoginBlock.php | 7 ++++---
 .../user/lib/Drupal/user/Plugin/Block/UserNewBlock.php   | 7 ++++---
 .../lib/Drupal/user/Plugin/Block/UserOnlineBlock.php     | 7 ++++---
 .../lib/Drupal/views/Plugin/Block/ViewsBlockBase.php     | 3 ++-
 19 files changed, 68 insertions(+), 47 deletions(-)

diff --git a/core/modules/aggregator/lib/Drupal/aggregator/Plugin/Block/AggregatorCategoryBlock.php b/core/modules/aggregator/lib/Drupal/aggregator/Plugin/Block/AggregatorCategoryBlock.php
index eccd4639c8e1..618a3c2f29d3 100644
--- a/core/modules/aggregator/lib/Drupal/aggregator/Plugin/Block/AggregatorCategoryBlock.php
+++ b/core/modules/aggregator/lib/Drupal/aggregator/Plugin/Block/AggregatorCategoryBlock.php
@@ -13,6 +13,7 @@
 use Drupal\Core\Annotation\Translation;
 use Drupal\Core\Database\Connection;
 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
+use Drupal\Core\Session\AccountInterface;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
@@ -83,11 +84,11 @@ public function defaultConfiguration() {
   }
 
   /**
-   * Overrides \Drupal\block\BlockBase::access().
+   * {@inheritdoc}
    */
-  public function access() {
+  public function access(AccountInterface $account) {
     // Only grant access to users with the 'access news feeds' permission.
-    return user_access('access news feeds');
+    return $account->hasPermission('access news feeds');
   }
 
   /**
diff --git a/core/modules/aggregator/lib/Drupal/aggregator/Plugin/Block/AggregatorFeedBlock.php b/core/modules/aggregator/lib/Drupal/aggregator/Plugin/Block/AggregatorFeedBlock.php
index 4fc69fedcae6..b5272e9c7bbf 100644
--- a/core/modules/aggregator/lib/Drupal/aggregator/Plugin/Block/AggregatorFeedBlock.php
+++ b/core/modules/aggregator/lib/Drupal/aggregator/Plugin/Block/AggregatorFeedBlock.php
@@ -13,6 +13,7 @@
 use Drupal\Core\Database\Connection;
 use Drupal\Core\Entity\EntityStorageControllerInterface;
 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
+use Drupal\Core\Session\AccountInterface;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
@@ -86,11 +87,11 @@ public function defaultConfiguration() {
   }
 
   /**
-   * Overrides \Drupal\block\BlockBase::access().
+   * {@inheritdoc}
    */
-  public function access() {
+  public function access(AccountInterface $account) {
     // Only grant access to users with the 'access news feeds' permission.
-    return user_access('access news feeds');
+    return $account->hasPermission('access news feeds');
   }
 
   /**
diff --git a/core/modules/block/lib/Drupal/block/BlockAccessController.php b/core/modules/block/lib/Drupal/block/BlockAccessController.php
index 61bf3a1ecd15..2355c00ad235 100644
--- a/core/modules/block/lib/Drupal/block/BlockAccessController.php
+++ b/core/modules/block/lib/Drupal/block/BlockAccessController.php
@@ -67,7 +67,7 @@ protected function checkAccess(EntityInterface $entity, $operation, $langcode, A
     }
 
     // If the plugin denies access, then deny access.
-    if (!$entity->getPlugin()->access()) {
+    if (!$entity->getPlugin()->access($account)) {
       return FALSE;
     }
 
diff --git a/core/modules/block/lib/Drupal/block/BlockBase.php b/core/modules/block/lib/Drupal/block/BlockBase.php
index fa603d26a693..a602ab911802 100644
--- a/core/modules/block/lib/Drupal/block/BlockBase.php
+++ b/core/modules/block/lib/Drupal/block/BlockBase.php
@@ -11,6 +11,7 @@
 use Drupal\block\BlockInterface;
 use Drupal\Component\Utility\Unicode;
 use Drupal\Core\Language\Language;
+use Drupal\Core\Session\AccountInterface;
 
 /**
  * Defines a base block implementation that most blocks plugins will extend.
@@ -66,7 +67,7 @@ public function setConfigurationValue($key, $value) {
   /**
    * {@inheritdoc}
    */
-  public function access() {
+  public function access(AccountInterface $account) {
     // By default, the block is visible unless user-configured rules indicate
     // that it should be hidden.
     return TRUE;
diff --git a/core/modules/block/lib/Drupal/block/BlockPluginInterface.php b/core/modules/block/lib/Drupal/block/BlockPluginInterface.php
index b5433e635797..598dff3d15e5 100644
--- a/core/modules/block/lib/Drupal/block/BlockPluginInterface.php
+++ b/core/modules/block/lib/Drupal/block/BlockPluginInterface.php
@@ -10,6 +10,7 @@
 use Drupal\Component\Plugin\PluginInspectionInterface;
 use Drupal\Component\Plugin\ConfigurablePluginInterface;
 use Drupal\Core\Plugin\PluginFormInterface;
+use Drupal\Core\Session\AccountInterface;
 
 /**
  * Defines the required interface for all block plugins.
@@ -27,12 +28,15 @@ interface BlockPluginInterface extends ConfigurablePluginInterface, PluginFormIn
    * This method allows base implementations to add general access restrictions
    * that should apply to all extending block plugins.
    *
+   * @param \Drupal\Core\Session\AccountInterface $account
+   *   The user session for which to check access.
+   *
    * @return bool
    *   TRUE if the block should be shown, or FALSE otherwise.
    *
    * @see \Drupal\block\BlockAccessController
    */
-  public function access();
+  public function access(AccountInterface $account);
 
   /**
    * Builds and returns the renderable array for this block plugin.
diff --git a/core/modules/block/tests/modules/block_test/lib/Drupal/block_test/Plugin/Block/TestBlockInstantiation.php b/core/modules/block/tests/modules/block_test/lib/Drupal/block_test/Plugin/Block/TestBlockInstantiation.php
index 1d5e947cf96c..e12b07d1d3f4 100644
--- a/core/modules/block/tests/modules/block_test/lib/Drupal/block_test/Plugin/Block/TestBlockInstantiation.php
+++ b/core/modules/block/tests/modules/block_test/lib/Drupal/block_test/Plugin/Block/TestBlockInstantiation.php
@@ -10,6 +10,7 @@
 use Drupal\block\BlockBase;
 use Drupal\block\Annotation\Block;
 use Drupal\Core\Annotation\Translation;
+use Drupal\Core\Session\AccountInterface;
 
 /**
  * Provides a basic block for testing block instantiation and configuration.
@@ -33,8 +34,8 @@ public function defaultConfiguration() {
   /**
    * {@inheritdoc}
    */
-  public function access() {
-    return user_access('access content');
+  public function access(AccountInterface $account) {
+    return $account->hasPermission('access content');
   }
 
   /**
diff --git a/core/modules/comment/lib/Drupal/comment/Plugin/Block/RecentCommentsBlock.php b/core/modules/comment/lib/Drupal/comment/Plugin/Block/RecentCommentsBlock.php
index 10358ae0d611..150d91a7204c 100644
--- a/core/modules/comment/lib/Drupal/comment/Plugin/Block/RecentCommentsBlock.php
+++ b/core/modules/comment/lib/Drupal/comment/Plugin/Block/RecentCommentsBlock.php
@@ -10,6 +10,7 @@
 use Drupal\block\BlockBase;
 use Drupal\block\Annotation\Block;
 use Drupal\Core\Annotation\Translation;
+use Drupal\Core\Session\AccountInterface;
 
 /**
  * Provides a 'Recent comments' block.
@@ -31,10 +32,10 @@ public function defaultConfiguration() {
   }
 
   /**
-   * Overrides \Drupal\block\BlockBase::access().
+   * {@inheritdoc}
    */
-  public function access() {
-    return user_access('access comments');
+  public function access(AccountInterface $account) {
+    return $account->hasPermission('access comments');
   }
 
   /**
diff --git a/core/modules/forum/lib/Drupal/forum/Plugin/Block/ForumBlockBase.php b/core/modules/forum/lib/Drupal/forum/Plugin/Block/ForumBlockBase.php
index 0f4fff4a78c6..c2ad994208ed 100644
--- a/core/modules/forum/lib/Drupal/forum/Plugin/Block/ForumBlockBase.php
+++ b/core/modules/forum/lib/Drupal/forum/Plugin/Block/ForumBlockBase.php
@@ -8,6 +8,7 @@
 namespace Drupal\forum\Plugin\Block;
 
 use Drupal\block\BlockBase;
+use Drupal\Core\Session\AccountInterface;
 
 /**
  * Provides a base class for Forum blocks.
@@ -28,10 +29,10 @@ public function defaultConfiguration() {
   }
 
   /**
-   * Overrides \Drupal\block\BlockBase::access().
+   * {@inheritdoc}
    */
-  public function access() {
-    return user_access('access content');
+  public function access(AccountInterface $account) {
+    return $account->hasPermission('access content');
   }
 
   /**
diff --git a/core/modules/language/lib/Drupal/language/Plugin/Block/LanguageBlock.php b/core/modules/language/lib/Drupal/language/Plugin/Block/LanguageBlock.php
index 5279dd6b0795..fab28d861057 100644
--- a/core/modules/language/lib/Drupal/language/Plugin/Block/LanguageBlock.php
+++ b/core/modules/language/lib/Drupal/language/Plugin/Block/LanguageBlock.php
@@ -10,6 +10,7 @@
 use Drupal\block\BlockBase;
 use Drupal\block\Annotation\Block;
 use Drupal\Core\Annotation\Translation;
+use Drupal\Core\Session\AccountInterface;
 
 /**
  * Provides a 'Language switcher' block.
@@ -23,9 +24,9 @@
 class LanguageBlock extends BlockBase {
 
   /**
-   * Overrides \Drupal\block\BlockBase::access().
+   * {@inheritdoc}
    */
-  function access() {
+  function access(AccountInterface $account) {
     return language_multilingual();
   }
 
diff --git a/core/modules/node/lib/Drupal/node/Plugin/Block/RecentContentBlock.php b/core/modules/node/lib/Drupal/node/Plugin/Block/RecentContentBlock.php
index feb93b215134..92a129d57ac6 100644
--- a/core/modules/node/lib/Drupal/node/Plugin/Block/RecentContentBlock.php
+++ b/core/modules/node/lib/Drupal/node/Plugin/Block/RecentContentBlock.php
@@ -10,6 +10,7 @@
 use Drupal\block\BlockBase;
 use Drupal\block\Annotation\Block;
 use Drupal\Core\Annotation\Translation;
+use Drupal\Core\Session\AccountInterface;
 
 /**
  * Provides a 'Recent content' block.
@@ -31,10 +32,10 @@ public function defaultConfiguration() {
   }
 
   /**
-   * Overrides \Drupal\block\BlockBase::access().
+   * {@inheritdoc}
    */
-  public function access() {
-    return user_access('access content');
+  public function access(AccountInterface $account) {
+    return $account->hasPermission('access content');
   }
 
   /**
diff --git a/core/modules/node/lib/Drupal/node/Plugin/Block/SyndicateBlock.php b/core/modules/node/lib/Drupal/node/Plugin/Block/SyndicateBlock.php
index 0f4504329bac..04ed8b9b95d7 100644
--- a/core/modules/node/lib/Drupal/node/Plugin/Block/SyndicateBlock.php
+++ b/core/modules/node/lib/Drupal/node/Plugin/Block/SyndicateBlock.php
@@ -10,6 +10,7 @@
 use Drupal\block\BlockBase;
 use Drupal\block\Annotation\Block;
 use Drupal\Core\Annotation\Translation;
+use Drupal\Core\Session\AccountInterface;
 
 /**
  * Provides a 'Syndicate' block that links to the site's RSS feed.
@@ -31,10 +32,10 @@ public function defaultConfiguration() {
   }
 
   /**
-   * Overrides \Drupal\block\BlockBase::access().
+   * {@inheritdoc}
    */
-  public function access() {
-    return user_access('access content');
+  public function access(AccountInterface $account) {
+    return $account->hasPermission('access content');
   }
 
   /**
diff --git a/core/modules/search/lib/Drupal/search/Plugin/Block/SearchBlock.php b/core/modules/search/lib/Drupal/search/Plugin/Block/SearchBlock.php
index 08652bb6b11a..99643e2377ce 100644
--- a/core/modules/search/lib/Drupal/search/Plugin/Block/SearchBlock.php
+++ b/core/modules/search/lib/Drupal/search/Plugin/Block/SearchBlock.php
@@ -7,6 +7,7 @@
 
 namespace Drupal\search\Plugin\Block;
 
+use Drupal\Core\Session\AccountInterface;
 use Drupal\block\BlockBase;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Component\HttpFoundation\Request;
@@ -22,10 +23,10 @@
 class SearchBlock extends BlockBase {
 
   /**
-   * Overrides \Drupal\block\BlockBase::access().
+   * {@inheritdoc}
    */
-  public function access() {
-    return user_access('search content');
+  public function access(AccountInterface $account) {
+    return $account->hasPermission('search content');
   }
 
   /**
diff --git a/core/modules/statistics/lib/Drupal/statistics/Plugin/Block/StatisticsPopularBlock.php b/core/modules/statistics/lib/Drupal/statistics/Plugin/Block/StatisticsPopularBlock.php
index ece474060832..659d755153e0 100644
--- a/core/modules/statistics/lib/Drupal/statistics/Plugin/Block/StatisticsPopularBlock.php
+++ b/core/modules/statistics/lib/Drupal/statistics/Plugin/Block/StatisticsPopularBlock.php
@@ -10,6 +10,7 @@
 use Drupal\block\BlockBase;
 use Drupal\block\Annotation\Block;
 use Drupal\Core\Annotation\Translation;
+use Drupal\Core\Session\AccountInterface;
 
 /**
  * Provides a 'Popular content' block.
@@ -53,11 +54,11 @@ public function defaultConfiguration() {
     );
   }
 
-    /**
-   * Overrides \Drupal\block\BlockBase::access().
+  /**
+   * {@inheritdoc}
    */
-  public function access() {
-    if (\Drupal::currentUser()->hasPermission('access content')) {
+  public function access(AccountInterface $account) {
+    if ($account->hasPermission('access content')) {
       $daytop = $this->configuration['top_day_num'];
       if (!$daytop || !($result = statistics_title_list('daycount', $daytop)) || !($this->day_list = node_title_list($result, t("Today's:")))) {
         return FALSE;
diff --git a/core/modules/system/lib/Drupal/system/Plugin/Block/SystemHelpBlock.php b/core/modules/system/lib/Drupal/system/Plugin/Block/SystemHelpBlock.php
index 48ad76febed1..a8c05fe3755f 100644
--- a/core/modules/system/lib/Drupal/system/Plugin/Block/SystemHelpBlock.php
+++ b/core/modules/system/lib/Drupal/system/Plugin/Block/SystemHelpBlock.php
@@ -12,6 +12,7 @@
 use Drupal\Core\Annotation\Translation;
 use Drupal\Core\Extension\ModuleHandlerInterface;
 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
+use Drupal\Core\Session\AccountInterface;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Component\HttpFoundation\Request;
 
@@ -76,9 +77,9 @@ public static function create(ContainerInterface $container, array $configuratio
   }
 
   /**
-   * Overrides \Drupal\block\BlockBase::access().
+   * {@inheritdoc}
    */
-  public function access() {
+  public function access(AccountInterface $account) {
     $this->help = $this->getActiveHelp($this->request);
     return (bool) $this->help;
   }
diff --git a/core/modules/system/lib/Drupal/system/Plugin/Block/SystemMenuBlock.php b/core/modules/system/lib/Drupal/system/Plugin/Block/SystemMenuBlock.php
index 92f137bdc092..c983672bc0ab 100644
--- a/core/modules/system/lib/Drupal/system/Plugin/Block/SystemMenuBlock.php
+++ b/core/modules/system/lib/Drupal/system/Plugin/Block/SystemMenuBlock.php
@@ -10,6 +10,7 @@
 use Drupal\block\BlockBase;
 use Drupal\block\Annotation\Block;
 use Drupal\Core\Annotation\Translation;
+use Drupal\Core\Session\AccountInterface;
 
 /**
  * Provides a generic Menu block.
@@ -24,12 +25,12 @@
 class SystemMenuBlock extends BlockBase {
 
   /**
-   * Overrides \Drupal\block\BlockBase::access().
+   * {@inheritdoc}
    */
-  public function access() {
+  public function access(AccountInterface $account) {
     // @todo Clean up when http://drupal.org/node/1874498 lands.
     list( , $derivative) = explode(':', $this->getPluginId());
-    return ($GLOBALS['user']->isAuthenticated() || in_array($derivative, array('main', 'tools', 'footer')));
+    return ($account->isAuthenticated() || in_array($derivative, array('main', 'tools', 'footer')));
   }
 
   /**
diff --git a/core/modules/user/lib/Drupal/user/Plugin/Block/UserLoginBlock.php b/core/modules/user/lib/Drupal/user/Plugin/Block/UserLoginBlock.php
index 8106ce383e7c..c4f3c4fb0d99 100644
--- a/core/modules/user/lib/Drupal/user/Plugin/Block/UserLoginBlock.php
+++ b/core/modules/user/lib/Drupal/user/Plugin/Block/UserLoginBlock.php
@@ -7,6 +7,7 @@
 
 namespace Drupal\user\Plugin\Block;
 
+use Drupal\Core\Session\AccountInterface;
 use Drupal\block\BlockBase;
 
 /**
@@ -20,10 +21,10 @@
 class UserLoginBlock extends BlockBase {
 
   /**
-   * Overrides \Drupal\block\BlockBase::access().
+   * {@inheritdoc}
    */
-  public function access() {
-    return (!$GLOBALS['user']->id() && !(arg(0) == 'user' && !is_numeric(arg(1))));
+  public function access(AccountInterface $account) {
+    return (!$account->id() && !(arg(0) == 'user' && !is_numeric(arg(1))));
   }
 
   /**
diff --git a/core/modules/user/lib/Drupal/user/Plugin/Block/UserNewBlock.php b/core/modules/user/lib/Drupal/user/Plugin/Block/UserNewBlock.php
index 71b28e3fe408..5a639563a05b 100644
--- a/core/modules/user/lib/Drupal/user/Plugin/Block/UserNewBlock.php
+++ b/core/modules/user/lib/Drupal/user/Plugin/Block/UserNewBlock.php
@@ -10,6 +10,7 @@
 use Drupal\block\BlockBase;
 use Drupal\block\Annotation\Block;
 use Drupal\Core\Annotation\Translation;
+use Drupal\Core\Session\AccountInterface;
 
 /**
  * Provides a "Who's new" block.
@@ -34,10 +35,10 @@ public function defaultConfiguration() {
   }
 
   /**
-   * Overrides \Drupal\block\BlockBase::access().
+   * {@inheritdoc}
    */
-  public function access() {
-    return user_access('access content');
+  public function access(AccountInterface $account) {
+    return $account->hasPermission('access content');
   }
 
   /**
diff --git a/core/modules/user/lib/Drupal/user/Plugin/Block/UserOnlineBlock.php b/core/modules/user/lib/Drupal/user/Plugin/Block/UserOnlineBlock.php
index 7370b7a823b4..d3e26b81005c 100644
--- a/core/modules/user/lib/Drupal/user/Plugin/Block/UserOnlineBlock.php
+++ b/core/modules/user/lib/Drupal/user/Plugin/Block/UserOnlineBlock.php
@@ -10,6 +10,7 @@
 use Drupal\block\BlockBase;
 use Drupal\block\Annotation\Block;
 use Drupal\Core\Annotation\Translation;
+use Drupal\Core\Session\AccountInterface;
 
 /**
  * Provides a "Who's online" block.
@@ -38,10 +39,10 @@ public function defaultConfiguration() {
   }
 
   /**
-   * Overrides \Drupal\block\BlockBase::access().
+   * {@inheritdoc}
    */
-  public function access() {
-    return user_access('access content');
+  public function access(AccountInterface $account) {
+    return $account->hasPermission('access content');
   }
 
   /**
diff --git a/core/modules/views/lib/Drupal/views/Plugin/Block/ViewsBlockBase.php b/core/modules/views/lib/Drupal/views/Plugin/Block/ViewsBlockBase.php
index 10f5dd2dd1a2..e20f146760e2 100644
--- a/core/modules/views/lib/Drupal/views/Plugin/Block/ViewsBlockBase.php
+++ b/core/modules/views/lib/Drupal/views/Plugin/Block/ViewsBlockBase.php
@@ -12,6 +12,7 @@
 use Drupal\views\ViewExecutableFactory;
 use Drupal\Core\Entity\EntityStorageControllerInterface;
 use Symfony\Component\DependencyInjection\ContainerInterface;
+use Drupal\Core\Session\AccountInterface;
 
 /**
  * Base class for Views block plugins.
@@ -79,7 +80,7 @@ public static function create(ContainerInterface $container, array $configuratio
   /**
    * {@inheritdoc}
    */
-  public function access() {
+  public function access(AccountInterface $account) {
     return $this->view->access($this->displayID);
   }
 
-- 
GitLab