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(); } // If the URL is malformed this constraint cannot check access. 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]); } } } }