This repository has been archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
relaxed.module
226 lines (204 loc) · 8.16 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\relaxed\Entity\Remote;
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();
} 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();
} 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();
} 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 = [];
$remote_checks = \Drupal::service('plugin.manager.remote_check')->runAll();
foreach ($remote_checks as $remote_id => $checks) {
$remote = Remote::load($remote_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[$remote_id] = [
'title' => t('Relaxed Remote: @remote', ['@remote' => $remote->label()]),
'description' => $description,
'severity' => $severity ? REQUIREMENT_OK : REQUIREMENT_ERROR,
];
$replicator_exists = class_exists('Relaxed\Replicator\Replicator');
$couchdbclient_exists = class_exists('Doctrine\CouchDB\CouchDBClient');
if (!$replicator_exists) {
$requirements['replicator'] = [
'title' => t('relaxedws/replicator'),
'value' => t('Not found'),
'severity' => REQUIREMENT_ERROR,
'description' => t('The <a href=":replicator_url">relaxedws/replicator</a> library is not available. This library is required by RELAXed Web Services.', [':replicator_url' => 'https://packagist.org/packages/relaxedws/replicator'])
];
}
if (!$couchdbclient_exists) {
$requirements['couchdb'] = [
'title' => t('doctrine/couchdb'),
'value' => t('Not found'),
'severity' => REQUIREMENT_ERROR,
'description' => t('The <a href=":couchdb_url">doctrine/couchdb</a> library is not available. This library is required by RELAXed Web Services.', [':couchdb_url' => 'https://packagist.org/packages/doctrine/couchdb'])
];
}
}
return $requirements;
}
}
/**
* Implements hook_entity_base_field_info().
*/
function relaxed_entity_base_field_info(EntityTypeInterface $entity_type) {
if ($entity_type->id() != 'workspace_pointer') {
return [];
}
$fields['remote_pointer'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Remote'))
->setDescription(t('A reference to the remote'))
->setSetting('target_type', 'remote')
->setRevisionable(TRUE);
$fields['remote_database'] = BaseFieldDefinition::create('string')
->setLabel(t('Remote database'))
->setDescription(t('The remote database name.'))
->setSettings([
'max_length' => 50,
'text_processing' => 0,
])
->setRevisionable(TRUE);
return $fields;
}
/**
* Implements hook_cron().
*/
function relaxed_cron() {
/** @var \Drupal\relaxed\RemotePointerInterface $remote_pointer_service */
$remote_pointer_service = \Drupal::service('relaxed.remote_pointer');
$remote_pointer_service->addAllPointers();
$remote_pointer_service->cleanupPointers();
}
function relaxed_entity_type_alter(array &$entity_types) {
/** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
if (isset($entity_types['rest_resource_config'])) {
$entity_types['rest_resource_config']->setClass('Drupal\relaxed\Entity\RestResourceConfig');
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function relaxed_form_workspace_basic_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
foreach ($form['upstream']['widget']['#options'] as $key => $option) {
if (!empty($form['upstream']['widget'][$key]['#disabled'])) {
$form['message']['#message_list']['warning'][] = t('One or more options for selecting the target ' .
'workspace is/are disabled because the remote workspace could not ' .
'be accessed during the last availability check, this can happen ' .
'because of different reasons, for example network connection ' .
'issues. The availability of the remote workspace is checked on ' .
'every cron run and if it becomes available, the option to select ' .
'the workspace is activated again.');
return;
}
}
}