Skip to content

Commit

Permalink
Fix lonnieezell#324: conversion int/bool for PostgreSQL and other str…
Browse files Browse the repository at this point in the history
…ict DB

This patch doesn't modify the database schema and, instead, forcibly converts
booleans to integers.

Another way of fixing lonnieezell#324 would be to use booleans at the database level : the
code change is bigger but, since MySQL & MariaDB would implictly convert to
tinyint and SQLite isn't typed, the impact on existing deployments may still be
acceptable.
  • Loading branch information
xlii-chl committed Apr 29, 2022
1 parent 31fc802 commit a51e556
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Entities/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,33 @@ public function setPassword(string $password)
$this->attributes['reset_expires'] = null;
}

/**
* Explicitly convert false and true to 0 and 1
*
* Some BDD (PostgreSQL for example) can be picky about data types and
* if 'active' or 'force_pass_reset' are set to (bool)true/false, the method
* CodeIgniter\Database\Postgre\Connection::escape() will translate it
* to a literal TRUE/FALSE. Since the database fields are defined as integer,
* the BDD will throw an error about mismatched type.
*
* @param int|bool $active
*/
public function setActive($active)
{
$this->attributes['active'] = $active ? 1 : 0;
}

/**
* Explicitly convert false and true to 0 and 1
*
* @see setActive() Explanation about strict typing at database level
* @param int|bool $force_pass_reset
*/
public function setForcePassReset($force_pass_reset)
{
$this->attributes['force_pass_reset'] = $force_pass_reset ? 1 : 0;
}

/**
* Force a user to reset their password on next page refresh
* or login. Checked in the LocalAuthenticator's check() method.
Expand Down

0 comments on commit a51e556

Please sign in to comment.