Commit
This commit is contained in:
parent
9af3df155b
commit
1e00dd7119
@ -30,7 +30,9 @@ class App
|
|||||||
$this->user = $user;
|
$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
|
public function model(string $model, $injection = NULL): object
|
||||||
{
|
{
|
||||||
// Require model file
|
// Require model file
|
||||||
@ -48,12 +50,14 @@ class App
|
|||||||
return new $model($injection);
|
return new $model($injection);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render given view
|
/**
|
||||||
|
* Render given view
|
||||||
|
*/
|
||||||
public function view(string $view, array $data = []): void
|
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);
|
extract($data);
|
||||||
// Require view file
|
// require view file
|
||||||
$path = $this->dir . '/view/' . $view . '.php';
|
$path = $this->dir . '/view/' . $view . '.php';
|
||||||
if (!file_exists($path))
|
if (!file_exists($path))
|
||||||
{
|
{
|
||||||
@ -62,7 +66,9 @@ class App
|
|||||||
require $path;
|
require $path;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Turn data array into JSON response
|
/**
|
||||||
|
* Turn data array into json response
|
||||||
|
*/
|
||||||
public function api(array $data, int $status_code = 200): void
|
public function api(array $data, int $status_code = 200): void
|
||||||
{
|
{
|
||||||
// Set headers
|
// Set headers
|
||||||
@ -73,7 +79,9 @@ class App
|
|||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Redirect to given url
|
/**
|
||||||
|
* Redirect to given url
|
||||||
|
*/
|
||||||
public function redirect(string $url): void
|
public function redirect(string $url): void
|
||||||
{
|
{
|
||||||
header("Location: $url");
|
header("Location: $url");
|
||||||
|
@ -11,7 +11,7 @@ class Session
|
|||||||
{
|
{
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
// Start new session if there is none
|
// start new session if there is none
|
||||||
if (session_status() === PHP_SESSION_NONE)
|
if (session_status() === PHP_SESSION_NONE)
|
||||||
{
|
{
|
||||||
session_start();
|
session_start();
|
||||||
|
@ -14,10 +14,10 @@ class User
|
|||||||
private Session $session;
|
private Session $session;
|
||||||
private Database $database;
|
private Database $database;
|
||||||
|
|
||||||
// Always initialized
|
// always initialized
|
||||||
public bool $loggedIn;
|
public bool $loggedIn;
|
||||||
|
|
||||||
// Initialized only if logged in
|
// initialized only if logged in
|
||||||
public string $username;
|
public string $username;
|
||||||
public string $password;
|
public string $password;
|
||||||
public int $powerLevel;
|
public int $powerLevel;
|
||||||
@ -29,14 +29,14 @@ class User
|
|||||||
|
|
||||||
$user = $this->session->get(self::SESSION_KEY);
|
$user = $this->session->get(self::SESSION_KEY);
|
||||||
|
|
||||||
// Check if user session has been set
|
// check if user session has been set
|
||||||
if (!$user)
|
if (!$user)
|
||||||
{
|
{
|
||||||
$this->loggedIn = FALSE;
|
$this->loggedIn = FALSE;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if username and password match
|
// check if username and password match
|
||||||
if (!$this->authenticate($user['username'], $user['password']))
|
if (!$this->authenticate($user['username'], $user['password']))
|
||||||
{
|
{
|
||||||
$this->loggedIn = FALSE;
|
$this->loggedIn = FALSE;
|
||||||
@ -45,14 +45,16 @@ class User
|
|||||||
return;
|
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->loggedIn = TRUE;
|
||||||
$this->username = $user['username'];
|
$this->username = $user['username'];
|
||||||
$this->password = $user['password'];
|
$this->password = $user['password'];
|
||||||
$this->powerLevel = $this->getPowerLevel();
|
$this->powerLevel = $this->getPowerLevel();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get current user power level
|
/**
|
||||||
|
* Get current user power level
|
||||||
|
*/
|
||||||
private function getPowerLevel(): int
|
private function getPowerLevel(): int
|
||||||
{
|
{
|
||||||
if (!$this->loggedIn)
|
if (!$this->loggedIn)
|
||||||
@ -67,7 +69,9 @@ class User
|
|||||||
return $row['Nivå'];
|
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
|
public function login(string $username, string $password): bool
|
||||||
{
|
{
|
||||||
if ($this->authenticate($username, $password))
|
if ($this->authenticate($username, $password))
|
||||||
@ -81,7 +85,9 @@ class User
|
|||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if user and password match database
|
/**
|
||||||
|
* Check if username and password match database
|
||||||
|
*/
|
||||||
private function authenticate(string $username, string $password): bool
|
private function authenticate(string $username, string $password): bool
|
||||||
{
|
{
|
||||||
$sth = $this->database->conn->prepare(
|
$sth = $this->database->conn->prepare(
|
||||||
|
Reference in New Issue
Block a user