Loading core/lib/Drupal/Core/Session/AccountProxy.php +16 −0 Original line number Diff line number Diff line Loading @@ -98,6 +98,22 @@ public function getRoles($exclude_locked_roles = FALSE) { return $this->getAccount()->getRoles($exclude_locked_roles); } /** * Whether a user has a certain role. * * @param string $rid * The role ID to check. * * @return bool * Returns TRUE if the user has the role, otherwise FALSE. * * @todo in Drupal 11, add method to Drupal\Core\Session\AccountInterface. * @see https://www.drupal.org/node/3228209 */ public function hasRole(string $rid): bool { return $this->getAccount()->hasRole($rid); } /** * {@inheritdoc} */ Loading core/lib/Drupal/Core/Session/UserSession.php +16 −0 Original line number Diff line number Diff line Loading @@ -100,6 +100,22 @@ public function getRoles($exclude_locked_roles = FALSE) { return $roles; } /** * Whether a user has a certain role. * * @param string $rid * The role ID to check. * * @return bool * Returns TRUE if the user has the role, otherwise FALSE. * * @todo in Drupal 11, add method to Drupal\Core\Session\AccountInterface. * @see https://www.drupal.org/node/3228209 */ public function hasRole(string $rid): bool { return in_array($rid, $this->getRoles(), TRUE); } /** * {@inheritdoc} */ Loading core/modules/user/src/UserInterface.php +3 −0 Original line number Diff line number Diff line Loading @@ -58,6 +58,9 @@ interface UserInterface extends ContentEntityInterface, EntityChangedInterface, * * @return bool * Returns TRUE if the user has the role, otherwise FALSE. * * @todo in Drupal 11, move method to Drupal\Core\Session\AccountInterface. * @see https://www.drupal.org/node/3228209 */ public function hasRole($rid); Loading core/tests/Drupal/Tests/Core/Session/AccountProxyTest.php +18 −0 Original line number Diff line number Diff line Loading @@ -4,7 +4,9 @@ use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountProxy; use Drupal\Core\Session\UserSession; use Drupal\Tests\UnitTestCase; use Drupal\user\RoleInterface; use Prophecy\Argument; use Symfony\Contracts\EventDispatcher\Event; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; Loading Loading @@ -49,4 +51,20 @@ public function testSetInitialAccountIdException() { $account_proxy->setInitialAccountId(1); } /** * @covers ::hasRole */ public function testHasRole() { $dispatcher = $this->prophesize(EventDispatcherInterface::class); $dispatcher->dispatch(Argument::any(), Argument::any())->willReturn(new Event()); $account_proxy = new AccountProxy($dispatcher->reveal()); $this->assertTrue($account_proxy->hasRole(RoleInterface::ANONYMOUS_ID)); $current_user = $this->prophesize(UserSession::class); $current_user->id()->willReturn(2); $current_user->hasRole(RoleInterface::AUTHENTICATED_ID)->willReturn(TRUE); $account_proxy->setAccount($current_user->reveal()); $this->assertTrue($account_proxy->hasRole(RoleInterface::AUTHENTICATED_ID)); } } core/tests/Drupal/Tests/Core/Session/UserSessionTest.php +13 −0 Original line number Diff line number Diff line Loading @@ -162,4 +162,17 @@ public function testUserGetRoles() { $this->assertEquals(['role_two'], $this->users['user_three']->getRoles(TRUE)); } /** * Tests the hasRole method. * * @covers ::hasRole */ public function testHasRole() { $this->assertTrue($this->users['user_one']->hasRole('role_one')); $this->assertFalse($this->users['user_two']->hasRole('no role')); $this->assertTrue($this->users['user_three']->hasRole(RoleInterface::AUTHENTICATED_ID)); $this->assertFalse($this->users['user_three']->hasRole(RoleInterface::ANONYMOUS_ID)); $this->assertTrue($this->users['user_last']->hasRole(RoleInterface::ANONYMOUS_ID)); } } Loading
core/lib/Drupal/Core/Session/AccountProxy.php +16 −0 Original line number Diff line number Diff line Loading @@ -98,6 +98,22 @@ public function getRoles($exclude_locked_roles = FALSE) { return $this->getAccount()->getRoles($exclude_locked_roles); } /** * Whether a user has a certain role. * * @param string $rid * The role ID to check. * * @return bool * Returns TRUE if the user has the role, otherwise FALSE. * * @todo in Drupal 11, add method to Drupal\Core\Session\AccountInterface. * @see https://www.drupal.org/node/3228209 */ public function hasRole(string $rid): bool { return $this->getAccount()->hasRole($rid); } /** * {@inheritdoc} */ Loading
core/lib/Drupal/Core/Session/UserSession.php +16 −0 Original line number Diff line number Diff line Loading @@ -100,6 +100,22 @@ public function getRoles($exclude_locked_roles = FALSE) { return $roles; } /** * Whether a user has a certain role. * * @param string $rid * The role ID to check. * * @return bool * Returns TRUE if the user has the role, otherwise FALSE. * * @todo in Drupal 11, add method to Drupal\Core\Session\AccountInterface. * @see https://www.drupal.org/node/3228209 */ public function hasRole(string $rid): bool { return in_array($rid, $this->getRoles(), TRUE); } /** * {@inheritdoc} */ Loading
core/modules/user/src/UserInterface.php +3 −0 Original line number Diff line number Diff line Loading @@ -58,6 +58,9 @@ interface UserInterface extends ContentEntityInterface, EntityChangedInterface, * * @return bool * Returns TRUE if the user has the role, otherwise FALSE. * * @todo in Drupal 11, move method to Drupal\Core\Session\AccountInterface. * @see https://www.drupal.org/node/3228209 */ public function hasRole($rid); Loading
core/tests/Drupal/Tests/Core/Session/AccountProxyTest.php +18 −0 Original line number Diff line number Diff line Loading @@ -4,7 +4,9 @@ use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountProxy; use Drupal\Core\Session\UserSession; use Drupal\Tests\UnitTestCase; use Drupal\user\RoleInterface; use Prophecy\Argument; use Symfony\Contracts\EventDispatcher\Event; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; Loading Loading @@ -49,4 +51,20 @@ public function testSetInitialAccountIdException() { $account_proxy->setInitialAccountId(1); } /** * @covers ::hasRole */ public function testHasRole() { $dispatcher = $this->prophesize(EventDispatcherInterface::class); $dispatcher->dispatch(Argument::any(), Argument::any())->willReturn(new Event()); $account_proxy = new AccountProxy($dispatcher->reveal()); $this->assertTrue($account_proxy->hasRole(RoleInterface::ANONYMOUS_ID)); $current_user = $this->prophesize(UserSession::class); $current_user->id()->willReturn(2); $current_user->hasRole(RoleInterface::AUTHENTICATED_ID)->willReturn(TRUE); $account_proxy->setAccount($current_user->reveal()); $this->assertTrue($account_proxy->hasRole(RoleInterface::AUTHENTICATED_ID)); } }
core/tests/Drupal/Tests/Core/Session/UserSessionTest.php +13 −0 Original line number Diff line number Diff line Loading @@ -162,4 +162,17 @@ public function testUserGetRoles() { $this->assertEquals(['role_two'], $this->users['user_three']->getRoles(TRUE)); } /** * Tests the hasRole method. * * @covers ::hasRole */ public function testHasRole() { $this->assertTrue($this->users['user_one']->hasRole('role_one')); $this->assertFalse($this->users['user_two']->hasRole('no role')); $this->assertTrue($this->users['user_three']->hasRole(RoleInterface::AUTHENTICATED_ID)); $this->assertFalse($this->users['user_three']->hasRole(RoleInterface::ANONYMOUS_ID)); $this->assertTrue($this->users['user_last']->hasRole(RoleInterface::ANONYMOUS_ID)); } }