class FileExtensionSecureConstraintValidator

Same name in other branches
  1. 11.x core/modules/file/src/Plugin/Validation/Constraint/FileExtensionSecureConstraintValidator.php \Drupal\file\Plugin\Validation\Constraint\FileExtensionSecureConstraintValidator

Validator for the FileExtensionSecureConstraint.

Hierarchy

Expanded class hierarchy of FileExtensionSecureConstraintValidator

File

core/modules/file/src/Plugin/Validation/Constraint/FileExtensionSecureConstraintValidator.php, line 17

Namespace

Drupal\file\Plugin\Validation\Constraint
View source
class FileExtensionSecureConstraintValidator extends BaseFileConstraintValidator implements ContainerInjectionInterface {
    
    /**
     * Creates a new FileExtensionSecureConstraintValidator.
     *
     * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
     *   The config factory.
     */
    public function __construct(ConfigFactoryInterface $configFactory) {
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container) {
        return new static($container->get('config.factory'));
    }
    
    /**
     * {@inheritdoc}
     */
    public function validate(mixed $value, Constraint $constraint) {
        $file = $this->assertValueIsFile($value);
        if (!$constraint instanceof FileExtensionSecureConstraint) {
            throw new UnexpectedTypeException($constraint, FileExtensionSecureConstraint::class);
        }
        $allowInsecureUploads = $this->configFactory
            ->get('system.file')
            ->get('allow_insecure_uploads');
        if (!$allowInsecureUploads && preg_match(FileSystemInterface::INSECURE_EXTENSION_REGEX, $file->getFilename())) {
            $this->context
                ->addViolation($constraint->message);
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
BaseFileConstraintValidator::assertValueIsFile protected function Checks the value is of type FileInterface.
FileExtensionSecureConstraintValidator::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
FileExtensionSecureConstraintValidator::validate public function
FileExtensionSecureConstraintValidator::__construct public function Creates a new FileExtensionSecureConstraintValidator.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.