field_permissions.module 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. /**
  3. * @file
  4. * This is the main script for the Field Permissions module. It merely contains
  5. * the implementation of hooks invoked by Drupal core and CCK.
  6. * All common functions are externalized into several scripts that are included
  7. * on demand to save memory consumption during normal site operation.
  8. */
  9. /**
  10. * Indicates that a field does not have any access control.
  11. */
  12. define('FIELD_PERMISSIONS_PUBLIC', 0);
  13. /**
  14. * Indicates that a field is private.
  15. *
  16. * Private fields are never displayed, and are only editable by the author (and
  17. * by site administrators with the 'access private fields' permission).
  18. */
  19. define('FIELD_PERMISSIONS_PRIVATE', 1);
  20. /**
  21. * Indicates that a field has custom permissions.
  22. */
  23. define('FIELD_PERMISSIONS_CUSTOM', 2);
  24. /**
  25. * Implements hook_help().
  26. */
  27. function field_permissions_help($path, $arg) {
  28. switch ($path) {
  29. // Main module help for the Field Permissions module.
  30. case 'admin/help#field_permissions':
  31. return '<p>' . t('Set field-level permissions to edit or view CCK fields in any node, edit field during node creation, and edit or view permissions for nodes owned by the current user.') . '</p>';
  32. // Help for the Field Permissions overview page.
  33. case 'admin/reports/fields/permissions':
  34. return '<p>' . t('Report and troubleshoot field permissions.') . '</p>';
  35. }
  36. }
  37. /**
  38. * Implements hook_menu().
  39. */
  40. function field_permissions_menu() {
  41. $items['admin/reports/fields/list'] = array(
  42. 'title' => 'List',
  43. 'type' => MENU_DEFAULT_LOCAL_TASK,
  44. 'weight' => -10,
  45. );
  46. $items['admin/reports/fields/permissions'] = array(
  47. 'title' => 'Permissions',
  48. 'description' => 'Report and troubleshoot field permissions.',
  49. 'page callback' => 'field_permissions_overview',
  50. 'access arguments' => array('administer field permissions'),
  51. 'file' => 'field_permissions.admin.inc',
  52. 'type' => MENU_LOCAL_TASK,
  53. 'weight' => 0,
  54. );
  55. return $items;
  56. }
  57. /**
  58. * Implementation of hook_permission().
  59. */
  60. function field_permissions_permission() {
  61. module_load_include('inc', 'field_permissions', 'field_permissions.admin');
  62. return _field_permissions_permission();
  63. }
  64. /**
  65. * Implements of hook_form_FORM_ID_alter().
  66. */
  67. function field_permissions_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
  68. // Injects the Field Permissions settings on the Edit field tab.
  69. form_load_include($form_state, 'inc', 'field_permissions', 'field_permissions.admin');
  70. return _field_permissions_field_settings_form_alter($form, $form_state, $form_id);
  71. }
  72. /**
  73. * Implementation of hook_field_access().
  74. *
  75. * @param $op
  76. * The operation to be performed. Possible values:
  77. * - 'edit'
  78. * - 'view'
  79. * @param $field
  80. * The field on which the operation is to be performed.
  81. * @param $entity_type
  82. * The type of entity; e.g. 'node' or 'user'.
  83. * @param $entity
  84. * The entity on which the operation is to be performed.
  85. * @param $account
  86. * The account to check.
  87. *
  88. * @return
  89. * FALSE if the operation is not allowed.
  90. * Note when field_access() is invoked, access is granted unless one
  91. * implementation of hook_field_access() explicitly returns FALSE.
  92. *
  93. * @see field_access()
  94. */
  95. function field_permissions_field_access($op, $field, $entity_type, $entity, $account) {
  96. // Ignore the request if permissions have not been enabled for this field.
  97. if (!isset($field['field_permissions']['type']) || $field['field_permissions']['type'] == FIELD_PERMISSIONS_PUBLIC) {
  98. return;
  99. }
  100. // If the field is private, then only the author (and administrators with the
  101. // 'access private fields' permissions) can view and edit it.
  102. elseif ($field['field_permissions']['type'] == FIELD_PERMISSIONS_PRIVATE) {
  103. if (isset($entity)) {
  104. return _field_permissions_entity_is_owned_by_account($entity, $account) || user_access('access private fields', $account);
  105. }
  106. // If the entity does not exist, we must check if there is access to any
  107. // entity; see comments in field_permissions_empty_entity_access(). In this
  108. // case that will always be true, since private fields are always editable
  109. // by their authors and in theory any user account can be the author of
  110. // some entity on the site.
  111. else {
  112. return TRUE;
  113. }
  114. }
  115. // Otherwise, check access by permission.
  116. elseif ($field['field_permissions']['type'] == FIELD_PERMISSIONS_CUSTOM) {
  117. if (!isset($entity)) {
  118. return field_permissions_empty_entity_access($op, $field['field_name'], $account);
  119. }
  120. elseif ($op == 'view') {
  121. return _field_permissions_field_view_access($field['field_name'], $entity_type, $entity, $account);
  122. }
  123. elseif ($op == 'edit') {
  124. return _field_permissions_field_edit_access($field['field_name'], $entity_type, $entity, $account);
  125. }
  126. }
  127. }
  128. /**
  129. * Determines custom field permissions access when the entity is unknown.
  130. *
  131. * When a module calls field_access() without providing an entity (which the
  132. * API allows it to do), it is doing so in order to check generic access to the
  133. * field. Therefore, we should only deny access if we know that there is no
  134. * entity anywhere on the site for which the user has access to the provided
  135. * field.
  136. *
  137. * For example, Views calls field_access('view') without providing the entity,
  138. * in order to determine if the field can be included in the query itself. So
  139. * we only want to return FALSE if we know that there are no entities for which
  140. * access will be granted. Later on, Views will invoke field_access('view')
  141. * again, indirectly, when rendering the fields using field_view_field(), and
  142. * at that point the entity will be passed along so we can do our normal checks
  143. * on it.
  144. *
  145. * As another example, the FileField Sources module uses field_access('edit')
  146. * as a menu access callback for the IMCE file browser and does not pass along
  147. * the entity. So we must return TRUE here if there is any entity for which the
  148. * user is allowed to edit the field (otherwise the user would not have access
  149. * to the IMCE file browser interface when editing the fields they do have
  150. * permission to edit).
  151. *
  152. * @param $op
  153. * The operation to be performed ('view' or 'edit').
  154. * @param $field_name
  155. * The name of the field whose access is being checked.
  156. * @param $account
  157. * The user account whose access is being checked.
  158. *
  159. * @return
  160. * TRUE if access should be allowed, or FALSE if it shouln't.
  161. */
  162. function field_permissions_empty_entity_access($op, $field_name, $account) {
  163. $all_permissions['view'] = array(
  164. 'view ' . $field_name,
  165. 'view own ' . $field_name,
  166. );
  167. $all_permissions['edit'] = array(
  168. 'create ' . $field_name,
  169. 'edit ' . $field_name,
  170. 'edit own ' . $field_name,
  171. );
  172. // If there's any scenario where the user might have permission to perform
  173. // the operation on the field, return TRUE.
  174. if (isset($all_permissions[$op])) {
  175. foreach ($all_permissions[$op] as $permission) {
  176. if (user_access($permission, $account)) {
  177. return TRUE;
  178. }
  179. }
  180. }
  181. return FALSE;
  182. }
  183. /**
  184. * Implementation of hook_field_access('view').
  185. */
  186. function _field_permissions_field_view_access($field_name, $entity_type, $entity, $account) {
  187. // Check if user has access to view this field in any entity.
  188. if (user_access('view ' . $field_name, $account)) {
  189. return TRUE;
  190. }
  191. // If the user has permission to view entities that they own, return TRUE if
  192. // they own this entity or FALSE if they don't.
  193. if (user_access('view own ' . $field_name, $account)) {
  194. return _field_permissions_entity_is_owned_by_account($entity, $account);
  195. }
  196. return FALSE;
  197. }
  198. /**
  199. * Implementation of hook_field_access('edit').
  200. */
  201. function _field_permissions_field_edit_access($field_name, $entity_type, $entity, $account) {
  202. // If this is a new entity, check if the user has access to edit the field on
  203. // entity creation.
  204. if (isset($entity->is_new)) {
  205. // Some entities provide an "is_new" property. If that is present, respect
  206. // whatever it's set to.
  207. $is_new = $entity->is_new;
  208. }
  209. else {
  210. // Otherwise, try to find out if the entity is new by checking its ID. Note
  211. // that checking empty() rather than !isset() is important here, to deal
  212. // with the case of entities that store "0" as their ID while the final
  213. // entity is in the process of being created (user accounts are a good
  214. // example of this).
  215. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  216. $is_new = empty($id);
  217. }
  218. if ($is_new) {
  219. return user_access('create ' . $field_name, $account);
  220. }
  221. // Check if user has access to edit this field in any entity.
  222. if (user_access('edit ' . $field_name, $account)) {
  223. return TRUE;
  224. }
  225. // If the user has permission to edit entities that they own, return TRUE if
  226. // they own this entity or FALSE if they don't.
  227. if (user_access('edit own ' . $field_name, $account)) {
  228. return _field_permissions_entity_is_owned_by_account($entity, $account);
  229. }
  230. return FALSE;
  231. }
  232. /**
  233. * Returns TRUE if an entity is owned by a user account, FALSE otherwise.
  234. */
  235. function _field_permissions_entity_is_owned_by_account($entity, $account) {
  236. // Try to get the uid of the entity owner from the entity itself. If it's not
  237. // set (for example, if the entity type does not store a uid or does not have
  238. // a concept of "ownership"), we need to assume that the provided user
  239. // account does not own it.
  240. return isset($entity->uid) && $entity->uid == $account->uid;
  241. }