uuid_services.module 9.2 KB

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