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 07f66d7
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/Dev/FixFilePermissionsTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace SilverStripe\Dev\Tasks;

use SilverStripe\Assets\File;
use SilverStripe\Dev\BuildTask;
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.
* 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)']
)->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 "Done!";
}
}
32 changes: 32 additions & 0 deletions tests/php/FixFilePermissionsTaskTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace SilverStripe\Assets\Tests;

use SilverStripe\Assets\File;
use SilverStripe\Assets\Folder;
use SilverStripe\Assets\Image;
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('Inherit', Folder::get()->where(['Name' => 'SubFolder'])->first()->CanViewType);
$this->assertEquals('LoggedInUsers', Image::get()->where(['Name' => 'myimage.jpg'])->first()->CanViewType);
$this->assertEquals('OnlyTheseUsers', Image::get()->where(['Name' => 'myotherimage.jpg'])->first()->CanViewType);
$this->assertEquals('Inherit', File::get()->where(['Name' => 'anotherfile.jpg'])->first()->CanViewType);
$this->assertEquals('Inherit', File::get()->where(['Name' => 'file.jpg'])->first()->CanViewType);
$this->assertEquals('Anyone', File::get()->where(['Name' => 'picture.jpg'])->first()->CanViewType);
$this->assertEquals('Inherit', File::get()->where(['Name' => 'file2.jpg'])->first()->CanViewType);
}
}
31 changes: 31 additions & 0 deletions tests/php/FixFilePermissionsTaskTest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
SilverStripe\Assets\Folder:
parent:
Name: ParentFolder
CanViewType: NULL
subfolder:
Name: SubFolder
Parent: =>SilverStripe\Assets\Folder.parent
SilverStripe\Assets\Image:
image1:
Name: myimage.jpg
CanViewType: LoggedInUsers
image2:
Name: myotherimage.jpg
ParentID: =>SilverStripe\Assets\Folder.subfolder
CanViewType: OnlyTheseUsers
SilverStripe\Assets\File:
file1:
Name: anotherfile.jpg
CanViewType: Inherit
file2:
Name: file.jpg
ParentID: =>SilverStripe\Assets\Folder.parent
CanViewType: NULL
file3:
Name: picture.jpg
ParentID: =>SilverStripe\Assets\Folder.subfolder
CanViewType: Anyone
file4:
Name: file2.jpg
ParentID: =>SilverStripe\Assets\Folder.parent
CanViewType:

0 comments on commit 07f66d7

Please sign in to comment.