Skip to content

Commit

Permalink
Merge pull request #1027 from kenjis/remove-hard-coded-datetime-format
Browse files Browse the repository at this point in the history
refactor: remove hard coded `'Y-m-d H:i:s'`
  • Loading branch information
datamweb authored Feb 24, 2024
2 parents 0b55fa2 + b05dbdc commit 0802f39
Show file tree
Hide file tree
Showing 12 changed files with 20 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/Authentication/Authenticators/AccessTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public function check(array $credentials): Result
]);
}

$token->last_used_at = Time::now()->format('Y-m-d H:i:s');
$token->last_used_at = Time::now();

if ($token->hasChanged()) {
$identityModel->save($token);
Expand Down
2 changes: 1 addition & 1 deletion src/Authentication/Authenticators/HmacSha256.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public function check(array $credentials): Result
]);
}

$token->last_used_at = Time::now()->format('Y-m-d H:i:s');
$token->last_used_at = Time::now();

if ($token->hasChanged()) {
$identityModel->save($token);
Expand Down
6 changes: 3 additions & 3 deletions src/Authentication/Authenticators/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -922,17 +922,17 @@ protected function rememberUser(User $user): void
$user,
$selector,
$this->hashValidator($validator),
$expires
$expires->format('Y-m-d H:i:s')
);

$this->setRememberMeCookie($rawToken);
}

private function calcExpires(): string
private function calcExpires(): Time
{
$timestamp = Time::now()->getTimestamp() + setting('Auth.sessionConfig')['rememberLength'];

return Time::createFromTimestamp($timestamp)->format('Y-m-d H:i:s');
return Time::createFromTimestamp($timestamp);
}

private function setRememberMeCookie(string $rawToken): void
Expand Down
2 changes: 1 addition & 1 deletion src/Authorization/Traits/Authorizable.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ private function saveGroupsOrPermissions(string $type, $model, array $cache): vo
$inserts[] = [
'user_id' => $this->id,
$type => $item,
'created_at' => Time::now()->format('Y-m-d H:i:s'),
'created_at' => Time::now(),
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/MagicLinkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function loginAction()
'user_id' => $user->id,
'type' => Session::ID_TYPE_MAGIC_LINK,
'secret' => $token,
'expires' => Time::now()->addSeconds(setting('Auth.magicLinkLifetime'))->format('Y-m-d H:i:s'),
'expires' => Time::now()->addSeconds(setting('Auth.magicLinkLifetime')),
]);

/** @var IncomingRequest $request */
Expand Down
6 changes: 3 additions & 3 deletions src/Models/LoginModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class LoginModel extends BaseModel
'identifier' => 'permit_empty|string',
'user_agent' => 'permit_empty|string',
'user_id' => 'permit_empty|integer',
'date' => 'required|valid_date',
'date' => 'required',
];
protected $validationMessages = [];
protected $skipValidation = false;
Expand Down Expand Up @@ -80,7 +80,7 @@ public function recordLoginAttempt(
'id_type' => $idType,
'identifier' => $identifier,
'user_id' => $userId,
'date' => Time::now()->format('Y-m-d H:i:s'),
'date' => Time::now(),
'success' => (int) $success,
]);

Expand Down Expand Up @@ -121,7 +121,7 @@ public function fake(Generator &$faker): Login
'id_type' => Session::ID_TYPE_EMAIL_PASSWORD,
'identifier' => $faker->email(),
'user_id' => null,
'date' => Time::parse('-1 day')->format('Y-m-d H:i:s'),
'date' => Time::parse('-1 day'),
'success' => true,
]);
}
Expand Down
9 changes: 4 additions & 5 deletions src/Models/RememberModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use CodeIgniter\I18n\Time;
use CodeIgniter\Shield\Entities\User;
use DateTime;
use Faker\Generator;
use stdClass;

Expand Down Expand Up @@ -45,22 +44,22 @@ public function fake(Generator &$faker): stdClass
'user_id' => 1,
'selector' => 'selector',
'hashedValidator' => 'validator',
'expires' => Time::parse('+1 day')->format('Y-m-d H:i:s'),
'expires' => Time::parse('+1 day'),
];
}

/**
* Stores a remember-me token for the user.
*
* @TODO `string $expires` → `Time $expires`
*/
public function rememberUser(User $user, string $selector, string $hashedValidator, string $expires): void
{
$expires = new DateTime($expires);

$return = $this->insert([
'user_id' => $user->id,
'selector' => $selector,
'hashedValidator' => $hashedValidator,
'expires' => $expires->format('Y-m-d H:i:s'),
'expires' => Time::parse($expires),
]);

$this->checkQueryReturn($return);
Expand Down
2 changes: 1 addition & 1 deletion src/Models/TokenLoginModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function fake(Generator &$faker): Login
'ip_address' => $faker->ipv4(),
'identifier' => 'token: ' . random_string('crypto', 64),
'user_id' => fake(UserModel::class)->id,
'date' => Time::parse('-1 day')->format('Y-m-d H:i:s'),
'date' => Time::parse('-1 day'),
'success' => true,
]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Models/UserIdentityModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ public function getIdentitiesByTypes(User $user, array $types): array
*/
public function touchIdentity(UserIdentity $identity): void
{
$identity->last_used_at = Time::now()->format('Y-m-d H:i:s');
$identity->last_used_at = Time::now();

$return = $this->save($identity);

Expand Down
2 changes: 1 addition & 1 deletion src/Models/UserModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ public function updateActiveDate(User $user): void
assert($user->last_active instanceof Time);

// Safe date string for database
$last_active = $user->last_active->format('Y-m-d H:i:s');
$last_active = $user->last_active;

$this->builder()
->set('last_active', $last_active)
Expand Down
4 changes: 2 additions & 2 deletions tests/Authentication/MagicLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function testMagicLinkVerifyExpired(): void
'user_id' => $user->id,
'type' => Session::ID_TYPE_MAGIC_LINK,
'secret' => 'abasdasdf',
'expires' => Time::now()->subDays(5)->format('Y-m-d H:i:s'),
'expires' => Time::now()->subDays(5),
]);

$result = $this->get(route_to('verify-magic-link') . '?token=abasdasdf');
Expand All @@ -136,7 +136,7 @@ public function testMagicLinkVerifySuccess(): void
'user_id' => $user->id,
'type' => Session::ID_TYPE_MAGIC_LINK,
'secret' => 'abasdasdf',
'expires' => Time::now()->addMinutes(60)->format('Y-m-d H:i:s'),
'expires' => Time::now()->addMinutes(60),
]);

$result = $this->get(route_to('verify-magic-link') . '?token=abasdasdf');
Expand Down
2 changes: 1 addition & 1 deletion tests/Controllers/MagicLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function testMagicLinkVerifyPendingRegistrationActivation(): void
'user_id' => $user->id,
'type' => Session::ID_TYPE_MAGIC_LINK,
'secret' => 'abasdasdf',
'expires' => Time::now()->addMinutes(60)->format('Y-m-d H:i:s'),
'expires' => Time::now()->addMinutes(60),
]);

$result = $this->get(route_to('verify-magic-link') . '?token=abasdasdf');
Expand Down

0 comments on commit 0802f39

Please sign in to comment.