Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\workflows;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Plugin\PluginManagerInterface;
|
Chris@0
|
6 use Drupal\Core\Entity\EntityAccessControlHandler;
|
Chris@0
|
7 use Drupal\Core\Entity\EntityHandlerInterface;
|
Chris@0
|
8 use Drupal\Core\Entity\EntityInterface;
|
Chris@0
|
9 use Drupal\Core\Entity\EntityTypeInterface;
|
Chris@0
|
10 use Drupal\Core\Session\AccountInterface;
|
Chris@0
|
11 use Drupal\Core\Access\AccessResult;
|
Chris@0
|
12 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * Access controller for the Workflow entity.
|
Chris@0
|
16 *
|
Chris@0
|
17 * @see \Drupal\workflows\Entity\Workflow.
|
Chris@0
|
18 */
|
Chris@0
|
19 class WorkflowAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface {
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * The workflow type plugin manager.
|
Chris@0
|
23 *
|
Chris@0
|
24 * @var \Drupal\Component\Plugin\PluginManagerInterface
|
Chris@0
|
25 */
|
Chris@0
|
26 protected $workflowTypeManager;
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * {@inheritdoc}
|
Chris@0
|
30 */
|
Chris@0
|
31 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
|
Chris@0
|
32 return new static(
|
Chris@0
|
33 $entity_type,
|
Chris@0
|
34 $container->get('plugin.manager.workflows.type')
|
Chris@0
|
35 );
|
Chris@0
|
36 }
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * Constructs the workflow access control handler instance.
|
Chris@0
|
40 *
|
Chris@0
|
41 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
|
Chris@0
|
42 * The entity type definition.
|
Chris@0
|
43 * @param \Drupal\Component\Plugin\PluginManagerInterface $workflow_type_manager
|
Chris@0
|
44 * The workflow type plugin manager.
|
Chris@0
|
45 */
|
Chris@0
|
46 public function __construct(EntityTypeInterface $entity_type, PluginManagerInterface $workflow_type_manager) {
|
Chris@0
|
47 parent::__construct($entity_type);
|
Chris@0
|
48 $this->workflowTypeManager = $workflow_type_manager;
|
Chris@0
|
49 }
|
Chris@0
|
50
|
Chris@0
|
51 /**
|
Chris@0
|
52 * {@inheritdoc}
|
Chris@0
|
53 */
|
Chris@0
|
54 protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
|
Chris@0
|
55 /** @var \Drupal\workflows\Entity\Workflow $entity */
|
Chris@0
|
56 $workflow_type = $entity->getTypePlugin();
|
Chris@0
|
57 if (strpos($operation, 'delete-state') === 0) {
|
Chris@0
|
58 list(, $state_id) = explode(':', $operation, 2);
|
Chris@0
|
59 // Deleting a state is editing a workflow, but also we should forbid
|
Chris@0
|
60 // access if there is only one state.
|
Chris@0
|
61 return AccessResult::allowedIf(count($entity->getTypePlugin()->getStates()) > 1)
|
Chris@0
|
62 ->andIf(parent::checkAccess($entity, 'edit', $account))
|
Chris@0
|
63 ->andIf(AccessResult::allowedIf(!in_array($state_id, $workflow_type->getRequiredStates(), TRUE)))
|
Chris@0
|
64 ->addCacheableDependency($entity);
|
Chris@0
|
65 }
|
Chris@0
|
66
|
Chris@0
|
67 return parent::checkAccess($entity, $operation, $account);
|
Chris@0
|
68 }
|
Chris@0
|
69
|
Chris@0
|
70 /**
|
Chris@0
|
71 * {@inheritdoc}
|
Chris@0
|
72 */
|
Chris@0
|
73 protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
|
Chris@0
|
74 $workflow_types_count = count($this->workflowTypeManager->getDefinitions());
|
Chris@0
|
75 $admin_access = parent::checkCreateAccess($account, $context, $entity_bundle);
|
Chris@0
|
76 // Allow access if there is at least one workflow type. Since workflow types
|
Chris@0
|
77 // are provided by modules this is cacheable until extensions change.
|
Chris@0
|
78 return $admin_access
|
Chris@0
|
79 ->andIf(AccessResult::allowedIf($workflow_types_count > 0))
|
Chris@0
|
80 ->addCacheTags(['workflow_type_plugins']);
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 }
|