-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodeaccess.install
More file actions
183 lines (166 loc) · 5.54 KB
/
nodeaccess.install
File metadata and controls
183 lines (166 loc) · 5.54 KB
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
<?php
/**
* @file
* Install/uninstall functions for Nodeaccess.
*/
use Drupal\node\Entity\NodeType;
use Drupal\user\Entity\Role;
use Drupal\Core\Database\Database;
/**
* Implements hook_install().
*/
function nodeaccess_install() {
$config = \Drupal::configFactory()->getEditable('nodeaccess.settings');
$roles = Role::loadMultiple();
/**
* Set up an ID for our roles that way we can use them with our various node_access
* grants (our nodeaccess ones too!). This becomes the gid for role based access
* permissions.
*/
$i = 0;
$roles_gids = [];
foreach ($roles as $role_id => $role) {
$roles_gids[$role_id] = $i;
$i++;
}
$config->set('role_map', $roles_gids);
$config->save();
/**
* Define the default grants for each role based on what is set up in our existing
* permissions. Note that nodeaccess will not be enabled by default for individual
* nodes, so we are just setting up defaults, not assigning them to each node.
*/
$anonymous_role = Role::load('anonymous');
$authenticated_role = Role::load('authenticated');
foreach (NodeType::loadMultiple() as $type => $bundle) {
$bundle->setThirdPartySetting('nodeaccess', 'nodeaccess_' . $type . '_' . 'anonymous_view', (int) $anonymous_role->hasPermission('access content'));
$bundle->setThirdPartySetting('nodeaccess', 'nodeaccess_' . $type . '_' . 'authenticated_view', (int) $authenticated_role->hasPermission('access content'));
$edit_perm = 'edit any ' . $type . ' content';
$bundle->setThirdPartySetting('nodeaccess', 'nodeaccess_' . $type . '_' . 'anonymous_update', (int) $anonymous_role->hasPermission($edit_perm));
$bundle->setThirdPartySetting('nodeaccess', 'nodeaccess_' . $type . '_' . 'authenticated_update', (int) $authenticated_role->hasPermission($edit_perm));
$delete_perm = 'delete any ' . $type . ' content';
$bundle->setThirdPartySetting('nodeaccess', 'nodeaccess_' . $type . '_' . 'anonymous_delete', (int) $anonymous_role->hasPermission($delete_perm));
$bundle->setThirdPartySetting('nodeaccess', 'nodeaccess_' . $type . '_' . 'authenticated_delete', (int) $authenticated_role->hasPermission($delete_perm));
$bundle->setThirdPartySetting('nodeaccess', 'nodeaccess_' . $type . '_' . 'author_view', 1);
$bundle->setThirdPartySetting('nodeaccess', 'nodeaccess_' . $type . '_' . 'author_update', 1);
$bundle->setThirdPartySetting('nodeaccess', 'nodeaccess_' . $type . '_' . 'author_delete', 1);
$bundle->save();
}
}
/**
* Implements hook_schema().
*/
function nodeaccess_schema() {
$schema = [];
$schema['nodeaccess_nodes_enabled'] = [
'fields' => [
'nid' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
],
'primary key' => ['nid'],
];
$schema['nodeaccess'] = [
'fields' => [
'nid' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'gid' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'realm' => [
'type' => 'varchar',
'length' => 191,
'not null' => TRUE,
'default' => '',
],
'grant_view' => [
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'grant_update' => [
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'grant_delete' => [
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
],
'primary key' => ['nid', 'gid', 'realm'],
];
return $schema;
}
/**
* Create our nodeaccess_nodes_enabled table.
*/
function nodeaccess_update_8200() {
$spec = [];
$spec = [
'fields' => [
'nid' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
],
'primary key' => ['nid'],
];
$schema = Database::getConnection()->schema();
$schema->createTable('nodeaccess_nodes_enabled', $spec);
}
/**
* Convert old Nodeaccess data.
*/
function nodeaccess_update_8201() {
$db = \Drupal::database();
$config = \Drupal::configFactory()->getEditable('nodeaccess.settings');
$allowed_types = $config->get('allowed_types');
$role_map = $config->get('role_map');
foreach($role_map as $machine_name => $rid) {
foreach($allowed_types as $node_type => $enabled) {
if ($enabled == 1) {
$type = NodeType::load($node_type);
$role = $config->get($node_type);
$view = $role[$machine_name]['grant_view'];
$update = $role[$machine_name]['grant_update'];
$delete = $role[$machine_name]['grant_delete'];;
$type->setThirdPartySetting('nodeaccess', 'nodeaccess_' . $node_type . '_' . $machine_name . '_view', $view);
$type->setThirdPartySetting('nodeaccess', 'nodeaccess_' . $node_type . '_' . $machine_name . '_update', $update);
$type->setThirdPartySetting('nodeaccess', 'nodeaccess_' . $node_type . '_' . $machine_name . '_delete', $delete);
$type->setThirdPartySetting('nodeaccess', 'nodeaccess_grant_tab_' . $node_type, $enabled);
}
}
}
$entries = $db->select('nodeaccess', 'n')
->fields('n', ['nid'])
->distinct()
->execute()
->fetchAll();
foreach ($entries as $entry) {
$db->insert('nodeaccess_nodes_enabled')
->fields([
'nid' => $entry->nid,
])
->execute();
}
}