X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Flink%2Fsrc%2FPlugin%2FValidation%2FConstraint%2FLinkAccessConstraintValidator.php;fp=web%2Fcore%2Fmodules%2Flink%2Fsrc%2FPlugin%2FValidation%2FConstraint%2FLinkAccessConstraintValidator.php;h=16ccae0fc7eb66f4603940ae234a090078620904;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/core/modules/link/src/Plugin/Validation/Constraint/LinkAccessConstraintValidator.php b/web/core/modules/link/src/Plugin/Validation/Constraint/LinkAccessConstraintValidator.php new file mode 100644 index 000000000..16ccae0fc --- /dev/null +++ b/web/core/modules/link/src/Plugin/Validation/Constraint/LinkAccessConstraintValidator.php @@ -0,0 +1,63 @@ +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]); + } + } + } + +}