Projet

Général

Profil

Paste
Télécharger (9,72 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / uuid / uuid_services / uuid_services.module @ bf6fb0ee

1
<?php
2

    
3
/**
4
 * @file
5
 * UUID Services module functions.
6
 */
7

    
8
/**
9
 * Implements hook_menu().
10
 */
11
function uuid_services_menu() {
12
  $items['admin/config/services/uuid-services'] = array(
13
    'title' => 'UUID Services',
14
    'description' => 'Configure settings for UUID Services.',
15
    'access arguments' => array('administer services'),
16
    'page callback' => 'drupal_get_form',
17
    'page arguments' => array('uuid_services_settings'),
18
    'file' => 'uuid_services.admin.inc',
19
  );
20
  return $items;
21
}
22

    
23
/**
24
 * Implements hook_services_resources_alter().
25
 *
26
 * Alter all resources that support UUIDs, to make use this functionality when
27
 * exposing them through Services.
28
 *
29
 * Since we are working with UUID enabled entities, the 'create' method is
30
 * redundant. Instead, clients should do a PUT to '<entity_type>/<uuid>'. This
31
 * will route through the 'update' method and create the entity if it doesn't
32
 * exist. This is the most logical thing to do, since it's up to the client to
33
 * generate and set the UUID on the entity.
34
 */
35
function uuid_services_services_resources_alter(&$resources, &$endpoint) {
36
  foreach (entity_get_info() as $entity_type => $entity_info) {
37
    if (isset($entity_info['uuid']) && $entity_info['uuid'] == TRUE && (isset($resources[$entity_type]) || variable_get('uuid_services_support_all_entity_types', FALSE))) {
38
      unset($resources[$entity_type]['operations']['create']);
39

    
40
      // Alter 'retrieve' method to use UUID enabled functions and arguments.
41
      $resources[$entity_type]['operations']['retrieve']['help'] = t('Retrieve %label entities based on UUID.', array('%label' => $entity_info['label']));
42
      $resources[$entity_type]['operations']['retrieve']['callback'] = '_uuid_services_entity_retrieve';
43
      $resources[$entity_type]['operations']['retrieve']['access callback'] = '_uuid_services_entity_access';
44
      $resources[$entity_type]['operations']['retrieve']['access arguments'] = array('view');
45
      $resources[$entity_type]['operations']['retrieve']['access arguments append'] = TRUE;
46
      $resources[$entity_type]['operations']['retrieve']['args'] = array(
47
        // This argument isn't exposed in the service, only used internally..
48
        array(
49
          'name' => 'entity_type',
50
          'description' => t('The entity type.'),
51
          'type' => 'string',
52
          'default value' => $entity_type,
53
          'optional' => TRUE,
54
        ),
55
        array(
56
          'name' => 'uuid',
57
          'description' => t('The %label UUID.', array('%label' => $entity_info['label'])),
58
          'type' => 'text',
59
          'source' => array('path' => 0),
60
        ),
61
      );
62

    
63
      // Alter 'update' method to use UUID enabled functions and arguments.
64
      $resources[$entity_type]['operations']['update']['help'] = t('Update or create %label entities based on UUID. The payload must be formatted according to the <a href="!url">OData protocol</a>.', array('%label' => $entity_info['label'], '!url' => 'http://www.odata.org/developers/protocols'));
65
      $resources[$entity_type]['operations']['update']['callback'] = '_uuid_services_entity_update';
66
      $resources[$entity_type]['operations']['update']['access callback'] = '_uuid_services_entity_access';
67
      $resources[$entity_type]['operations']['update']['access arguments'] = array('update');
68
      $resources[$entity_type]['operations']['update']['access arguments append'] = TRUE;
69
      $resources[$entity_type]['operations']['update']['args'] = array(
70
        // This argument isn't exposed in the service, only used internally..
71
        array(
72
          'name' => 'entity_type',
73
          'description' => t('The entity type.'),
74
          'type' => 'string',
75
          'default value' => $entity_type,
76
          'optional' => TRUE,
77
        ),
78
        array(
79
          'name' => 'uuid',
80
          'description' => t('The %label UUID.', array('%label' => $entity_info['label'])),
81
          'type' => 'text',
82
          'source' => array('path' => 0),
83
        ),
84
        array(
85
          'name' => 'entity',
86
          'description' => t('The %label entity object.', array('%label' => $entity_info['label'])),
87
          'type' => 'struct',
88
          'source' => 'data',
89
        ),
90
      );
91

    
92
      // Alter 'delete' method to use UUID enabled functions and arguments.
93
      $resources[$entity_type]['operations']['delete']['help'] = t('Delete %label entities based on UUID.', array('%label' => $entity_info['label']));
94
      $resources[$entity_type]['operations']['delete']['callback'] = '_uuid_services_entity_delete';
95
      $resources[$entity_type]['operations']['delete']['access callback'] = '_uuid_services_entity_access';
96
      $resources[$entity_type]['operations']['delete']['access arguments'] = array('delete');
97
      $resources[$entity_type]['operations']['delete']['access arguments append'] = TRUE;
98
      $resources[$entity_type]['operations']['delete']['args'] = array(
99
        // This argument isn't exposed in the service, only used internally..
100
        array(
101
          'name' => 'entity_type',
102
          'description' => t('The entity type.'),
103
          'type' => 'string',
104
          'default value' => $entity_type,
105
          'optional' => TRUE,
106
        ),
107
        array(
108
          'name' => 'uuid',
109
          'description' => t('The %label UUID.', array('%label' => $entity_info['label'])),
110
          'type' => 'text',
111
          'source' => array('path' => 0),
112
        ),
113
      );
114
    }
115
  }
116
}
117

    
118
/**
119
 * Callback for the 'retrieve' method.
120
 *
121
 * @see entity_uuid_load()
122
 */
123
function _uuid_services_entity_retrieve($entity_type, $uuid) {
124
  try {
125
    $entities = entity_uuid_load($entity_type, array($uuid));
126
    $entity = reset($entities);
127
    return $entity;
128
  }
129
  catch (Exception $exception) {
130
    watchdog_exception('uuid_services', $exception);
131
    return services_error($exception, 406, $uuid);
132
  }
133
}
134

    
135
/**
136
 * Callback for the 'update' method.
137
 *
138
 * @see entity_uuid_save()
139
 */
140
function _uuid_services_entity_update($entity_type, $uuid, $entity) {
141
  try {
142
    $controller = entity_get_controller($entity_type);
143
    if ($controller instanceof EntityAPIControllerInterface) {
144
      $entity = $controller->create($entity);
145
    }
146
    else {
147
      $entity = (object) $entity;
148
    }
149
    $entity->uuid_services = TRUE;
150
    // Sanitize file user input.
151
    if ($entity_type == 'file') {
152
      $entity->filename = _services_file_check_name_extension($entity->filename);
153
      $entity->uri = _services_file_check_destination_uri($entity->uri);
154
      if (!empty($entity->filepath)) {
155
        $entity->filepath = _services_file_check_destination($entity->filepath);
156
      }
157
    }
158
    entity_uuid_save($entity_type, $entity);
159
    return $entity;
160
  }
161
  catch (Exception $exception) {
162
    watchdog_exception('uuid_services', $exception);
163
    return services_error($exception, 406, $entity);
164
  }
165
}
166

    
167
/**
168
 * Callback for the 'delete' method.
169
 *
170
 * @see entity_uuid_delete()
171
 */
172
function _uuid_services_entity_delete($entity_type, $uuid) {
173
  try {
174
    $uuid_exist = (bool) entity_get_id_by_uuid($entity_type, array($uuid));
175
    if (!$uuid_exist) {
176
      /* UUID not found. Don't try to delete something that doesn't exist. */
177
      $args = array('@uuid' => $uuid, '@type' => $entity_type);
178
      watchdog('uuid_services', 'UUID @uuid not found for entity type @type', $args, WATCHDOG_WARNING);
179
      return TRUE;
180
    }
181

    
182
    $return = entity_uuid_delete($entity_type, array($uuid)) !== FALSE;
183
    return $return;
184
  }
185
  catch (Exception $exception) {
186
    watchdog_exception('uuid_services', $exception);
187
    return services_error($exception, 406, $uuid);
188
  }
189
}
190

    
191
/**
192
 * Access callback.
193
 *
194
 * @param string $op
195
 *   The operation we are trying to do on the entity. Can only be:
196
 *   - "view"
197
 *   - "update"
198
 *   - "delete"
199
 *   See 'uuid_services_services_resources_alter()' for an explanation why
200
 *   'create' is missing.
201
 * @param array $args
202
 *   The arguments passed to the method. The keys are holding the following:
203
 *   0. <entity_type>
204
 *   1. <uuid>
205
 *   2. <entity> (only available if $op == 'update')
206
 */
207
function _uuid_services_entity_access($op, $args) {
208
  try {
209
    // Fetch the information we have to work with.
210
    $entity_type = $args[0];
211
    // Load functions always deal with multiple entities. So does this lookup
212
    // function. But in practice this will always only be one id.
213
    $entity_ids = entity_get_id_by_uuid($entity_type, array($args[1]));
214
    $entity = NULL;
215
    if (!empty($args[2])) {
216
      $entity = entity_create($entity_type, $args[2]);
217
      // We have to make the entity local (i.e. only have local references), for
218
      // access functions to work on it.
219
      entity_make_entity_local($entity_type, $entity);
220
    }
221
    // Fetch the local entity if we've got an id.
222
    elseif (!empty($entity_ids)) {
223
      $entities = entity_load($entity_type, $entity_ids);
224
      $entity = reset($entities);
225
    }
226

    
227
    // If we've been routed to the 'update' method and the entity we are
228
    // operating on doesn't exist yet, that should be reflected.
229
    if ($op == 'update' && empty($entity_ids)) {
230
      $op = 'create';
231
    }
232
    // If the user doesn't exist return 406 like services does.
233
    if (($entity_type == 'user' && empty($entity) && $op == 'view')) {
234
      return services_error(t('There is no user with UUID @uuid.', array('@uuid' => $args[1])), 406);;
235
    }
236
    // The following code is taken from entity_access() with some extra logic
237
    // to handle the case where an entity type is not defining an access
238
    // callback. With this logic, it's important that all entity types that
239
    // needs access control have an access callback defined.
240
    if (($info = entity_get_info()) && isset($info[$entity_type]['access callback'])) {
241
      return $info[$entity_type]['access callback']($op, $entity, NULL, $entity_type);
242
    }
243
    return TRUE;
244
  }
245
  catch (Exception $exception) {
246
    watchdog_exception('uuid_services', $exception);
247
    return services_error($exception, 406, $entity_type);
248
  }
249
}