2022-01-23 21:56:36 +00:00
|
|
|
<?php
|
|
|
|
|
2022-01-26 11:25:17 +00:00
|
|
|
// TODO: this shit stinks...
|
2022-01-25 16:50:32 +00:00
|
|
|
class User
|
2022-01-23 21:56:36 +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-01-26 19:28:00 +00:00
|
|
|
public ?bool $loggedIn;
|
|
|
|
public ?string $username;
|
|
|
|
public ?string $password;
|
2022-02-02 11:47:06 +00:00
|
|
|
public ?int $level;
|
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
|
|
|
{
|
|
|
|
$this->session = $session;
|
2022-02-02 11:47:06 +00:00
|
|
|
$this->database = $database;
|
2022-01-26 19:28:00 +00:00
|
|
|
$this->setProps();
|
2022-01-23 21:56:36 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 19:28:00 +00:00
|
|
|
private function setProps(): void
|
2022-01-25 16:50:32 +00:00
|
|
|
{
|
2022-01-26 19:28:00 +00:00
|
|
|
$this->loggedIn = $this->session->get('loggedIn');
|
|
|
|
$this->username = $this->session->get('username');
|
|
|
|
$this->password = $this->session->get('password');
|
2022-01-25 16:50:32 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
if ($this->loggedIn) {
|
|
|
|
$sth = $this->database->conn->prepare('SELECT * FROM brukertabell WHERE Navn = ? AND Passord = ?');
|
|
|
|
$sth->execute([$this->username, $this->password]);
|
|
|
|
$row = $sth->fetch(PDO::FETCH_ASSOC);
|
|
|
|
|
|
|
|
$this->level = $row['Nivå'];
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
$this->session->set('loggedIn', TRUE);
|
2022-01-25 16:50:32 +00:00
|
|
|
$this->session->set('username', $username);
|
|
|
|
$this->session->set('password', $password);
|
2022-01-26 19:28:00 +00:00
|
|
|
$this->setProps();
|
2022-01-25 16:50:32 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if user and pass match
|
|
|
|
private function authenticate(string $username, string $password): bool
|
|
|
|
{
|
2022-02-02 11:47:06 +00:00
|
|
|
$sth = $this->database->conn->prepare('SELECT * FROM brukertabell WHERE Navn = ? AND Passord = ?');
|
|
|
|
$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-01-26 19:28:00 +00:00
|
|
|
$this->session->remove('loggedIn');
|
2022-01-26 11:25:17 +00:00
|
|
|
$this->session->remove('username');
|
|
|
|
$this->session->remove('password');
|
2022-01-26 19:28:00 +00:00
|
|
|
$this->setProps();
|
2022-01-25 16:50:32 +00:00
|
|
|
}
|
2022-01-23 21:56:36 +00:00
|
|
|
}
|