You are here

function _uuid_services_entity_update in Universally Unique IDentifier 7

Callback for the 'update' method.

See also

entity_uuid_save()

1 string reference to '_uuid_services_entity_update'
uuid_services_services_resources_alter in uuid_services/uuid_services.module
Implements hook_services_resources_alter().

File

uuid_services/uuid_services.module, line 148
UUID Services module functions.

Code

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;

    // Check that the mime type is whitelisted.
    $valid_media_mimes = variable_get('uuid_services_allowed_media_mimes', UUID_SERVICES_DEFAULT_ALLOWED_MEDIA_MIMES);

    // Sanitize file user input.
    if ($entity_type == 'file') {

      // We have to make sure to whitelist mime types, to avoid the video files
      // getting converted into text files, when deployed from one env to other.
      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);
        }
      }
    }

    // Sanitize user roles if user is not allowed to modify them.
    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);
  }
}