Security update to Drupal 8.4.6
[yaffs-website] / web / core / modules / link / src / Plugin / Validation / Constraint / LinkAccessConstraintValidator.php
1 <?php
2
3 namespace Drupal\link\Plugin\Validation\Constraint;
4
5 use Drupal\Core\Session\AccountProxyInterface;
6 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
7 use Symfony\Component\DependencyInjection\ContainerInterface;
8 use Symfony\Component\Validator\Constraint;
9 use Symfony\Component\Validator\ConstraintValidator;
10
11 /**
12  * Validates the LinkAccess constraint.
13  */
14 class LinkAccessConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
15
16   /**
17    * Proxy for the current user account.
18    *
19    * @var \Drupal\Core\Session\AccountProxyInterface
20    */
21   protected $current_user;
22
23   /**
24    * Constructs an instance of the LinkAccessConstraintValidator class.
25    *
26    * @param \Drupal\Core\Session\AccountProxyInterface $current_user
27    *   The current user account.
28    */
29   public function __construct(AccountProxyInterface $current_user) {
30     $this->current_user = $current_user;
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public static function create(ContainerInterface $container) {
37     return new static(
38       $container->get('current_user')
39     );
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public function validate($value, Constraint $constraint) {
46     if (isset($value)) {
47       try {
48         $url = $value->getUrl();
49       }
50       // If the URL is malformed this constraint cannot check access.
51       catch (\InvalidArgumentException $e) {
52         return;
53       }
54       // Disallow URLs if the current user doesn't have the 'link to any page'
55       // permission nor can access this URI.
56       $allowed = $this->current_user->hasPermission('link to any page') || $url->access();
57       if (!$allowed) {
58         $this->context->addViolation($constraint->message, ['@uri' => $value->uri]);
59       }
60     }
61   }
62
63 }