You are here

public function ScheduledTransitionsEntityHooks::entityAccess in Scheduled Transitions 8

Same name and namespace in other branches
  1. 2.x src/ScheduledTransitionsEntityHooks.php \Drupal\scheduled_transitions\ScheduledTransitionsEntityHooks::entityAccess()

Implements hook_entity_access().

See also

\scheduled_transitions_entity_access()

File

src/ScheduledTransitionsEntityHooks.php, line 142

Class

ScheduledTransitionsEntityHooks
Entity related hooks for Scheduled Transitions module.

Namespace

Drupal\scheduled_transitions

Code

public function entityAccess(EntityInterface $entity, string $operation, AccountInterface $account) : AccessResultInterface {

  // Determines if a user has access to view or add scheduled transitions for
  // an entity. Users must always have the entity:bundle permission. If the
  // mirror operation config is enabled then we allow via that. Otherwise if
  // the mirror operation is off some custom code provided by the site must
  // respond with allowed for
  // ScheduledTransitionsPermissions::ENTITY_OPERATION* operations.
  $access = AccessResult::neutral();
  if ($operation === Permissions::ENTITY_OPERATION_VIEW_TRANSITIONS) {
    $access
      ->cachePerPermissions();
    $permission = Permissions::viewScheduledTransitionsPermission($entity
      ->getEntityTypeId(), $entity
      ->bundle());
    if ($account
      ->hasPermission($permission)) {
      $access
        ->addCacheTags([
        SettingsForm::SETTINGS_TAG,
      ]);
      $mirrorOperation = $this
        ->mirrorOperations('view scheduled transition');
      if (isset($mirrorOperation)) {
        $access = $access
          ->orIf($entity
          ->access($mirrorOperation, $account, TRUE));
      }
    }
    else {
      $access = $access
        ->andIf(AccessResult::forbidden("The '{$permission}' permission is required."));
    }
  }
  elseif ($operation === Permissions::ENTITY_OPERATION_ADD_TRANSITION) {
    $access
      ->cachePerPermissions();
    $permission = Permissions::addScheduledTransitionsPermission($entity
      ->getEntityTypeId(), $entity
      ->bundle());
    if ($account
      ->hasPermission($permission)) {
      $access
        ->addCacheTags([
        SettingsForm::SETTINGS_TAG,
      ]);
      $mirrorOperation = $this
        ->mirrorOperations('add scheduled transition');
      if (isset($mirrorOperation)) {
        $access = $access
          ->orIf($entity
          ->access($mirrorOperation, $account, TRUE));
      }
    }
    else {
      $access = $access
        ->andIf(AccessResult::forbidden("The '{$permission}' permission is required."));
    }
  }
  if ($operation === Permissions::ENTITY_OPERATION_RESCHEDULE_TRANSITIONS) {
    $access
      ->cachePerPermissions();
    $permission = Permissions::rescheduleScheduledTransitionsPermission($entity
      ->getEntityTypeId(), $entity
      ->bundle());
    if ($account
      ->hasPermission($permission)) {
      $access
        ->addCacheTags([
        SettingsForm::SETTINGS_TAG,
      ]);
      $mirrorOperation = $this
        ->mirrorOperations('reschedule scheduled transitions');
      if (isset($mirrorOperation)) {
        $access = $access
          ->orIf($entity
          ->access($mirrorOperation, $account, TRUE));
      }
    }
    else {
      $access = $access
        ->andIf(AccessResult::forbidden("The '{$permission}' permission is required."));
    }
  }
  return $access;
}