From 1e00dd7119da7bfd6224f6110c8e25d7b3176efc Mon Sep 17 00:00:00 2001 From: William Date: Thu, 3 Mar 2022 05:11:14 +0100 Subject: [PATCH] Commit --- app/lib/App/Core/App.php | 20 ++++++++++++++------ app/lib/App/Core/Session.php | 2 +- app/lib/App/Core/User.php | 22 ++++++++++++++-------- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/app/lib/App/Core/App.php b/app/lib/App/Core/App.php index 75e0610..65159e7 100644 --- a/app/lib/App/Core/App.php +++ b/app/lib/App/Core/App.php @@ -30,7 +30,9 @@ class App $this->user = $user; } - // Grab model + // grab model + // TODO: have a look to see if this might name conflict with anything and + // maybe also throw an exception if the model class is not found within the file public function model(string $model, $injection = NULL): object { // Require model file @@ -48,12 +50,14 @@ class App return new $model($injection); } - // Render given view + /** + * Render given view + */ public function view(string $view, array $data = []): void { - // Import variables into the current symbol table from an array + // import variables into the current symbol table from an array extract($data); - // Require view file + // require view file $path = $this->dir . '/view/' . $view . '.php'; if (!file_exists($path)) { @@ -62,7 +66,9 @@ class App require $path; } - // Turn data array into JSON response + /** + * Turn data array into json response + */ public function api(array $data, int $status_code = 200): void { // Set headers @@ -73,7 +79,9 @@ class App die(); } - // Redirect to given url + /** + * Redirect to given url + */ public function redirect(string $url): void { header("Location: $url"); diff --git a/app/lib/App/Core/Session.php b/app/lib/App/Core/Session.php index 509290c..2483587 100644 --- a/app/lib/App/Core/Session.php +++ b/app/lib/App/Core/Session.php @@ -11,7 +11,7 @@ class Session { public function __construct() { - // Start new session if there is none + // start new session if there is none if (session_status() === PHP_SESSION_NONE) { session_start(); diff --git a/app/lib/App/Core/User.php b/app/lib/App/Core/User.php index 07d8b07..f4add72 100644 --- a/app/lib/App/Core/User.php +++ b/app/lib/App/Core/User.php @@ -14,10 +14,10 @@ class User private Session $session; private Database $database; - // Always initialized + // always initialized public bool $loggedIn; - // Initialized only if logged in + // initialized only if logged in public string $username; public string $password; public int $powerLevel; @@ -29,14 +29,14 @@ class User $user = $this->session->get(self::SESSION_KEY); - // Check if user session has been set + // check if user session has been set if (!$user) { $this->loggedIn = FALSE; return; } - // Check if username and password match + // check if username and password match if (!$this->authenticate($user['username'], $user['password'])) { $this->loggedIn = FALSE; @@ -45,14 +45,16 @@ class User return; } - // All is good, we should be logged in now! (hopefully) + // all is good, we should be logged in now! (hopefully) $this->loggedIn = TRUE; $this->username = $user['username']; $this->password = $user['password']; $this->powerLevel = $this->getPowerLevel(); } - // Get current user power level + /** + * Get current user power level + */ private function getPowerLevel(): int { if (!$this->loggedIn) @@ -67,7 +69,9 @@ class User return $row['NivÄ']; } - // Set session if user and password match + /** + * Set session if username and password match + */ public function login(string $username, string $password): bool { if ($this->authenticate($username, $password)) @@ -81,7 +85,9 @@ class User return FALSE; } - // Check if user and password match database + /** + * Check if username and password match database + */ private function authenticate(string $username, string $password): bool { $sth = $this->database->conn->prepare(