mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge branch '4.10' into 4.11
This commit is contained in:
commit
dc98cad48a
@ -11,6 +11,7 @@ use SilverStripe\ORM\DataObject;
|
||||
use SilverStripe\View\ArrayData;
|
||||
use SilverStripe\View\SSViewer;
|
||||
use LogicException;
|
||||
use SilverStripe\Core\Injector\Injector;
|
||||
|
||||
/**
|
||||
* GridFieldSortableHeader adds column headers to a {@link GridField} that can
|
||||
@ -271,6 +272,16 @@ class GridFieldSortableHeader extends AbstractGridFieldComponent implements Grid
|
||||
return $dataList;
|
||||
}
|
||||
|
||||
// Prevent SQL Injection by validating that SortColumn exists
|
||||
/** @var GridFieldDataColumns $columns */
|
||||
$columns = $gridField->getConfig()->getComponentByType(GridFieldDataColumns::class);
|
||||
$fields = $columns->getDisplayFields($gridField);
|
||||
if (!array_key_exists($state->SortColumn, $fields) &&
|
||||
!in_array($state->SortColumn, $this->getFieldSorting())
|
||||
) {
|
||||
throw new LogicException('Invalid SortColumn: ' . $state->SortColumn);
|
||||
}
|
||||
|
||||
return $dataList->sort($state->SortColumn, $state->SortDirection('asc'));
|
||||
}
|
||||
|
||||
|
@ -71,13 +71,14 @@ class GridFieldSortableHeaderTest extends SapphireTest
|
||||
$list = Team::get()->filter([ 'ClassName' => Team::class ]);
|
||||
$config = new GridFieldConfig_RecordEditor();
|
||||
$gridField = new GridField('testfield', 'testfield', $list, $config);
|
||||
$component = $gridField->getConfig()->getComponentByType(GridFieldSortableHeader::class);
|
||||
|
||||
// Test normal sorting
|
||||
$component->setFieldSorting(['Name' => 'City']);
|
||||
$state = $gridField->State->GridFieldSortableHeader;
|
||||
$state->SortColumn = 'City';
|
||||
$state->SortDirection = 'asc';
|
||||
|
||||
$component = $gridField->getConfig()->getComponentByType(GridFieldSortableHeader::class);
|
||||
$listA = $component->getManipulatedData($gridField, $list);
|
||||
|
||||
$state->SortDirection = 'desc';
|
||||
@ -93,6 +94,7 @@ class GridFieldSortableHeaderTest extends SapphireTest
|
||||
);
|
||||
|
||||
// Test one relation 'deep'
|
||||
$component->setFieldSorting(['Name' => 'Cheerleader.Name']);
|
||||
$state->SortColumn = 'Cheerleader.Name';
|
||||
$state->SortDirection = 'asc';
|
||||
$relationListA = $component->getManipulatedData($gridField, $list);
|
||||
@ -110,6 +112,7 @@ class GridFieldSortableHeaderTest extends SapphireTest
|
||||
);
|
||||
|
||||
// Test two relations 'deep'
|
||||
$component->setFieldSorting(['Name' => 'Cheerleader.Hat.Colour']);
|
||||
$state->SortColumn = 'Cheerleader.Hat.Colour';
|
||||
$state->SortDirection = 'asc';
|
||||
$relationListC = $component->getManipulatedData($gridField, $list);
|
||||
@ -139,6 +142,7 @@ class GridFieldSortableHeaderTest extends SapphireTest
|
||||
$component = $gridField->getConfig()->getComponentByType(GridFieldSortableHeader::class);
|
||||
|
||||
// Test that inherited dataobjects will work correctly
|
||||
$component->setFieldSorting(['Name' => 'Cheerleader.Hat.Colour']);
|
||||
$state->SortColumn = 'Cheerleader.Hat.Colour';
|
||||
$state->SortDirection = 'asc';
|
||||
$relationListA = $component->getManipulatedData($gridField, $list);
|
||||
@ -179,6 +183,7 @@ class GridFieldSortableHeaderTest extends SapphireTest
|
||||
);
|
||||
|
||||
// Test subclasses of tables
|
||||
$component->setFieldSorting(['Name' => 'CheerleadersMom.Hat.Colour']);
|
||||
$state->SortColumn = 'CheerleadersMom.Hat.Colour';
|
||||
$state->SortDirection = 'asc';
|
||||
$relationListB = $component->getManipulatedData($gridField, $list);
|
||||
@ -229,4 +234,21 @@ class GridFieldSortableHeaderTest extends SapphireTest
|
||||
$relationListBdesc->column('City')
|
||||
);
|
||||
}
|
||||
|
||||
public function testSortColumnValidation()
|
||||
{
|
||||
$this->expectException(\LogicException::class);
|
||||
$this->expectExceptionMessage('Invalid SortColumn: INVALID');
|
||||
|
||||
$list = Team::get()->filter([ 'ClassName' => Team::class ]);
|
||||
$config = new GridFieldConfig_RecordEditor();
|
||||
$gridField = new GridField('testfield', 'testfield', $list, $config);
|
||||
$component = $gridField->getConfig()->getComponentByType(GridFieldSortableHeader::class);
|
||||
|
||||
$state = $gridField->State->GridFieldSortableHeader;
|
||||
$state->SortColumn = 'INVALID';
|
||||
$state->SortDirection = 'asc';
|
||||
|
||||
$component->getManipulatedData($gridField, $list);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user