Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/workflows/src/WorkflowAccessControlHandler.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
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 Workflow entity. | |
16 * | |
17 * @see \Drupal\workflows\Entity\Workflow. | |
18 */ | |
19 class WorkflowAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface { | |
20 | |
21 /** | |
22 * The workflow type plugin manager. | |
23 * | |
24 * @var \Drupal\Component\Plugin\PluginManagerInterface | |
25 */ | |
26 protected $workflowTypeManager; | |
27 | |
28 /** | |
29 * {@inheritdoc} | |
30 */ | |
31 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { | |
32 return new static( | |
33 $entity_type, | |
34 $container->get('plugin.manager.workflows.type') | |
35 ); | |
36 } | |
37 | |
38 /** | |
39 * Constructs the workflow access control handler instance. | |
40 * | |
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. | |
45 */ | |
46 public function __construct(EntityTypeInterface $entity_type, PluginManagerInterface $workflow_type_manager) { | |
47 parent::__construct($entity_type); | |
48 $this->workflowTypeManager = $workflow_type_manager; | |
49 } | |
50 | |
51 /** | |
52 * {@inheritdoc} | |
53 */ | |
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); | |
65 } | |
66 | |
67 return parent::checkAccess($entity, $operation, $account); | |
68 } | |
69 | |
70 /** | |
71 * {@inheritdoc} | |
72 */ | |
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. | |
78 return $admin_access | |
79 ->andIf(AccessResult::allowedIf($workflow_types_count > 0)) | |
80 ->addCacheTags(['workflow_type_plugins']); | |
81 } | |
82 | |
83 } |