You are here

class Security in Bamboo Twig 8

Same name and namespace in other branches
  1. 8.5 bamboo_twig_security/src/TwigExtension/Security.php \Drupal\bamboo_twig_security\TwigExtension\Security
  2. 8.2 bamboo_twig_security/src/TwigExtension/Security.php \Drupal\bamboo_twig_security\TwigExtension\Security
  3. 8.3 bamboo_twig_security/src/TwigExtension/Security.php \Drupal\bamboo_twig_security\TwigExtension\Security
  4. 8.4 bamboo_twig_security/src/TwigExtension/Security.php \Drupal\bamboo_twig_security\TwigExtension\Security

Provides a 'Security' Twig Extensions.

Hierarchy

  • class \Drupal\bamboo_twig_security\TwigExtension\Security extends \Drupal\bamboo_twig_security\TwigExtension\Twig_Extension

Expanded class hierarchy of Security

1 string reference to 'Security'
bamboo_twig_security.services.yml in bamboo_twig_security/bamboo_twig_security.services.yml
bamboo_twig_security/bamboo_twig_security.services.yml
1 service uses Security
bamboo_twig_security.twig.security in bamboo_twig_security/bamboo_twig_security.services.yml
Drupal\bamboo_twig_security\TwigExtension\Security

File

bamboo_twig_security/src/TwigExtension/Security.php, line 11

Namespace

Drupal\bamboo_twig_security\TwigExtension
View source
class Security extends \Twig_Extension {

  /**
   * The account object.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * The storage handler class for users.
   *
   * @var \Drupal\user\UserStorage
   */
  protected $userStorage;

  /**
   * TwigExtension constructor class.
   *
   * @param \Drupal\Core\Session\AccountInterface $currentUser
   *   The current user.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity
   *   The Entity type manager service.
   */
  public function __construct(AccountInterface $currentUser, EntityTypeManagerInterface $entity) {
    $this->currentUser = $currentUser;
    $this->userStorage = $entity
      ->getStorage('user');
  }

  /**
   * List of all Twig functions.
   */
  public function getFunctions() {
    return [
      new \Twig_SimpleFunction('get_current_user', [
        $this,
        'getCurrentUser',
      ]),
      new \Twig_SimpleFunction('has_permission', [
        $this,
        'hasPermission',
      ]),
      new \Twig_SimpleFunction('has_role', [
        $this,
        'hasRole',
      ]),
    ];
  }

  /**
   * Unique identifier for this Twig extension.
   */
  public function getName() {
    return 'bamboo_twig.twig.security';
  }

  /**
   * Return the current user.
   *
   * @return \Drupal\Core\Session\AccountInterface|null
   *   The current user AccountInterface or NULL when anonymous.
   */
  public function getCurrentUser($user = NULL) {
    if ($this->currentUser
      ->isAnonymous()) {
      return NULL;
    }
    return $this->userStorage
      ->load($this->currentUser
      ->id());
  }

  /**
   * Does the current|given user has the given permission ?
   *
   * @param string $permission
   *   Drupal permission string.
   * @param int $user
   *   (Optional) user id to check permission. Otherwise current user is used.
   *
   * @return bool
   *   True if the current|given user has the given permission. Otherwise FALSE.
   */
  public function hasPermission($permission, $user = NULL) {
    if (is_null($user) && $this->currentUser
      ->isAnonymous()) {
      return NULL;
    }
    $user_id = $this->currentUser
      ->id();
    if (!is_null($user) && is_int($user)) {
      $user_id = $user;
    }
    $account = $this->userStorage
      ->load($user_id);
    if (!$account) {
      return NULL;
    }
    return $account
      ->hasPermission($permission);
  }

  /**
   * Does the current|given user has the given permission ?
   *
   * @param string $role
   *   Drupal role name.
   * @param int $user
   *   (Optional) user id to check permission. Otherwise current user is used.
   *
   * @return bool
   *   True if the current|given user has the given permission. Otherwise FALSE.
   */
  public function hasRole($role, $user = NULL) {
    if (is_null($user) && $this->currentUser
      ->isAnonymous()) {
      return NULL;
    }
    $user_id = $this->currentUser
      ->id();
    if (!is_null($user) && is_int($user)) {
      $user_id = $user;
    }
    $account = $this->userStorage
      ->load($user_id);
    if (!$account) {
      return NULL;
    }
    return $account
      ->hasRole($role);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Security::$currentUser protected property The account object.
Security::$userStorage protected property The storage handler class for users.
Security::getCurrentUser public function Return the current user.
Security::getFunctions public function List of all Twig functions.
Security::getName public function Unique identifier for this Twig extension.
Security::hasPermission public function Does the current|given user has the given permission ?
Security::hasRole public function Does the current|given user has the given permission ?
Security::__construct public function TwigExtension constructor class.