WIP
This commit is contained in:
parent
21977588d8
commit
f7fa124535
21 changed files with 388 additions and 58 deletions
|
|
@ -4,11 +4,124 @@
|
|||
namespace Venom\Models;
|
||||
|
||||
|
||||
use Venom\Core\DatabaseHandler;
|
||||
|
||||
class User
|
||||
{
|
||||
private string $username;
|
||||
private string $password;
|
||||
private string $salt;
|
||||
private string $token;
|
||||
private array $roles;
|
||||
public const ADMIN_ROLE = 'ROLE_ADMIN';
|
||||
public const GUEST_ROLE = 'ROLE_GUEST';
|
||||
private string $username = 'GUEST';
|
||||
private string $email = 'GUEST';
|
||||
private string $password = '---';
|
||||
private string $salt = '---';
|
||||
private string $token = '---';
|
||||
private string $id = '-1';
|
||||
private array $roles = [];
|
||||
private bool $isLoaded = false;
|
||||
|
||||
public function hasRole(string $role): bool
|
||||
{
|
||||
return in_array($role, $this->roles, true);
|
||||
}
|
||||
|
||||
public function loadUser(): bool
|
||||
{
|
||||
if (isset($_SESSION['userID'])) {
|
||||
// try to load user from id!
|
||||
$user = DatabaseHandler::get()->getOne("SELECT * FROM users WHERE id = :id OR username = :name AND isActive = 1", [
|
||||
':id' => $_SESSION['userID'],
|
||||
':name' => $this->username
|
||||
]);
|
||||
if ($user) {
|
||||
$this->username = $user->username ?? '';
|
||||
$this->email = $user->email ?? '';
|
||||
$this->password = $user->password ?? '';
|
||||
$this->token = $user->token ?? '';
|
||||
$this->salt = $user->salt ?? '';
|
||||
$this->id = $user->id ?? '-1';
|
||||
$this->roles = explode(',', $user->roles ?? '');
|
||||
$this->isLoaded = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getUsername(): string
|
||||
{
|
||||
return $this->username;
|
||||
}
|
||||
|
||||
public function setUsername(string $username): void
|
||||
{
|
||||
$this->username = $username;
|
||||
}
|
||||
|
||||
public function getEmail(): string
|
||||
{
|
||||
return $this->email;
|
||||
}
|
||||
|
||||
public function setEmail(string $email): void
|
||||
{
|
||||
$this->email = $email;
|
||||
}
|
||||
|
||||
public function getPassword(): string
|
||||
{
|
||||
return $this->password;
|
||||
}
|
||||
|
||||
public function setPassword(string $password): void
|
||||
{
|
||||
$this->password = $password;
|
||||
}
|
||||
|
||||
public function getSalt(): string
|
||||
{
|
||||
return $this->salt;
|
||||
}
|
||||
|
||||
public function setSalt(string $salt): void
|
||||
{
|
||||
$this->salt = $salt;
|
||||
}
|
||||
|
||||
public function getToken(): string
|
||||
{
|
||||
return $this->token;
|
||||
}
|
||||
|
||||
public function setToken(string $token): void
|
||||
{
|
||||
$this->token = $token;
|
||||
}
|
||||
|
||||
public function getRoles(): array
|
||||
{
|
||||
return $this->roles;
|
||||
}
|
||||
|
||||
public function setRoles(array $roles): void
|
||||
{
|
||||
$this->roles = $roles;
|
||||
}
|
||||
|
||||
public function addRole($value): void
|
||||
{
|
||||
if (!in_array($value, $this->roles, true)) {
|
||||
$this->roles[] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
public function isLoaded(): bool
|
||||
{
|
||||
return $this->isLoaded;
|
||||
}
|
||||
|
||||
public function getId(): string
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue