uuid_services.module 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * Implements hook_services_resources_alter().
  4. *
  5. * Alter all resources that support UUIDs, to make use this functionality when
  6. * exposing them through Services.
  7. *
  8. * Since we are working with UUID enabled entities, the 'create' method is
  9. * redundant. Instead, clients should do a PUT to '<entity_type>/<uuid>'. This
  10. * will route through the 'update' method and create the entity if it doesn't
  11. * exist. This is the most logical thing to do, since it's up to the client to
  12. * generate and set the UUID on the entity.
  13. */
  14. function uuid_services_services_resources_alter(&$resources, &$endpoint) {
  15. foreach (entity_get_info() as $entity_type => $entity_info) {
  16. if (isset($entity_info['uuid']) && $entity_info['uuid'] == TRUE && isset($resources[$entity_type])) {
  17. unset($resources[$entity_type]['operations']['create']);
  18. // Alter 'retrieve' method to use UUID enabled functions and arguments.
  19. $resources[$entity_type]['operations']['retrieve']['help'] = t('Retrieve %label entities based on UUID.', array('%label' => $entity_info['label']));
  20. $resources[$entity_type]['operations']['retrieve']['callback'] = '_uuid_services_entity_retrieve';
  21. $resources[$entity_type]['operations']['retrieve']['access callback'] = '_uuid_services_entity_access';
  22. $resources[$entity_type]['operations']['retrieve']['access arguments'] = array('view');
  23. $resources[$entity_type]['operations']['retrieve']['access arguments append'] = TRUE;
  24. $resources[$entity_type]['operations']['retrieve']['args'] = array(
  25. // This argument isn't exposed in the service, only used internally..
  26. array(
  27. 'name' => 'entity_type',
  28. 'description' => t('The entity type.'),
  29. 'type' => 'string',
  30. 'default value' => $entity_type,
  31. 'optional' => TRUE,
  32. ),
  33. array(
  34. 'name' => 'uuid',
  35. 'description' => t('The %label UUID.', array('%label' => $entity_info['label'])),
  36. 'type' => 'text',
  37. 'source' => array('path' => 0),
  38. ),
  39. );
  40. // Alter 'update' method to use UUID enabled functions and arguments.
  41. $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'));
  42. $resources[$entity_type]['operations']['update']['callback'] = '_uuid_services_entity_update';
  43. $resources[$entity_type]['operations']['update']['access callback'] = '_uuid_services_entity_access';
  44. $resources[$entity_type]['operations']['update']['access arguments'] = array('update');
  45. $resources[$entity_type]['operations']['update']['access arguments append'] = TRUE;
  46. $resources[$entity_type]['operations']['update']['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. array(
  62. 'name' => 'entity',
  63. 'description' => t('The %label entity object.', array('%label' => $entity_info['label'])),
  64. 'type' => 'struct',
  65. 'source' => 'data',
  66. ),
  67. );
  68. // Alter 'delete' method to use UUID enabled functions and arguments.
  69. $resources[$entity_type]['operations']['delete']['help'] = t('Delete %label entities based on UUID.', array('%label' => $entity_info['label']));
  70. $resources[$entity_type]['operations']['delete']['callback'] = '_uuid_services_entity_delete';
  71. $resources[$entity_type]['operations']['delete']['access callback'] = '_uuid_services_entity_access';
  72. $resources[$entity_type]['operations']['delete']['access arguments'] = array('delete');
  73. $resources[$entity_type]['operations']['delete']['access arguments append'] = TRUE;
  74. $resources[$entity_type]['operations']['delete']['args'] = array(
  75. // This argument isn't exposed in the service, only used internally..
  76. array(
  77. 'name' => 'entity_type',
  78. 'description' => t('The entity type.'),
  79. 'type' => 'string',
  80. 'default value' => $entity_type,
  81. 'optional' => TRUE,
  82. ),
  83. array(
  84. 'name' => 'uuid',
  85. 'description' => t('The %label UUID.', array('%label' => $entity_info['label'])),
  86. 'type' => 'text',
  87. 'source' => array('path' => 0),
  88. ),
  89. );
  90. }
  91. }
  92. }
  93. /**
  94. * Callback for the 'retrieve' method.
  95. *
  96. * @see entity_uuid_load()
  97. */
  98. function _uuid_services_entity_retrieve($entity_type, $uuid) {
  99. try {
  100. $entities = entity_uuid_load($entity_type, array($uuid));
  101. $entity = reset($entities);
  102. return $entity;
  103. }
  104. catch (Exception $exception) {
  105. watchdog_exception('uuid_services', $exception);
  106. return services_error($exception, 406, $uuid);
  107. }
  108. }
  109. /**
  110. * Callback for the 'update' method.
  111. *
  112. * @see entity_uuid_save()
  113. */
  114. function _uuid_services_entity_update($entity_type, $uuid, $entity) {
  115. try {
  116. $controller = entity_get_controller($entity_type);
  117. if ($controller instanceof EntityAPIControllerInterface) {
  118. $entity = $controller->create($entity);
  119. }
  120. else {
  121. $entity = (object) $entity;
  122. }
  123. entity_uuid_save($entity_type, $entity);
  124. return $entity;
  125. }
  126. catch (Exception $exception) {
  127. watchdog_exception('uuid_services', $exception);
  128. return services_error($exception, 406, $entity);
  129. }
  130. }
  131. /**
  132. * Callback for the 'delete' method.
  133. *
  134. * @see entity_uuid_delete()
  135. */
  136. function _uuid_services_entity_delete($entity_type, $uuid) {
  137. try {
  138. $return = entity_uuid_delete($entity_type, array($uuid));
  139. return $return;
  140. }
  141. catch (Exception $exception) {
  142. watchdog_exception('uuid_services', $exception);
  143. return services_error($exception, 406, $uuid);
  144. }
  145. }
  146. /**
  147. * Access callback.
  148. *
  149. * @param $op
  150. * The operation we are trying to do on the entity. Can only be:
  151. * - "view"
  152. * - "update"
  153. * - "delete"
  154. * See 'uuid_services_services_resources_alter()' for an explanation why
  155. * 'create' is missing.
  156. * @param $args
  157. * The arguments passed to the method. The keys are holding the following:
  158. * 0. <entity_type>
  159. * 1. <uuid>
  160. * 2. <entity> (only available if $op == 'update')
  161. */
  162. function _uuid_services_entity_access($op, $args) {
  163. try {
  164. // Fetch the information we have to work with.
  165. $entity_type = $args[0];
  166. // Load functions always deal with multiple entities. So does this lookup
  167. // function. But in practice this will always only be one id.
  168. $entity_ids = entity_get_id_by_uuid($entity_type, array($args[1]));
  169. $entity = NULL;
  170. if (!empty($args[2])) {
  171. $entity = entity_create($entity_type, $args[2]);
  172. // We have to make the entity local (i.e. only have local references), for
  173. // access functions to work on it.
  174. entity_make_entity_local($entity_type, $entity);
  175. }
  176. // Fetch the local entity if we've got an id.
  177. elseif (!empty($entity_id)) {
  178. $entities = entity_load($entity_type, $entity_ids);
  179. $entity = reset($entities);
  180. }
  181. // If we've been routed to the 'update' method and the entity we are
  182. // operating on doesn't exist yet, that should be reflected.
  183. if ($op == 'update' && empty($entity_ids)) {
  184. $op = 'create';
  185. }
  186. // Taxonomy and Comment module uses 'edit' instead of 'update'.
  187. // Oh, how I love Drupal consistency.
  188. if (($entity_type == 'taxonomy_term' || $entity_type == 'comment') && $op == 'update') {
  189. $op = 'edit';
  190. }
  191. // The following code is taken from entity_access() with some extra logic
  192. // to handle the case where an entity type is not defining an access
  193. // callback. With this logic, it's important that all entity types that
  194. // needs access control have an access callback defined.
  195. if (($info = entity_get_info()) && isset($info[$entity_type]['access callback'])) {
  196. return $info[$entity_type]['access callback']($op, $entity, NULL, $entity_type);
  197. }
  198. return TRUE;
  199. }
  200. catch (Exception $exception) {
  201. watchdog_exception('uuid_services', $exception);
  202. return services_error($exception, 406, $entity_type);
  203. }
  204. }
  205. /**
  206. * Implements hook_services_resources().
  207. */
  208. function uuid_services_services_resources() {
  209. module_load_include('inc', 'uuid_services', 'resources/field_collection.resource');
  210. $resources = array(
  211. '#api_version' => 3002,
  212. );
  213. $resources += _field_collection_resource_definition();
  214. return $resources;
  215. }