You are here

class LinkAccessConstraintValidator in Drupal 8

Same name and namespace in other branches
  1. 9 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

File

core/modules/link/src/Plugin/Validation/Constraint/LinkAccessConstraintValidator.php, line 14

Namespace

Drupal\link\Plugin\Validation\Constraint
View source
class LinkAccessConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {

  /**
   * 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 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, [
          '@uri' => $value->uri,
        ]);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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::validate public function Checks if the passed value is valid.
LinkAccessConstraintValidator::__construct public function Constructs an instance of the LinkAccessConstraintValidator class.