|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Auth; |
|
6
|
|
|
|
|
7
|
|
|
use App\User\User; |
|
8
|
|
|
use Cycle\Annotated\Annotation\Column; |
|
9
|
|
|
use Cycle\Annotated\Annotation\Entity; |
|
10
|
|
|
use Cycle\Annotated\Annotation\Relation\BelongsTo; |
|
11
|
|
|
use Yiisoft\Security\Random; |
|
12
|
|
|
use Yiisoft\User\Login\Cookie\CookieLoginIdentityInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @Entity(repository="App\Auth\IdentityRepository") |
|
16
|
|
|
*/ |
|
17
|
|
|
final class Identity implements CookieLoginIdentityInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @Column(type="primary") |
|
21
|
|
|
*/ |
|
22
|
|
|
private ?int $id = null; |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @Column(type="string(32)") |
|
26
|
|
|
*/ |
|
27
|
|
|
private string $authKey; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @BelongsTo(target="App\User\User", nullable=false, load="eager") |
|
31
|
|
|
* |
|
32
|
|
|
* @var \Cycle\ORM\Promise\Reference|User |
|
33
|
|
|
*/ |
|
34
|
|
|
private $user = null; |
|
35
|
|
|
private ?int $user_id = null; |
|
|
|
|
|
|
36
|
|
|
private bool $shouldAddCookie = false; |
|
37
|
|
|
|
|
38
|
|
|
public function __construct() |
|
39
|
|
|
{ |
|
40
|
|
|
$this->regenerateCookieLoginKey(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function getId(): ?string |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->user->getId(); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function getCookieLoginKey(): string |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->authKey; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function getUser(): ?User |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->user; |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function shouldLoginByCookie(): bool |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->shouldAddCookie; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function setShouldLoginByCookie(bool $value): void |
|
64
|
|
|
{ |
|
65
|
|
|
$this->shouldAddCookie = $value; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function validateCookieLoginKey(string $key): bool |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->authKey === $key; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function regenerateCookieLoginKey(): void |
|
74
|
|
|
{ |
|
75
|
|
|
$this->authKey = Random::string(32); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|