Verified Commit 419b4920 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2991232 by jhedstrom, Matroskeen, dagmar, Medha Kumari, longwave, xjm,...

Issue #2991232 by jhedstrom, Matroskeen, dagmar, Medha Kumari, longwave, xjm, Berdir, wturrell, andypost, alexpott, apaderno, catch: Add hasRole() method to AccountProxy and UserSession classes
parent b25f20ba
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -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}
   */
+16 −0
Original line number Diff line number Diff line
@@ -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}
   */
+3 −0
Original line number Diff line number Diff line
@@ -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);

+18 −0
Original line number Diff line number Diff line
@@ -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;
@@ -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));
  }

}
+13 −0
Original line number Diff line number Diff line
@@ -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));
  }

}