1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\User; |
6
|
|
|
|
7
|
|
|
use Cycle\Annotated\Annotation\Column; |
8
|
|
|
use Cycle\Annotated\Annotation\Entity; |
9
|
|
|
use Cycle\Annotated\Annotation\Table\Index; |
10
|
|
|
use Cycle\ORM\Entity\Behavior\CreatedAt; |
11
|
|
|
use Cycle\ORM\Entity\Behavior\UpdatedAt; |
12
|
|
|
use DateTimeImmutable; |
13
|
|
|
use Yiisoft\Auth\IdentityInterface; |
14
|
|
|
use Yiisoft\Security\PasswordHasher; |
15
|
|
|
use Yiisoft\Security\Random; |
16
|
|
|
|
17
|
|
|
#[Entity(repository: UserRepository::class)] |
18
|
|
|
#[Index(columns: ['login', 'token'], unique: true)] |
19
|
|
|
#[CreatedAt(field: 'created_at', column: 'created_at')] |
20
|
|
|
#[UpdatedAt(field: 'updated_at', column: 'updated_at')] |
21
|
|
|
class User implements IdentityInterface |
22
|
|
|
{ |
23
|
|
|
#[Column(type: 'primary')] |
24
|
|
|
private ?int $id = null; |
25
|
|
|
|
26
|
|
|
#[Column(type: 'string(128)')] |
27
|
|
|
private string $token; |
28
|
|
|
|
29
|
|
|
#[Column(type: 'string(48)')] |
30
|
|
|
private string $login; |
31
|
|
|
|
32
|
|
|
#[Column(type: 'string')] |
33
|
|
|
private string $passwordHash; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Annotations for this field placed in a mapper class |
37
|
|
|
*/ |
38
|
|
|
private DateTimeImmutable $created_at; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Annotations for this field placed in a mapper class |
42
|
|
|
*/ |
43
|
|
|
private DateTimeImmutable $updated_at; |
44
|
|
|
|
45
|
|
|
public function __construct(string $login, string $password) |
46
|
|
|
{ |
47
|
|
|
$this->login = $login; |
48
|
|
|
$this->created_at = new DateTimeImmutable(); |
49
|
|
|
$this->updated_at = new DateTimeImmutable(); |
50
|
|
|
$this->setPassword($password); |
51
|
|
|
$this->resetToken(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getId(): ?string |
55
|
|
|
{ |
56
|
|
|
return $this->id === null ? null : (string)$this->id; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getToken(): string |
60
|
|
|
{ |
61
|
|
|
return $this->token; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function resetToken(): void |
65
|
|
|
{ |
66
|
1 |
|
$this->token = Random::string(128); |
67
|
|
|
} |
68
|
1 |
|
|
69
|
|
|
public function getLogin(): string |
70
|
|
|
{ |
71
|
1 |
|
return $this->login; |
72
|
|
|
} |
73
|
1 |
|
|
74
|
|
|
public function setLogin(string $login): void |
75
|
|
|
{ |
76
|
2 |
|
$this->login = $login; |
77
|
|
|
} |
78
|
2 |
|
|
79
|
|
|
public function validatePassword(string $password): bool |
80
|
|
|
{ |
81
|
2 |
|
return (new PasswordHasher())->validate($password, $this->passwordHash); |
82
|
|
|
} |
83
|
2 |
|
|
84
|
|
|
public function setPassword(string $password): void |
85
|
|
|
{ |
86
|
|
|
$this->passwordHash = (new PasswordHasher())->hash($password); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getCreatedAt(): DateTimeImmutable |
90
|
|
|
{ |
91
|
1 |
|
return $this->created_at; |
92
|
|
|
} |
93
|
1 |
|
|
94
|
|
|
public function getUpdatedAt(): DateTimeImmutable |
95
|
|
|
{ |
96
|
|
|
return $this->updated_at; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|