You are here

class PublishContentAccess in Publish Content 8

Access class checking the permissions for publishing and unpublishing.

Hierarchy

Expanded class hierarchy of PublishContentAccess

1 file declares its use of PublishContentAccess
publishcontent.module in ./publishcontent.module
Contains publishcontent.module.
1 string reference to 'PublishContentAccess'
publishcontent.services.yml in ./publishcontent.services.yml
publishcontent.services.yml
1 service uses PublishContentAccess
publishcontent.access in ./publishcontent.services.yml
Drupal\publishcontent\Access\PublishContentAccess

File

src/Access/PublishContentAccess.php, line 14

Namespace

Drupal\publishcontent\Access
View source
class PublishContentAccess implements AccessInterface {

  /**
   * The account performing the action.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $account;

  /**
   * The node that the action is performed on.
   *
   * @var \Drupal\node\NodeInterface
   */
  protected $node;

  /**
   * The arguments passed to the helper class.
   *
   * @var array
   */
  protected $arguments;

  /**
   * {@inheritdoc}
   */
  public function access(AccountInterface $account, NodeInterface $node) {
    $this->account = $account;
    $this->node = $node;
    $this->arguments = [
      '@type' => $node
        ->bundle(),
    ];
    $langcode = $node
      ->language()
      ->getId();

    // We don't want to show the action if the translation doesn't even exist.
    if ($node
      ->isTranslatable() && !$node
      ->hasTranslation($langcode)) {
      return AccessResult::forbidden();
    }
    $action = $node
      ->isPublished() ? 'unpublish' : 'publish';
    if ($action == 'publish' && ($this
      ->accessPublishAny() || $this
      ->accessPublishEditable() || $this
      ->accessPublishAnyType() || $this
      ->accessPublishOwnType() || $this
      ->accessPublishEditableType()) || $action == 'unpublish' && ($this
      ->accessUnpublishAny() || $this
      ->accessUnpublishEditable() || $this
      ->accessUnpublishAnyType() || $this
      ->accessUnpublishOwnType() || $this
      ->accessUnpublishEditableType())) {
      return AccessResult::allowed();
    }
    return AccessResult::forbidden();
  }

  /**
   * Helper method to check permission of user.
   */
  public function checkPermission($permission) {
    return $this->account
      ->hasPermission(Perm::getPermission($permission, $this->arguments));
  }

  /**
   * Access publish any content.
   */
  public function accessPublishAny() {
    return $this->account
      ->hasPermission('publish any content');
  }

  /**
   * Access unpublish any content.
   */
  public function accessUnpublishAny() {
    return $this->account
      ->hasPermission('unpublish any content');
  }

  /**
   * Access publish content which you have access to edit for.
   */
  public function accessPublishEditable() {
    return $this->account
      ->hasPermission('publish editable content') && $this->node
      ->access('update', $this->account);
  }

  /**
   * Access unpublish content which you have access to edit for.
   */
  public function accessUnpublishEditable() {
    return $this->account
      ->hasPermission('unpublish editable content') && $this->node
      ->access('update', $this->account);
  }

  /**
   * Access publish any content of specified type (bundle).
   */
  public function accessPublishAnyType() {
    return $this
      ->checkPermission(Perm::PUBLISH_ANY_TYPE);
  }

  /**
   * Access unpublish any content of specified type (bundle).
   */
  public function accessUnpublishAnyType() {
    return $this
      ->checkPermission(Perm::UNPUBLISH_ANY_TYPE);
  }

  /**
   * Access publish own content of specified type (bundle).
   */
  public function accessPublishOwnType() {
    return $this
      ->checkPermission(Perm::PUBLISH_OWN_TYPE) && $this->node
      ->getOwnerId() == $this->account
      ->id();
  }

  /**
   * Access unpublish own content of specified type (bundle).
   */
  public function accessUnpublishOwnType() {
    return $this
      ->checkPermission(Perm::UNPUBLISH_OWN_TYPE) && $this->node
      ->getOwnerId() == $this->account
      ->id();
  }

  /**
   * Access publish content of specified which you have access to edit for.
   */
  public function accessPublishEditableType() {
    return $this
      ->checkPermission(Perm::PUBLISH_EDITABLE_TYPE) && $this->node
      ->access('update', $this->account);
  }

  /**
   * Access unpublish content of specified which you have access to edit for.
   */
  public function accessUnpublishEditableType() {
    return $this
      ->checkPermission(Perm::UNPUBLISH_EDITABLE_TYPE) && $this->node
      ->access('update', $this->account);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PublishContentAccess::$account protected property The account performing the action.
PublishContentAccess::$arguments protected property The arguments passed to the helper class.
PublishContentAccess::$node protected property The node that the action is performed on.
PublishContentAccess::access public function
PublishContentAccess::accessPublishAny public function Access publish any content.
PublishContentAccess::accessPublishAnyType public function Access publish any content of specified type (bundle).
PublishContentAccess::accessPublishEditable public function Access publish content which you have access to edit for.
PublishContentAccess::accessPublishEditableType public function Access publish content of specified which you have access to edit for.
PublishContentAccess::accessPublishOwnType public function Access publish own content of specified type (bundle).
PublishContentAccess::accessUnpublishAny public function Access unpublish any content.
PublishContentAccess::accessUnpublishAnyType public function Access unpublish any content of specified type (bundle).
PublishContentAccess::accessUnpublishEditable public function Access unpublish content which you have access to edit for.
PublishContentAccess::accessUnpublishEditableType public function Access unpublish content of specified which you have access to edit for.
PublishContentAccess::accessUnpublishOwnType public function Access unpublish own content of specified type (bundle).
PublishContentAccess::checkPermission public function Helper method to check permission of user.