-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodeaccess.module
397 lines (348 loc) · 13.2 KB
/
nodeaccess.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
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
/*
* Notes:
*
* - skip priority, it's been deprecated
*/
function nodeaccess_menu() {
/* main prefs */
$items['admin/config/content/nodeaccess'] = array(
'title' => 'Nodeaccess settings',
'description' => 'Global congiruation for nodeaccess',
'page callback' => 'drupal_get_form',
'page arguments' => array('nodeaccess_admin_settings'),
'file' => 'nodeaccess.admin.inc',
'access arguments' => array('administer nodeaccess'),
);
/* per content type prefs */
$items['admin/structure/types/manage/%node_type/nodeaccess'] = array(
'title' => t('Permissions'),
'page callback' => 'drupal_get_form',
'page arguments' => array('nodeaccess_admin_form', 4),
'access callback' => 'user_access',
'access arguments' => array('administer content types'),
'type' => MENU_LOCAL_TASK,
'file' => 'nodeaccess.admin.inc'
);
$items['nodeaccess/usersearch'] = array(
'page callback' => 'nodeaccess_usersearch',
'type' => MENU_CALLBACK,
'access callback' => 'user_access',
'access arguments' => array('grant node permissions'),
);
return $items;
}
function nodeaccess_usersearch() {
$header = array(
'username' => array('data' => t('Username'), 'field' => 'u.name'),
'mail' => array('data' => t('Email'), 'field' => 'u.mail')
);
$query = db_select('users', 'u');
$query->condition('u.uid', 0, '<>');
user_build_filter_query($query);
$count_query = clone $query;
$count_query->addExpression('COUNT(u.uid)');
$query = $query->extend('PagerDefault')->extend('TableSort');
$query
->fields('u', array('uid', 'name', 'mail'))
->limit(1)
->orderByHeader($header)
->setCountQuery($count_query);
$result = $query->execute();
$accounts = array();
foreach ($result as $account) {
$options[$account->uid] = array(
'username' => theme('username', array('account' => $account)),
'mail' => $account->mail,
);
}
$form['accounts'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#empty' => t('No people available.'),
);
$form['pager'] = array('#markup' => theme('pager'));
echo drupal_render($form);
}
function nodeaccess_permission() {
return array(
'administer nodeaccess' => array(
'title' => t('Administer nodeaccess'),
'description' => t('Administer global options for nodeaccess')
),
'grant node permissions' => array(
'title' => t('Grant node permissions'),
'description' => t('May grant any permissions on a node')
),
'grant editable node permissions' => array(
'title' => t('Grant edit permissions on nodes')
),
'grant deletable node permissions' => array(
'title' => t('Grant delete permissions on nodes')
),
'grant own node permissions' => array(
'title' => t('Grant own permissions on nodes')
)
);
}
function nodeaccess_theme() {
return array(
'nodeaccess_grants' => array(
'arguments' => array('form' => NULL)
),
'nodeaccess_admin_roles' => array(
'render element' => 'form',
'template' => 'templates/admin_roles'
),
'nodeaccess_admin_users' => array(
'render element' => 'form',
'template' => 'templates/admin_users'
)
);
}
function nodeaccess_node_form($form) {
return 'foo';
}
/* hook_form_alter
*
* Implement vertical tab for permissions
*/
function nodeaccess_form_node_form_alter(&$form, $form_state) {
$form['nodeaccess'] = array(
'#type' => 'fieldset',
'#access' => user_access('grant node permissions'), // TODO: add in other perms
'#title' => 'Node Access',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'additional_settings',
'#attached' => array(
'js' => array(
'vertical-tabs' => drupal_get_path('module', 'nodeaccess') . 'nodeaccess-node-form.js'
)
),
'#tree' => TRUE,
'#weight' => 35,
'#theme' => 'nodeaccess_grants'
);
// summary
$summary = "rwx by foobar";
// get all roles
$roles = user_roles();
// which roles are configured to show up here?
$allowed_roles = variable_get('nodeaccess-roles', $roles);
// which grants are configured to show up here?
$allowed_grants = variable_get('nodeaccess-grants', array('view' => 1, 'edit' => 1, 'delete' => 1));
// If $preserve is true, the fields the user is not allowed to view or
// edit are included in the form as hidden fields to preserve them.
$preserve = variable_get('nodeaccess-preserve', 1);
// print out roles table
if ( is_array($roles) ) {
$form['nodeaccess']['rid'] = array('#tree' => TRUE, '#title' => 'Roles', '#theme' => 'foobar');
foreach( $roles as $id => $name ) {
// do we include this role?
if ( isset( $allowed_roles[$id] ) ) {
if ( $allowed_grants['view'] ) {
$form['nodeaccess']['rid'][$id]['grant_view'] = array('#type' => 'checkbox', '#theme' => 'foobar');
} else if ( $preserve ) {
$form['nodeaccess']['rid'][$id]['grant_view'] = array('#type' => 'hidden');
}
if ( $allowed_grants['edit'] ) {
$form['nodeaccess']['rid'][$id]['grant_edit'] = array('#type' => 'checkbox');
} else if ( $preserve ) {
$form['nodeaccess']['rid'][$id]['grant_edit'] = array('#type' => 'hidden');
}
if ( $allowed_grants['delete'] ) {
$form['nodeaccess']['rid'][$id]['grant_delete'] = array('#type' => 'checkbox');
} else if ( $preserve ) {
$form['nodeaccess']['rid'][$id]['grant_delete'] = array('#type' => 'hidden');
}
} else if ($preserve) {
//$form['rid'][$key]['name'] = array('#type' => 'hidden', '#value' => $field['name']);
//$form['rid'][$key]['grant_view'] = array('#type' => 'hidden', '#value' => $field['grant_view']);
//$form['rid'][$key]['grant_update'] = array('#type' => 'hidden', '#value' => $field['grant_update']);
//$form['rid'][$key]['grant_delete'] = array('#type' => 'hidden', '#value' => $field['grant_delete']);
}
}
}
// print out user table
}
function theme_nodeaccess_grants($form) {
return 'asdf';
}
/**
* Theme function for nodeaccess_grants_form.
*/
function theme_nodeaccess_grants_form($form) {
$allowed_roles = variable_get('nodeaccess-roles', array());
$allowed_grants = variable_get('nodeaccess-grants', array());
// Roles table.
$roles = element_children($form['rid']);
if (count($roles) && count($allowed_roles)) {
$header = array();
$header[] = t('Role');
if ($allowed_grants['view']) {
$header[] = t('View');
}
if ($allowed_grants['edit']) {
$header[] = t('Edit');
}
if ($allowed_grants['delete']) {
$header[] = t('Delete');
}
foreach ($roles as $key) {
if ($allowed_roles[$key]) {
$row = array();
$row[] = $form['rid'][$key]['name']['#value'] . drupal_render($form['rid'][$key]['name']);
if ($allowed_grants['view']) {
$row[] = drupal_render($form['rid'][$key]['grant_view']);
}
if ($allowed_grants['edit']) {
$row[] = drupal_render($form['rid'][$key]['grant_update']);
}
if ($allowed_grants['delete']) {
$row[] = drupal_render($form['rid'][$key]['grant_delete']);
}
$rows[] = $row;
}
}
$output .= theme('table', $header, $rows);
}
// Search form.
$output .= '<p><div class="search-form">';
$output .= '<strong>' . t('Enter names to search for users:') . '</strong>';
$output .= '<div class="container-inline">';
$output .= drupal_render($form['keys']);
$output .= drupal_render($form['search']);
$output .= '</div></div></p>';
// Users table.
unset($rows);
$users = element_children($form['uid']);
if (count($users) > 0) {
$header = array();
$header[] = t('User');
$header[] = t('Keep?');
if ($allowed_grants['view']) {
$header[] = t('View');
}
if ($allowed_grants['edit']) {
$header[] = t('Edit');
}
if ($allowed_grants['delete']) {
$header[] = t('Delete');
}
foreach ($users as $key) {
$row = array();
$row[] = $form['uid'][$key]['name']['#value'];
$row[] = drupal_render($form['uid'][$key]['keep']);
if ($allowed_grants['view']) {
$row[] = drupal_render($form['uid'][$key]['grant_view']);
}
if ($allowed_grants['edit']) {
$row[] = drupal_render($form['uid'][$key]['grant_update']);
}
if ($allowed_grants['delete']) {
$row[] = drupal_render($form['uid'][$key]['grant_delete']);
}
$rows[] = $row;
}
$output .= theme('table', $header, $rows);
}
$output .= drupal_render($form);
return $output;
}
function nodeaccess_get_settings() {
$settings = variable_get('nodeaccess', array(
'defaults_type' => 1,
'allow_view' => 1,
'allow_edit' => 1,
'allow_delete' => 1
));
return $settings;
}
function nodeaccess_permissions_form( ) {
// Select roles the permissions of which you want to allow users to
// view and edit, and the aliases and weights of those roles.
$form['roles'] = array('#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#title' => t('Allowed Roles'),
'#tree' => TRUE,
'#theme' => 'nodeaccess_admin_form_roles',
'#description' => '<small>' . t('The selected roles will be listed on individual node add/edit forms. If you wish for certain roles to be hidden from users, make sure they are not selected here.') . '</small>');
$roles = user_roles();
foreach ($roles as $id => $role) {
$form['roles'][$id]['name'] = array('#type' => 'hidden', '#value' => $role);
$form['roles'][$id]['allow'] = array('#type' => 'checkbox', '#title' => $role, '#default_value' => $allowed_roles[$id]);
}
$form['role_grants'] = array('#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#title' => 'Default Role Permissions',
'#tree' => TRUE,
'#theme' => 'nodeaccess_admin_roles');
foreach( $roles as $id => $role ) {
$form['role_grants'][$id]['name'] = array('#type' => 'hidden', '#value' => $role);
$form['role_grants'][$id]['grant_view'] = array('#type' => 'checkbox', '#default_value' => $author_prefs[$type]['grant_view']);
$form['role_grants'][$id]['grant_update'] = array('#type' => 'checkbox', '#default_value' => $author_prefs[$type]['grant_view']);
$form['role_grants'][$id]['grant_delete'] = array('#type' => 'checkbox', '#default_value' => $author_prefs[$type]['grant_view']);
}
// user based permissions
$form['user_grants'] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#title' => t('Default User Permissions'),
'#tree' => TRUE,
'#theme' => 'nodeaccess_admin_users',
'#attached' => array(
'js' => array( drupal_get_path('module', 'nodeaccess') . '/nodeaccess-admin.js' ),
'css' => array( drupal_get_path('module', 'nodeaccess') . '/nodeaccess.css' ),
'library' => array( array( 'system', 'ui.dialog' ) )
)
);
$users = array();
foreach( $users as $id => $user ) {
$form['user_grants'][$id]['name'] = array('#type' => 'markup', '#value' => $user);
$form['user_grants'][$id]['grant_view'] = array('#type' => 'checkbox', '#default_value' => $author_prefs[$type]['grant_view']);
$form['user_grants'][$id]['grant_update'] = array('#type' => 'checkbox', '#default_value' => $author_prefs[$type]['grant_view']);
$form['user_grants'][$id]['grant_delete'] = array('#type' => 'checkbox', '#default_value' => $author_prefs[$type]['grant_view']);
}
// user search...
$header = array(
'username' => array('data' => t('Username'), 'field' => 'u.name'),
'mail' => array('data' => t('Email'), 'field' => 'u.mail')
);
$query = db_select('users', 'u');
$query->condition('u.uid', 0, '<>');
user_build_filter_query($query);
$count_query = clone $query;
$count_query->addExpression('COUNT(u.uid)');
$query = $query->extend('PagerDefault')->extend('TableSort');
$query
->fields('u', array('uid', 'name', 'mail'))
->limit(1)
->orderByHeader($header)
->setCountQuery($count_query);
$result = $query->execute();
$accounts = array();
foreach ($result as $account) {
$options[$account->uid] = array(
'username' => theme('username', array('account' => $account)),
'mail' => $account->mail,
);
}
$form['user_grants']['filter'] = array(
'#type' => 'textfield',
'#title' => t('Filter Users'),
'#size' => 20
);
$form['user_grants']['search']['accounts'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#empty' => t('No people available.'),
);
$form['user_grants']['search']['pager'] = array('#markup' => theme('pager'));
return $form;
}