Projet

Général

Profil

Paste
Télécharger (6,15 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / field_permissions / field_permissions.install @ a8cee257

1
<?php
2

    
3
/**
4
 * @file
5
 * Install, update and uninstall functions for the Field Permissions module.
6
 */
7

    
8
/**
9
 * Implements hook_install().
10
 */
11
function field_permissions_install() {
12
  // Set a larger weight for the module.
13
  db_update('system')
14
    ->fields(array('weight' => 50))
15
    ->condition('name', 'field_permissions')
16
    ->execute();
17
}
18

    
19
/**
20
 * Implements hook_uninstall().
21
 */
22
function field_permissions_uninstall() {
23

    
24
  // Collect all the field data that reference "field_permissions", within
25
  // the field_config table.
26
  //
27
  $records = db_query("SELECT * FROM {field_config} WHERE data LIKE '%field_permissions%'");
28

    
29
  foreach ($records as $record) {
30
    $data = $record->data;
31
    $data = unserialize($data);
32
    unset($data['field_permissions']);
33
    $data = serialize($data);
34

    
35
    // Update the record.
36
    db_query("UPDATE {field_config} SET data = :data WHERE id = :id",
37
      array(':data' => $data, ':id' => $record->id));
38
  }
39
}
40

    
41
/**
42
 * Sets a larger weight for the module so that the Field Permissions become available.
43
 */
44
function field_permissions_update_7000(&$sandbox) {
45
  db_update('system')
46
    ->fields(array('weight' => 50))
47
    ->condition('name', 'field_permissions')
48
    ->execute();
49
}
50

    
51
/**
52
 * Migrate field permission settings to the new system (public/private/custom).
53
 */
54
function field_permissions_update_7001() {
55
  foreach (field_info_fields() as $field_name => $field) {
56
    // If the field has any field permissions enabled, it will be using custom
57
    // permissions under the new system and needs to be converted. Otherwise,
58
    // it is a public field (the default) and can be ignored.
59
    if (!empty($field['settings']['field_permissions']) && array_filter($field['settings']['field_permissions'])) {
60
      // Set the type to FIELD_PERMISSIONS_CUSTOM. (The module may be disabled
61
      // when this update function runs, so we need to use the numeric value
62
      // rather than relying on the constant being defined.)
63
      $field['field_permissions']['type'] = 2;
64

    
65
      $field_permissions = $field['settings']['field_permissions'];
66
      $permissions_by_operation = array(
67
        // View-related permissions.
68
        array(
69
          'view' => "view $field_name",
70
          'view own' => "view own $field_name",
71
        ),
72
        // Edit-related permissions.
73
        array(
74
          'create' => "create $field_name",
75
          'edit' => "edit $field_name",
76
          'edit own' => "edit own $field_name",
77
        ),
78
      );
79

    
80
      // Loop through each type of operation (view or edit).
81
      foreach ($permissions_by_operation as $permissions) {
82
        $actions = array_keys($permissions);
83
        // If none of the related permissions were enabled, all users were
84
        // allowed to perform the relevant actions on this field, so we need to
85
        // assign permissions here to preserve that behavior.
86
        $has_enabled_permissions = (bool) array_filter(array_intersect_key($field_permissions, array_flip($actions)));
87
        if (!$has_enabled_permissions) {
88
          _update_7000_user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, $permissions, 'field_permissions');
89
          _update_7000_user_role_grant_permissions(DRUPAL_AUTHENTICATED_RID, $permissions, 'field_permissions');
90
        }
91
        // Otherwise, for each permission that was disabled, no users should be
92
        // allowed to perform that action; therefore, make sure to unassign any
93
        // (stale) permissions that they may have.
94
        else {
95
          foreach ($actions as $action) {
96
            if (empty($field_permissions[$action])) {
97
              if ($action != 'create') {
98
                $permission = $permissions[$action];
99
                $rids = array_keys(user_roles(FALSE, $permission));
100
                foreach ($rids as $rid) {
101
                  user_role_revoke_permissions($rid, array($permission));
102
                }
103
              }
104
              // The 'create' action needs special handling, since previously,
105
              // if create permissions were not enabled the code would have
106
              // fallen back on checking edit permissions. Now, though, create
107
              // permissions are always enabled (and always checked when an
108
              // entity is being created). Therefore, we need to figure out
109
              // what the fallback would have been and assign new create
110
              // permissions based on that.
111
              else {
112
                $rids_with_create_access = array();
113
                // The first fallback is edit permissions; if those are
114
                // enabled, any role with edit permission would have been
115
                // granted access.
116
                if (!empty($field_permissions['edit'])) {
117
                  $rids_with_create_access = array_keys(user_roles(FALSE, $permissions['edit']));
118
                }
119
                // The final fallback is 'edit own' permissions; if those are
120
                // enabled, any role with 'edit own' permission would have been
121
                // granted access. (It is additionally required that the entity
122
                // being checked is owned by the current user, but in the case
123
                // of nodes being created that will always be the case anyway,
124
                // and nodes are the only entities we need to support for the
125
                // D6-to-D7 upgrade.)
126
                if (!empty($field_permissions['edit own'])) {
127
                  $rids_with_create_access = array_unique(array_merge($rids_with_create_access, array_keys(user_roles(FALSE, $permissions['edit own']))));
128
                }
129
                // Assign create permissions to all the relevant roles.
130
                foreach ($rids_with_create_access as $rid) {
131
                  _update_7000_user_role_grant_permissions($rid, array($permissions['create']), 'field_permissions');
132
                }
133
              }
134
            }
135
          }
136
        }
137
      }
138
    }
139

    
140
    // Remove the old field permissions settings if necessary, and save the
141
    // field.
142
    if (isset($field['settings']['field_permissions'])) {
143
      // We can't unset this or field_update_field() will automatically add it
144
      // back (using the prior field data), so do the next best thing.
145
      $field['settings']['field_permissions'] = NULL;
146
      field_update_field($field);
147
    }
148
  }
149
}