3 namespace Drupal\rest\Plugin;
5 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
6 use Drupal\Core\Plugin\PluginBase;
7 use Drupal\Core\Routing\BcRoute;
8 use Psr\Log\LoggerInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10 use Symfony\Component\Routing\Route;
11 use Symfony\Component\Routing\RouteCollection;
14 * Common base class for resource plugins.
16 * Note that this base class' implementation of the permissions() method
17 * generates a permission for every method for a resource. If your resource
18 * already has its own access control mechanism, you should opt out from this
19 * default permissions() method by overriding it.
21 * @see \Drupal\rest\Annotation\RestResource
22 * @see \Drupal\rest\Plugin\Type\ResourcePluginManager
23 * @see \Drupal\rest\Plugin\ResourceInterface
26 * @ingroup third_party
28 abstract class ResourceBase extends PluginBase implements ContainerFactoryPluginInterface, ResourceInterface {
31 * The available serialization formats.
35 protected $serializerFormats = [];
40 * @var \Psr\Log\LoggerInterface
45 * Constructs a Drupal\rest\Plugin\ResourceBase object.
47 * @param array $configuration
48 * A configuration array containing information about the plugin instance.
49 * @param string $plugin_id
50 * The plugin_id for the plugin instance.
51 * @param mixed $plugin_definition
52 * The plugin implementation definition.
53 * @param array $serializer_formats
54 * The available serialization formats.
55 * @param \Psr\Log\LoggerInterface $logger
58 public function __construct(array $configuration, $plugin_id, $plugin_definition, array $serializer_formats, LoggerInterface $logger) {
59 parent::__construct($configuration, $plugin_id, $plugin_definition);
60 $this->serializerFormats = $serializer_formats;
61 $this->logger = $logger;
67 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
72 $container->getParameter('serializer.formats'),
73 $container->get('logger.factory')->get('rest')
78 * Implements ResourceInterface::permissions().
80 * Every plugin operation method gets its own user permission. Example:
81 * "restful delete entity:node" with the title "Access DELETE on Node
84 public function permissions() {
86 $definition = $this->getPluginDefinition();
87 foreach ($this->availableMethods() as $method) {
88 $lowered_method = strtolower($method);
89 $permissions["restful $lowered_method $this->pluginId"] = [
90 'title' => $this->t('Access @method on %label resource', ['@method' => $method, '%label' => $definition['label']]),
99 public function routes() {
100 $collection = new RouteCollection();
102 $definition = $this->getPluginDefinition();
103 $canonical_path = isset($definition['uri_paths']['canonical']) ? $definition['uri_paths']['canonical'] : '/' . strtr($this->pluginId, ':', '/') . '/{id}';
104 $create_path = isset($definition['uri_paths']['create']) ? $definition['uri_paths']['create'] : '/' . strtr($this->pluginId, ':', '/');
105 // BC: the REST module originally created the POST URL for a resource by
106 // reading the 'https://www.drupal.org/link-relations/create' URI path from
107 // the plugin annotation. For consistency with entity type definitions, that
108 // then changed to reading the 'create' URI path. For any REST Resource
109 // plugins that were using the old mechanism, we continue to support that.
110 if (!isset($definition['uri_paths']['create']) && isset($definition['uri_paths']['https://www.drupal.org/link-relations/create'])) {
111 $create_path = $definition['uri_paths']['https://www.drupal.org/link-relations/create'];
114 $route_name = strtr($this->pluginId, ':', '.');
116 $methods = $this->availableMethods();
117 foreach ($methods as $method) {
118 $path = $method === 'POST'
121 $route = $this->getBaseRoute($path, $method);
123 // Note that '_format' and '_content_type_format' route requirements are
124 // added in ResourceRoutes::getRoutesForResourceConfig().
125 $collection->add("$route_name.$method", $route);
127 // BC: the REST module originally created per-format GET routes, instead
128 // of a single route. To minimize the surface of this BC layer, this uses
129 // route definitions that are as empty as possible, plus an outbound route
131 // @see \Drupal\rest\RouteProcessor\RestResourceGetRouteProcessorBC
132 if ($method === 'GET' || $method === 'HEAD') {
133 foreach ($this->serializerFormats as $format_name) {
134 $collection->add("$route_name.$method.$format_name", (new BcRoute())->setRequirement('_format', $format_name));
143 * Provides predefined HTTP request methods.
145 * Plugins can override this method to provide additional custom request
149 * The list of allowed HTTP request method strings.
151 protected function requestMethods() {
168 public function availableMethods() {
169 $methods = $this->requestMethods();
171 foreach ($methods as $method) {
172 // Only expose methods where the HTTP request method exists on the plugin.
173 if (method_exists($this, strtolower($method))) {
174 $available[] = $method;
181 * Gets the base route for a particular method.
183 * @param string $canonical_path
184 * The canonical path for the resource.
185 * @param string $method
186 * The HTTP method to be used for the route.
188 * @return \Symfony\Component\Routing\Route
189 * The created base route.
191 protected function getBaseRoute($canonical_path, $method) {
192 return new Route($canonical_path, [
193 '_controller' => 'Drupal\rest\RequestHandler::handle',
195 $this->getBaseRouteRequirements($method),
199 // The HTTP method is a requirement for this route.
205 * Gets the base route requirements for a particular method.
208 * The HTTP method to be used for the route.
211 * An array of requirements for parameters.
213 protected function getBaseRouteRequirements($method) {
214 $lower_method = strtolower($method);
215 // Every route MUST have requirements that result in the access manager
216 // having access checks to check. If it does not, the route is made
217 // inaccessible. So, we default to granting access to everyone. If a
218 // permission exists, then we add that below. The access manager requires
219 // that ALL access checks must grant access, so this still results in
225 // Only specify route requirements if the default permission exists. For any
226 // more advanced route definition, resource plugins extending this base
227 // class must override this method.
228 $permission = "restful $lower_method $this->pluginId";
229 if (isset($this->permissions()[$permission])) {
230 $requirements['_permission'] = $permission;
233 return $requirements;