uuid_services.module 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /**
  3. * @file
  4. * UUID Services module functions.
  5. */
  6. /**
  7. * Defines defaults for UUID_SERVICES_ALLOWED_MEDIA_MIMES.
  8. */
  9. define('UUID_SERVICES_DEFAULT_ALLOWED_MEDIA_MIMES',
  10. 'video/brightcove
  11. video/youtube'
  12. );
  13. /**
  14. * Implements hook_menu().
  15. */
  16. function uuid_services_menu() {
  17. $items['admin/config/services/uuid-services'] = array(
  18. 'title' => 'UUID Services',
  19. 'description' => 'Configure settings for UUID Services.',
  20. 'access arguments' => array('administer services'),
  21. 'page callback' => 'drupal_get_form',
  22. 'page arguments' => array('uuid_services_settings'),
  23. 'file' => 'uuid_services.admin.inc',
  24. );
  25. return $items;
  26. }
  27. /**
  28. * Implements hook_services_resources_alter().
  29. *
  30. * Alter all resources that support UUIDs, to make use this functionality when
  31. * exposing them through Services.
  32. *
  33. * Since we are working with UUID enabled entities, the 'create' method is
  34. * redundant. Instead, clients should do a PUT to '<entity_type>/<uuid>'. This
  35. * will route through the 'update' method and create the entity if it doesn't
  36. * exist. This is the most logical thing to do, since it's up to the client to
  37. * generate and set the UUID on the entity.
  38. */
  39. function uuid_services_services_resources_alter(&$resources, &$endpoint) {
  40. foreach (entity_get_info() as $entity_type => $entity_info) {
  41. if (isset($entity_info['uuid']) && $entity_info['uuid'] == TRUE && (isset($resources[$entity_type]) || variable_get('uuid_services_support_all_entity_types', FALSE))) {
  42. unset($resources[$entity_type]['operations']['create']);
  43. // Alter 'retrieve' method to use UUID enabled functions and arguments.
  44. $resources[$entity_type]['operations']['retrieve']['help'] = t('Retrieve %label entities based on UUID.', array('%label' => $entity_info['label']));
  45. $resources[$entity_type]['operations']['retrieve']['callback'] = '_uuid_services_entity_retrieve';
  46. $resources[$entity_type]['operations']['retrieve']['access callback'] = '_uuid_services_entity_access';
  47. $resources[$entity_type]['operations']['retrieve']['access arguments'] = array('view');
  48. $resources[$entity_type]['operations']['retrieve']['access arguments append'] = TRUE;
  49. $resources[$entity_type]['operations']['retrieve']['args'] = array(
  50. // This argument isn't exposed in the service, only used internally..
  51. array(
  52. 'name' => 'entity_type',
  53. 'description' => t('The entity type.'),
  54. 'type' => 'string',
  55. 'default value' => $entity_type,
  56. 'optional' => TRUE,
  57. ),
  58. array(
  59. 'name' => 'uuid',
  60. 'description' => t('The %label UUID.', array('%label' => $entity_info['label'])),
  61. 'type' => 'text',
  62. 'source' => array('path' => 0),
  63. ),
  64. );
  65. // Alter 'update' method to use UUID enabled functions and arguments.
  66. $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'));
  67. $resources[$entity_type]['operations']['update']['callback'] = '_uuid_services_entity_update';
  68. $resources[$entity_type]['operations']['update']['access callback'] = '_uuid_services_entity_access';
  69. $resources[$entity_type]['operations']['update']['access arguments'] = array('update');
  70. $resources[$entity_type]['operations']['update']['access arguments append'] = TRUE;
  71. $resources[$entity_type]['operations']['update']['args'] = array(
  72. // This argument isn't exposed in the service, only used internally..
  73. array(
  74. 'name' => 'entity_type',
  75. 'description' => t('The entity type.'),
  76. 'type' => 'string',
  77. 'default value' => $entity_type,
  78. 'optional' => TRUE,
  79. ),
  80. array(
  81. 'name' => 'uuid',
  82. 'description' => t('The %label UUID.', array('%label' => $entity_info['label'])),
  83. 'type' => 'text',
  84. 'source' => array('path' => 0),
  85. ),
  86. array(
  87. 'name' => 'entity',
  88. 'description' => t('The %label entity object.', array('%label' => $entity_info['label'])),
  89. 'type' => 'struct',
  90. 'source' => 'data',
  91. ),
  92. );
  93. // Alter 'delete' method to use UUID enabled functions and arguments.
  94. $resources[$entity_type]['operations']['delete']['help'] = t('Delete %label entities based on UUID.', array('%label' => $entity_info['label']));
  95. $resources[$entity_type]['operations']['delete']['callback'] = '_uuid_services_entity_delete';
  96. $resources[$entity_type]['operations']['delete']['access callback'] = '_uuid_services_entity_access';
  97. $resources[$entity_type]['operations']['delete']['access arguments'] = array('delete');
  98. $resources[$entity_type]['operations']['delete']['access arguments append'] = TRUE;
  99. $resources[$entity_type]['operations']['delete']['args'] = array(
  100. // This argument isn't exposed in the service, only used internally..
  101. array(
  102. 'name' => 'entity_type',
  103. 'description' => t('The entity type.'),
  104. 'type' => 'string',
  105. 'default value' => $entity_type,
  106. 'optional' => TRUE,
  107. ),
  108. array(
  109. 'name' => 'uuid',
  110. 'description' => t('The %label UUID.', array('%label' => $entity_info['label'])),
  111. 'type' => 'text',
  112. 'source' => array('path' => 0),
  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. * Callback for the 'update' method.
  136. *
  137. * @see entity_uuid_save()
  138. */
  139. function _uuid_services_entity_update($entity_type, $uuid, $entity) {
  140. try {
  141. $controller = entity_get_controller($entity_type);
  142. if ($controller instanceof EntityAPIControllerInterface) {
  143. $entity = $controller->create($entity);
  144. }
  145. else {
  146. $entity = (object) $entity;
  147. }
  148. $entity->uuid_services = TRUE;
  149. // Check that the mime type is whitelisted.
  150. $valid_media_mimes = variable_get('uuid_services_allowed_media_mimes', UUID_SERVICES_DEFAULT_ALLOWED_MEDIA_MIMES);
  151. // Sanitize file user input.
  152. if ($entity_type == 'file') {
  153. // We have to make sure to whitelist mime types, to avoid the video files
  154. // getting converted into text files, when deployed from one env to other.
  155. if (!in_array($entity->filemime, preg_split('/\r?\n/', $valid_media_mimes))) {
  156. $entity->filename = _services_file_check_name_extension($entity->filename);
  157. $entity->uri = _services_file_check_destination_uri($entity->uri);
  158. if (!empty($entity->filepath)) {
  159. $entity->filepath = _services_file_check_destination($entity->filepath);
  160. }
  161. }
  162. }
  163. // Sanitize user roles if user is not allowed to modify them.
  164. if ($entity_type == 'user' && !empty($entity->roles) && !user_access('administer permissions')) {
  165. $original_user = user_load(entity_get_id_by_uuid('user', array($entity->uuid))[$entity->uuid]);
  166. $entity->roles = $original_user->roles;
  167. }
  168. entity_uuid_save($entity_type, $entity);
  169. return $entity;
  170. }
  171. catch (Exception $exception) {
  172. watchdog_exception('uuid_services', $exception);
  173. return services_error($exception, 406, $entity);
  174. }
  175. }
  176. /**
  177. * Callback for the 'delete' method.
  178. *
  179. * @see entity_uuid_delete()
  180. */
  181. function _uuid_services_entity_delete($entity_type, $uuid) {
  182. try {
  183. $uuid_exist = (bool) entity_get_id_by_uuid($entity_type, array($uuid));
  184. if (!$uuid_exist) {
  185. /* UUID not found. Don't try to delete something that doesn't exist. */
  186. $args = array('@uuid' => $uuid, '@type' => $entity_type);
  187. watchdog('uuid_services', 'UUID @uuid not found for entity type @type', $args, WATCHDOG_WARNING);
  188. return TRUE;
  189. }
  190. $return = entity_uuid_delete($entity_type, $uuid) !== FALSE;
  191. return $return;
  192. }
  193. catch (Exception $exception) {
  194. watchdog_exception('uuid_services', $exception);
  195. return services_error($exception, 406, $uuid);
  196. }
  197. }
  198. /**
  199. * Access callback.
  200. *
  201. * @param string $op
  202. * The operation we are trying to do on the entity. Can only be:
  203. * - "view"
  204. * - "update"
  205. * - "delete"
  206. * See 'uuid_services_services_resources_alter()' for an explanation why
  207. * 'create' is missing.
  208. * @param array $args
  209. * The arguments passed to the method. The keys are holding the following:
  210. * 0. <entity_type>
  211. * 1. <uuid>
  212. * 2. <entity> (only available if $op == 'update')
  213. */
  214. function _uuid_services_entity_access($op, $args) {
  215. try {
  216. // Fetch the information we have to work with.
  217. $entity_type = $args[0];
  218. // Load functions always deal with multiple entities. So does this lookup
  219. // function. But in practice this will always only be one id.
  220. $entity_ids = entity_get_id_by_uuid($entity_type, array($args[1]));
  221. $entity = NULL;
  222. if (!empty($args[2])) {
  223. $entity = entity_create($entity_type, $args[2]);
  224. // We have to make the entity local (i.e. only have local references), for
  225. // access functions to work on it.
  226. entity_make_entity_local($entity_type, $entity);
  227. }
  228. // Fetch the local entity if we've got an id.
  229. elseif (!empty($entity_ids)) {
  230. $entities = entity_load($entity_type, $entity_ids);
  231. $entity = reset($entities);
  232. }
  233. // If we've been routed to the 'update' method and the entity we are
  234. // operating on doesn't exist yet, that should be reflected.
  235. if ($op == 'update' && empty($entity_ids)) {
  236. $op = 'create';
  237. }
  238. // If the user doesn't exist return 406 like services does.
  239. if (($entity_type == 'user' && empty($entity) && $op == 'view')) {
  240. return services_error(t('There is no user with UUID @uuid.', array('@uuid' => $args[1])), 406);;
  241. }
  242. // The following code is taken from entity_access() with some extra logic
  243. // to handle the case where an entity type is not defining an access
  244. // callback. With this logic, it's important that all entity types that
  245. // needs access control have an access callback defined.
  246. if (($info = entity_get_info()) && isset($info[$entity_type]['access callback'])) {
  247. return $info[$entity_type]['access callback']($op, $entity, NULL, $entity_type);
  248. }
  249. return TRUE;
  250. }
  251. catch (Exception $exception) {
  252. watchdog_exception('uuid_services', $exception);
  253. return services_error($exception, 406, $entity_type);
  254. }
  255. }