Skip to content

Commit

Permalink
NEW FixFilePermissionsTask to fix secureassets permissions (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
bergice authored and chillu committed May 2, 2019
1 parent 9475c8a commit 236094c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/Dev/FixFolderPermissionsHelper.php
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();
}
}
26 changes: 26 additions & 0 deletions tests/php/Dev/Tasks/FixFolderPermissionsHelperTest.php
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);
}
}
11 changes: 11 additions & 0 deletions tests/php/Dev/Tasks/FixFolderPermissionsHelperTest.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 236094c

Please sign in to comment.