Only allow sort fields that match the current object alias.

Instead of modifying aliases that do not match, only allow aliases that
do match.

Refs #3803
This commit is contained in:
mark_story 2013-04-30 12:41:42 -04:00
parent fab5a6f4d9
commit 37ce6dfc81
2 changed files with 28 additions and 3 deletions

View file

@ -384,10 +384,11 @@ class PaginatorComponent extends Component {
if (strpos($key, '.') !== false) {
list($alias, $field) = explode('.', $key);
}
$correctAlias = ($object->alias == $alias);
if ($object->hasField($field)) {
if ($correctAlias && $object->hasField($field)) {
$order[$object->alias . '.' . $field] = $value;
} elseif ($object->hasField($key, true)) {
} elseif ($correctAlias && $object->hasField($key, true)) {
$order[$field] = $value;
} elseif (isset($object->{$alias}) && $object->{$alias}->hasField($field, true)) {
$order[$alias . '.' . $field] = $value;

View file

@ -959,6 +959,30 @@ class PaginatorComponentTest extends CakeTestCase {
$this->assertEquals('desc', $result['order']['something']);
}
/**
* test that sorting fields is alias specific
*
* @return void
*/
public function testValidateSortSharedFields() {
$model = $this->getMock('Model');
$model->alias = 'Parent';
$model->Child = $this->getMock('Model');
$model->Child->alias = 'Child';
$model->expects($this->never())
->method('hasField');
$model->Child->expects($this->at(0))
->method('hasField')
->with('something')
->will($this->returnValue(true));
$options = array('sort' => 'Child.something', 'direction' => 'desc');
$result = $this->Paginator->validateSort($model, $options);
$this->assertEquals('desc', $result['order']['Child.something']);
}
/**
* test that multiple sort works.
*
@ -1016,7 +1040,7 @@ class PaginatorComponentTest extends CakeTestCase {
$options = array('sort' => 'Derp.id');
$result = $this->Paginator->validateSort($model, $options);
$this->assertEquals(array('Model.id' => 'asc'), $result['order']);
$this->assertEquals(array(), $result['order']);
}
/**