WIP on document conversion

This commit is contained in:
2024-05-30 21:58:54 -04:00
parent cfa56ec44f
commit df20936af2
34 changed files with 674 additions and 204 deletions

View File

@@ -1,4 +1,13 @@
<?php
namespace FeedReaderCentral;
use BitBadger\Documents\Field;
use BitBadger\Documents\SQLite\Document;
use BitBadger\Documents\SQLite\Find;
use BitBadger\Documents\SQLite\Patch;
use FeedReaderCentral\Domain\Table;
use FeedReaderCentral\Domain\User;
use SQLite3;
/**
* Security functions
@@ -28,13 +37,14 @@ class Security {
*
* @param string $email The e-mail address of the user to retrieve
* @param SQLite3 $db The data connection to use to retrieve the user
* @return array|false The user information, or null if the user is not found
* @return User|false The user information, or null if the user is not found
*/
public static function findUserByEmail(string $email, SQLite3 $db): array|false {
$query = $db->prepare('SELECT * FROM frc_user WHERE email = :email');
$query->bindValue(':email', $email);
$result = $query->execute();
return $result ? $result->fetchArray(SQLITE3_ASSOC) : false;
public static function findUserByEmail(string $email, SQLite3 $db): User|false {
return Find::firstByField(Table::USER, Field::EQ('email', $email), User::class, $db);
// $query = $db->prepare('SELECT * FROM frc_user WHERE email = :email');
// $query->bindValue(':email', $email);
// $result = $query->execute();
// return $result ? $result->fetchArray(SQLITE3_ASSOC) : false;
}
/**
@@ -45,27 +55,32 @@ class Security {
* @param SQLite3 $db The data connection to use to add the user
*/
public static function addUser(string $email, string $password, SQLite3 $db): void {
$query = $db->prepare('INSERT INTO frc_user (email, password) VALUES (:email, :password)');
$query->bindValue(':email', $email);
$query->bindValue(':password', password_hash($password, self::PW_ALGORITHM));
$query->execute();
$user = new User();
$user->email = $email;
$user->password = $password;
Document::insert(Table::USER, $user, $db);
// $query = $db->prepare('INSERT INTO frc_user (email, password) VALUES (:email, :password)');
// $query->bindValue(':email', $email);
// $query->bindValue(':password', password_hash($password, self::PW_ALGORITHM));
// $query->execute();
}
/**
* Verify a user's password
*
* @param array $user The user information retrieved from the database
* @param User $user The user information retrieved from the database
* @param string $password The password provided by the user
* @param string|null $returnTo The URL to which the user should be redirected
* @param SQLite3 $db The database connection to use to verify the user's credentials
*/
private static function verifyPassword(array $user, string $password, ?string $returnTo, SQLite3 $db): void {
if (password_verify($password, $user['password'])) {
if (password_needs_rehash($user['password'], self::PW_ALGORITHM)) {
$rehash = $db->prepare('UPDATE frc_user SET password = :hash WHERE id = :id');
$rehash->bindValue(':hash', password_hash($password, self::PW_ALGORITHM));
$rehash->bindValue(':id', $user['id']);
$rehash->execute();
private static function verifyPassword(User $user, string $password, ?string $returnTo, SQLite3 $db): void {
if (password_verify($password, $user->password)) {
if (password_needs_rehash($user->password, self::PW_ALGORITHM)) {
Patch::byId(Table::USER, $user->id, ['password' => password_hash($password, self::PW_ALGORITHM)], $db);
// $rehash = $db->prepare('UPDATE frc_user SET password = :hash WHERE id = :id');
// $rehash->bindValue(':hash', password_hash($password, self::PW_ALGORITHM));
// $rehash->bindValue(':id', $user['id']);
// $rehash->execute();
}
$_SESSION[Key::USER_ID] = $user['id'];
$_SESSION[Key::USER_EMAIL] = $user['email'];
@@ -104,10 +119,12 @@ class Security {
* @param SQLite3 $db The database connection to use in updating the password
*/
public static function updatePassword(string $email, string $password, SQLite3 $db): void {
$query = $db->prepare('UPDATE frc_user SET password = :password WHERE email = :email');
$query->bindValue(':password', password_hash($password, self::PW_ALGORITHM));
$query->bindValue(':email', $email);
$query->execute();
Patch::byField(Table::USER, Field::EQ('email', $email),
['password' => password_hash($password, self::PW_ALGORITHM)], $db);
// $query = $db->prepare('UPDATE frc_user SET password = :password WHERE email = :email');
// $query->bindValue(':password', password_hash($password, self::PW_ALGORITHM));
// $query->bindValue(':email', $email);
// $query->execute();
}
/**