Version 1
[yaffs-website] / web / core / modules / workflows / src / WorkflowAccessControlHandler.php
1 <?php
2
3 namespace Drupal\workflows;
4
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;
13
14 /**
15  * Access controller for the Moderation State entity.
16  *
17  * @see \Drupal\workflows\Entity\Workflow.
18  *
19  * @internal
20  *   The workflow system is currently experimental and should only be leveraged
21  *   by experimental modules and development releases of contributed modules.
22  */
23 class WorkflowAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface {
24
25   /**
26    * The workflow type plugin manager.
27    *
28    * @var \Drupal\Component\Plugin\PluginManagerInterface
29    */
30   protected $workflowTypeManager;
31
32   /**
33    * {@inheritdoc}
34    */
35   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
36     return new static(
37       $entity_type,
38       $container->get('plugin.manager.workflows.type')
39     );
40   }
41
42   /**
43    * Constructs the workflow access control handler instance.
44    *
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.
49    */
50   public function __construct(EntityTypeInterface $entity_type, PluginManagerInterface $workflow_type_manager) {
51     parent::__construct($entity_type);
52     $this->workflowTypeManager = $workflow_type_manager;
53   }
54
55   /**
56    * {@inheritdoc}
57    */
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);
69     }
70     else {
71       $admin_access = parent::checkAccess($entity, $operation, $account);
72     }
73     return $workflow_type->checkWorkflowAccess($entity, $operation, $account)->orIf($admin_access);
74   }
75
76   /**
77    * {@inheritdoc}
78    */
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']);
85   }
86
87 }