3 namespace Drupal\workflows;
5 use Drupal\Component\Plugin\PluginManagerInterface;
6 use Drupal\Core\Entity\EntityAccessControlHandler;
7 use Drupal\Core\Entity\EntityHandlerInterface;
8 use Drupal\Core\Entity\EntityInterface;
9 use Drupal\Core\Entity\EntityTypeInterface;
10 use Drupal\Core\Session\AccountInterface;
11 use Drupal\Core\Access\AccessResult;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
15 * Access controller for the Moderation State entity.
17 * @see \Drupal\workflows\Entity\Workflow.
20 * The workflow system is currently experimental and should only be leveraged
21 * by experimental modules and development releases of contributed modules.
23 class WorkflowAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface {
26 * The workflow type plugin manager.
28 * @var \Drupal\Component\Plugin\PluginManagerInterface
30 protected $workflowTypeManager;
35 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
38 $container->get('plugin.manager.workflows.type')
43 * Constructs the workflow access control handler instance.
45 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
46 * The entity type definition.
47 * @param \Drupal\Component\Plugin\PluginManagerInterface $workflow_type_manager
48 * The workflow type plugin manager.
50 public function __construct(EntityTypeInterface $entity_type, PluginManagerInterface $workflow_type_manager) {
51 parent::__construct($entity_type);
52 $this->workflowTypeManager = $workflow_type_manager;
58 protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
59 /** @var \Drupal\workflows\Entity\Workflow $entity */
60 $workflow_type = $entity->getTypePlugin();
61 if (strpos($operation, 'delete-state') === 0) {
62 list(, $state_id) = explode(':', $operation, 2);
63 // Deleting a state is editing a workflow, but also we should forbid
64 // access if there is only one state.
65 $admin_access = AccessResult::allowedIf(count($entity->getStates()) > 1)
66 ->andIf(parent::checkAccess($entity, 'edit', $account))
67 ->andIf(AccessResult::allowedIf(!in_array($state_id, $workflow_type->getRequiredStates(), TRUE)))
68 ->addCacheableDependency($entity);
71 $admin_access = parent::checkAccess($entity, $operation, $account);
73 return $workflow_type->checkWorkflowAccess($entity, $operation, $account)->orIf($admin_access);
79 protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
80 $workflow_types_count = count($this->workflowTypeManager->getDefinitions());
81 $admin_access = parent::checkCreateAccess($account, $context, $entity_bundle);
82 // Allow access if there is at least one workflow type. Since workflow types
83 // are provided by modules this is cacheable until extensions change.
84 return $admin_access->andIf(AccessResult::allowedIf($workflow_types_count > 0))->addCacheTags(['config:core.extension']);