field_permissions.install 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the Field Permissions module.
  5. */
  6. /**
  7. * Implements hook_install().
  8. */
  9. function field_permissions_install() {
  10. // Set a larger weight for the module.
  11. db_update('system')
  12. ->fields(array('weight' => 50))
  13. ->condition('name', 'field_permissions')
  14. ->execute();
  15. }
  16. /**
  17. * Sets a larger weight for the module so that the Field Permissions become available.
  18. */
  19. function field_permissions_update_7000(&$sandbox) {
  20. db_update('system')
  21. ->fields(array('weight' => 50))
  22. ->condition('name', 'field_permissions')
  23. ->execute();
  24. }
  25. /**
  26. * Migrate field permission settings to the new system (public/private/custom).
  27. */
  28. function field_permissions_update_7001() {
  29. foreach (field_info_fields() as $field_name => $field) {
  30. // If the field has any field permissions enabled, it will be using custom
  31. // permissions under the new system and needs to be converted. Otherwise,
  32. // it is a public field (the default) and can be ignored.
  33. if (!empty($field['settings']['field_permissions']) && array_filter($field['settings']['field_permissions'])) {
  34. // Set the type to FIELD_PERMISSIONS_CUSTOM. (The module may be disabled
  35. // when this update function runs, so we need to use the numeric value
  36. // rather than relying on the constant being defined.)
  37. $field['field_permissions']['type'] = 2;
  38. $field_permissions = $field['settings']['field_permissions'];
  39. $permissions_by_operation = array(
  40. // View-related permissions.
  41. array(
  42. 'view' => "view $field_name",
  43. 'view own' => "view own $field_name",
  44. ),
  45. // Edit-related permissions.
  46. array(
  47. 'create' => "create $field_name",
  48. 'edit' => "edit $field_name",
  49. 'edit own' => "edit own $field_name",
  50. ),
  51. );
  52. // Loop through each type of operation (view or edit).
  53. foreach ($permissions_by_operation as $permissions) {
  54. $actions = array_keys($permissions);
  55. // If none of the related permissions were enabled, all users were
  56. // allowed to perform the relevant actions on this field, so we need to
  57. // assign permissions here to preserve that behavior.
  58. $has_enabled_permissions = (bool) array_filter(array_intersect_key($field_permissions, array_flip($actions)));
  59. if (!$has_enabled_permissions) {
  60. _update_7000_user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, $permissions, 'field_permissions');
  61. _update_7000_user_role_grant_permissions(DRUPAL_AUTHENTICATED_RID, $permissions, 'field_permissions');
  62. }
  63. // Otherwise, for each permission that was disabled, no users should be
  64. // allowed to perform that action; therefore, make sure to unassign any
  65. // (stale) permissions that they may have.
  66. else {
  67. foreach ($actions as $action) {
  68. if (empty($field_permissions[$action])) {
  69. if ($action != 'create') {
  70. $permission = $permissions[$action];
  71. $rids = array_keys(user_roles(FALSE, $permission));
  72. foreach ($rids as $rid) {
  73. user_role_revoke_permissions($rid, array($permission));
  74. }
  75. }
  76. // The 'create' action needs special handling, since previously,
  77. // if create permissions were not enabled the code would have
  78. // fallen back on checking edit permissions. Now, though, create
  79. // permissions are always enabled (and always checked when an
  80. // entity is being created). Therefore, we need to figure out
  81. // what the fallback would have been and assign new create
  82. // permissions based on that.
  83. else {
  84. $rids_with_create_access = array();
  85. // The first fallback is edit permissions; if those are
  86. // enabled, any role with edit permission would have been
  87. // granted access.
  88. if (!empty($field_permissions['edit'])) {
  89. $rids_with_create_access = array_keys(user_roles(FALSE, $permissions['edit']));
  90. }
  91. // The final fallback is 'edit own' permissions; if those are
  92. // enabled, any role with 'edit own' permission would have been
  93. // granted access. (It is additionally required that the entity
  94. // being checked is owned by the current user, but in the case
  95. // of nodes being created that will always be the case anyway,
  96. // and nodes are the only entities we need to support for the
  97. // D6-to-D7 upgrade.)
  98. if (!empty($field_permissions['edit own'])) {
  99. $rids_with_create_access = array_unique(array_merge($rids_with_create_access, array_keys(user_roles(FALSE, $permissions['edit own']))));
  100. }
  101. // Assign create permissions to all the relevant roles.
  102. foreach ($rids_with_create_access as $rid) {
  103. _update_7000_user_role_grant_permissions($rid, array($permissions['create']), 'field_permissions');
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }
  111. // Remove the old field permissions settings if necessary, and save the
  112. // field.
  113. if (isset($field['settings']['field_permissions'])) {
  114. // We can't unset this or field_update_field() will automatically add it
  115. // back (using the prior field data), so do the next best thing.
  116. $field['settings']['field_permissions'] = NULL;
  117. field_update_field($field);
  118. }
  119. }
  120. }