Reworking how the preq_quote on Query named bindings is applied to help with #580

This commit is contained in:
Lonnie Ezell 2017-07-03 14:36:26 -05:00
parent 60ab63b83e
commit 3a0a5e85fd
No known key found for this signature in database
GPG Key ID: 8EB408F8D82F5002
4 changed files with 32 additions and 6 deletions

View File

@ -412,19 +412,21 @@ class Query implements QueryInterface
{
foreach ($value as &$item)
{
$item = preg_quote($item);
$item = preg_quote($item, '|');
}
$escapedValue = '('.implode(',', $escapedValue).')';
}
else
{
$escapedValue = strpos($escapedValue, '\\') !== false
? preg_quote(trim($escapedValue, $this->db->escapeChar))
: $escapedValue;
$escapedValue = preg_quote(trim($escapedValue, $this->db->escapeChar), '|');
}
$sql = preg_replace('/:'.$placeholder.'(?!\w)/', $escapedValue, $sql);
// preg_quoting can cause issues with some characters in the final query,
// but NOT preg_quoting causes other characters to be intepreted, like $.
$escapedValue = str_replace('\\.', '.', $escapedValue);
$sql = preg_replace('|:'.$placeholder.'(?!\w)|', $escapedValue, $sql);
}
return $sql;

View File

@ -13,7 +13,7 @@ class Migration_Create_test_tables extends \CodeIgniter\Database\Migration
],
'name' => [
'type' => 'VARCHAR',
'constraint' => 40,
'constraint' => 80,
],
'email' => [
'type' => 'VARCHAR',

View File

@ -6,6 +6,8 @@ class UserModel extends Model
{
protected $table = 'user';
protected $allowedFields = ['name', 'email', 'country', 'deleted'];
protected $returnType = 'object';
protected $useSoftDeletes = true;

View File

@ -492,4 +492,26 @@ class ModelTest extends \CIDatabaseTestCase
$this->seeInDatabase('job', ['name' => 'Senior Developer']);
}
/**
* @see https://github.com/bcit-ci/CodeIgniter4/issues/580
*/
public function testPasswordsStoreCorrectly()
{
$model = new UserModel();
$pass = password_hash('secret123', PASSWORD_BCRYPT);
$data = [
'name' => $pass,
'email' => 'foo@example.com',
'country' => 'US',
'deleted' => 0
];
$model->insert($data);
$this->seeInDatabase('user', $data);
}
}