From 74bc83f26694003bb9010e31447e6f935401f490 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Fri, 5 Apr 2024 22:14:24 -0400 Subject: [PATCH] Add single user security mode (#3) - Tweaks to SQL column definitions - Implement class autoloading - Split user config into its own file --- src/lib/Data.php | 47 ++++++++++++++++++++++++++++++++++------ src/lib/Security.php | 51 ++++++++++++++++++++++++++++++++++++++++++++ src/public/index.php | 4 +++- src/start.php | 27 +++++++++-------------- src/user-config.php | 23 ++++++++++++++++++++ 5 files changed, 127 insertions(+), 25 deletions(-) create mode 100644 src/lib/Security.php create mode 100644 src/user-config.php diff --git a/src/lib/Data.php b/src/lib/Data.php index 39dea42..ee9062b 100644 --- a/src/lib/Data.php +++ b/src/lib/Data.php @@ -14,6 +14,9 @@ class Data { return $db; } + /** + * Make sure the expected tables exist + */ public static function ensureDb(): void { $db = self::getConnection(); $tables = array(); @@ -24,8 +27,7 @@ class Data { CREATE TABLE frc_user ( id INTEGER NOT NULL PRIMARY KEY, email TEXT NOT NULL, - password TEXT NOT NULL, - salt TEXT NOT NULL) + password TEXT NOT NULL) SQL; $db->exec($query); $db->exec('CREATE INDEX idx_user_email ON frc_user (email)'); @@ -50,15 +52,46 @@ class Data { published_on TEXT NOT NULL, updated_on TEXT, content TEXT NOT NULL, - is_encoded INTEGER NOT NULL, - is_read INTEGER NOT NULL, - is_bookmarked INTEGER NOT NULL, + is_encoded BOOLEAN NOT NULL, + is_read BOOLEAN NOT NULL, + is_bookmarked BOOLEAN NOT NULL, FOREIGN KEY (feed_id) REFERENCES feed (id)) SQL; $db->exec($query); } $db->close(); } -} -Data::ensureDb(); + /** + * Find a user by their ID + * + * @param string $email The e-mail address of the user to retrieve + * @return array|null The user information, or null if the user is not found + */ + public static function findUserByEmail(string $email): ?array { + $db = self::getConnection(); + $query = $db->prepare('SELECT * FROM frc_user WHERE email = :email'); + $query->bindValue(':email', $email); + $result = $query->execute(); + if ($result) { + $user = $result->fetchArray(SQLITE3_ASSOC); + if ($user) return $user; + return null; + } + return null; + } + + /** + * Add a user + * + * @param string $email The e-mail address for the user + * @param string $password The user's password + */ + public static function addUser(string $email, string $password): void { + $db = self::getConnection(); + $query = $db->prepare('INSERT INTO frc_user (email, password) VALUES (:email, :password)'); + $query->bindValue(':email', $email); + $query->bindValue(':password', password_hash($password, PASSWORD_DEFAULT)); + $query->execute(); + } +} diff --git a/src/lib/Security.php b/src/lib/Security.php new file mode 100644 index 0000000..9ab5d77 --- /dev/null +++ b/src/lib/Security.php @@ -0,0 +1,51 @@ + -

Startup worked

+

User ID - e-mail