Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / action / src / Plugin / Action / GotoAction.php
1 <?php
2
3 namespace Drupal\action\Plugin\Action;
4
5 use Drupal\Component\Utility\UrlHelper;
6 use Drupal\Core\Access\AccessResult;
7 use Drupal\Core\Action\ConfigurableActionBase;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10 use Drupal\Core\Session\AccountInterface;
11 use Drupal\Core\Utility\UnroutedUrlAssemblerInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
14 use Symfony\Component\HttpFoundation\RedirectResponse;
15 use Symfony\Component\HttpKernel\KernelEvents;
16
17 /**
18  * Redirects to a different URL.
19  *
20  * @Action(
21  *   id = "action_goto_action",
22  *   label = @Translation("Redirect to URL"),
23  *   type = "system"
24  * )
25  */
26 class GotoAction extends ConfigurableActionBase implements ContainerFactoryPluginInterface {
27
28   /**
29    * The event dispatcher service.
30    *
31    * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
32    */
33   protected $dispatcher;
34
35   /**
36    * The unrouted URL assembler service.
37    *
38    * @var \Drupal\Core\Utility\UnroutedUrlAssemblerInterface
39    */
40   protected $unroutedUrlAssembler;
41
42   /**
43    * Constructs a new DeleteNode object.
44    *
45    * @param array $configuration
46    *   A configuration array containing information about the plugin instance.
47    * @param string $plugin_id
48    *   The plugin ID for the plugin instance.
49    * @param mixed $plugin_definition
50    *   The plugin implementation definition.
51    * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
52    *   The tempstore factory.
53    * @param \Drupal\Core\Utility\UnroutedUrlAssemblerInterface $url_assembler
54    *   The unrouted URL assembler service.
55    */
56   public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $dispatcher, UnroutedUrlAssemblerInterface $url_assembler) {
57     parent::__construct($configuration, $plugin_id, $plugin_definition);
58
59     $this->dispatcher = $dispatcher;
60     $this->unroutedUrlAssembler = $url_assembler;
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
67     return new static($configuration, $plugin_id, $plugin_definition, $container->get('event_dispatcher'), $container->get('unrouted_url_assembler'));
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function execute($object = NULL) {
74     $url = $this->configuration['url'];
75     // Leave external URLs unchanged, and assemble others as absolute URLs
76     // relative to the site's base URL.
77     if (!UrlHelper::isExternal($url)) {
78       $parts = UrlHelper::parse($url);
79       // @todo '<front>' is valid input for BC reasons, may be removed by
80       //   https://www.drupal.org/node/2421941
81       if ($parts['path'] === '<front>') {
82         $parts['path'] = '';
83       }
84       $uri = 'base:' . $parts['path'];
85       $options = [
86         'query' => $parts['query'],
87         'fragment' => $parts['fragment'],
88         'absolute' => TRUE,
89       ];
90       // Treat this as if it's user input of a path relative to the site's
91       // base URL.
92       $url = $this->unroutedUrlAssembler->assemble($uri, $options);
93     }
94     $response = new RedirectResponse($url);
95     $listener = function ($event) use ($response) {
96       $event->setResponse($response);
97     };
98     // Add the listener to the event dispatcher.
99     $this->dispatcher->addListener(KernelEvents::RESPONSE, $listener);
100   }
101
102   /**
103    * {@inheritdoc}
104    */
105   public function defaultConfiguration() {
106     return [
107       'url' => '',
108     ];
109   }
110
111   /**
112    * {@inheritdoc}
113    */
114   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
115     $form['url'] = [
116       '#type' => 'textfield',
117       '#title' => t('URL'),
118       '#description' => t('The URL to which the user should be redirected. This can be an internal URL like /node/1234 or an external URL like @url.', ['@url' => 'http://example.com']),
119       '#default_value' => $this->configuration['url'],
120       '#required' => TRUE,
121     ];
122     return $form;
123   }
124
125   /**
126    * {@inheritdoc}
127    */
128   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
129     $this->configuration['url'] = $form_state->getValue('url');
130   }
131
132   /**
133    * {@inheritdoc}
134    */
135   public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
136     $access = AccessResult::allowed();
137     return $return_as_object ? $access : $access->isAllowed();
138   }
139
140 }