You are here

class GenericGuard in State Machine 8

Hierarchy

Expanded class hierarchy of GenericGuard

1 string reference to 'GenericGuard'
state_machine_test.services.yml in tests/modules/state_machine_test/state_machine_test.services.yml
tests/modules/state_machine_test/state_machine_test.services.yml
1 service uses GenericGuard
state_machine_test.generic_guard in tests/modules/state_machine_test/state_machine_test.services.yml
Drupal\state_machine_test\Guard\GenericGuard

File

tests/modules/state_machine_test/src/Guard/GenericGuard.php, line 11

Namespace

Drupal\state_machine_test\Guard
View source
class GenericGuard implements GuardInterface {

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * Constructs a new GenericGuard object.
   *
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   */
  public function __construct(AccountInterface $current_user) {
    $this->currentUser = $current_user;
  }

  /**
   * {@inheritdoc}
   */
  public function allowed(WorkflowTransition $transition, WorkflowInterface $workflow, EntityInterface $entity) {

    // Only a "supervisor" user can cancel an entity in validation.
    if ($transition
      ->getId() == 'cancel' && $entity->field_state
      ->first()->value == 'validation') {
      return (bool) array_intersect([
        'merchant',
      ], $this->currentUser
        ->getRoles());
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GenericGuard::$currentUser protected property The current user.
GenericGuard::allowed public function Checks whether the given transition is allowed. Overrides GuardInterface::allowed
GenericGuard::__construct public function Constructs a new GenericGuard object.