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
2022-01-26 20:28:00 +01:00

62 lines
1.6 KiB
PHP

<?php
// TODO: this shit stinks...
class User
{
private Session $session;
public ?bool $loggedIn;
public ?string $username;
public ?string $password;
public function __construct(Session $session)
{
$this->session = $session;
$this->setProps();
}
private function setProps(): void
{
$this->loggedIn = $this->session->get('loggedIn');
$this->username = $this->session->get('username');
$this->password = $this->session->get('password');
if ($this->loggedIn && !$this->authenticate($this->username, $this->password))
{
$this->logout();
$this->session->flash('Kontodetaljer endret, vennligst logg inn igjen', 'warning');
}
}
// Set session if user and password match
public function login(string $username, string $password): bool
{
if ($this->authenticate($username, $password))
{
$this->session->set('loggedIn', TRUE);
$this->session->set('username', $username);
$this->session->set('password', $password);
$this->setProps();
return TRUE;
}
return FALSE;
}
// Check if user and pass match
private function authenticate(string $username, string $password): bool
{
if ($username === 'William' && $password === 'William')
{
return TRUE;
}
return FALSE;
}
public function logout(): void
{
$this->session->remove('loggedIn');
$this->session->remove('username');
$this->session->remove('password');
$this->setProps();
}
}