You are here

function _uuid_services_entity_access in Universally Unique IDentifier 7

Access callback.

Parameters

string $op: The operation we are trying to do on the entity. Can only be:

  • "view"
  • "update"
  • "delete"

See 'uuid_services_services_resources_alter()' for an explanation why 'create' is missing.

array $args: The arguments passed to the method. The keys are holding the following: 0. <entity_type> 1. <uuid> 2. <entity> (only available if $op == 'update')

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

File

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

Code

function _uuid_services_entity_access($op, $args) {
  try {

    // Fetch the information we have to work with.
    $entity_type = $args[0];

    // Load functions always deal with multiple entities. So does this lookup
    // function. But in practice this will always only be one id.
    $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]);

      // We have to make the entity local (i.e. only have local references), for
      // access functions to work on it.
      entity_make_entity_local($entity_type, $entity);
    }
    elseif (!empty($entity_ids)) {
      $entities = entity_load($entity_type, $entity_ids);
      $entity = reset($entities);
    }

    // If we've been routed to the 'update' method and the entity we are
    // operating on doesn't exist yet, that should be reflected.
    if ($op == 'update' && empty($entity_ids)) {
      $op = 'create';
    }

    // If the user doesn't exist return 406 like services does.
    if ($entity_type == 'user' && empty($entity) && $op == 'view') {
      return services_error(t('There is no user with UUID @uuid.', array(
        '@uuid' => $args[1],
      )), 406);
    }

    // The following code is taken from entity_access() with some extra logic
    // to handle the case where an entity type is not defining an access
    // callback. With this logic, it's important that all entity types that
    // needs access control have an access callback defined.
    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);
  }
}