|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Application\Repository; |
|
6
|
|
|
|
|
7
|
|
|
use Application\Model\User; |
|
8
|
|
|
use Cake\Chronos\Chronos; |
|
9
|
|
|
|
|
10
|
|
|
class UserRepository extends AbstractRepository implements LimitedAccessSubQueryInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Returns the user authenticated by its email and password |
|
14
|
|
|
* |
|
15
|
|
|
* @param string $login |
|
16
|
|
|
* @param string $password |
|
17
|
|
|
* |
|
18
|
|
|
* @return null|User |
|
19
|
|
|
*/ |
|
20
|
2 |
|
public function getByLoginPassword(string $login, string $password): ?User |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var User $user */ |
|
23
|
2 |
|
$user = $this->getByLogin($login); |
|
24
|
|
|
|
|
25
|
2 |
|
if (!$user || ($user->getActiveUntil() && $user->getActiveUntil() < new Chronos())) { |
|
|
|
|
|
|
26
|
1 |
|
return null; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
2 |
|
$hashFromDb = $user->getPassword(); |
|
30
|
2 |
|
$isMd5 = mb_strlen($hashFromDb) === 32 && ctype_xdigit($hashFromDb); |
|
31
|
|
|
|
|
32
|
|
|
// If we found a user and he has a correct MD5 or correct new hash, then return the user |
|
33
|
2 |
|
if (($isMd5 && md5($password) === $hashFromDb) || password_verify($password, $hashFromDb)) { |
|
34
|
|
|
|
|
35
|
|
|
// Update the hash in DB, if we are still MD5, or if PHP default options changed |
|
36
|
2 |
|
if ($isMd5 || password_needs_rehash($hashFromDb, PASSWORD_DEFAULT)) { |
|
37
|
2 |
|
$user->setPassword($password); |
|
38
|
2 |
|
_em()->flush(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
2 |
|
return $user; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
return null; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Unsecured way to get a user from its ID. |
|
49
|
|
|
* |
|
50
|
|
|
* This should only be used in tests or controlled environment. |
|
51
|
|
|
* |
|
52
|
|
|
* @param int $id |
|
53
|
|
|
* |
|
54
|
|
|
* @return null|User |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function getOneById(int $id): ?User |
|
57
|
|
|
{ |
|
58
|
1 |
|
$this->getAclFilter()->setEnabled(false); |
|
59
|
1 |
|
$user = $this->findOneById($id); |
|
|
|
|
|
|
60
|
1 |
|
$this->getAclFilter()->setEnabled(true); |
|
61
|
|
|
|
|
62
|
1 |
|
return $user; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Unsecured way to get a user from its login. |
|
67
|
|
|
* |
|
68
|
|
|
* This should only be used in tests or controlled environment. |
|
69
|
|
|
* |
|
70
|
|
|
* @param null|string $login |
|
71
|
|
|
* |
|
72
|
|
|
* @return null|User |
|
73
|
|
|
*/ |
|
74
|
11 |
|
public function getByLogin(?string $login): ?User |
|
75
|
|
|
{ |
|
76
|
11 |
|
$this->getAclFilter()->setEnabled(false); |
|
77
|
11 |
|
$user = $this->findOneByLogin($login); |
|
|
|
|
|
|
78
|
11 |
|
$this->getAclFilter()->setEnabled(true); |
|
79
|
|
|
|
|
80
|
11 |
|
return $user; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Returns pure SQL to get ID of all objects that are accessible to given user. |
|
85
|
|
|
* |
|
86
|
|
|
* @param null|User $user |
|
87
|
|
|
* |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
7 |
|
public function getAccessibleSubQuery(?User $user): string |
|
91
|
|
|
{ |
|
92
|
7 |
|
if ($user) { |
|
93
|
6 |
|
return $this->getAllIdsQuery(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
1 |
|
return '-1'; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|