Skip to content

Commit

Permalink
DB: use 'boolean' database fields
Browse files Browse the repository at this point in the history
This patch allows Myth:Auth to support all three of SQLite, MariaDB and
PostgreSQL.

- change 'tinyint' to 'boolean' on the migration,
- switch 0/1 to true/false coherently throughout the code.

Fix lonnieezell#324
  • Loading branch information
xlii-chl committed Feb 22, 2022
1 parent a8f45cc commit f60f579
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Authentication/AuthenticationBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public function recordLoginAttempt(string $email, string $ipAddress=null, int $u
'email' => $email,
'user_id' => $userID,
'date' => date('Y-m-d H:i:s'),
'success' => (int)$success
'success' => (bool)$success
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public function up()
'activate_hash' => ['type' => 'varchar', 'constraint' => 255, 'null' => true],
'status' => ['type' => 'varchar', 'constraint' => 255, 'null' => true],
'status_message' => ['type' => 'varchar', 'constraint' => 255, 'null' => true],
'active' => ['type' => 'tinyint', 'constraint' => 1, 'null' => 0, 'default' => 0],
'force_pass_reset' => ['type' => 'tinyint', 'constraint' => 1, 'null' => 0, 'default' => 0],
'active' => ['type' => 'boolean', 'null' => false, 'default' => false],
'force_pass_reset' => ['type' => 'boolean', 'null' => false, 'default' => false],
'created_at' => ['type' => 'datetime', 'null' => true],
'updated_at' => ['type' => 'datetime', 'null' => true],
'deleted_at' => ['type' => 'datetime', 'null' => true],
Expand All @@ -42,7 +42,7 @@ public function up()
'email' => ['type' => 'varchar', 'constraint' => 255, 'null' => true],
'user_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'null' => true], // Only for successful logins
'date' => ['type' => 'datetime'],
'success' => ['type' => 'tinyint', 'constraint' => 1],
'success' => ['type' => 'boolean'],
]);
$this->forge->addKey('id', true);
$this->forge->addKey('email');
Expand Down
21 changes: 21 additions & 0 deletions src/Entities/Cast/BooleanCast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Myth\Auth\Entities\Cast;

use CodeIgniter\Entity\Cast\BaseCast;

/**
* Class BooleanCast
*/
class BooleanCast extends BaseCast
{
/**
* {@inheritDoc}
*/
public static function get($value, array $params = []): bool
{
// PostgreSQL (via PDO, etc.) returns (string)'f' or 't' for boolean
// fields
return (bool) $value && $value !== 'f';
}
}
18 changes: 13 additions & 5 deletions src/Entities/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use CodeIgniter\Entity;
use Myth\Auth\Authorization\GroupModel;
use Myth\Auth\Authorization\PermissionModel;
use Myth\Auth\Entities\Cast\BooleanCast;
use Myth\Auth\Password;

class User extends Entity
Expand All @@ -29,8 +30,15 @@ class User extends Entity
* when they are accessed.
*/
protected $casts = [
'active' => 'boolean',
'force_pass_reset' => 'boolean',
'active' => 'booleanAuthMyth',
'force_pass_reset' => 'booleanAuthMyth',
];

/**
* Registering our custom casting handler
*/
protected $castHandlers = [
'booleanAuthMyth' => BooleanCast::class,
];

/**
Expand Down Expand Up @@ -81,7 +89,7 @@ public function setPassword(string $password)
public function forcePasswordReset()
{
$this->generateResetHash();
$this->attributes['force_pass_reset'] = 1;
$this->attributes['force_pass_reset'] = true;

return $this;
}
Expand Down Expand Up @@ -121,7 +129,7 @@ public function generateActivateHash()
*/
public function activate()
{
$this->attributes['active'] = 1;
$this->attributes['active'] = true;
$this->attributes['activate_hash'] = null;

return $this;
Expand All @@ -134,7 +142,7 @@ public function activate()
*/
public function deactivate()
{
$this->attributes['active'] = 0;
$this->attributes['active'] = false;

return $this;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/authentication/AuthenticationBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function testLoginByID()
$this->seeInDatabase('auth_logins', [
'email' => $user->email,
'user_id' => $user->id,
'success' => 1
'success' => true,
]);
}

Expand All @@ -70,7 +70,7 @@ public function testLoginSuccessNoRemember()
$this->seeInDatabase('auth_logins', [
'email' => $user->email,
'user_id' => $user->id,
'success' => 1
'success' => true,
]);

$this->assertEquals(4, $_SESSION['logged_in']);
Expand Down Expand Up @@ -99,7 +99,7 @@ public function testLoginSuccessAndRemember()
$this->seeInDatabase('auth_logins', [
'email' => $user->email,
'user_id' => $user->id,
'success' => 1
'success' => true,
]);

$this->assertEquals($user->id, $_SESSION['logged_in']);
Expand Down
4 changes: 2 additions & 2 deletions tests/controllers/LoginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function testAttemptLoginSuccess()
'username' => 'Joe Cool',
'email' => '[email protected]',
'password' => 'xaH96AhjglK',
'active' => 1,
'active' => true,
];
$this->createUser($user);

Expand Down Expand Up @@ -98,7 +98,7 @@ public function testAttemptLoginSuccessWithRememberMe()
'username' => 'Joe Cool',
'email' => '[email protected]',
'password' => 'xaH96AhjglK',
'active' => 1,
'active' => true,
];
$this->createUser($user);

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/AuthenticationBaseLoginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testRecordLoginAttemptSuccess()
'email' => '[email protected]',
'user_id' => 12,
'date' => date('Y-m-d H:i:s'),
'success' => 0
'success' => false,
]))->andReturn(true);

$this->assertTrue($this->auth->recordLoginAttempt($credentials['email'], '0.0.0.0', 12, false));
Expand Down

0 comments on commit f60f579

Please sign in to comment.