34 lines
816 B
PHP
34 lines
816 B
PHP
|
|
<?php
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace MailAccountAdmin\Common;
|
||
|
|
|
||
|
|
use http\Exception\RuntimeException;
|
||
|
|
use MailAccountAdmin\Models\AdminUser;
|
||
|
|
use MailAccountAdmin\Repositories\AdminUserRepository;
|
||
|
|
|
||
|
|
class UserHelper
|
||
|
|
{
|
||
|
|
/** @var AdminUserRepository */
|
||
|
|
private $adminUserRepository;
|
||
|
|
|
||
|
|
public function __construct(AdminUserRepository $adminUserRepository)
|
||
|
|
{
|
||
|
|
$this->adminUserRepository = $adminUserRepository;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function isLoggedIn(): bool
|
||
|
|
{
|
||
|
|
return !empty($_SESSION['username']);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getCurrentUser(): AdminUser
|
||
|
|
{
|
||
|
|
$username = $_SESSION['username'] ?? null;
|
||
|
|
if (empty($username)) {
|
||
|
|
throw new RuntimeException('Not logged in!');
|
||
|
|
}
|
||
|
|
return $this->adminUserRepository->getUserByName($username);
|
||
|
|
}
|
||
|
|
}
|