View source
<?php
define('UUID_SERVICES_DEFAULT_ALLOWED_MEDIA_MIMES', 'video/brightcove
video/youtube');
function uuid_services_menu() {
$items['admin/config/services/uuid-services'] = array(
'title' => 'UUID Services',
'description' => 'Configure settings for UUID Services.',
'access arguments' => array(
'administer services',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'uuid_services_settings',
),
'file' => 'uuid_services.admin.inc',
);
return $items;
}
function uuid_services_services_resources_alter(&$resources, &$endpoint) {
foreach (entity_get_info() as $entity_type => $entity_info) {
if (isset($entity_info['uuid']) && $entity_info['uuid'] == TRUE && (isset($resources[$entity_type]) || variable_get('uuid_services_support_all_entity_types', FALSE))) {
unset($resources[$entity_type]['operations']['create']);
$resources[$entity_type]['operations']['retrieve']['help'] = t('Retrieve %label entities based on UUID.', array(
'%label' => $entity_info['label'],
));
$resources[$entity_type]['operations']['retrieve']['callback'] = '_uuid_services_entity_retrieve';
$resources[$entity_type]['operations']['retrieve']['access callback'] = '_uuid_services_entity_access';
$resources[$entity_type]['operations']['retrieve']['access arguments'] = array(
'view',
);
$resources[$entity_type]['operations']['retrieve']['access arguments append'] = TRUE;
$resources[$entity_type]['operations']['retrieve']['args'] = array(
array(
'name' => 'entity_type',
'description' => t('The entity type.'),
'type' => 'string',
'default value' => $entity_type,
'optional' => TRUE,
),
array(
'name' => 'uuid',
'description' => t('The %label UUID.', array(
'%label' => $entity_info['label'],
)),
'type' => 'text',
'source' => array(
'path' => 0,
),
),
);
$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',
));
$resources[$entity_type]['operations']['update']['callback'] = '_uuid_services_entity_update';
$resources[$entity_type]['operations']['update']['access callback'] = '_uuid_services_entity_access';
$resources[$entity_type]['operations']['update']['access arguments'] = array(
'update',
);
$resources[$entity_type]['operations']['update']['access arguments append'] = TRUE;
$resources[$entity_type]['operations']['update']['args'] = array(
array(
'name' => 'entity_type',
'description' => t('The entity type.'),
'type' => 'string',
'default value' => $entity_type,
'optional' => TRUE,
),
array(
'name' => 'uuid',
'description' => t('The %label UUID.', array(
'%label' => $entity_info['label'],
)),
'type' => 'text',
'source' => array(
'path' => 0,
),
),
array(
'name' => 'entity',
'description' => t('The %label entity object.', array(
'%label' => $entity_info['label'],
)),
'type' => 'struct',
'source' => 'data',
),
);
$resources[$entity_type]['operations']['delete']['help'] = t('Delete %label entities based on UUID.', array(
'%label' => $entity_info['label'],
));
$resources[$entity_type]['operations']['delete']['callback'] = '_uuid_services_entity_delete';
$resources[$entity_type]['operations']['delete']['access callback'] = '_uuid_services_entity_access';
$resources[$entity_type]['operations']['delete']['access arguments'] = array(
'delete',
);
$resources[$entity_type]['operations']['delete']['access arguments append'] = TRUE;
$resources[$entity_type]['operations']['delete']['args'] = array(
array(
'name' => 'entity_type',
'description' => t('The entity type.'),
'type' => 'string',
'default value' => $entity_type,
'optional' => TRUE,
),
array(
'name' => 'uuid',
'description' => t('The %label UUID.', array(
'%label' => $entity_info['label'],
)),
'type' => 'text',
'source' => array(
'path' => 0,
),
),
);
}
}
}
function _uuid_services_entity_retrieve($entity_type, $uuid) {
try {
$entities = entity_uuid_load($entity_type, array(
$uuid,
));
$entity = reset($entities);
return $entity;
} catch (Exception $exception) {
watchdog_exception('uuid_services', $exception);
return services_error($exception, 406, $uuid);
}
}
function _uuid_services_entity_update($entity_type, $uuid, $entity) {
try {
$controller = entity_get_controller($entity_type);
if ($controller instanceof EntityAPIControllerInterface) {
$entity = $controller
->create($entity);
}
else {
$entity = (object) $entity;
}
$entity->uuid_services = TRUE;
$valid_media_mimes = variable_get('uuid_services_allowed_media_mimes', UUID_SERVICES_DEFAULT_ALLOWED_MEDIA_MIMES);
if ($entity_type == 'file') {
if (!in_array($entity->filemime, preg_split('/\\r?\\n/', $valid_media_mimes))) {
$entity->filename = _services_file_check_name_extension($entity->filename);
$entity->uri = _services_file_check_destination_uri($entity->uri);
if (!empty($entity->filepath)) {
$entity->filepath = _services_file_check_destination($entity->filepath);
}
}
}
if ($entity_type == 'user' && !empty($entity->roles) && !user_access('administer permissions')) {
$original_user = user_load(entity_get_id_by_uuid('user', array(
$entity->uuid,
))[$entity->uuid]);
$entity->roles = $original_user->roles;
}
entity_uuid_save($entity_type, $entity);
return $entity;
} catch (Exception $exception) {
watchdog_exception('uuid_services', $exception);
return services_error($exception, 406, $entity);
}
}
function _uuid_services_entity_delete($entity_type, $uuid) {
try {
$uuid_exist = (bool) entity_get_id_by_uuid($entity_type, array(
$uuid,
));
if (!$uuid_exist) {
$args = array(
'@uuid' => $uuid,
'@type' => $entity_type,
);
watchdog('uuid_services', 'UUID @uuid not found for entity type @type', $args, WATCHDOG_WARNING);
return TRUE;
}
$return = entity_uuid_delete($entity_type, $uuid) !== FALSE;
return $return;
} catch (Exception $exception) {
watchdog_exception('uuid_services', $exception);
return services_error($exception, 406, $uuid);
}
}
function _uuid_services_entity_access($op, $args) {
try {
$entity_type = $args[0];
$entity_ids = entity_get_id_by_uuid($entity_type, array(
$args[1],
));
$entity = NULL;
if (!empty($args[2])) {
$entity = entity_create($entity_type, $args[2]);
entity_make_entity_local($entity_type, $entity);
}
elseif (!empty($entity_ids)) {
$entities = entity_load($entity_type, $entity_ids);
$entity = reset($entities);
}
if ($op == 'update' && empty($entity_ids)) {
$op = 'create';
}
if ($entity_type == 'user' && empty($entity) && $op == 'view') {
return services_error(t('There is no user with UUID @uuid.', array(
'@uuid' => $args[1],
)), 406);
}
if (($info = entity_get_info()) && isset($info[$entity_type]['access callback'])) {
return $info[$entity_type]['access callback']($op, $entity, NULL, $entity_type);
}
return TRUE;
} catch (Exception $exception) {
watchdog_exception('uuid_services', $exception);
return services_error($exception, 406, $entity_type);
}
}