Repository URL to install this package:
|
Version:
2.0.2 ▾
|
<?php
namespace Drupal\custom_forms;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Session\AccountInterface;
/**
* Defines the access control handler for the custom form entity type.
*/
class CustomFormHandlerAccessControlHandler extends EntityAccessControlHandler {
/**
* {@inheritdoc}
*/
public function access(EntityInterface $entity, $operation, AccountInterface $account = NULL, $return_as_object = FALSE) {
$account = $this->prepareUser($account);
if ($account->hasPermission('bypass custom form access')) {
$result = AccessResult::allowed()->cachePerPermissions();
return $return_as_object ? $result : $result->isAllowed();
}
if (!$account->hasPermission('view published custom forms')) {
$result = AccessResult::forbidden("The 'view published custom forms' permission is required.")->cachePerPermissions();
return $return_as_object ? $result : $result->isAllowed();
}
$result = parent::access($entity, $operation, $account, TRUE)->cachePerPermissions();
return $return_as_object ? $result : $result->isAllowed();
}
/**
* {@inheritdoc}
*/
public function createAccess($entity_bundle = NULL, AccountInterface $account = NULL, array $context = [], $return_as_object = FALSE) {
$account = $this->prepareUser($account);
if ($account->hasPermission('bypass custom form access')) {
$result = AccessResult::allowed()->cachePerPermissions();
return $return_as_object ? $result : $result->isAllowed();
}
if (!$account->hasPermission('view published custom forms')) {
$result = AccessResult::forbidden("The 'view published custom forms' permission is required.")->cachePerPermissions();
return $return_as_object ? $result : $result->isAllowed();
}
$result = parent::access($entity_bundle, $account, $context, TRUE)->cachePerPermissions();
return $return_as_object ? $result : $result->isAllowed();
}
/**
* {@inheritdoc}
*/
protected function checkAccess(EntityInterface $custom_form, $operation, AccountInterface $account) {
/** @var \Drupal\custom_forms\CustomFormInterface $custom_form */
if ($account->hasPermission('bypass custom form access')) {
return AccessResult::allowed()->cachePerPermissions();
}
// Fetch information from the custom form object if possible.
$status = $custom_form->isPublished();
$uid = $custom_form->getOwnerId();
// Check if authors can view their own unpublished custom forms.
if ($operation === 'view' && !$status && $account->hasPermission('view own unpublished custom forms') && $account->isAuthenticated() && $account->id() == $uid) {
return AccessResult::allowed()->cachePerPermissions()->cachePerUser()->addCacheableDependency($custom_form);
}
switch ($operation) {
case 'view':
return AccessResult::allowedIfHasPermission($account, 'view published custom forms');
case 'admin_view':
return AccessResult::allowedIfHasPermissions($account, ['view custom form', 'edit custom form'], 'AND');
case 'update':
return AccessResult::allowedIfHasPermissions($account, ['edit custom form', 'administer custom form'], 'OR');
case 'delete':
return AccessResult::allowedIfHasPermissions($account, ['delete custom form', 'administer custom form'], 'OR');
default:
// No opinion.
return AccessResult::neutral();
}
}
/**
* {@inheritdoc}
*/
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
return AccessResult::allowedIf($account->hasPermission('create ' . $entity_bundle . ' custom form'))->cachePerPermissions();
}
/**
* {@inheritdoc}
*/
protected function checkFieldAccess($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
// Only users with the administer custom forms permission can edit
// administrative fields.
$administrative_fields = ['uid', 'status', 'created', 'promote', 'sticky'];
if ($operation == 'edit' && in_array($field_definition->getName(), $administrative_fields, TRUE)) {
return AccessResult::allowedIfHasPermission($account, 'administer custom forms');
}
// No user can change read only fields.
$read_only_fields = ['revision_timestamp', 'revision_uid'];
if ($operation == 'edit' && in_array($field_definition->getName(), $read_only_fields, TRUE)) {
return AccessResult::forbidden();
}
// Users have access to the revision_log field either if they have
// administrative permissions or if the new revision option is enabled.
if ($operation == 'edit' && $field_definition->getName() == 'revision_log') {
if ($account->hasPermission('administer custom forms')) {
return AccessResult::allowed()->cachePerPermissions();
}
return AccessResult::allowedIf($items->getEntity()->type->entity->isNewRevision())->cachePerPermissions();
}
return parent::checkFieldAccess($operation, $field_definition, $account, $items);
}
}