forked from Gizra/pluggable_node_access
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpluggable_node_access.module
247 lines (225 loc) · 7.17 KB
/
pluggable_node_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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
/**
* @file
* Control the group and group content view access of groups by custom logic.
*/
/**
* Implements hook_entity_info().
*/
function pluggable_node_access_entity_info() {
$items['pluggable_node_access'] = array(
'label' => t('Pluggable node access'),
'controller class' => 'EntityAPIController',
'entity class' => 'PluggableNodeAccess',
'base table' => 'pluggable_node_access',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'id',
'label' => FALSE,
'bundle' => 'type',
'language' => 'language',
),
'bundles' => array(),
'bundle keys' => array(
'bundle' => 'name',
),
'view modes' => array(
'full' => array(
'label' => t('Full'),
'custom settings' => FALSE,
),
),
'module' => 'pluggable_node_access',
'metadata controller class' => 'EntityDefaultMetadataController',
'views controller class' => 'EntityDefaultViewsController',
'entity cache' => module_exists('entitycache'),
);
// Add bundles by plugins.
foreach (pluggable_node_access_get_pluggable_node_access_plugins() as $plugin_name=> $plugin) {
$items['pluggable_node_access']['bundles'][$plugin_name] = array(
'label' => $plugin['label'],
'admin' => array(
'path' => 'admin/structure/pluggable_node_access/manage/%pluggable_node_access',
'real path' => 'admin/structure/pluggable_node_access/manage/' . $plugin_name,
'bundle argument' => 4,
'access arguments' => array('administer Pluggable node access types'),
),
);
}
return $items;
}
/**
* Implements hook_og_fields_info().
*/
function pluggable_node_access_og_fields_info() {
$items['pluggable_node_access'] = array(
'type' => array('group', 'group_content'),
'description' => t('Reference to Pluggable node access entities.'),
'field' => array(
'field_name' => 'pluggable_node_access',
'type' => 'entityreference',
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
'settings' => array(
'target_type' => 'pluggable_node_access',
),
),
'instance' => array(
'label' => t('Pluggable node access'),
'description' => t('Reference to Pluggable node access entities.'),
),
);
return $items;
}
/**
* Implements hook_ctools_plugin_directory().
*/
function pluggable_node_access_ctools_plugin_directory($module, $plugin) {
if ($module == 'pluggable_node_access') {
return 'plugins/' . $plugin;
}
}
/**
* Implements hook_ctools_plugin_type().
*/
function pluggable_node_access_ctools_plugin_type() {
$plugins['pluggable_node_access'] = array(
'classes' => array('class'),
'process' => 'pluggable_node_access_plugin_process',
);
return $plugins;
}
/**
* Add defaults values to the normalizer related plugins.
*/
function pluggable_node_access_plugin_process(&$plugin, $info) {
// Common operations.
$plugin += array(
'description' => '',
);
// Call the plugin specific process functions.
$function = 'pluggable_node_access_process_' . $info['type'];
if (function_exists($function)) {
$function($plugin, $info);
}
}
/**
* Include CTools plugins and get all normalizer plugins.
*
* @return array
* All normalizing plugins.
*/
function pluggable_node_access_get_pluggable_node_access_plugins() {
ctools_include('plugins');
return ctools_get_plugins('pluggable_node_access', 'pluggable_node_access');
}
/**
* Include CTools plugins and get the specified electricity normalizer plugin.
*
* @param string $plugin_name
* If provided this function only returns the selected plugin.
*
* @return array
* The selected plugin for electricity normalizer.
*/
function pluggable_node_access_get_pluggable_node_access_plugin($plugin_name) {
ctools_include('plugins');
return ctools_get_plugins('pluggable_node_access', 'pluggable_node_access', $plugin_name);
}
/**
* Return the handler based on major and minor version, and resource name.
*
* @param $plugin_name
* Name of plugin.
* @param $node
* The node object.
*
* @throws Exception
* Class not found.
*
* @return PluggableNodeAccessInterface
* The handler object.
*/
function pluggable_node_access_get_pluggable_node_access_handler($plugin_name, $node) {
$cache = &drupal_static(__FUNCTION__);
if (isset($cache[$plugin_name])) {
$handler = $cache[$plugin_name];
$handler->setNode($node);
return $handler;
}
$cache[$plugin_name] = NULL;
ctools_include('plugins');
$plugin = ctools_get_plugins('pluggable_node_access', 'pluggable_node_access', $plugin_name);
if (!$class = ctools_plugin_load_class('pluggable_node_access', 'pluggable_node_access', $plugin_name, 'class')) {
throw new \Exception("OG access plugin class for '$plugin_name' not found.");
}
$handler = new $class($plugin, $node);
$cache[$plugin_name] = $handler;
return $cache[$plugin_name];
}
/**
* Implements hook_node_grants().
*/
function pluggable_node_access_node_grants($account, $op) {
$grants = array();
foreach (pluggable_node_access_get_pluggable_node_access_plugins() as $plugin_name => $plugin) {
$result = call_user_func(array($plugin['class'], 'getNodeGrants'), $account, $op);
$grants = array_merge_recursive($grants, $result);
}
return $grants;
}
/**
* Implements hook_node_access_records().
*/
function pluggable_node_access_node_access_records($node) {
$grants = array();
foreach (pluggable_node_access_get_pluggable_node_access_plugins() as $plugin_name => $plugin) {
$handler = pluggable_node_access_get_pluggable_node_access_handler($plugin_name, $node);
$result = $handler->getNodeAccessRecords();
$grants = array_merge_recursive($grants, $result);
}
return $grants;
}
/**
* Implements hook_og_membership_insert().
*/
function pluggable_node_access_og_membership_insert(OgMembership $og_membership) {
pluggable_node_access_set_og_group_content_grants($og_membership);
}
/**
* Implements hook_og_membership_update().
*/
function pluggable_node_access_og_membership_update(OgMembership $og_membership) {
pluggable_node_access_set_og_group_content_grants($og_membership);
}
/**
* Invalidate cache, and use updated membership for acquiring grants.
*
* Granting access based on group membership should happen after the database
* change of the og_membership entity, and in order to make it work the cache
* should be invalidated.
*
* @param OgMembership $og_membership
* The OG membership entity.
*/
function pluggable_node_access_set_og_group_content_grants(OgMembership $og_membership) {
if ($og_membership->entity_type != 'node') {
return;
}
og_invalidate_cache();
$node = node_load($og_membership->etid);
node_access_acquire_grants($node);
}
/**
* Implements hook_og_access_invoke_node_access_acquire_grants_alter().
*
* Check privacy change made by pluggable_node_access.
*/
function pluggable_node_access_og_access_invoke_node_access_acquire_grants_alter(&$context) {
foreach (pluggable_node_access_get_pluggable_node_access_plugins() as $plugin_name => $plugin) {
$handler = pluggable_node_access_get_pluggable_node_access_handler($plugin_name, $context['entity']);
if ($handler->checkForNodeAccessChange()) {
$context['change_detected'] = TRUE;
break;
}
}
}