-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
FixFilePermissionsTask
to fix up NULL
file permissions from SS3
- Loading branch information
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |