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

67 lines
1.8 KiB
PHP
Raw Normal View History

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-01-25 16:50:32 +00:00
public bool $loggedIn;
public string $username;
public string $password;
2022-01-23 21:56:36 +00:00
public function __construct(Session $session)
{
$this->session = $session;
2022-01-25 16:50:32 +00:00
$this->setProperties();
2022-01-23 21:56:36 +00:00
}
2022-01-25 16:50:32 +00:00
private function setProperties(): void
{
if ($this->session->get('loggedIn'))
{
$this->loggedIn = $this->session->get('loggedIn');
$this->username = $this->session->get('username');
$this->password = $this->session->get('password');
2022-01-26 11:25:17 +00:00
if (!$this->authenticate($this->username, $this->password))
{
2022-01-25 16:50:32 +00:00
$this->logout();
$this->session->flash('Kontodetaljer endret, vennligst logg inn igjen', 'warning');
}
} else {
$this->loggedIn = FALSE;
}
}
// 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);
$this->setProperties();
return TRUE;
}
return FALSE;
}
// Check if user and pass match
private function authenticate(string $username, string $password): bool
{
2022-01-26 11:25:17 +00:00
if ($username === 'William' && $password === 'William')
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
{
$this->session->set('loggedIn', FALSE);
2022-01-26 11:25:17 +00:00
$this->session->remove('username');
$this->session->remove('password');
2022-01-25 16:50:32 +00:00
$this->setProperties();
}
2022-01-23 21:56:36 +00:00
}