You are here

class EditContentCheck in Workbench Moderation State Access 8

Provides an access checker for editing content in WBM states.

Hierarchy

Expanded class hierarchy of EditContentCheck

1 string reference to 'EditContentCheck'
workbench_moderation_state_access.services.yml in ./workbench_moderation_state_access.services.yml
workbench_moderation_state_access.services.yml
1 service uses EditContentCheck
access_check.edit_content in ./workbench_moderation_state_access.services.yml
Drupal\workbench_moderation_state_access\Access\EditContentCheck

File

src/Access/EditContentCheck.php, line 20
Contains \Drupal\workbench_moderation_state_access\Access\EditContentCheck.

Namespace

Drupal\workbench_moderation_state_access\Access
View source
class EditContentCheck extends EntityAccessCheck {

  /**
   * The moderation information service.
   *
   * @var \Drupal\workbench_moderation\ModerationInformationInterface
   */
  protected $moderationInfo;

  /**
   * Constructs a new EditContentCheck.
   *
   * @param \Drupal\workbench_moderation\ModerationInformationInterface $moderation_information
   *   The moderation information service.
   */
  public function __construct(ModerationInformationInterface $moderation_information) {
    $this->moderationInfo = $moderation_information;
  }

  /**
   * Checks that the user has the edit permissions for the state of the content.
   *
   * This checker assumes the presence of an '_entity_access' requirement key
   * in the same form as used by EntityAccessCheck.
   *
   * @param \Symfony\Component\Routing\Route $route
   *   The route to check against.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The parametrized route.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   *
   * @see EntityAccessCheck
   */
  public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {

    // Split the entity type and the operation.
    $requirement = $route
      ->getRequirement('_entity_access');
    list($entity_type, $operation) = explode('.', $requirement);

    // Only act on update operations.
    if ($operation != 'update') {
      return parent::access($route, $route_match, $account);
    }

    // If there is valid entity of the given entity type, check its access.
    $parameters = $route_match
      ->getParameters();
    if ($parameters
      ->has($entity_type)) {
      $entity = $parameters
        ->get($entity_type);
      if ($this->moderationInfo
        ->isModeratableEntity($entity)) {
        $current_state = $entity->moderation_state->target_id;
        if (!$account
          ->hasPermission('edit content in the ' . $current_state . ' state')) {
          return AccessResult::forbidden()
            ->addCacheableDependency($entity);
        }
      }
    }

    // No opinion, so other access checks should decide if access should be
    // allowed or not.
    return parent::access($route, $route_match, $account);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EditContentCheck::$moderationInfo protected property The moderation information service.
EditContentCheck::access public function Checks that the user has the edit permissions for the state of the content. Overrides EntityAccessCheck::access
EditContentCheck::__construct public function Constructs a new EditContentCheck.