You are here

public function WorkflowTransition::isAllowed in Workflow 8

Determines if the current transition between 2 states is allowed.

This is checked in the following locations:

  • in settings;
  • in permissions;
  • by permission hooks, implemented by other modules.

Parameters

\Drupal\user\UserInterface $user: The user to act upon. May have the custom WORKFLOW_ROLE_AUTHOR_RID role.

bool $force: Indicates if the transition must be forced(E.g., by cron, rules).

Return value

bool TRUE if OK, else FALSE.

Overrides WorkflowConfigTransitionInterface::isAllowed

1 call to WorkflowTransition::isAllowed()
WorkflowTransition::execute in src/Entity/WorkflowTransition.php
Execute a transition (change state of an entity).

File

src/Entity/WorkflowTransition.php, line 376

Class

WorkflowTransition
Implements an actual, executed, Transition.

Namespace

Drupal\workflow\Entity

Code

public function isAllowed(UserInterface $user, $force = FALSE) {
  if ($force) {

    // $force allows Rules to cause transition.
    return TRUE;
  }

  // @todo Keep below code aligned between WorkflowState, ~Transition, ~HistoryAccess

  /*
   * Get  user's permissions.
   */
  $type_id = $this
    ->getWorkflowId();
  if ($user
    ->hasPermission("bypass {$type_id} workflow_transition access")) {

    // Superuser is special (might be cron).
    // And $force allows Rules to cause transition.
    return TRUE;
  }

  // Determine if user is owner of the entity.
  $is_owner = WorkflowManager::isOwner($user, $this
    ->getTargetEntity());
  if ($is_owner) {
    $user
      ->addRole(WORKFLOW_ROLE_AUTHOR_RID);
  }

  /*
   * Get the object and its permissions.
   */

  /** @var \Drupal\workflow\Entity\WorkflowTransitionInterface[] $config_transitions */
  $config_transitions = $this
    ->getWorkflow()
    ->getTransitionsByStateId($this
    ->getFromSid(), $this
    ->getToSid());

  /*
   * Determine if user has Access.
   */
  $result = FALSE;
  foreach ($config_transitions as $config_transition) {
    $result = $result || $config_transition
      ->isAllowed($user, $force);
  }
  if ($result == FALSE) {

    // @todo There is a watchdog error, but no UI-error. Is this OK?
    $message = $this
      ->t('Attempt to go to nonexistent transition (from %sid1 to %sid2)');
    $this
      ->logError($message);
  }
  return $result;
}