Skip to content
Snippets Groups Projects

Issue #3231996: Ensure file system is writeable

1 unresolved thread
Compare and
2 files
+ 86
0
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 80
0
 
<?php
 
 
namespace Drupal\automatic_updates\Validator;
 
 
use Drupal\automatic_updates\AutomaticUpdatesEvents;
 
use Drupal\automatic_updates\Event\UpdateEvent;
 
use Drupal\automatic_updates\PathLocator;
 
use Drupal\automatic_updates\Validation\ValidationResult;
 
use Drupal\Core\StringTranslation\StringTranslationTrait;
 
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
 
 
class WritableFileSystemValidator implements EventSubscriberInterface {
 
 
use StringTranslationTrait;
 
 
/**
 
* The path locator service.
 
*
 
* @var \Drupal\automatic_updates\PathLocator
 
*/
 
protected $pathLocator;
 
 
/**
 
* Constructs a WritableFileSystemValidator object.
 
*
 
* @param \Drupal\automatic_updates\PathLocator $path_locator
 
* The path locator service.
 
*/
 
public function __construct(PathLocator $path_locator) {
 
$this->pathLocator = $path_locator;
 
}
 
 
/**
 
* Checks that the file system is writable.
 
*
 
* @param \Drupal\automatic_updates\Event\UpdateEvent $event
 
* The event object.
 
*
 
* @todo It might make sense to use a more sophisticated method of testing
 
* writability than is_writable(), since it's not clear if that can return
 
* false negatives/positives due to things like SELinux, exotic file
 
* systems, and so forth.
 
*/
 
public function checkPermissions(UpdateEvent $event): void {
 
$messages = [];
 
 
$dir = $this->pathLocator->getProjectRoot();
 
if (!is_writable($dir)) {
 
$messages[] = $this->t('The project directory "@dir" is not writable.', ['@dir' => $dir]);
 
}
 
 
$web_root = $this->pathLocator->getWebRoot();
 
if ($web_root) {
 
$dir .= DIRECTORY_SEPARATOR . $web_root;
 
if (!is_writable($dir)) {
 
$messages[] = $this->t('The Drupal directory "@dir" is not writable.', ['@dir' => $dir]);
 
}
 
}
 
 
$dir = $this->pathLocator->getVendorDirectory();
 
if (!is_writable($dir)) {
 
$messages[] = $this->t('The vendor directory "@dir" is not writable.', ['@dir' => $dir]);
 
}
 
 
if ($messages) {
 
$result = ValidationResult::createError($messages, $this->t('The file system is not writable.'));
 
$event->addValidationResult($result);
 
}
 
}
 
 
/**
 
* {@inheritdoc}
 
*/
 
public static function getSubscribedEvents() {
 
return [
 
AutomaticUpdatesEvents::READINESS_CHECK => 'checkPermissions',
 
];
 
}
 
 
}
Loading