3 namespace Drupal\workspaces;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
7 use Drupal\Core\Entity\EntityInterface;
8 use Drupal\Core\Entity\EntityTypeManagerInterface;
9 use Drupal\Core\Session\AccountInterface;
10 use Drupal\Core\StringTranslation\StringTranslationTrait;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Service wrapper for hooks relating to entity access control.
18 class EntityAccess implements ContainerInjectionInterface {
20 use StringTranslationTrait;
23 * The entity type manager service.
25 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
27 protected $entityTypeManager;
30 * The workspace manager service.
32 * @var \Drupal\workspaces\WorkspaceManagerInterface
34 protected $workspaceManager;
37 * Constructs a new EntityAccess instance.
39 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
40 * The entity type manager service.
41 * @param \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager
42 * The workspace manager service.
44 public function __construct(EntityTypeManagerInterface $entity_type_manager, WorkspaceManagerInterface $workspace_manager) {
45 $this->entityTypeManager = $entity_type_manager;
46 $this->workspaceManager = $workspace_manager;
52 public static function create(ContainerInterface $container) {
54 $container->get('entity_type.manager'),
55 $container->get('workspaces.manager')
60 * Implements a hook bridge for hook_entity_access().
62 * @param \Drupal\Core\Entity\EntityInterface $entity
63 * The entity to check access for.
64 * @param string $operation
65 * The operation being performed.
66 * @param \Drupal\Core\Session\AccountInterface $account
67 * The user account making the to check access for.
69 * @return \Drupal\Core\Access\AccessResult
70 * The result of the access check.
72 * @see hook_entity_access()
74 public function entityOperationAccess(EntityInterface $entity, $operation, AccountInterface $account) {
75 // Workspaces themselves are handled by their own access handler and we
76 // should not try to do any access checks for entity types that can not
77 // belong to a workspace.
78 if ($entity->getEntityTypeId() === 'workspace' || !$this->workspaceManager->isEntityTypeSupported($entity->getEntityType())) {
79 return AccessResult::neutral();
82 return $this->bypassAccessResult($account);
86 * Implements a hook bridge for hook_entity_create_access().
88 * @param \Drupal\Core\Session\AccountInterface $account
89 * The user account making the to check access for.
90 * @param array $context
91 * The context of the access check.
92 * @param string $entity_bundle
93 * The bundle of the entity.
95 * @return \Drupal\Core\Access\AccessResult
96 * The result of the access check.
98 * @see hook_entity_create_access()
100 public function entityCreateAccess(AccountInterface $account, array $context, $entity_bundle) {
101 // Workspaces themselves are handled by their own access handler and we
102 // should not try to do any access checks for entity types that can not
103 // belong to a workspace.
104 $entity_type = $this->entityTypeManager->getDefinition($context['entity_type_id']);
105 if ($entity_type->id() === 'workspace' || !$this->workspaceManager->isEntityTypeSupported($entity_type)) {
106 return AccessResult::neutral();
109 return $this->bypassAccessResult($account);
113 * Checks the 'bypass' permissions.
115 * @param \Drupal\Core\Session\AccountInterface $account
116 * The user account making the to check access for.
118 * @return \Drupal\Core\Access\AccessResult
119 * The result of the access check.
121 protected function bypassAccessResult(AccountInterface $account) {
122 // This approach assumes that the current "global" active workspace is
123 // correct, i.e. if you're "in" a given workspace then you get ALL THE PERMS
124 // to ALL THE THINGS! That's why this is a dangerous permission.
125 $active_workspace = $this->workspaceManager->getActiveWorkspace();
127 return AccessResult::allowedIf($active_workspace->getOwnerId() == $account->id())->cachePerUser()->addCacheableDependency($active_workspace)
128 ->andIf(AccessResult::allowedIfHasPermission($account, 'bypass entity access own workspace'));