You are here

function restful_menu_access_callback in RESTful 7.2

Same name and namespace in other branches
  1. 7 restful.module \restful_menu_access_callback()

Access callback; Determine access for an API call.

Parameters

string $resource_name: The name of the resource (e.g. "articles").

string $version_string: The version array.

Return value

bool TRUE if user is allowed to access resource.

File

./restful.module, line 176

Code

function restful_menu_access_callback($resource_name, $version_string = NULL) {
  $resource_manager = restful()
    ->getResourceManager();
  if (!empty($version_string) && preg_match('/v[0-9]+(\\.[0-9]+)?/', $version_string)) {
    $version_string = substr($version_string, 1);
    $parsed_versions = explode('.', $version_string);
    if (count($parsed_versions) == 2) {

      // If there is only the major we need to get the version from the request,
      // to get the latest version within the major version.
      $versions = $parsed_versions;
    }
  }
  if (empty($versions) && !($versions = $resource_manager
    ->getVersionFromRequest())) {

    // No version could be found.
    return FALSE;
  }
  try {
    $instance_id = $resource_name . PluginBase::DERIVATIVE_SEPARATOR . implode('.', $versions);
    $resource = $resource_manager
      ->getPlugin($instance_id, restful()
      ->getRequest());
    if (!$resource) {

      // Throw a PluginNotFoundException exception instead of a denied access.
      throw new PluginNotFoundException($instance_id);
    }
    return $resource
      ->access();
  } catch (RestfulException $e) {

    // We can get here if the request method is not valid or if no resource can
    // be negotiated.
    $response = restful()
      ->getResponse();
    $output = _restful_build_http_api_error($e, $response);
    $response
      ->setStatusCode($e
      ->getCode());
    $response
      ->setContent(drupal_json_encode($output));
    $response
      ->send();
    exit;
  } catch (PluginNotFoundException $e) {
    restful_delivery(MENU_NOT_FOUND);
    exit;
  }
}