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

62 lines
1.6 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-26 19:28:00 +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-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
}
}
// 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-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
{
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
}