PublishContentAccess.php in Publish Content 8
File
src/Access/PublishContentAccess.php
View source
<?php
namespace Drupal\publishcontent\Access;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Routing\Access\AccessInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\NodeInterface;
use Drupal\publishcontent\PublishContentPermissions as Perm;
class PublishContentAccess implements AccessInterface {
protected $account;
protected $node;
protected $arguments;
public function access(AccountInterface $account, NodeInterface $node) {
$this->account = $account;
$this->node = $node;
$this->arguments = [
'@type' => $node
->bundle(),
];
$langcode = $node
->language()
->getId();
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();
}
public function checkPermission($permission) {
return $this->account
->hasPermission(Perm::getPermission($permission, $this->arguments));
}
public function accessPublishAny() {
return $this->account
->hasPermission('publish any content');
}
public function accessUnpublishAny() {
return $this->account
->hasPermission('unpublish any content');
}
public function accessPublishEditable() {
return $this->account
->hasPermission('publish editable content') && $this->node
->access('update', $this->account);
}
public function accessUnpublishEditable() {
return $this->account
->hasPermission('unpublish editable content') && $this->node
->access('update', $this->account);
}
public function accessPublishAnyType() {
return $this
->checkPermission(Perm::PUBLISH_ANY_TYPE);
}
public function accessUnpublishAnyType() {
return $this
->checkPermission(Perm::UNPUBLISH_ANY_TYPE);
}
public function accessPublishOwnType() {
return $this
->checkPermission(Perm::PUBLISH_OWN_TYPE) && $this->node
->getOwnerId() == $this->account
->id();
}
public function accessUnpublishOwnType() {
return $this
->checkPermission(Perm::UNPUBLISH_OWN_TYPE) && $this->node
->getOwnerId() == $this->account
->id();
}
public function accessPublishEditableType() {
return $this
->checkPermission(Perm::PUBLISH_EDITABLE_TYPE) && $this->node
->access('update', $this->account);
}
public function accessUnpublishEditableType() {
return $this
->checkPermission(Perm::UNPUBLISH_EDITABLE_TYPE) && $this->node
->access('update', $this->account);
}
}