Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix prevent soft delete all without conditions set #2090

Merged
merged 5 commits into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1587,6 +1587,18 @@ public function countAllResults(bool $reset = true, bool $test = false)
return (int) $row->numrows;
}

//--------------------------------------------------------------------
/**
* Get compiled 'where' condition string
*
* Compiles the set conditions and returns the sql statement
*
* @return string
*/
public function getCompiledQBWhere()
{
return $this->QBWhere;
}
//--------------------------------------------------------------------

/**
Expand Down
19 changes: 14 additions & 5 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
use CodeIgniter\Database\ConnectionInterface;
use CodeIgniter\Validation\ValidationInterface;
use CodeIgniter\Database\Exceptions\DataException;
use CodeIgniter\Database\Exceptions\DatabaseException;
use ReflectionClass;
use ReflectionProperty;
use stdClass;
Expand Down Expand Up @@ -714,13 +715,13 @@ public function insert($data = null, bool $returnID = true)
$result = $this->builder()
->set($data['data'], '', $escape)
->insert();

// If insertion succeeded then save the insert ID
if ($result)
{
$this->insertID = $this->db->insertID();
}

$this->trigger('afterInsert', ['data' => $originalData, 'result' => $result]);

// If insertion failed, get out of here
Expand Down Expand Up @@ -913,6 +914,14 @@ public function delete($id = null, bool $purge = false)

if ($this->useSoftDeletes && ! $purge)
{
if (empty($builder->getCompiledQBWhere()))
{
if (CI_DEBUG)
{
throw new DatabaseException('Deletes are not allowed unless they contain a "where" or "like" clause.');
}
return false;
}
$set[$this->deletedField] = $this->setDate();

if ($this->useTimestamps && ! empty($this->updatedField))
Expand Down Expand Up @@ -948,8 +957,8 @@ public function purgeDeleted()
}

return $this->builder()
->where($this->table . '.' . $this->deletedField . ' IS NOT NULL')
->delete();
->where($this->table . '.' . $this->deletedField . ' IS NOT NULL')
->delete();
}

//--------------------------------------------------------------------
Expand Down Expand Up @@ -982,7 +991,7 @@ public function onlyDeleted()
$this->tempUseSoftDeletes = false;

$this->builder()
->where($this->table . '.' . $this->deletedField . ' IS NOT NULL');
->where($this->table . '.' . $this->deletedField . ' IS NOT NULL');

return $this;
}
Expand Down
53 changes: 53 additions & 0 deletions tests/system/Database/Live/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,62 @@ public function testOnlyDeleted()

$this->assertCount(1, $users);
}
/**
* If where condition is set, beyond the value was empty (0,'', NULL, etc.),
* Exception should not be thrown because condition was explicity set
*
* @dataProvider emptyPkValues
* @return void
*/
public function testDontThrowExceptionWhenSoftDeleteConditionIsSetWithEmptyValue($emptyValue)
{
$model = new UserModel();
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted_at IS NULL' => null]);
$model->where('id', $emptyValue)->delete();
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted_at IS NULL' => null]);
unset($model);
} //--------------------------------------------------------------------

/**
* @expectedException \CodeIgniter\Database\Exceptions\DatabaseException
* @expectedExceptionMessage Deletes are not allowed unless they contain a "where" or "like" clause.
* @dataProvider emptyPkValues
* @return void
*/
public function testThrowExceptionWhenSoftDeleteParamIsEmptyValue($emptyValue)
{
$model = new UserModel();
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted_at IS NULL' => null]);
$model->delete($emptyValue);
}

//--------------------------------------------------------------------

/**
* @expectedException \CodeIgniter\Database\Exceptions\DatabaseException
* @expectedExceptionMessage Deletes are not allowed unless they contain a "where" or "like" clause.
* @dataProvider emptyPkValues
* @return void
*/
public function testDontDeleteRowsWhenSoftDeleteParamIsEmpty($emptyValue)
{
$model = new UserModel();
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted_at IS NULL' => null]);
$model->delete($emptyValue);
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted_at IS NULL' => null]);
unset($model);
}

public function emptyPkValues()
{
return [
[0],
[null],
['0'],
];
}
//--------------------------------------------------------------------

public function testChunk()
{
$model = new UserModel();
Expand Down