1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* A dummy module implementing node access related hooks for testing purposes.
|
6
|
*
|
7
|
* A dummy module implementing node access related hooks to test API interaction
|
8
|
* with the Node module. This module restricts view permission to those with
|
9
|
* a special 'node test view' permission.
|
10
|
*/
|
11
|
|
12
|
/**
|
13
|
* Implements hook_node_grants().
|
14
|
*/
|
15
|
function node_access_test_node_grants($account, $op) {
|
16
|
$grants = array();
|
17
|
// First grant a grant to the author for own content.
|
18
|
$grants['node_access_test_author'] = array($account->uid);
|
19
|
if ($op == 'view' && user_access('node test view', $account)) {
|
20
|
$grants['node_access_test'] = array(8888, 8889);
|
21
|
}
|
22
|
if ($op == 'view' && $account->uid == variable_get('node_test_node_access_all_uid', 0)) {
|
23
|
$grants['node_access_all'] = array(0);
|
24
|
}
|
25
|
return $grants;
|
26
|
}
|
27
|
|
28
|
/**
|
29
|
* Implements hook_node_access_records().
|
30
|
*/
|
31
|
function node_access_test_node_access_records($node) {
|
32
|
$grants = array();
|
33
|
// For NodeAccessBaseTableTestCase, only set records for private nodes.
|
34
|
if (!variable_get('node_access_test_private') || $node->private) {
|
35
|
$grants[] = array(
|
36
|
'realm' => 'node_access_test',
|
37
|
'gid' => 8888,
|
38
|
'grant_view' => 1,
|
39
|
'grant_update' => 0,
|
40
|
'grant_delete' => 0,
|
41
|
'priority' => 0,
|
42
|
);
|
43
|
$grants[] = array(
|
44
|
'realm' => 'node_access_test',
|
45
|
'gid' => 8889,
|
46
|
'grant_view' => 1,
|
47
|
'grant_update' => 0,
|
48
|
'grant_delete' => 0,
|
49
|
'priority' => 0,
|
50
|
);
|
51
|
// For the author realm, the GID is equivalent to a UID, which
|
52
|
// means there are many many groups of just 1 user.
|
53
|
$grants[] = array(
|
54
|
'realm' => 'node_access_test_author',
|
55
|
'gid' => $node->uid,
|
56
|
'grant_view' => 1,
|
57
|
'grant_update' => 1,
|
58
|
'grant_delete' => 1,
|
59
|
'priority' => 0,
|
60
|
);
|
61
|
}
|
62
|
|
63
|
return $grants;
|
64
|
}
|
65
|
|
66
|
/**
|
67
|
* Implements hook_permission().
|
68
|
*
|
69
|
* Sets up permissions for this module.
|
70
|
*/
|
71
|
function node_access_test_permission() {
|
72
|
return array('node test view' => array('title' => 'View content'));
|
73
|
}
|
74
|
|
75
|
/**
|
76
|
* Implements hook_menu().
|
77
|
*
|
78
|
* Sets up a page that lists nodes.
|
79
|
*/
|
80
|
function node_access_test_menu() {
|
81
|
$items = array();
|
82
|
$items['node_access_test_page'] = array(
|
83
|
'title' => 'Node access test',
|
84
|
'page callback' => 'node_access_test_page',
|
85
|
'access arguments' => array('access content'),
|
86
|
'type' => MENU_SUGGESTED_ITEM,
|
87
|
);
|
88
|
$items['node_access_entity_test_page'] = array(
|
89
|
'title' => 'Node access test',
|
90
|
'page callback' => 'node_access_entity_test_page',
|
91
|
'access arguments' => array('access content'),
|
92
|
'type' => MENU_SUGGESTED_ITEM,
|
93
|
);
|
94
|
return $items;
|
95
|
}
|
96
|
|
97
|
/**
|
98
|
* Page callback for node access test page.
|
99
|
*
|
100
|
* Page should say "No nodes" if there are no nodes, and "Yes, # nodes" (with
|
101
|
* the number filled in) if there were nodes the user could access. Also, the
|
102
|
* database query is shown, and a list of the node IDs, for debugging purposes.
|
103
|
* And if there is a query exception, the page says "Exception" and gives the
|
104
|
* error.
|
105
|
*/
|
106
|
function node_access_test_page() {
|
107
|
$output = '';
|
108
|
|
109
|
try {
|
110
|
$query = db_select('node', 'mytab')
|
111
|
->fields('mytab');
|
112
|
$query->addTag('node_access');
|
113
|
$result = $query->execute()->fetchAll();
|
114
|
|
115
|
if (count($result)) {
|
116
|
$output .= '<p>Yes, ' . count($result) . ' nodes</p>';
|
117
|
$output .= '<ul>';
|
118
|
foreach ($result as $item) {
|
119
|
$output .= '<li>' . $item->nid . '</li>';
|
120
|
}
|
121
|
$output .= '</ul>';
|
122
|
}
|
123
|
else {
|
124
|
$output .= '<p>No nodes</p>';
|
125
|
}
|
126
|
|
127
|
$output .= '<p>' . ((string) $query ) . '</p>';
|
128
|
}
|
129
|
catch (Exception $e) {
|
130
|
$output = '<p>Exception</p>';
|
131
|
$output .= '<p>' . $e->getMessage() . '</p>';
|
132
|
}
|
133
|
|
134
|
return $output;
|
135
|
}
|
136
|
|
137
|
/**
|
138
|
* Page callback for node access entity test page.
|
139
|
*
|
140
|
* Page should say "No nodes" if there are no nodes, and "Yes, # nodes" (with
|
141
|
* the number filled in) if there were nodes the user could access. Also, the
|
142
|
* database query is shown, and a list of the node IDs, for debugging purposes.
|
143
|
* And if there is a query exception, the page says "Exception" and gives the
|
144
|
* error.
|
145
|
*
|
146
|
* @see node_access_test_menu()
|
147
|
*/
|
148
|
function node_access_entity_test_page() {
|
149
|
$output = '';
|
150
|
try {
|
151
|
$query = new EntityFieldQuery;
|
152
|
$result = $query->fieldCondition('body', 'value', 'A', 'STARTS_WITH')->execute();
|
153
|
if (!empty($result['node'])) {
|
154
|
$output .= '<p>Yes, ' . count($result['node']) . ' nodes</p>';
|
155
|
$output .= '<ul>';
|
156
|
foreach ($result['node'] as $nid => $v) {
|
157
|
$output .= '<li>' . $nid . '</li>';
|
158
|
}
|
159
|
$output .= '</ul>';
|
160
|
}
|
161
|
else {
|
162
|
$output .= '<p>No nodes</p>';
|
163
|
}
|
164
|
}
|
165
|
catch (Exception $e) {
|
166
|
$output = '<p>Exception</p>';
|
167
|
$output .= '<p>' . $e->getMessage() . '</p>';
|
168
|
}
|
169
|
|
170
|
return $output;
|
171
|
}
|
172
|
|
173
|
/**
|
174
|
* Implements hook_form_BASE_FORM_ID_alter().
|
175
|
*/
|
176
|
function node_access_test_form_node_form_alter(&$form, $form_state) {
|
177
|
// Only show this checkbox for NodeAccessBaseTableTestCase.
|
178
|
if (variable_get('node_access_test_private')) {
|
179
|
$form['private'] = array(
|
180
|
'#type' => 'checkbox',
|
181
|
'#title' => t('Private'),
|
182
|
'#description' => t('Check here if this content should be set private and only shown to privileged users.'),
|
183
|
'#default_value' => isset($form['#node']->private) ? $form['#node']->private : FALSE,
|
184
|
);
|
185
|
}
|
186
|
}
|
187
|
|
188
|
/**
|
189
|
* Implements hook_node_load().
|
190
|
*/
|
191
|
function node_access_test_node_load($nodes, $types) {
|
192
|
$result = db_query('SELECT nid, private FROM {node_access_test} WHERE nid IN(:nids)', array(':nids' => array_keys($nodes)));
|
193
|
foreach ($result as $record) {
|
194
|
$nodes[$record->nid]->private = $record->private;
|
195
|
}
|
196
|
}
|
197
|
|
198
|
/**
|
199
|
* Implements hook_node_delete().
|
200
|
*/
|
201
|
|
202
|
function node_access_test_node_delete($node) {
|
203
|
db_delete('node_access_test')->condition('nid', $node->nid)->execute();
|
204
|
}
|
205
|
|
206
|
/**
|
207
|
* Implements hook_node_insert().
|
208
|
*/
|
209
|
function node_access_test_node_insert($node) {
|
210
|
_node_access_test_node_write($node);
|
211
|
}
|
212
|
|
213
|
/**
|
214
|
* Implements hook_node_update().
|
215
|
*/
|
216
|
function node_access_test_node_update($node) {
|
217
|
_node_access_test_node_write($node);
|
218
|
}
|
219
|
|
220
|
/**
|
221
|
* Helper for node insert/update.
|
222
|
*/
|
223
|
function _node_access_test_node_write($node) {
|
224
|
if (isset($node->private)) {
|
225
|
db_merge('node_access_test')
|
226
|
->key(array('nid' => $node->nid))
|
227
|
->fields(array('private' => (int) $node->private))
|
228
|
->execute();
|
229
|
}
|
230
|
}
|