2020-06-18 00:19:42 +02:00
|
|
|
<?php namespace MapGuesser\PersistentData\Model;
|
2020-06-09 00:55:26 +02:00
|
|
|
|
|
|
|
use MapGuesser\Interfaces\Authentication\IUser;
|
|
|
|
|
2020-06-18 00:19:42 +02:00
|
|
|
class User extends Model implements IUser
|
2020-06-09 00:55:26 +02:00
|
|
|
{
|
2020-06-18 00:19:42 +02:00
|
|
|
protected static string $table = 'users';
|
|
|
|
|
2020-06-14 17:13:54 +02:00
|
|
|
protected static array $fields = ['email', 'password', 'type', 'active'];
|
2020-06-09 00:55:26 +02:00
|
|
|
|
2020-06-18 00:21:18 +02:00
|
|
|
private static array $types = ['user', 'admin'];
|
|
|
|
|
|
|
|
private string $email = '';
|
2020-06-09 00:55:26 +02:00
|
|
|
|
2020-06-18 00:21:18 +02:00
|
|
|
private string $password = '';
|
2020-06-09 00:55:26 +02:00
|
|
|
|
|
|
|
private string $type = 'user';
|
|
|
|
|
2020-06-14 17:13:54 +02:00
|
|
|
private bool $active = false;
|
|
|
|
|
2020-06-09 00:55:26 +02:00
|
|
|
public function setEmail(string $email): void
|
|
|
|
{
|
|
|
|
$this->email = $email;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setPassword(string $hashedPassword): void
|
|
|
|
{
|
|
|
|
$this->password = $hashedPassword;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setPlainPassword(string $plainPassword): void
|
|
|
|
{
|
|
|
|
$this->password = password_hash($plainPassword, PASSWORD_BCRYPT);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setType(string $type): void
|
|
|
|
{
|
|
|
|
if (in_array($type, self::$types)) {
|
|
|
|
$this->type = $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-14 17:13:54 +02:00
|
|
|
public function setActive($active): void
|
|
|
|
{
|
|
|
|
$this->active = (bool) $active;
|
|
|
|
}
|
|
|
|
|
2020-06-09 00:55:26 +02:00
|
|
|
public function getEmail(): string
|
|
|
|
{
|
|
|
|
return $this->email;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPassword(): string
|
|
|
|
{
|
|
|
|
return $this->password;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getType(): string
|
|
|
|
{
|
|
|
|
return $this->type;
|
|
|
|
}
|
|
|
|
|
2020-06-14 17:13:54 +02:00
|
|
|
public function getActive(): bool
|
|
|
|
{
|
|
|
|
return $this->active;
|
|
|
|
}
|
|
|
|
|
2020-06-09 00:55:26 +02:00
|
|
|
public function hasPermission(int $permission): bool
|
|
|
|
{
|
|
|
|
switch ($permission) {
|
|
|
|
case IUser::PERMISSION_NORMAL:
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
case IUser::PERMISSION_ADMIN:
|
|
|
|
return $this->type === 'admin';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-14 17:13:54 +02:00
|
|
|
public function getDisplayName(): string
|
|
|
|
{
|
|
|
|
return $this->email;
|
|
|
|
}
|
|
|
|
|
2020-06-09 00:55:26 +02:00
|
|
|
public function checkPassword(string $password): bool
|
|
|
|
{
|
|
|
|
return password_verify($password, $this->password);
|
|
|
|
}
|
|
|
|
}
|