forked from agentrickard/workbench_access
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workbench_access.post_update.php
203 lines (190 loc) · 6.8 KB
/
workbench_access.post_update.php
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
<?php
/**
* @file
* Contains post update hooks.
*/
use Drupal\block\BlockInterface;
use Drupal\Core\Config\Entity\ConfigEntityUpdater;
use Drupal\Core\Utility\UpdateException;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\workbench_access\Entity\AccessScheme;
use Drupal\workbench_access\RoleSectionStorageInterface;
use Drupal\workbench_access\WorkbenchAccessManagerInterface;
/**
* Convert configuration into a scheme.
*/
function workbench_access_post_update_convert_to_scheme() {
$config = \Drupal::state()->get('workbench_access_original_configuration', FALSE);
if (!$config) {
throw new UpdateException('Did not find expected original configuration');
}
$settings = [];
if ($config->get('scheme') === 'taxonomy') {
$fields = [];
foreach ($config->get('fields') as $entity_type => $field_info) {
foreach (array_filter($field_info) as $bundle => $field_name) {
$fields[] = [
'entity_type' => $entity_type,
'bundle' => $bundle,
'field' => $field_name,
];
}
}
$settings = [
'vocabularies' => array_values($config->get('parents')),
'fields' => $fields,
];
}
elseif ($config->get('scheme') === 'menu') {
$settings = [
'menus' => $config->get('parents'),
'bundles' => array_keys($config->get('fields')['node']),
];
}
// Let other modules intervene for additional types.
\Drupal::moduleHandler()->alter('workbench_access_scheme_update', $settings, $config);
// No settings? Do nothing but mark the update.
if (empty($settings)) {
$message = t('Workbench Access has not been configured. Disabling the module is recommended.');
}
else {
$scheme = AccessScheme::create([
'id' => 'default',
'label' => $config->get('label'),
'plural_label' => $config->get('plural_label'),
'scheme' => $config->get('scheme'),
'scheme_settings' => $settings,
]);
$scheme->save();
}
\Drupal::state()->set('workbench_access_upgraded_scheme_id', 'default');
/** @var \Drupal\node\NodeTypeInterface $node_type */
foreach (\Drupal::entityTypeManager()->getStorage('node_type')->loadMultiple() as $node_type) {
$node_type->unsetThirdPartySetting('workbench_access', 'workbench_access_status');
$node_type->save();
}
if (isset($message)) {
return $message;
}
}
/**
* Convert role storage.
*/
function workbench_access_post_update_convert_role_storage_keys() {
foreach (\Drupal::entityTypeManager()->getStorage('user_role')->loadMultiple() as $rid => $role) {
$prefix = 'workbench_access_roles_';
$old_key = $prefix . $rid;
$new_key = $prefix . 'default__' . $rid;
$state = \Drupal::state();
if ($existing = $state->get($old_key, FALSE)) {
$state->set($new_key, $existing);
$state->delete($old_key);
}
}
}
/**
* Convert user storage.
*/
function workbench_access_post_update_convert_user_storage_keys(array &$sandbox) {
$user_storage = \Drupal::entityTypeManager()->getStorage('user');
if (!isset($sandbox['ids'])) {
$sandbox['ids'] = $user_storage
->getQuery()
->exists(WorkbenchAccessManagerInterface::FIELD_NAME)
->execute();
$sandbox['count'] = count($sandbox['ids']);
}
foreach (array_splice($sandbox['ids'], 0, 50) as $id) {
$user = $user_storage->load($id);
$existing = array_column($user->get(WorkbenchAccessManagerInterface::FIELD_NAME)->getValue(), 'value');
$user->set(WorkbenchAccessManagerInterface::FIELD_NAME, array_map(function ($item) {
return 'default:' . $item;
}, $existing));
$user->save();
}
$sandbox['#finished'] = empty($sandbox['ids']) ? 1 : ($sandbox['count'] - count($sandbox['ids'])) / $sandbox['count'];
return t('Updated user assigments');
}
/**
* Transform existing role data to new storage.
*/
function workbench_access_post_update_section_role_association(&$sandbox) {
$schemes = \Drupal::entityTypeManager()->getStorage('access_scheme')->loadMultiple();
$storage = \Drupal::service('workbench_access.role_section_storage');
$state = \Drupal::state();
foreach ($schemes as $scheme) {
foreach (\Drupal::entityTypeManager()->getStorage('user_role')->loadMultiple() as $rid => $role) {
$prefix = 'workbench_access_roles_';
$potential_ids = [
$prefix . $rid,
$prefix . 'default__' . $rid,
];
foreach ($potential_ids as $key) {
if ($existing = $state->get($key, FALSE)) {
// Save the new roles.
$storage->addRole($scheme, $rid, array_values($existing));
// Delete the old storage.
$state->delete($key);
}
}
}
}
}
/**
* Transform existing user data to new storage.
*/
function workbench_access_post_update_section_user_association(&$sandbox) {
$schemes = \Drupal::entityTypeManager()->getStorage('access_scheme')->loadMultiple();
$storage = \Drupal::service('workbench_access.user_section_storage');
$user_storage = \Drupal::entityTypeManager()->getStorage('user');
if (!isset($sandbox['ids'])) {
$sandbox['ids'] = $user_storage
->getQuery()
->exists(WorkbenchAccessManagerInterface::FIELD_NAME)
->execute();
$sandbox['count'] = count($sandbox['ids']);
}
foreach (array_splice($sandbox['ids'], 0, 50) as $id) {
$user = $user_storage->load($id);
$existing = array_column($user->get(WorkbenchAccessManagerInterface::FIELD_NAME)->getValue(), 'value');
foreach ($schemes as $scheme_id => $scheme) {
$add_sections = [];
foreach ($existing as $item) {
$split = explode(':', $item);
if ($split[0] == $scheme_id) {
$add_sections[] = $split[1];
}
}
}
$storage->addUser($scheme, $user, $add_sections);
}
$sandbox['#finished'] = empty($sandbox['ids']) ? 1 : ($sandbox['count'] - count($sandbox['ids'])) / $sandbox['count'];
return t('Updated user assigments');
}
/**
* Delete the old workbench_access field.
*/
function workbench_access_post_update_workbench_access_field_delete(&$sandbox) {
$field_storage = \Drupal::entityTypeManager()->getStorage('field_storage_config');
if ($field_storage = FieldStorageConfig::loadByName('user', WorkbenchAccessManagerInterface::FIELD_NAME)) {
if (!$field_storage->isDeleted()) {
$field_storage->delete();
}
}
}
/**
* Updates all instances of the WBA block to include context mappings.
*/
function workbench_access_post_update_apply_context_mapping_to_blocks(&$sandbox) {
\Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'block', function(BlockInterface $block) {
if ($block->getPluginId() === 'workbench_access_block') {
$settings = $block->get('settings');
if (!isset($settings['context_mapping']['node'])) {
$settings['context_mapping']['node'] = '@node.node_route_context:node';
}
$block->set('settings', $settings);
return TRUE;
}
return FALSE;
});
}