/[drupal]/contributions/modules/node_access_control/node_access_control_interface.inc
ViewVC logotype

Contents of /contributions/modules/node_access_control/node_access_control_interface.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Fri Jun 6 11:31:00 2008 UTC (17 months, 3 weeks ago) by rmuilwijk
Branch: MAIN
CVS Tags: DRUPAL-5--1-0-BETA1, HEAD
Branch point for: DRUPAL-5
Changes since 1.1: +2 -2 lines
File MIME type: text/x-php
Changed the url to our upcoming english based community site.
1 <?php
2 // $Id: node_access_control_interface.inc,v 1.1 2008/06/06 11:14:49 rmuilwijk Exp $
3
4 /**
5 * @file
6 * Provides the interfaces for node based access control
7 *
8 * This module was developed by Madcap, open.madcap.nl
9 *
10 * @author Raymond Muilwijk <r.muilwijk@madcap.nl>
11 */
12
13 /**
14 * Implementation of hook_menu().
15 * @see hook_menu()
16 *
17 * @param boolean $may_cache
18 * @return array
19 */
20 function node_access_control_menu($may_cache) {
21 $items = array();
22
23 if ($may_cache) {
24 $items[] = array(
25 'path' => 'admin/settings/node_access_control',
26 'title' => t('Node Access Control'),
27 'type' => MENU_NORMAL_ITEM,
28 'description' => t('Add permissions for use with node access control.'),
29 'callback' => 'node_access_control_settings',
30 'access' => user_access('administer site configuration'),
31 );
32 $items[] = array(
33 'path' => 'admin/settings/node_access_control/delete',
34 'type' => MENU_CALLBACK,
35 'callback' => 'node_access_control_delete',
36 'access' => user_access('administer site configuration'),
37 );
38 }
39
40 return $items;
41 }
42
43 /**
44 * Implementation of hook_form_alter().
45 * @see hook_form_alter()
46 *
47 * @param string $form_id
48 * @param array $form
49 */
50 function node_access_control_form_alter($form_id, &$form) {
51 if (user_access('administer node access')) {
52 if ($form_id == 'node_type_form') {
53 $form['workflow']['node_access_control_enabled'] = array(
54 '#type' => 'checkbox',
55 '#title' => t('Control Access'),
56 '#description' => t('Allow permissions for view, updating and deleting of nodes of this type to be dynamically generated'),
57 '#default_value' => variable_get('node_access_control_enabled_' . $form['#node_type']->type, FALSE)
58 );
59 }
60
61 if ($form['#id'] == 'node-form' && node_access_control_valid_type($form['#node']->type)) {
62 $form['node_access_control'] = array(
63 '#type' => 'fieldset',
64 '#title' => t('Node Access Control'),
65 '#collapsible' => TRUE,
66 '#collapsed' => FALSE,
67 '#tree' => TRUE,
68 '#weight' => 8
69 );
70
71 $options = array_merge(array(t('No access control')), node_access_control_get_permissions());
72
73 $form['node_access_control']['perm_view'] = array(
74 '#type' => 'select',
75 '#title' => t('View permission'),
76 '#options' => $options,
77 '#default_value' => node_access_control_get_perm($form['#node'], 'view'),
78 );
79 $form['node_access_control']['perm_update'] = array(
80 '#type' => 'select',
81 '#title' => t('Update permission'),
82 '#options' => $options,
83 '#default_value' => node_access_control_get_perm($form['#node'], 'update'),
84 );
85 $form['node_access_control']['perm_delete'] = array(
86 '#type' => 'select',
87 '#title' => t('Delete permission'),
88 '#options' => $options,
89 '#default_value' => node_access_control_get_perm($form['#node'], 'delete'),
90 );
91 }
92 }
93
94 // This part is not for the interface, it adds a submit function to permissions that are being saved
95 if ($form_id == 'user_admin_perm') {
96 $form['#submit'] = array_merge(
97 array('node_access_control_permission_set_role_cache_submit' => array()),
98 $form['#submit'],
99 array('node_access_control_permission_rebuild_access_submit' => array())
100 );
101 }
102 }
103
104 /**
105 * Callback for the 'admin/settings/node_access_control' path
106 *
107 * @return string
108 */
109 function node_access_control_settings() {
110 $output = '';
111 $perms = node_access_control_get_permissions();
112 $header = array(t('Permission'), t('Operations'));
113
114 $output .= drupal_get_form('node_access_control_add_permission_form');
115
116 $rows = array();
117 foreach ($perms as $perm) {
118 $rows[] = array($perm, l(t('Delete'), 'admin/settings/node_access_control/delete/' . $perm));
119 }
120
121 $output .= theme('table', $header, $rows);
122
123 return $output;
124 }
125
126 /**
127 * Form for adding permissions to node access control
128 *
129 * @return array
130 */
131 function node_access_control_add_permission_form() {
132 $form = array();
133
134 $form['node_access_control'] = array(
135 '#type' => 'fieldset',
136 '#title' => t('Add a new permission'),
137 '#collapsible' => FALSE,
138 '#collapsed' => FALSE,
139 '#tree' => FALSE,
140 );
141 $form['node_access_control']['permission'] = array(
142 '#type' => 'textfield',
143 '#title' => t('Permission name'),
144 '#description' => t('The name of the permission you want to add'),
145 '#required' => TRUE
146 );
147
148 $form['node_access_control']['add'] = array(
149 '#value' => t('Add'),
150 '#type' => 'submit'
151 );
152
153 return $form;
154 }
155
156 /**
157 * Validate function for node_access_control_add_permission_form
158 *
159 * @param string $form_id
160 * @param array $form
161 */
162 function node_access_control_add_permission_form_validate($form_id, $form) {
163 $all_perms = module_invoke_all('perm');
164
165 if (in_array($form['permission'], $all_perms)) {
166 form_set_error('permission', t('This permission already exists'));
167 }
168 if (check_plain($form['permission']) != $form['permission']) {
169 form_set_error('permission', t('You are not allowed to use special characters'));
170 }
171 if (trim($form['permission']) != $form['permission']) {
172 form_set_error('permission', t('You are not allowed to begin or end with spaces'));
173 }
174 }
175
176 /**
177 * Submit function for node_access_control_add_permission_form
178 *
179 * @param string $form_id
180 * @param array $form
181 */
182 function node_access_control_add_permission_form_submit($form_id, $form) {
183 $perms = node_access_control_get_permissions();
184
185 $perms[$form['permission']] = $form['permission'];
186
187 node_access_control_set_permissions($perms);
188 }
189
190 /**
191 * Callback for 'admin/settings/node_access_control/delete'
192 *
193 * @return string
194 */
195 function node_access_control_delete() {
196 $perm = arg(4); // Path: 'admin/settings/node_access_control/delete/$permission'
197
198 $perms = node_access_control_get_permissions();
199
200 if (!isset($perms[$perm])) {
201 return MENU_ACCESS_DENIED;
202 }
203
204 drupal_set_title(t('Node Access Control Delete: !perm', array('!perm' => $perm)));
205
206 $rs = db_query(
207 "SELECT DISTINCT nid
208 FROM {node_access_control}
209 WHERE (perm_view='%s' OR perm_update='%s' OR perm_delete='%s')",
210 $perm, $perm, $perm
211 );
212
213 if (db_num_rows($rs) > 0) {
214 $nodes = array();
215 while ($row = db_fetch_array($rs)) {
216 $node = node_load($row['nid']);
217 $nodes[] = l($node->title, 'node/' . $node->nid);
218 }
219
220 drupal_set_message(t('Some nodes are still using this permission, please remove them before deleting this permission.'), 'error');
221 return theme('item_list', $nodes);
222 }
223
224 return drupal_get_form('node_access_control_delete_form', $perm);
225 }
226
227 /**
228 * Form for deleting permissions generated by node access control
229 *
230 * @param string $perm
231 * @return array
232 */
233 function node_access_control_delete_form($perm) {
234 $form = array();
235
236 $form['permission'] = array(
237 '#type' => 'value',
238 '#value' => $perm
239 );
240
241 return confirm_form(
242 $form,
243 t('Are you sure you want to delete "!perm"?', array('!perm' => $perm)),
244 'admin/settings/node_access_control',
245 t('This action cannot be undone.'),
246 t('Delete'),
247 t('Cancel')
248 );
249 }
250
251 /**
252 * Submit function for node_access_control_delete_form
253 *
254 * @param string $form_id
255 * @param array $form
256 * @return string
257 */
258 function node_access_control_delete_form_submit($form_id, $form) {
259 $perms = node_access_control_get_permissions();
260
261 unset($perms[$form['permission']]);
262 node_access_control_set_permissions($perms);
263
264 return 'admin/settings/node_access_control';
265 }

  ViewVC Help
Powered by ViewVC 1.1.2