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 Workflow entity.
17 * @see \Drupal\workflows\Entity\Workflow.
19 class WorkflowAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface {
22 * The workflow type plugin manager.
24 * @var \Drupal\Component\Plugin\PluginManagerInterface
26 protected $workflowTypeManager;
31 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
34 $container->get('plugin.manager.workflows.type')
39 * Constructs the workflow access control handler instance.
41 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
42 * The entity type definition.
43 * @param \Drupal\Component\Plugin\PluginManagerInterface $workflow_type_manager
44 * The workflow type plugin manager.
46 public function __construct(EntityTypeInterface $entity_type, PluginManagerInterface $workflow_type_manager) {
47 parent::__construct($entity_type);
48 $this->workflowTypeManager = $workflow_type_manager;
54 protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
55 /** @var \Drupal\workflows\Entity\Workflow $entity */
56 $workflow_type = $entity->getTypePlugin();
57 if (strpos($operation, 'delete-state') === 0) {
58 list(, $state_id) = explode(':', $operation, 2);
59 // Deleting a state is editing a workflow, but also we should forbid
60 // access if there is only one state.
61 return AccessResult::allowedIf(count($entity->getTypePlugin()->getStates()) > 1)
62 ->andIf(parent::checkAccess($entity, 'edit', $account))
63 ->andIf(AccessResult::allowedIf(!in_array($state_id, $workflow_type->getRequiredStates(), TRUE)))
64 ->addCacheableDependency($entity);
67 return parent::checkAccess($entity, $operation, $account);
73 protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
74 $workflow_types_count = count($this->workflowTypeManager->getDefinitions());
75 $admin_access = parent::checkCreateAccess($account, $context, $entity_bundle);
76 // Allow access if there is at least one workflow type. Since workflow types
77 // are provided by modules this is cacheable until extensions change.
79 ->andIf(AccessResult::allowedIf($workflow_types_count > 0))
80 ->addCacheTags(['workflow_type_plugins']);