mapguesser/src/PersistentData/Model/User.php

107 lines
2.2 KiB
PHP
Raw Normal View History

<?php namespace MapGuesser\PersistentData\Model;
2020-06-09 00:55:26 +02:00
use MapGuesser\Interfaces\Authentication\IUser;
class User extends Model implements IUser
2020-06-09 00:55:26 +02:00
{
protected static string $table = 'users';
2020-06-21 01:28:17 +02:00
protected static array $fields = ['email', 'password', 'type', 'active', 'google_sub'];
2020-06-09 00:55:26 +02:00
private static array $types = ['user', 'admin'];
private string $email = '';
2020-06-09 00:55:26 +02:00
2020-06-21 01:28:17 +02:00
private ?string $password = null;
2020-06-09 00:55:26 +02:00
private string $type = 'user';
private bool $active = false;
2020-06-21 01:28:17 +02:00
private ?string $googleSub = null;
2020-06-09 00:55:26 +02:00
public function setEmail(string $email): void
{
$this->email = $email;
}
2020-06-21 01:28:17 +02:00
public function setPassword(?string $hashedPassword): void
2020-06-09 00:55:26 +02:00
{
$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;
}
}
public function setActive($active): void
{
$this->active = (bool) $active;
}
2020-06-21 01:28:17 +02:00
public function setGoogleSub(?string $googleSub): void
{
$this->googleSub = $googleSub;
}
2020-06-09 00:55:26 +02:00
public function getEmail(): string
{
return $this->email;
}
2020-06-21 01:28:17 +02:00
public function getPassword(): ?string
2020-06-09 00:55:26 +02:00
{
return $this->password;
}
public function getType(): string
{
return $this->type;
}
public function getActive(): bool
{
return $this->active;
}
2020-06-21 01:28:17 +02:00
public function getGoogleSub(): ?string
{
return $this->googleSub;
}
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;
}
}
public function getUniqueId()
{
return $this->id;
}
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);
}
}