WorkspaceAccessControlHandler.php in Drupal 10
File
core/modules/workspaces/src/WorkspaceAccessControlHandler.php
View source
<?php
namespace Drupal\workspaces;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
class WorkspaceAccessControlHandler extends EntityAccessControlHandler {
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
if ($operation === 'publish' && $entity
->hasParent()) {
$message = $this
->t('Only top-level workspaces can be published.');
return AccessResult::forbidden((string) $message)
->addCacheableDependency($entity);
}
if ($account
->hasPermission('administer workspaces')) {
return AccessResult::allowed()
->cachePerPermissions();
}
$permission_operation = $operation === 'update' || $operation === 'publish' ? 'edit' : $operation;
$access_result = AccessResult::allowedIfHasPermission($account, $permission_operation . ' any workspace');
if ($access_result
->isNeutral() && $account
->isAuthenticated() && $account
->id() === $entity
->getOwnerId()) {
$access_result = AccessResult::allowedIfHasPermission($account, $permission_operation . ' own workspace')
->cachePerUser()
->addCacheableDependency($entity);
}
return $access_result;
}
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
return AccessResult::allowedIfHasPermissions($account, [
'administer workspaces',
'create workspace',
], 'OR');
}
}