Skip to content

Commit

Permalink
Add FixFilePermissionsTask to fix up NULL file permissions from SS3
Browse files Browse the repository at this point in the history
  • Loading branch information
bergice committed Apr 30, 2019
1 parent b44662b commit 9abae4f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/Dev/FixFilePermissionsTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace SilverStripe\Dev\Tasks;

use SilverStripe\Assets\File;
use SilverStripe\Assets\Folder;
use SilverStripe\Dev\BuildTask;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\Queries\SQLUpdate;
use SilverStripe\Security\InheritedPermissionFlusher;

/**
* Files imported from SS3 might end up with broken permissions if there is a case conflict.
* @see https://github.com/silverstripe/silverstripe-secureassets
* This task resets the `CanViewType` of files that are `NULL`.
* You need to flush your cache after running this task via CLI.
*/
class FixFilePermissionsTask extends BuildTask
{
private static $segment = 'FixFilePermissionsTask';

protected $title = 'Fix File Permissions';

protected $description = '
Reset the permission of files that have a `NULL` CanViewType value.
You need to flush your cache after running this task via CLI.
';

public function run($request)
{
SQLUpdate::create(
File::singleton()->baseTable(),
['CanViewType' => 'Inherit'],
['ISNULL(CanViewType)', 'ClassName' => Folder::class]
)->execute();

// This part won't work if run from the CLI, because Apache and the CLI don't share the same cache.
InheritedPermissionFlusher::flush();

echo 'Repaired ' . DB::affected_rows() . ' folders with broken CanViewType settings';
}
}
25 changes: 25 additions & 0 deletions tests/php/FixFilePermissionsTaskTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace SilverStripe\Assets\Tests;

use SilverStripe\Assets\Folder;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Dev\Tasks\FixFilePermissionsTask;

/**
* Ensures that files with invalid permissions can be fixed.
*/
class FixFilePermissionsTaskTest extends SapphireTest
{
protected static $fixture_file = 'FixFilePermissionsTaskTest.yml';

public function testTask()
{
$fixFilePermissionsTask = new FixFilePermissionsTask();
$fixFilePermissionsTask->run(null);

$this->assertEquals('Inherit', Folder::get()->where(['Name' => 'ParentFolder'])->first()->CanViewType);
$this->assertEquals('Anyone', Folder::get()->where(['Name' => 'SubFolder'])->first()->CanViewType);
$this->assertEquals('Inherit', Folder::get()->where(['Name' => 'AnotherFolder'])->first()->CanViewType);
}
}
11 changes: 11 additions & 0 deletions tests/php/FixFilePermissionsTaskTest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
SilverStripe\Assets\Folder:
parent:
Name: ParentFolder
CanViewType: NULL
subfolder:
Name: SubFolder
Parent: =>SilverStripe\Assets\Folder.parent
CanViewType: Anyone
anotherfolder:
Name: AnotherFolder
CanViewType:

0 comments on commit 9abae4f

Please sign in to comment.