-
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.
NEW FixFilePermissionsTask to fix secureassets permissions (#250)
- 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,41 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Dev\Tasks; | ||
|
||
use SilverStripe\Assets\File; | ||
use SilverStripe\Assets\Folder; | ||
use SilverStripe\Core\Injector\Injectable; | ||
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 helper class resets the `CanViewType` of files that are `NULL`. | ||
* You need to flush your cache after running this via CLI. | ||
*/ | ||
class FixFolderPermissionsHelper | ||
{ | ||
use Injectable; | ||
|
||
/** | ||
* @return int Returns the number of records updated. | ||
*/ | ||
public function run() | ||
{ | ||
SQLUpdate::create() | ||
->setTable('"' . File::singleton()->baseTable() . '"') | ||
->setAssignments(['"CanViewType"' => 'Inherit']) | ||
->setWhere([ | ||
'"CanViewType" IS NULL', | ||
'"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(); | ||
|
||
return DB::affected_rows(); | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Assets\Tests\Dev\Tasks; | ||
|
||
use SilverStripe\Assets\Folder; | ||
use SilverStripe\Dev\SapphireTest; | ||
use SilverStripe\Dev\Tasks\FixFolderPermissionsHelper; | ||
|
||
/** | ||
* Ensures that files with invalid permissions can be fixed. | ||
*/ | ||
class FixFolderPermissionsHelperTest extends SapphireTest | ||
{ | ||
protected static $fixture_file = 'FixFolderPermissionsHelperTest.yml'; | ||
|
||
public function testTask() | ||
{ | ||
$task = new FixFolderPermissionsHelper(); | ||
$updated = $task->run(); | ||
|
||
$this->assertEquals('Inherit', Folder::get()->filter('Name', 'ParentFolder')->first()->CanViewType); | ||
$this->assertEquals('Anyone', Folder::get()->filter('Name', 'SubFolder')->first()->CanViewType); | ||
$this->assertEquals('Inherit', Folder::get()->filter('Name', 'AnotherFolder')->first()->CanViewType); | ||
$this->assertEquals(2, $updated); | ||
} | ||
} |
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: |