You are here

class LinkAccessConstraintValidator in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/link/src/Plugin/Validation/Constraint/LinkAccessConstraintValidator.php \Drupal\link\Plugin\Validation\Constraint\LinkAccessConstraintValidator

Validates the LinkAccess constraint.

Hierarchy

Expanded class hierarchy of LinkAccessConstraintValidator

1 file declares its use of LinkAccessConstraintValidator
LinkAccessConstraintValidatorTest.php in core/modules/link/tests/src/Unit/Plugin/Validation/Constraint/LinkAccessConstraintValidatorTest.php
Contains \Drupal\Tests\link\Unit\Plugin\Validation\Constraint\LinkAccessConstraintValidatorTest.

File

core/modules/link/src/Plugin/Validation/Constraint/LinkAccessConstraintValidator.php, line 20
Contains \Drupal\link\Plugin\Validation\Constraint\LinkAccessConstraintValidator.

Namespace

Drupal\link\Plugin\Validation\Constraint
View source
class LinkAccessConstraintValidator implements ConstraintValidatorInterface, ContainerInjectionInterface {

  /**
   * Stores the validator's state during validation.
   *
   * @var \Symfony\Component\Validator\ExecutionContextInterface
   */
  protected $context;

  /**
   * Proxy for the current user account.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $current_user;

  /**
   * Constructs an instance of the LinkAccessConstraintValidator class.
   *
   * @param \Drupal\Core\Session\AccountProxyInterface $current_user
   *   The current user account.
   */
  public function __construct(AccountProxyInterface $current_user) {
    $this->current_user = $current_user;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('current_user'));
  }

  /**
   * {@inheritdoc}
   */
  public function initialize(ExecutionContextInterface $context) {
    $this->context = $context;
  }

  /**
   * {@inheritdoc}
   */
  public function validate($value, Constraint $constraint) {
    if (isset($value)) {
      try {
        $url = $value
          ->getUrl();
      } catch (\InvalidArgumentException $e) {
        return;
      }

      // Disallow URLs if the current user doesn't have the 'link to any page'
      // permission nor can access this URI.
      $allowed = $this->current_user
        ->hasPermission('link to any page') || $url
        ->access();
      if (!$allowed) {
        $this->context
          ->addViolation($constraint->message, array(
          '@uri' => $value->uri,
        ));
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LinkAccessConstraintValidator::$context protected property Stores the validator's state during validation.
LinkAccessConstraintValidator::$current_user protected property Proxy for the current user account.
LinkAccessConstraintValidator::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
LinkAccessConstraintValidator::initialize public function Initializes the constraint validator. Overrides ConstraintValidatorInterface::initialize
LinkAccessConstraintValidator::validate public function Checks if the passed value is valid. Overrides ConstraintValidatorInterface::validate
LinkAccessConstraintValidator::__construct public function Constructs an instance of the LinkAccessConstraintValidator class.