cck_field_perms.module

Tracking 5.x-1.x branch
  1. drupal
    1. 5 contributions/cck_field_perms/cck_field_perms.module

Functions & methods

NameDescription
cck_field_perms_admin_settingspage for the admin settings form use this instead of standard settings for b/c we serialize all the data into one variable
cck_field_perms_admin_settings_formcreates the admin form
cck_field_perms_admin_settings_form_submitsave the incoming values
cck_field_perms_alter_optionsdefines the types of form alterting that can be done with data
cck_field_perms_form_alterImplementation of hook form_alter
cck_field_perms_helpImplementation of hook_help().
cck_field_perms_menuImplentation of hook menu
cck_field_perms_nodeapiImplementation of hook_nodeapi removes fields if a user does not have perms to view it
cck_field_perms_permImpementation of hook perms
cck_field_perms_verbsDefines the CRUD. @ TODO make these translatable
_cfp_content_to_readable
_cfp_field_perms_name_pairdisplay helper retrieve both the human and machine readable form of the permission allows cck fields to change labels without losing permission settings
_cfp_form_group_fieldset_helpergets list of form groups from a form and removes field groups if the user does not have permission helper function for the form alter
_cfp_form_helperhelper function to unset form values
_cfp_group_form_fieldsfetches the list of fields contained within a group returns fieldset form of check boxes to handle content

File

