You are here

class Uri in Content-Security-Policy 8

CSP Reporting Plugin for a URI endpoint.

Plugin annotation


@CspReportingHandler(
  id = "uri",
  label = "URI",
  description = @Translation("Reports will be sent to a URI."),
)

Hierarchy

Expanded class hierarchy of Uri

File

src/Plugin/CspReportingHandler/Uri.php, line 20

Namespace

Drupal\csp\Plugin\CspReportingHandler
View source
class Uri extends ReportingHandlerBase {
  use StringTranslationTrait;

  /**
   * {@inheritdoc}
   */
  public function getForm(array $form) {
    $form['uri'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('URI'),
      '#description' => $this
        ->t('The URI to send reports to.'),
      '#default_value' => isset($this->configuration['uri']) ? $this->configuration['uri'] : '',
      '#states' => [
        'required' => [
          ':input[name="' . $this->configuration['type'] . '[enable]"]' => [
            'checked' => TRUE,
          ],
          ':input[name="' . $this->configuration['type'] . '[reporting][handler]"]' => [
            'value' => $this->pluginId,
          ],
        ],
      ],
    ];
    unset($form['#description']);
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $uri = $form_state
      ->getValue($form['uri']['#parents']);
    if (!(UrlHelper::isValid($uri, TRUE) && preg_match('/^https?:/', $uri))) {
      $form_state
        ->setError($form['uri'], 'Must be a valid http or https URL.');
    }
  }

  /**
   * {@inheritdoc}
   */
  public function alterPolicy(Csp $policy) {
    $policy
      ->setDirective('report-uri', $this->configuration['uri']);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ReportingHandlerBase::$configuration protected property The Plugin Configuration.
ReportingHandlerBase::$pluginDefinition protected property The Plugin Definition.
ReportingHandlerBase::$pluginId protected property The Plugin ID.
ReportingHandlerBase::__construct public function Reporting Handler plugin constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
Uri::alterPolicy public function Alter the provided policy according to the plugin settings. Overrides ReportingHandlerBase::alterPolicy
Uri::getForm public function Get the form fields for configuring this reporting handler. Overrides ReportingHandlerBase::getForm
Uri::validateForm public function Validate the form fields of this report handler. Overrides ReportingHandlerBase::validateForm