-
Notifications
You must be signed in to change notification settings - Fork 1
/
islandora_hierarchical_access.module
122 lines (111 loc) · 4.63 KB
/
islandora_hierarchical_access.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
* @file
* General module hook implementations.
*/
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Database\Query\AlterableInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Sql\SqlEntityStorageInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\islandora_hierarchical_access\EntityAccessHandler;
use Drupal\islandora_hierarchical_access\EntityCUDHandler;
/**
* Implements hook_entity_type_alter().
*/
function islandora_hierarchical_access_entity_type_alter(array &$entity_types) : void {
/** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */
foreach ($entity_types as $entity_type) {
if (!is_a($entity_type->getStorageClass(), SqlEntityStorageInterface::class, TRUE)) {
continue;
}
EntityCUDHandler::attach($entity_type);
EntityAccessHandler::attach($entity_type);
}
}
/**
* Implements hook_entity_insert().
*/
function islandora_hierarchical_access_entity_insert(EntityInterface $entity) : void {
/** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
$entity_type_manager = \Drupal::service('entity_type.manager');
$entity_type_id = $entity->getEntityTypeId();
if ($entity_type_manager->hasHandler($entity_type_id, EntityCUDHandler::NAME)) {
/** @var \Drupal\islandora_hierarchical_access\EntityCUDHandlerInterface $handler */
$handler = $entity_type_manager->getHandler($entity_type_id, EntityCUDHandler::NAME);
$handler->create($entity);
}
}
/**
* Implements hook_entity_update().
*/
function islandora_hierarchical_access_entity_update(EntityInterface $entity) : void {
/** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
$entity_type_manager = \Drupal::service('entity_type.manager');
$entity_type_id = $entity->getEntityTypeId();
if ($entity_type_manager->hasHandler($entity_type_id, EntityCUDHandler::NAME)) {
/** @var \Drupal\islandora_hierarchical_access\EntityCUDHandlerInterface $handler */
$handler = $entity_type_manager->getHandler($entity_type_id, EntityCUDHandler::NAME);
$handler->update($entity);
}
}
/**
* Implements hook_entity_delete().
*/
function islandora_hierarchical_access_entity_delete(EntityInterface $entity) : void {
/** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
$entity_type_manager = \Drupal::service('entity_type.manager');
$entity_type_id = $entity->getEntityTypeId();
if ($entity_type_manager->hasHandler($entity_type_id, EntityCUDHandler::NAME)) {
/** @var \Drupal\islandora_hierarchical_access\EntityCUDHandlerInterface $handler */
$handler = $entity_type_manager->getHandler($entity_type_id, EntityCUDHandler::NAME);
$handler->delete($entity);
}
}
/**
* Implements hook_entity_access().
*/
function islandora_hierarchical_access_entity_access(
EntityInterface $entity,
$operation,
AccountInterface $account
) : AccessResultInterface {
/** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
$entity_type_manager = \Drupal::service('entity_type.manager');
$entity_type_id = $entity->getEntityTypeId();
if ($entity_type_manager->hasHandler($entity_type_id, EntityAccessHandler::NAME)) {
/** @var \Drupal\islandora_hierarchical_access\EntityAccessHandlerInterface $handler */
$handler = $entity_type_manager->getHandler($entity_type_id, EntityAccessHandler::NAME);
return $handler->check(
$entity,
$operation,
$account
);
}
return AccessResult::neutral("Unhandled entity type.");
}
/**
* Implements hook_query_TAG_alter() for file_access tagged queries.
*/
function islandora_hierarchical_access_query_file_access_alter(AlterableInterface $query) : void {
if ($query->hasTag('islandora_hierarchical_access_subquery')) {
// We expect that things should be handled via our event instead.
return;
}
assert($query->getMetaData('islandora_hierarchical_access_tag_type') === NULL);
\Drupal::service('islandora_hierarchical_access.query_tagger')
->tagQuery($query->addMetaData('islandora_hierarchical_access_tag_type', 'file'));
}
/**
* Implements hook_query_TAG_alter() for media_access tagged queries.
*/
function islandora_hierarchical_access_query_media_access_alter(AlterableInterface $query) : void {
if ($query->hasTag('islandora_hierarchical_access_subquery')) {
// We expect that things should be handled via our event instead.
return;
}
assert($query->getMetaData('islandora_hierarchical_access_tag_type') === NULL);
\Drupal::service('islandora_hierarchical_access.query_tagger')
->tagQuery($query->addMetaData('islandora_hierarchical_access_tag_type', 'media'));
}