field_permissions.admin.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <?php
  2. /**
  3. * @file
  4. * Administrative interface for the Field Permissions module.
  5. */
  6. /**
  7. * Obtain the list of field permissions.
  8. *
  9. * @param $field_label
  10. * The human readable name of the field to use when constructing permission
  11. * names. Usually this will be derived from one or more of the field instance
  12. * labels.
  13. */
  14. function field_permissions_list($field_label = '') {
  15. return array(
  16. 'create' => array(
  17. 'label' => t('Create field'),
  18. 'title' => t('Create own value for field %field', array('%field' => $field_label)),
  19. ),
  20. 'edit own' => array(
  21. 'label' => t('Edit own field'),
  22. 'title' => t('Edit own value for field %field', array('%field' => $field_label)),
  23. ),
  24. 'edit' => array(
  25. 'label' => t('Edit field'),
  26. 'title' => t("Edit anyone's value for field %field", array('%field' => $field_label)),
  27. ),
  28. 'view own' => array(
  29. 'label' => t('View own field'),
  30. 'title' => t('View own value for field %field', array('%field' => $field_label)),
  31. ),
  32. 'view' => array(
  33. 'label' => t('View field'),
  34. 'title' => t("View anyone's value for field %field", array('%field' => $field_label)),
  35. ),
  36. );
  37. }
  38. /**
  39. * Returns field permissions in a format suitable for use in hook_permission().
  40. *
  41. * @param $field
  42. * The field to return permissions for.
  43. * @param $label
  44. * (optional) The human readable name of the field to use when constructing
  45. * permission names; for example, this might be the label of one of the
  46. * corresponding field instances. If not provided, an appropriate label will
  47. * be automatically derived from all the field's instances.
  48. *
  49. * @return
  50. * An array of permission information, suitable for use in hook_permission().
  51. */
  52. function field_permissions_list_field_permissions($field, $label = NULL) {
  53. $description = '';
  54. // If there is no preferred label, construct one from all the instance
  55. // labels.
  56. if (!isset($label)) {
  57. $labels = array();
  58. foreach ($field['bundles'] as $entity_type => $bundles) {
  59. foreach ($bundles as $bundle_name) {
  60. $instance = field_info_instance($entity_type, $field['field_name'], $bundle_name);
  61. $labels[] = $instance['label'];
  62. }
  63. }
  64. // If all the instances have the same label, just use that. Otherwise, use
  65. // the field name (with the full list of instance labels as the permission
  66. // description).
  67. $labels = array_unique($labels);
  68. if (count($labels) == 1) {
  69. $label = array_shift($labels);
  70. }
  71. else {
  72. $label = $field['field_name'];
  73. $description = t('This field appears as: %instances', array('%instances' => implode(', ', $labels)));
  74. }
  75. }
  76. $permissions = array();
  77. foreach (field_permissions_list($label) as $permission_type => $permission_info) {
  78. $permission = $permission_type . ' ' . $field['field_name'];
  79. $permissions[$permission] = array(
  80. 'title' => $permission_info['title'],
  81. 'description' => $description,
  82. );
  83. }
  84. return $permissions;
  85. }
  86. /**
  87. * Implementation of hook_permission().
  88. */
  89. function _field_permissions_permission() {
  90. $perms = array(
  91. 'administer field permissions' => array(
  92. 'title' => t('Administer field permissions'),
  93. 'description' => t('Manage field permissions and field permissions settings.'),
  94. 'restrict access' => TRUE,
  95. ),
  96. 'access private fields' => array(
  97. 'title' => t("Access other users' private fields"),
  98. 'description' => t('View and edit the stored values of all private fields.'),
  99. 'restrict access' => TRUE,
  100. ),
  101. );
  102. foreach (field_info_fields() as $field) {
  103. if (isset($field['field_permissions']['type']) && $field['field_permissions']['type'] == FIELD_PERMISSIONS_CUSTOM) {
  104. $perms += field_permissions_list_field_permissions($field);
  105. }
  106. }
  107. return $perms;
  108. }
  109. /**
  110. * Alter the field settings form.
  111. */
  112. function _field_permissions_field_settings_form_alter(&$form, $form_state, $form_id) {
  113. // Put the field permissions extensions at the top of the field settings
  114. // fieldset.
  115. $form['field']['field_permissions'] = array(
  116. '#weight' => -10,
  117. '#access' => user_access('administer field permissions'),
  118. );
  119. $form['field']['field_permissions']['type'] = array(
  120. '#title' => t('Field visibility and permissions'),
  121. '#type' => 'radios',
  122. '#options' => array(
  123. FIELD_PERMISSIONS_PUBLIC => t('Public (author and administrators can edit, everyone can view)'),
  124. FIELD_PERMISSIONS_PRIVATE => t('Private (only author and administrators can edit and view)'),
  125. FIELD_PERMISSIONS_CUSTOM => t('Custom permissions'),
  126. ),
  127. '#default_value' => isset($form['#field']['field_permissions']['type']) ? $form['#field']['field_permissions']['type'] : FIELD_PERMISSIONS_PUBLIC,
  128. );
  129. // Add the container in which the field permissions matrix will be displayed.
  130. // (and make it so that it is only visible when custom permissions are being
  131. // used).
  132. $form['field']['field_permissions']['permissions'] = array(
  133. '#type' => 'container',
  134. '#states' => array(
  135. 'visible' => array(
  136. // We must cast this to a string until http://drupal.org/node/879580 is
  137. // fixed.
  138. ':input[name="field[field_permissions][type]"]' => array('value' => (string) FIELD_PERMISSIONS_CUSTOM),
  139. ),
  140. ),
  141. // Custom styling for the permissions matrix on the field settings page.
  142. '#attached' => array(
  143. 'css' => array(drupal_get_path('module', 'field_permissions') . '/field_permissions.admin.css'),
  144. ),
  145. );
  146. // Add the field permissions matrix itself. Wait until the #pre_render stage
  147. // to move it to the above container, to avoid having the permissions data
  148. // saved as part of the field record.
  149. $form['field_permissions']['#tree'] = TRUE;
  150. $form['field_permissions']['#access'] = user_access('administer field permissions');
  151. $form['field_permissions']['permissions'] = field_permissions_permissions_matrix($form['#field'], $form['#instance']);
  152. $form['#pre_render'][] = '_field_permissions_field_settings_form_pre_render';
  153. // Add a submit handler to process the field permissions settings. Note that
  154. // it is important for this to run *after* the main field UI submit handler
  155. // (which saves the field itself), since when a new field is being created,
  156. // our submit handler will try to assign any new custom permissions
  157. // immediately, and our hook_permission() implementation relies on the field
  158. // info being up-to-date in order for that to work correctly.
  159. $form['#submit'][] = '_field_permissions_field_settings_form_submit';
  160. }
  161. /**
  162. * Returns a field permissions matrix that can be inserted into a form.
  163. *
  164. * The matrix's display is based on that of Drupal's default permissions page.
  165. *
  166. * Note that this matrix must be accompanied by an appropriate submit handler
  167. * (attached to the top level of the form) in order for the permissions in it
  168. * to actually be saved. For an example submit handler, see
  169. * _field_permissions_field_settings_form_submit().
  170. *
  171. * @param $field
  172. * The field whose permissions will be displayed in the matrix.
  173. * @param $instance
  174. * The field instance for which the permissions will be displayed. Although
  175. * the permissions are per-field rather than per-instance, the instance label
  176. * will be used to display an appropriate human-readable name for each
  177. * permission.
  178. *
  179. * @return
  180. * A form array defining the permissions matrix.
  181. *
  182. * @see user_admin_permissions()
  183. * @see _field_permissions_field_settings_form_submit()
  184. */
  185. function field_permissions_permissions_matrix($field, $instance) {
  186. // This function primarily contains a simplified version of the code from
  187. // user_admin_permissions().
  188. $form['#theme'] = 'user_admin_permissions';
  189. $options = array();
  190. $status = array();
  191. // Retrieve all role names for use in the submit handler.
  192. $role_names = user_roles();
  193. $form['role_names'] = array(
  194. '#type' => 'value',
  195. '#value' => $role_names,
  196. );
  197. // Retrieve the permissions for each role, and the field permissions we will
  198. // be assigning here.
  199. $role_permissions = user_role_permissions($role_names);
  200. $field_permissions = field_permissions_list_field_permissions($field, $instance['label']);
  201. // Determine if it is safe to reset the default values for this field's
  202. // permissions. If this is a new field (never saved with field permission
  203. // data before), or if it's an existing field that is not currently using
  204. // custom permissions and doesn't have any previously-saved ones already in
  205. // the database, then it will be safe to reset them.
  206. $reset_permissions_defaults = FALSE;
  207. if (!isset($field['field_permissions']['type'])) {
  208. $reset_permissions_defaults = TRUE;
  209. }
  210. elseif ($field['field_permissions']['type'] != FIELD_PERMISSIONS_CUSTOM) {
  211. $all_assigned_permissions = call_user_func_array('array_merge_recursive', $role_permissions);
  212. $assigned_field_permissions = array_intersect_key($all_assigned_permissions, $field_permissions);
  213. $reset_permissions_defaults = empty($assigned_field_permissions);
  214. }
  215. // Store this information on the form so that other modules can use it (for
  216. // example, if they want to set default permissions for other roles besides
  217. // the admin role which we use it for below).
  218. $form['#field_permissions_are_new'] = $reset_permissions_defaults;
  219. // Go through each field permission we will display.
  220. foreach ($field_permissions as $permission => $info) {
  221. // Display the name of the permission as a form item.
  222. $form['permission'][$permission] = array(
  223. '#type' => 'item',
  224. '#markup' => $info['title'],
  225. );
  226. // Save it to be displayed as one of the role checkboxes.
  227. $options[$permission] = '';
  228. // If we are in a situation where we can reset the field permissions
  229. // defaults, we do so by pre-checking the admin role's checkbox for this
  230. // permission.
  231. if ($reset_permissions_defaults) {
  232. if (($admin_rid = variable_get('user_admin_role', 0)) && isset($role_names[$admin_rid])) {
  233. $status[$admin_rid][] = $permission;
  234. }
  235. // For fields attached to users, we also pre-check the anonymous user's
  236. // checkbox for the permission to create the field, since that is the
  237. // most common way in which new user entities are created.
  238. if ($instance['entity_type'] == 'user' && $permission == 'create ' . $field['field_name']) {
  239. $status[DRUPAL_ANONYMOUS_RID][] = $permission;
  240. }
  241. }
  242. // Otherwise (e.g., for fields with custom permissions already saved),
  243. // determine whether the permission is already assigned and check each
  244. // checkbox accordingly.
  245. else {
  246. foreach ($role_names as $rid => $name) {
  247. if (isset($role_permissions[$rid][$permission])) {
  248. $status[$rid][] = $permission;
  249. }
  250. }
  251. }
  252. }
  253. // Build the checkboxes for each role.
  254. foreach ($role_names as $rid => $name) {
  255. $form['checkboxes'][$rid] = array(
  256. '#type' => 'checkboxes',
  257. '#options' => $options,
  258. '#default_value' => isset($status[$rid]) ? $status[$rid] : array(),
  259. '#attributes' => array('class' => array('rid-' . $rid)),
  260. );
  261. $form['role_names'][$rid] = array('#markup' => check_plain($name), '#tree' => TRUE);
  262. }
  263. // Attach the default permissions page JavaScript.
  264. $form['#attached']['js'][] = drupal_get_path('module', 'user') . '/user.permissions.js';
  265. // Attach our custom JavaScript for the permission matrix.
  266. $form['#attached']['js'][] = drupal_get_path('module', 'field_permissions') . '/field_permissions.admin.js';
  267. return $form;
  268. }
  269. /**
  270. * Pre-render function for the permissions matrix on the field settings form.
  271. */
  272. function _field_permissions_field_settings_form_pre_render($form) {
  273. // Move the permissions matrix to its final location.
  274. $form['field']['field_permissions']['permissions']['matrix'] = $form['field_permissions']['permissions'];
  275. unset($form['field_permissions']);
  276. return $form;
  277. }
  278. /**
  279. * Form callback; Submit handler for the Field Settings form.
  280. */
  281. function _field_permissions_field_settings_form_submit($form, &$form_state) {
  282. // Save the field permissions when appropriate to do so.
  283. $new_field_permissions_type = $form_state['values']['field']['field_permissions']['type'];
  284. if ($new_field_permissions_type == FIELD_PERMISSIONS_CUSTOM && isset($form_state['values']['field_permissions']['permissions'])) {
  285. $field_permissions = $form_state['values']['field_permissions']['permissions'];
  286. foreach ($field_permissions['role_names'] as $rid => $name) {
  287. user_role_change_permissions($rid, $field_permissions['checkboxes'][$rid]);
  288. }
  289. }
  290. // We must clear the page and block caches whenever the field permission type
  291. // setting has changed (because users may now be allowed to see a different
  292. // set of fields). For similar reasons, we must clear these caches whenever
  293. // custom field permissions are being used, since those may have changed too;
  294. // see user_admin_permissions_submit().
  295. if (!isset($form['#field']['field_permissions']['type']) || $new_field_permissions_type != $form['#field']['field_permissions']['type'] || $new_field_permissions_type == FIELD_PERMISSIONS_CUSTOM) {
  296. cache_clear_all();
  297. }
  298. }
  299. /**
  300. * Menu callback; Field permissions overview.
  301. */
  302. function field_permissions_overview() {
  303. drupal_add_css(drupal_get_path('module', 'field_permissions') .'/field_permissions.admin.css');
  304. $headers = array(t('Field name'), t('Field type'), t('Entity type'), t('Used in'));
  305. foreach (field_permissions_list() as $permission_type => $permission_info) {
  306. $headers[] = array('data' => $permission_info['label'], 'class' => 'field-permissions-header');
  307. }
  308. $destination = drupal_get_destination();
  309. // Load list of field instances, types and bundles in the system.
  310. $instances = field_info_instances();
  311. $field_types = field_info_field_types();
  312. $bundles = field_info_bundles();
  313. // Retrieve the permissions for each role.
  314. $role_permissions = user_role_permissions(user_roles());
  315. // Based on field_ui_fields_list() in field_ui.admin.inc.
  316. $rows = array();
  317. foreach ($instances as $obj_type => $type_bundles) {
  318. foreach ($type_bundles as $bundle => $bundle_instances) {
  319. foreach ($bundle_instances as $field_name => $instance) {
  320. // Each field will have a row in the table.
  321. $field = field_info_field($field_name);
  322. $admin_path = _field_ui_bundle_admin_path($obj_type, $bundle);
  323. $rows[$field_name]['data'][0] = $field['locked'] ? t('@field_name (Locked)', array('@field_name' => $field_name)) : $field_name;
  324. $rows[$field_name]['data'][1] = t($field_types[$field['type']]['label']);
  325. $rows[$field_name]['data'][2] = $obj_type;
  326. $rows[$field_name]['data'][3][] = l($bundles[$obj_type][$bundle]['label'], $admin_path . '/fields/'. $field_name, array(
  327. 'query' => $destination,
  328. 'fragment' => 'edit-field-field-permissions-type',
  329. ));
  330. $rows[$field_name]['class'] = $field['locked'] ? array('menu-disabled') : array('');
  331. // Append field permissions information to the report.
  332. $type = isset($field['field_permissions']['type']) ? $field['field_permissions']['type'] : FIELD_PERMISSIONS_PUBLIC;
  333. foreach (array_keys(field_permissions_list_field_permissions($field)) as $index => $permission) {
  334. // Put together the data value for the cell.
  335. $data = '';
  336. $full_colspan = FALSE;
  337. if ($type == FIELD_PERMISSIONS_PUBLIC) {
  338. $data = t('Public field (author and administrators can edit, everyone can view)');
  339. $full_colspan = TRUE;
  340. }
  341. elseif ($type == FIELD_PERMISSIONS_PRIVATE) {
  342. $data = t('Private field (only author and administrators can edit and view)');
  343. $full_colspan = TRUE;
  344. }
  345. else {
  346. // This is a field with custom permissions. Link the field to the
  347. // appropriate row of the permissions page, and theme it based on
  348. // whether all users have access.
  349. $all_users_have_access = isset($role_permissions[DRUPAL_ANONYMOUS_RID][$permission]) && isset($role_permissions[DRUPAL_AUTHENTICATED_RID][$permission]);
  350. $status_class = $all_users_have_access ? 'field-permissions-status-on' : 'field-permissions-status-off';
  351. $title = $all_users_have_access ? t('All users have this permission') : t('Not all users have this permission');
  352. $data = l('', 'admin/people/permissions', array(
  353. 'attributes' => array(
  354. 'class' => array('field-permissions-status', $status_class),
  355. 'title' => $title,
  356. ),
  357. 'query' => $destination,
  358. 'fragment' => drupal_html_class("edit $permission"),
  359. ));
  360. }
  361. // Construct the cell.
  362. $rows[$field_name]['data'][4 + $index] = array(
  363. 'data' => $data,
  364. 'class' => array('field-permissions-cell'),
  365. );
  366. if ($full_colspan) {
  367. $rows[$field_name]['data'][4 + $index]['colspan'] = 5;
  368. break;
  369. }
  370. }
  371. }
  372. }
  373. }
  374. foreach ($rows as $field_name => $cell) {
  375. $rows[$field_name]['data'][3] = implode(', ', $cell['data'][3]);
  376. }
  377. if (empty($rows)) {
  378. $output = t('No fields have been defined for any content type yet.');
  379. }
  380. else {
  381. // Sort rows by field name.
  382. ksort($rows);
  383. // Allow external modules alter the table headers and rows.
  384. foreach (module_implements('field_permissions_overview_alter') as $module) {
  385. $function = $module .'_field_permissions_overview_alter';
  386. $function($headers, $rows);
  387. }
  388. $output = theme('table', array('header' => $headers, 'rows' => $rows));
  389. }
  390. return $output;
  391. }