You are here

BoardAccessControlHandler.php in Drupal PM (Project Management) 4.x

Namespace

Drupal\pm_board

File

modules/pm_board/src/BoardAccessControlHandler.php
View source
<?php

namespace Drupal\pm_board;

use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;

/**
 * Access controller for the Board entity.
 *
 * @see \Drupal\pm_board\Entity\Board.
 */
class BoardAccessControlHandler extends EntityAccessControlHandler {

  /**
   * {@inheritdoc}
   */
  protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {

    /** @var \Drupal\pm_board\Entity\BoardInterface $entity */
    switch ($operation) {
      case 'view':
        $permission = $this
          ->checkOwn($entity, $operation, $account);
        if (!empty($permission)) {
          return AccessResult::allowed();
        }
        return AccessResult::allowedIfHasPermission($account, 'view board entities');
      case 'update':
        $permission = $this
          ->checkOwn($entity, $operation, $account);
        if (!empty($permission)) {
          return AccessResult::allowed();
        }
        return AccessResult::allowedIfHasPermission($account, 'edit board entities');
      case 'delete':
        $permission = $this
          ->checkOwn($entity, $operation, $account);
        if (!empty($permission)) {
          return AccessResult::allowed();
        }
        return AccessResult::allowedIfHasPermission($account, 'delete board entities');
    }

    // Unknown operation, no opinion.
    return AccessResult::neutral();
  }

  /**
   * {@inheritdoc}
   */
  protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
    return AccessResult::allowedIfHasPermission($account, 'add board entities');
  }

  /**
   * Test for given 'own' permission.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity.
   * @param string $operation
   *   The entity operation that needs to be performed.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The user account.
   *
   * @return string|null
   *   The permission string indicating it's allowed.
   */
  protected function checkOwn(EntityInterface $entity, string $operation, AccountInterface $account) {
    $uid = $entity
      ->getOwnerId();
    $is_own = $account
      ->isAuthenticated() && $account
      ->id() == $uid;
    if (!$is_own) {
      return NULL;
    }
    $bundle = $entity
      ->bundle();
    $ops = [
      'create' => 'pm_board %bundle add own %bundle entities',
      'view' => 'pm_board %bundle view own entities',
      'update' => 'pm_board %bundle edit own entities',
      'delete' => 'pm_board %bundle delete own entities',
    ];
    $permission = strtr($ops[$operation], [
      '%bundle' => $bundle,
    ]);
    if ($account
      ->hasPermission($permission)) {
      return $permission;
    }
    return NULL;
  }

}

Classes

Namesort descending Description
BoardAccessControlHandler Access controller for the Board entity.