View source
  1. <?php
  2. /*
  3. * This module adds the ability to restrict some fields in a CCK node
  4. * to certian user roles based on create, updated, and view. Note that
  5. * when this module is enabled, you must positively grant view access
  6. * if you want users to have view access.
  7. *
  8. *
  9. */
  10. /* ********************************************* */
  11. /* Drupal functions */
  12. /* ********************************************* */
  13. /**
  14. * Implementation of hook_help().
  15. */
  16. function cck_field_perms_help($section) {
  17. switch ($section) {
  18. case 'admin/help#cck_field_perms':
  19. return t('<p>Gives the option of limited access to CCK data for user roles.</p>');
  20. break;
  21. }
  22. }
  23. /**
  24. * Implentation of hook menu
  25. */
  26. function cck_field_perms_menu($may_cache) {
  27. $items = array();
  28. $items[] = array(
  29. 'path' => 'admin/settings/cck_field_perms',
  30. 'title' => t('CCK Field Permissions'),
  31. 'description' => t('Restrict create, update and view access on specific CCK fields.'),
  32. 'callback' => 'cck_field_perms_admin_settings',
  33. 'access' => user_access('administer cck field permissions'),
  34. 'type' => MENU_NORMAL_ITEM,
  35. );
  36. return $items;
  37. }
  38. /**
  39. * Impementation of hook perms
  40. */
  41. function cck_field_perms_perm() {
  42. $perm_list[] = "administer cck field permissions";
  43. $field_perms = unserialize(variable_get('cfp_values', null));
  44. if ($field_perms) {
  45. foreach ($field_perms as $type_name => $fields) {
  46. foreach ($fields as $field_name => $value) {
  47. if ($value) {
  48. foreach (cck_field_perms_verbs() as $verb) {
  49. $perm_list[] = _cfp_content_to_readable($type_name, $field_name, $verb);
  50. }
  51. }
  52. }
  53. }
  54. }
  55. return $perm_list;
  56. }
  57. /**
  58. * Implementation of hook_nodeapi
  59. * removes fields if a user does not have perms to view it
  60. */
  61. function cck_field_perms_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  62. switch ($op) {
  63. // prevent viewing of fields
  64. case 'view':
  65. $type = $node->type;
  66. if ($types = variable_get('cfp_types', null)) {
  67. if ($types[$type]) {
  68. $disallowed_fields = unserialize(variable_get('cfp_values', null));
  69. if ($disallowed_fields) {
  70. foreach ($disallowed_fields[$type] as $disallowed_field => $value ) {
  71. if ($value == 0) {continue; }
  72. if (!(user_access(_cfp_content_to_readable($type, $disallowed_field, "view")))) {
  73. $node->$disallowed_field['#access'] = false;
  74. $node->content[$disallowed_field]['#access'] = false;
  75. }
  76. }
  77. }
  78. }
  79. }
  80. break;
  81. }
  82. }
  83. /**
  84. * Implementation of hook form_alter
  85. */
  86. function cck_field_perms_form_alter($form_id, &$form) {
  87. //Alter the permissions page to show cck field labels instead of machine readable cck field names
  88. //Still store permissions by machine readable name
  89. if($form_id == 'user_admin_perm') {
  90. foreach(_cfp_field_perms_name_pair() as $field_name => $field_label) {
  91. $form['permission'][$field_name]['#value'] = $field_label;
  92. }
  93. return;
  94. }
  95. if ($form['#id'] != 'node-form') return;
  96. // detect if we're editing or creating
  97. if ($form['#node']->nid) {
  98. global $user;
  99. $verb = $form['uid']['#value'] == $user->uid ? "edit own" : "edit";
  100. }
  101. else {
  102. $verb = "create";
  103. }
  104. $form = _cfp_form_helper($form_id, $form, $verb);
  105. }
  106. /* ********************************************* */
  107. /* CFP functions */
  108. /* ********************************************* */
  109. /**
  110. * Defines the CRUD.
  111. * @ TODO make these translatable
  112. */
  113. function cck_field_perms_verbs() {
  114. return array('create', 'edit', 'edit own', 'view');
  115. }
  116. /**
  117. * defines the types of form alterting that can be done with data
  118. */
  119. function cck_field_perms_alter_options() {
  120. $options = array(
  121. 0 => t('Hide data'),
  122. 1 => t('Show data, but disable form'),
  123. );
  124. return $options;
  125. }
  126. /**
  127. * display helper
  128. * retrieve both the human and machine readable form of the permission
  129. * allows cck fields to change labels without losing permission settings
  130. *
  131. * @return array
  132. */
  133. function _cfp_field_perms_name_pair(){
  134. $perm_list = array();
  135. $field_perms = unserialize(variable_get('cfp_values', null));
  136. if ($field_perms) {
  137. foreach ($field_perms as $type_name => $fields) {
  138. foreach ($fields as $field_name => $value) {
  139. if ($value) {
  140. foreach (cck_field_perms_verbs() as $verb){
  141. $perm_list[_cfp_content_to_readable($type_name, $field_name, $verb)]
  142. = _cfp_content_to_readable($type_name, $field_name, $verb, False);
  143. }
  144. }
  145. }
  146. }
  147. }
  148. return $perm_list;
  149. }
  150. /**
  151. * perms helper
  152. * converts drupal content names to human readable names
  153. */
  154. function _cfp_content_to_readable ($content_type, $field_name, $verb, $by_field_name = True) {
  155. static $content_types;
  156. if (! $content_types) {$content_types = content_types();}
  157. // support fieldgroup content and return the label for display if requested
  158. if ($by_field_name || strstr($field_name, "group_")) {
  159. $output = $verb ." ". $content_types[$content_type]['name'] ." ". $field_name;
  160. } else {
  161. $output = $verb ." ". $content_types[$content_type]['name'] ." ". $content_types[$content_type]['fields'][$field_name]['widget']['label'];
  162. }
  163. return $output;
  164. }
  165. /**
  166. * gets list of form groups from a form and removes
  167. * field groups if the user does not have permission
  168. * helper function for the form alter
  169. *
  170. */
  171. function _cfp_form_group_fieldset_helper(&$form, $disallowed, $content_type, $verb, $type_setting) {
  172. if (module_exists("fieldgroup")) {
  173. foreach ($form as $field_name => $value) {
  174. if ($value == 0 ) {continue;}
  175. if ($disallowed[$field_name]) {
  176. if (! user_access(_cfp_content_to_readable($content_type, $field_name, $verb))) {
  177. // if this is a fieldset change acces
  178. if($form[$field_name]['#type'] == 'fieldset') {
  179. $form[$field_name]['#access'] = false;
  180. }
  181. elseif ($type_setting[$field_name] == 1) {
  182. $form[$field_name]['keys']['#disabled'] = true;
  183. }
  184. else {
  185. $form[$field_name]['keys']['#access'] = false;
  186. }
  187. }
  188. }
  189. // check this item to see if it is a group
  190. // if group, recurse to check for sub groups and or fieldsets
  191. if (strstr($field_name, "group")) {
  192. if (is_array($form[$field_name])) {
  193. _cfp_form_group_fieldset_helper($form[$field_name], $disallowed, $content_type, $verb, $type_setting);
  194. }
  195. }
  196. }
  197. }
  198. }
  199. /**
  200. * helper function to unset form values
  201. *
  202. * @form is drupal form
  203. * @verb is create or update
  204. * @return is the modified drupal form
  205. *
  206. */
  207. function _cfp_form_helper($form_id, $form, $verb) {
  208. //content_video_node_form
  209. $types = variable_get('cfp_types', null);
  210. if ($types) {
  211. foreach ($types as $type) {
  212. if ($form_id == $type ."_node_form") {
  213. $disallowed_fields = unserialize(variable_get('cfp_values', null));
  214. if ($disallowed_fields) {
  215. $type_settings = unserialize(variable_get('cfp_display_types', ''));
  216. // removes fieldgroups
  217. _cfp_form_group_fieldset_helper($form, $disallowed_fields[$type], $type, $verb, $type_settings[$type]);
  218. // now check fields
  219. foreach ($disallowed_fields[$type] as $disallowed_field => $value) {
  220. if ($value == 0 ) {continue;}
  221. if (! user_access(_cfp_content_to_readable($type, $disallowed_field, $verb))) {
  222. //check the display type
  223. if ($type_settings[$type][$disallowed_field] == 1) {
  224. $form[$disallowed_field][0]['value']['#disabled'] = true;
  225. $form[$disallowed_field][0]['value']['#description'] .= ' '. t('You do not have access to edit this.');
  226. }
  227. else {
  228. $form[$disallowed_field][0]['value']['#access'] = false;
  229. }
  230. }
  231. }
  232. }
  233. }
  234. }
  235. }
  236. return $form;
  237. }
  238. /* ************************ */
  239. /* ADMIN Functions */
  240. /* ************************ */
  241. /**
  242. * page for the admin settings form
  243. * use this instead of standard settings for b/c we
  244. * serialize all the data into one variable
  245. */
  246. function cck_field_perms_admin_settings() {
  247. $output = drupal_get_form('cck_field_perms_admin_settings_form');
  248. return $output;
  249. }
  250. /**
  251. * creates the admin form
  252. */
  253. function cck_field_perms_admin_settings_form() {
  254. $form['field_perms'] = array(
  255. '#type' => 'fieldset',
  256. '#title' => t('Enable field permissions on content types.'),
  257. '#description' => t('This module allows an administrator to restrict CCK fields to some roles. '.
  258. 'Once you have enabled a content type, select which fields you\'d like to restrict. '.
  259. 'Then, go to '. l("admin/user/access", "admin/user/access") .' to allow user roles acces to that field. '.
  260. 'Please note: you must grant view access for a field once you enable this field to be permissions controlled. '.
  261. 'If you do not do this, no users will be able to view the field. '.
  262. '<p>This module can also restrict a CCK field from being indexed by Drupal\'s search engine.'),
  263. );
  264. $types = content_types();
  265. foreach ($types as $key => $value) {
  266. $options[$key] = $value['name'];
  267. }
  268. $enabled_types = variable_get('cfp_types', null);
  269. $form['field_perms']['cfp_types'] = array(
  270. '#type' => 'checkboxes',
  271. '#title' => t('Content types'),
  272. '#description' => t('Select content types for which you wish to enable permissions.'),
  273. '#options' => $options,
  274. '#default_value' => $enabled_types,
  275. );
  276. $the_settings = unserialize(variable_get('cfp_values', '' ));
  277. $type_settings = unserialize(variable_get('cfp_display_types', ''));
  278. $search_settings = unserialize(variable_get('cfp_search_limit', ''));
  279. if ($enabled_types) {
  280. foreach ($enabled_types as $enabled_type) {
  281. if ($enabled_type) {
  282. $form['field_fields'][$enabled_type] = array(
  283. '#type' => 'fieldset',
  284. '#title' => $types[$enabled_type]['name'] ." content fields",
  285. );
  286. if ($types[$enabled_type]['fields']) {
  287. // deal with grouped content
  288. _cfp_group_form_fields($form, $types[$enabled_type]['fields'], $enabled_type, $the_settings, $type_settings);
  289. // creates the table for the display
  290. $form['field_fields'][$enabled_type]['markup_top'] = array(
  291. '#type' => 'markup',
  292. '#value' => '<table>',
  293. );
  294. foreach ($types[$enabled_type]['fields'] as $field) {
  295. $field_perms[$field['field_name']] = $field['widget']['label'];
  296. $form['field_fields'][$enabled_type]["{$enabled_type}_{$field['field_name']}"]= array(
  297. '#type' => 'checkbox',
  298. '#title' => $field['widget']['label'],
  299. '#description' => t(''),
  300. '#default_value' => $the_settings[$enabled_type][$field['field_name']],
  301. '#description' => t("Enable permissions on this field."),
  302. '#prefix' => '<tr><td>',
  303. '#suffix' => '</td>',
  304. );
  305. $form['field_fields'][$enabled_type]["{$enabled_type}_{$field['field_name']}_cfp_type"] = array(
  306. '#title' => $field['widget']['label'] . t(' no permission settings'),
  307. '#type' => 'select',
  308. '#options' => cck_field_perms_alter_options(),
  309. '#default_value' => $type_settings[$enabled_type][$field['field_name']],
  310. '#description' => t('Sets how the field is displayed if the user does not have access'),
  311. '#prefix' => '<td>',
  312. '#suffix' => '</td>',
  313. );
  314. $form['field_fields'][$enabled_type]["{$enabled_type}_{$field['field_name']}_search"]= array(
  315. '#type' => 'checkbox',
  316. '#title' => $field['widget']['label'],
  317. '#description' => t('Prevent indexing'),
  318. '#default_value' => $search_settings[$enabled_type][$field['field_name']],
  319. '#description' => t('Prevent indexing of this field.'),
  320. '#prefix' => '<td>',
  321. '#suffix' => '</td></tr>',
  322. );
  323. }
  324. }
  325. // closes table
  326. $form['field_fields'][$enabled_type]['markup_bottom'] = array(
  327. '#type' => 'markup',
  328. '#value' => '</table>',
  329. );
  330. }
  331. }
  332. }
  333. $form['submit'] = array(
  334. '#type' => 'submit',
  335. '#value' => t('Save')
  336. );
  337. return $form;
  338. }
  339. /**
  340. * fetches the list of fields contained within a group
  341. * returns fieldset form of check boxes to handle content
  342. */
  343. function _cfp_group_form_fields(&$form, &$fields, $type, $values, $type_settings) {
  344. if (module_exists("fieldgroup")) {
  345. $groups = fieldgroup_groups($type);
  346. foreach ($groups as $group_name => $data) {
  347. $form['field_fields'][$type][$group_name ."_fields"] = array(
  348. '#type' => 'fieldset',
  349. '#title' => $data['label'] ." field group",
  350. );
  351. $form['field_fields'][$type][$group_name ."_fields"][$type ."_". $group_name] = array(
  352. '#type' => 'checkbox',
  353. '#title' => $data['label'],
  354. '#description' => t("Checking this box will hide this complete field group"),
  355. '#default_value' => $values[$type][$group_name],
  356. );
  357. // creates the table for the display
  358. $form['field_fields'][$type][$group_name ."_fields"]['fields_markup_top'] = array(
  359. '#type' => 'markup',
  360. '#value' => '<table>',
  361. );
  362. foreach ($data['fields'] as $afield) {
  363. $form['field_fields'][$type][$group_name ."_fields"][$type ."_". $afield['field_name']] = array(
  364. '#type' => 'checkbox',
  365. '#title' => $afield['label'] ." field",
  366. '#description' => t('Checking this box will hide this field.'),
  367. '#default_value' => $values[$type][$afield['field_name']],
  368. '#prefix' => '<tr><td>',
  369. '#suffix' => '</td>',
  370. );
  371. // remove this field from the list of fields that we have to
  372. // prevent duplicates
  373. unset($fields[$afield['field_name']]);
  374. $form['field_fields'][$type][$group_name ."_fields"]["{$type}_{$afield['field_name']}_cfp_type"] = array(
  375. '#title' => $afield['label'] . t(' no permission settings'),
  376. '#type' => 'select',
  377. '#options' => cck_field_perms_alter_options(),
  378. '#default_value' => $type_settings[$type][$afield['field_name']],
  379. '#description' => t('Sets how the field is displayed if the user does not have access'),
  380. '#prefix' => '<td>',
  381. '#suffix' => '</td></tr>',
  382. );
  383. }
  384. // closes the table for the display
  385. $form['field_fields'][$type][$group_name ."_fields"]['fields_markup_bottom'] = array(
  386. '#type' => 'markup',
  387. '#value' => '</table>',
  388. );
  389. }
  390. }
  391. }
  392. /**
  393. * save the incoming values
  394. */
  395. function cck_field_perms_admin_settings_form_submit($form_id, $form_values) {
  396. // @ TODO figure out why the hell it is neccessary to do this
  397. // maybe body and title?
  398. if ($form_values['cfp_types'][0]) { unset($form_values['cfp_types'][0]); }
  399. if ($form_values['cfp_types'][1]) { unset($form_values['cfp_types'][1]); }
  400. variable_set('cfp_types', $form_values['cfp_types']);
  401. foreach ($form_values as $key => $value) {
  402. if (strstr($key, '_cfp_type')) {
  403. $type = substr($key, 0, strpos($key, "_field"));
  404. $field = str_replace('_cfp_type', '', substr($key, strpos($key, "field")));
  405. $display_types[$type][$field] = $value;
  406. }
  407. elseif (strstr($key, '_search')) {
  408. $type = substr($key, 0, strpos($key, "_field"));
  409. $field = str_replace('_search', '', substr($key, strpos($key, "field")));
  410. $limit_search[$type][$field] = $value;
  411. }
  412. elseif (strstr($key, '_field_') ) {
  413. $type = substr($key, 0, strpos($key, "_field"));
  414. $field = substr($key, strpos($key, "field"));
  415. $stored_values[$type][$field] = $value;
  416. }
  417. elseif (strstr($key, '_group_') ) {
  418. $type = substr($key, 0, strpos($key, "_group"));
  419. $field = substr($key, strpos($key, "group"));
  420. $stored_values[$type][$field] = $value;
  421. }
  422. }
  423. variable_set('cfp_values', serialize($stored_values));
  424. variable_set('cfp_display_types', serialize($display_types));
  425. variable_set('cfp_search_limit', serialize($limit_search));
  426. drupal_set_message("Remember to update ". l("admin/user/access", "admin/user/access") ." after you've changed permissions.");
  427. }