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