This commit is contained in:
William 2022-02-28 04:14:53 +01:00
parent 46c83e377f
commit 5c891551df
3 changed files with 21 additions and 13 deletions

View File

@ -1,6 +1,7 @@
<?php <?php
// Janky error handler page thingy that looks cool // Bit of a janky way to display a custom page on error or exception.
// But looks pretty cool and professional!
class ErrorHandler class ErrorHandler
{ {
public array $errors; // Holds error messages (even though for now we only display a maximum of 1) public array $errors; // Holds error messages (even though for now we only display a maximum of 1)

View File

@ -1,8 +1,8 @@
<?php <?php
// TODO: ...
class User class User
{ {
private const SESSION_KEY = 'UserClass';
private Session $session; private Session $session;
private Database $database; private Database $database;
@ -19,7 +19,7 @@ class User
$this->session = $session; $this->session = $session;
$this->database = $database; $this->database = $database;
$user = $this->session->get('user'); $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)
@ -38,17 +38,25 @@ class User
} }
// 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();
}
// Set powerLevel // Get current user power level
private function getPowerLevel(): int
{
if (!$this->loggedIn)
{
throw new Exception("Can't get power level without being logged in!");
}
$sth = $this->database->conn->prepare( $sth = $this->database->conn->prepare(
'SELECT * FROM brukertabell WHERE Navn = ? AND Passord = ?' 'SELECT Nivå FROM brukertabell WHERE Navn = ? AND Passord = ?'
); );
$sth->execute([$this->username, $this->password]); $sth->execute([$this->username, $this->password]);
$row = $sth->fetch(PDO::FETCH_ASSOC); $row = $sth->fetch(PDO::FETCH_ASSOC);
$this->powerLevel = $row['Nivå']; return $row['Nivå'];
} }
// Set session if user and password match // Set session if user and password match
@ -56,7 +64,7 @@ class User
{ {
if ($this->authenticate($username, $password)) if ($this->authenticate($username, $password))
{ {
$this->session->set('user', [ $this->session->set(self::SESSION_KEY, [
'loggedIn' => TRUE, 'loggedIn' => TRUE,
'username' => $username, 'username' => $username,
'password' => $password 'password' => $password
@ -82,6 +90,6 @@ class User
public function logout(): void public function logout(): void
{ {
$this->session->remove('user'); $this->session->remove(self::SESSION_KEY);
} }
} }

View File

@ -36,5 +36,4 @@
echo '</tr>'; echo '</tr>';
} }
?> ?>
</table> </table>
<span class="float-right">[&nbsp;<a class="success" href="add.php">Opprett lag</a>&nbsp;]</span>