/** * {@inheritdoc} */ public function access($op, DataInterpreterInterface $interpreter) { foreach ($this->getAccessCallbacks() as $callback) { $result = ResourceManager::executeCallback($callback, array($op, $this, $interpreter)); if ($result == ResourceFieldBase::ACCESS_DENY) { return FALSE; } } return TRUE; }
/** * {@inheritdoc} */ public function index($path) { $values = array(); foreach ($this->publicFields() as $public_property => $info) { $value = NULL; if ($info['callback']) { $value = ResourceManager::executeCallback($info['callback']); } if ($value && !empty($info['process_callbacks'])) { foreach ($info['process_callbacks'] as $process_callback) { $value = ResourceManager::executeCallback($process_callback, array($value)); } } $values[$public_property] = $value; } return $values; }
/** * Helper function to get a formatter and apply a method. * * @param string $method * A valid method to call on the FormatterInterface object. * @param array $data * The array of data to process. * @param string $formatter_name * The name of the formatter for the current resource. Leave it NULL to use * the Accept headers. * * @return string * The processed output. */ protected function processData($method, array $data, $formatter_name = NULL) { if ($resource = $this->resource) { $request = $resource->getRequest(); } else { $request = restful()->getRequest(); } $accept = $request->getHeaders()->get('accept')->getValueString(); $formatter = $this->negotiateFormatter($accept, $formatter_name); $output = ResourceManager::executeCallback(array($formatter, $method), array($data, $formatter_name)); // The content type header is modified after the massaging if there is // an error code. Therefore we need to set the content type header after // formatting the output. $content_type = $formatter->getContentTypeHeader(); $response_headers = restful()->getResponse()->getHeaders(); $response_headers->add(HttpHeader::create('Content-Type', $content_type)); return $output; }
/** * Adds the Allowed-Origin headers. * * @param string $path * The requested path. */ protected function preflight($path) { $plugin_definition = $this->getPluginDefinition(); $header_bag = restful()->getResponse()->getHeaders(); // Populate the Accept header. $accepted_formats = array(); $formatter_manager = restful()->getFormatterManager(); if (empty($plugin_definition['formatter'])) { foreach ($formatter_manager->getPlugins() as $formatter) { /** @var $formatter \Drupal\restful\Plugin\formatter\FormatterInterface */ $header_bag->append(HttpHeader::create('Accept', $formatter->getContentTypeHeader())); } } else { try { $accepted_format = $formatter_manager->getPlugin($plugin_definition['formatter'])->getContentTypeHeader(); $header_bag->add(HttpHeader::create('Accept', $accepted_format)); } catch (PluginNotFoundException $e) { throw new NotImplementedException($e->getMessage()); } } $allowed_origin = empty($plugin_definition['allowOrigin']) ? variable_get('restful_allowed_origin', NULL) : $plugin_definition['allowOrigin']; // Always add the allow origin if configured. if ($allowed_origin) { $header_bag->add(HttpHeader::create('Access-Control-Allow-Origin', check_plain($allowed_origin))); // @see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Requests_with_credentials $accepts_credentials = $allowed_origin == '*' ? 'false' : 'true'; $header_bag->add(HttpHeader::create('Access-Control-Allow-Credentials', $accepts_credentials)); } // Make sure the Access-Control-Allow-Methods is populated. $allowed_methods = array(); foreach ($this->getControllers() as $pattern => $controllers) { // Find the controllers for the provided path. if ($pattern == $path || $pattern && preg_match('/' . $pattern . '/', $path)) { foreach ($controllers as $method => $controller) { if (is_array($controller)) { // If there is a custom access method for this endpoint check it. if (!empty($selected_controller['access callback']) && !ResourceManager::executeCallback(array($this, $selected_controller['access callback']), array($path))) { // There is no access for this method. continue; } } $allowed_methods[] = $method; } $header_bag->add(HttpHeader::create('Access-Control-Allow-Methods', implode(',', $allowed_methods))); break; } } }
/** * {@inheritdoc} */ public function executeProcessCallbacks($value) { $process_callbacks = $this->getProcessCallbacks(); if (!isset($value) || empty($process_callbacks)) { return $value; } foreach ($process_callbacks as $process_callback) { $value = ResourceManager::executeCallback($process_callback, array($value)); } return $value; }
/** * {@inheritdoc} */ public function process() { $path = $this->getPath(); return ResourceManager::executeCallback($this->getControllerFromPath($path), array($path)); }