This repository has been archived on 2023-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
web/app/core/User.php

86 lines
2.4 KiB
PHP
Raw Normal View History

2022-01-23 21:56:36 +00:00
<?php
2022-02-23 15:45:12 +00:00
// TODO:
2022-01-25 16:50:32 +00:00
class User
2022-01-23 21:56:36 +00:00
{
2022-02-23 15:45:12 +00:00
private Session $session;
2022-02-02 11:47:06 +00:00
private Database $database;
2022-01-23 21:56:36 +00:00
2022-02-23 15:45:12 +00:00
public bool $loggedIn;
public int $powerLevel; // Set to 0 when not logged in
public string $username; // Username and password is only initalized if logged in
public string $password;
2022-01-23 21:56:36 +00:00
2022-02-02 11:47:06 +00:00
public function __construct(Session $session, Database $database)
2022-01-23 21:56:36 +00:00
{
2022-02-23 15:45:12 +00:00
$this->session = $session;
2022-02-02 11:47:06 +00:00
$this->database = $database;
2022-01-23 21:56:36 +00:00
2022-02-23 15:45:12 +00:00
$user = $this->session->get('user');
if ($user)
{
// User session was set previously
$this->loggedIn = $user['loggedIn'];
$this->username = $user['username'];
$this->password = $user['password'];
} else {
// User session has not been set yet
$this->loggedIn = FALSE;
}
2022-01-25 16:50:32 +00:00
2022-02-23 15:45:12 +00:00
// Check if username and password matches
2022-01-26 19:28:00 +00:00
if ($this->loggedIn && !$this->authenticate($this->username, $this->password))
{
$this->logout();
$this->session->flash('Kontodetaljer endret, vennligst logg inn igjen', 'warning');
2022-01-25 16:50:32 +00:00
}
2022-02-02 11:47:06 +00:00
2022-02-23 15:45:12 +00:00
// Set powerLevel
if ($this->loggedIn)
{
$sth = $this->database->conn->prepare(
'SELECT * FROM brukertabell WHERE Navn = ? AND Passord = ?'
);
2022-02-02 11:47:06 +00:00
$sth->execute([$this->username, $this->password]);
$row = $sth->fetch(PDO::FETCH_ASSOC);
2022-02-07 09:28:28 +00:00
$this->powerLevel = $row['Nivå'];
2022-02-15 11:29:40 +00:00
} else {
$this->powerLevel = 0;
2022-02-02 11:47:06 +00:00
}
2022-02-23 15:45:12 +00:00
}
2022-01-25 16:50:32 +00:00
// Set session if user and password match
2022-01-23 21:56:36 +00:00
public function login(string $username, string $password): bool
{
2022-01-25 16:50:32 +00:00
if ($this->authenticate($username, $password))
2022-01-23 21:56:36 +00:00
{
2022-02-23 15:45:12 +00:00
$this->session->set('user', [
'loggedIn' => TRUE,
'username' => $username,
'password' => $password
]);
2022-01-25 16:50:32 +00:00
return TRUE;
}
return FALSE;
}
2022-02-23 15:45:12 +00:00
// Check if user and password match database
2022-01-25 16:50:32 +00:00
private function authenticate(string $username, string $password): bool
{
2022-02-23 15:45:12 +00:00
$sth = $this->database->conn->prepare(
'SELECT * FROM brukertabell WHERE Navn = ? AND Passord = ?'
);
2022-02-02 11:47:06 +00:00
$sth->execute([$username, $password]);
if ($sth->rowCount())
2022-01-25 16:50:32 +00:00
{
2022-01-23 21:56:36 +00:00
return TRUE;
}
return FALSE;
}
2022-01-25 16:50:32 +00:00
public function logout(): void
{
2022-02-23 15:45:12 +00:00
$this->session->remove('user');
2022-01-25 16:50:32 +00:00
}
2022-01-23 21:56:36 +00:00
}