Newer
Older
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<?php
/**
* @file
* A tremendously simple access control module -- it allows users to mark
* individual nodes as private; users with 'access private content' perms can
* read these nodes, while others cannot.
*/
define('PRIVATE_DISABLED', 0);
define('PRIVATE_ALLOWED', 1);
define('PRIVATE_AUTOMATIC', 2);
define('PRIVATE_ALWAYS', 3);
define('PRIVATE_GRANT_ALL', 1);
/**
* Implements hook_enable().
*
* A node access module needs to force a rebuild of the node access table
* when it is enabled to ensure that things are set up.
*/
function private_enable() {
node_access_needs_rebuild(TRUE);
}
/**
* Implements hook_disable().
*
* A node access module needs to force a rebuild of the node access table
* when it is disabled to ensure that its entries are removed from the table.
*/
function private_disable() {
private_disabling(TRUE);
node_access_needs_rebuild(TRUE);
}
/**
* Simple function to make sure we don't respond with grants when disabling
* ourselves.
*/
function private_disabling($set = NULL) {
static $disabling = FALSE;
if ($set !== NULL) {
$disabling = $set;
}
return $disabling;
}
/**
* Implements hook_permission().
*
* In this example, we will use a simple permission to determine whether a user
* has access to "private" content. This permission is defined here.
*/
function private_permission() {
return array(
'mark content as private' => array(
'title' => t('Mark content as private'),
'description' => t('Make content only viewable by people with access to view private content'),
),
'access private content' => array(
'title' => t('Access private content'),
'description' => t('Access any content marked as private'),
),
'edit private content' => array(
'title' => t('Edit private content'),
'description' => t('Edit content marked as private'),
),
);
}
/**
* Implements hook_node_grants().
*
* Tell the node access system what GIDs the user belongs to for each realm.
* In this example, we are providing two realms: the example realm, which
* has just one group id (1) and the user is either a member or not depending
* upon the operation and the access permission set.
*
* We are also setting up a realm for the node author, though, to give it
* special privileges. That has 1 GID for every UID, and each user is
* automatically a member of the group where GID == UID.
*
*/
function private_node_grants($account, $op) {
// First grant a grant to the author for own content.
$grants['private_author'] = array($account->uid);
if ($op == 'view' && user_access('access private content', $account)) {
$grants['private_view'] = array(PRIVATE_GRANT_ALL);
}
if (($op == 'update' || $op == 'delete') && user_access('edit private content', $account)) {
$grants['private_edit'] = array(PRIVATE_GRANT_ALL);
}
return $grants;
}
/**
* Implements hook_node_access_records().
*
* All node access modules must implement this hook. If the module is
* interested in the privacy of the node passed in, return a list
* of node access values for each grant ID we offer.
*/
function private_node_access_records($node) {
if (private_disabling()) {
return;
}
// We only care about the node if it's been marked private. If not, it is
// treated just like any other node and we completely ignore it.
if (isset($node->private) && $node->private == 1) {
$grants = array();
$grants[] = array(
'realm' => 'private_view',
'gid' => PRIVATE_GRANT_ALL,
'grant_view' => 1,
'grant_update' => 0,
'grant_delete' => 0,
'priority' => 0,
);
$grants[] = array(
'realm' => 'private_edit',
'gid' => PRIVATE_GRANT_ALL,
'grant_view' => 1,
'grant_update' => 1,
'grant_delete' => 1,
'priority' => 0,
);
$grants[] = array(
'realm' => 'private_author',
'gid' => $node->uid,
'grant_view' => 1,
'grant_update' => 1,
'grant_delete' => 1,
'priority' => 0,
);
return $grants;
}
}
/**
* Implements hook_form_alter().
*
* This module adds a simple checkbox to the node form labeled private. If the
* checkbox is labelled, only the node author and users with 'access private content'
* privileges may see it.
*/
function private_form_alter(&$form, &$form_state, $form_id) {
if (!empty($form['#node_edit_form'])) {
$node = $form['#node'];
$default = variable_get('private_' . $node->type, PRIVATE_ALLOWED);
if ($default != PRIVATE_DISABLED || !empty($node->private)) {
if (empty($node->nid)) {
$privacy = ($default > PRIVATE_ALLOWED);
}
else {
$privacy = isset($node->private) ? $node->private : 0;
}
if (user_access('mark content as private') && $default != PRIVATE_ALWAYS) {
if (user_access('administer nodes')) {
$form['options']['private'] = array(
'#type' => 'checkbox',
'#title' => t('Make this post private'),
'#attributes' => array('title' => t('When checked, only users with proper access permissions will be able to see this post.')),
'#default_value' => $privacy,
);
}
else {
$form['private'] = array(
'#type' => 'checkbox',
'#title' => t('Make this post private'),
'#attributes' => array('title' => t('When checked, only users with proper access permissions will be able to see this post.')),
'#default_value' => $privacy,
'#weight' => 99,
);
}
}
else {
$form['private'] = array(
'#type' => 'value',
'#value' => $privacy,
);
}
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function private_form_node_type_form_alter(&$form, &$form_state, $form_id) {
if (isset($form['type'])) {
$form['workflow']['private'] = array(
'#type' => 'radios',
'#title' => t('Privacy'),
'#options' => array(
PRIVATE_DISABLED => t('Disabled (always public)'),
PRIVATE_ALLOWED => t('Enabled (public by default)'),
PRIVATE_AUTOMATIC => t('Enabled (private by default)'),
PRIVATE_ALWAYS => t('Hidden (always private)'),
),
'#default_value' => variable_get('private_' . $form['#node_type']->type, PRIVATE_ALLOWED),
);
}
}
/**
* Implements hook_node_load().
*/
function private_node_load($nodes, $types) {
$result = db_query('SELECT * FROM {private} WHERE nid IN(:nids)', array(':nids' => array_keys($nodes)));
foreach ($result as $record) {
$nodes[$record->nid]->private = $record->private;
}
}
/**
* Implements hook_node_delete().
*/
function private_node_delete($node) {
db_delete('private')
->condition('nid', $node->nid)
->execute();
}
/**
* Implements hook_node_insert().
*/
function private_node_insert($node) {
private_node_update($node);
}
/**
* Implements hook_node_update().
*/
function private_node_update($node) {
if (isset($node->private)) {
db_merge('private')
->key(array('nid' => $node->nid))
->fields(array(
'nid' => $node->nid,
'private' => (int)$node->private,
))
->execute();
}
}
/**
* Implements hook_node_view().
*/
function private_node_view($node, $view_mode) {
if (isset($node->private) && $node->private == 1) {
$links['private_icon']['title'] = theme('private_node_link');
$links['private_icon']['html'] = TRUE;
$node->content['links']['private'] = array(
'#theme' => 'links__node__private',
'#links' => $links,
'#attributes' => array('class' => array('links', 'inline')),
);
}
}
/**
* Implements hook_theme().
*/
function private_theme() {
return array(
'private_node_link' => array(
'variables' => array(),
),
);
}
/**
* Custom theme function
* @see private_theme()
*/
function theme_private_node_link() {
$vars = array(
'path' => drupal_get_path('module', 'private') . '/icon_key.gif',
'width' => '16',
'height' => '16',
'alt' => t('Private'),
'title' => t('This content is private.')
);
return theme('image', $vars);
}
/**
* Implements hook_action_info().
*/
function private_action_info() {
return array(
'private_set_private_action' => array(
'type' => 'node',
'label' => t('Make post private'),
'configurable' => FALSE,
'triggers' => array(
'node_insert',
'node_update',
),
),
'private_set_public_action' => array(
'type' => 'node',
'label' => t('Make post public'),
'configurable' => FALSE,
'triggers' => array(
'node_insert',
'node_update',
),
),
);
}
/**
* Implementation of a Drupal action.
*/
function private_set_public_action(&$node, $context = array()) {
$node->private = FALSE;
$nids = array($node->nid);
private_node_mark_public($nids);
}
/**
* Implementation of a Drupal action.
*/
function private_set_private_action(&$node, $context = array()) {
$node->private = TRUE;
$nids = array($node->nid);
private_node_mark_private($nids);
}
/**
* Implements hook_node_operations().
*/
function private_node_operations() {
$operations = array(
'private_mark_as_private' => array(
'label' => t('Mark as private'),
'callback' => 'private_node_mark_private',
),
'private_mark_as_public' => array(
'label' => t('Mark as public'),
'callback' => 'private_node_mark_public',
),
);
return $operations;
}
/**
* Callback for 'Mark as private' node operation
*/
function private_node_mark_private($nids) {
foreach ($nids as $nid) {
db_merge('private')
->key(array('nid' => $nid))
->fields(array(
'nid' => $nid,
'private' => 1,
))
->execute();
}
}
/**
* Callback for 'Mark as public' node operation
*/
function private_node_mark_public($nids) {
foreach ($nids as $nid) {
db_merge('private')
->key(array('nid' => $nid))
->fields(array(
'nid' => $nid,
'private' => 0,
))
->execute();
}
}
/**
* Tell Views that we're down with it, yo.
*/
function private_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'private'),
);
}