forked from relaxedws/drupal-relaxed
/
relaxed.module
144 lines (132 loc) · 5.13 KB
/
relaxed.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\relaxed\Entity\Endpoint;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
/**
* Implements hook_entity_insert().
*/
function relaxed_entity_insert(EntityInterface $entity) {
if ($entity->getEntityTypeId() == 'workspace') {
// In some cases (mostly testing) the plugin manager is not yet initialized.
// @todo {@link https://www.drupal.org/node/2599864 Fix this.}
try {
\Drupal::service('plugin.manager.rest')->clearCachedDefinitions();
\Drupal::service('plugin.manager.endpoint')->clearCachedDefinitions();
} catch (InvalidArgumentException $e) {
watchdog_exception('relaxed', $e);
}
}
}
/**
* Implements hook_entity_update().
*/
function relaxed_entity_update(EntityInterface $entity) {
if ($entity->getEntityTypeId() == 'workspace') {
// In some cases (mostly testing) the plugin manager is not yet initialized.
// @todo {@link https://www.drupal.org/node/2599864 Fix this.}
try {
\Drupal::service('plugin.manager.rest')->clearCachedDefinitions();
\Drupal::service('plugin.manager.endpoint')->clearCachedDefinitions();
} catch (InvalidArgumentException $e) {
watchdog_exception('relaxed', $e);
}
}
}
/**
* Implements hook_entity_delete().
*/
function relaxed_entity_delete(EntityInterface $entity) {
if ($entity->getEntityTypeId() == 'workspace') {
// In some cases (mostly testing) the plugin manager is not yet initialized.
// @todo {@link https://www.drupal.org/node/2599864 Fix this.}
try {
\Drupal::service('plugin.manager.rest')->clearCachedDefinitions();
\Drupal::service('plugin.manager.endpoint')->clearCachedDefinitions();
} catch (InvalidArgumentException $e) {
watchdog_exception('relaxed', $e);
}
}
}
/**
* Implements hook_entity_access().
*/
function relaxed_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
/** @var \Drupal\multiversion\MultiversionManagerInterface $manager */
$multiversion_manager = \Drupal::service('multiversion.manager');
$entity_type = $entity->getEntityType();
$entity_type_id = $entity_type->id();
// Set entity access restrictions depending on pull and push permissions.
if ($multiversion_manager->isEnabledEntityType($entity_type) || $entity_type_id == 'replication_log') {
if ($operation == 'view') {
return AccessResult::allowedIfHasPermissions($account, ['perform push replication', 'perform pull replication'], 'OR');
}
else {
return AccessResult::allowedIfHasPermission($account, 'perform push replication');
}
}
// No opinion.
return AccessResult::neutral();
}
/**
* Implements hook_entity_create_access().
*/
function multiversion_entity_create_access(AccountInterface $account, array $context, $entity_bundle) {
/** @var \Drupal\multiversion\MultiversionManagerInterface $manager */
$multiversion_manager = \Drupal::service('multiversion.manager');
$bundles_info = \Drupal::service('entity_type.bundle.info')->getAllBundleInfo();
foreach ($bundles_info as $entity_type_id => $bundles) {
if (in_array($entity_bundle, array_keys($bundles))) {
$entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id);
if ($multiversion_manager->isEnabledEntityType($entity_type) || $entity_type_id == 'replication_log') {
return AccessResult::allowedIfHasPermission($account, 'perform push replication');
}
}
}
// No opinion.
return AccessResult::neutral();
}
/**
* Implements hook_entity_field_access().
*/
function relaxed_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
// Set entity access restrictions depending on pull and push permissions.
if ($operation == 'view') {
return AccessResult::allowedIfHasPermissions($account, ['perform push replication', 'perform pull replication'], 'OR');
}
else {
return AccessResult::allowedIfHasPermission($account, 'perform push replication');
}
}
/**
* Implements hook_requirements().
*/
function relaxed_requirements($phase) {
if ($phase === 'runtime') {
$requirements = [];
$endpoint_checks = \Drupal::service('plugin.manager.endpoint_check')->runAll();
foreach ($endpoint_checks as $endpoint_id => $checks) {
$endpoint = Endpoint::load($endpoint_id);
$severity = true;
$messages = [];
foreach ($checks as $check_id => $check) {
$messages[] = $check['message'];
$severity = $check['result'] ? true : false;
}
$build = [
'#theme' => 'item_list',
'#items' => $messages,
];
$description = \Drupal::service('renderer')->render($build);
$requirements[$endpoint_id] = [
'title' => t('Relaxed Endpoint: @endpoint', ['@endpoint' => $endpoint->label()]),
'description' => $description,
'severity' => $severity ? REQUIREMENT_OK : REQUIREMENT_ERROR,
];
}
return $requirements;
}
}