|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Application\Repository; |
|
6
|
|
|
|
|
7
|
|
|
use Application\Api\Exception; |
|
8
|
|
|
use Application\Model\User; |
|
9
|
|
|
use DateTimeImmutable; |
|
10
|
|
|
|
|
11
|
|
|
class UserRepository extends AbstractRepository implements LimitedAccessSubQueryInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Returns the user authenticated by its login and password |
|
15
|
|
|
*/ |
|
16
|
2 |
|
public function getLoginPassword(string $login, string $password, string $site): ?User |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var User $user */ |
|
19
|
2 |
|
$user = $this->getOneByLogin($login, $site); |
|
20
|
|
|
|
|
21
|
2 |
|
if (!$user) { |
|
|
|
|
|
|
22
|
1 |
|
return null; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
2 |
|
if (($user->getActiveUntil() && $user->getActiveUntil() < new DateTimeImmutable())) { |
|
26
|
|
|
throw new Exception("Ce compte n'est plus actif"); |
|
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 login. |
|
49
|
|
|
* |
|
50
|
|
|
* This should only be used in tests or controlled environment. |
|
51
|
|
|
*/ |
|
52
|
57 |
|
public function getOneByLogin(?string $login, string $site): ?User |
|
53
|
|
|
{ |
|
54
|
57 |
|
$user = $this->getAclFilter()->runWithoutAcl(function () use ($login, $site) { |
|
55
|
57 |
|
return $this->findOneBy([ |
|
56
|
57 |
|
'login' => $login, |
|
57
|
57 |
|
'site' => $site, |
|
58
|
|
|
]); |
|
59
|
57 |
|
}); |
|
60
|
|
|
|
|
61
|
57 |
|
return $user; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Unsecured way to get a user from its ID. |
|
66
|
|
|
* |
|
67
|
|
|
* This should only be used in tests or controlled environment. |
|
68
|
|
|
*/ |
|
69
|
4 |
|
public function getOneById(int $id): ?User |
|
70
|
|
|
{ |
|
71
|
4 |
|
$user = $this->getAclFilter()->runWithoutAcl(function () use ($id) { |
|
72
|
4 |
|
return $this->findOneById($id); |
|
|
|
|
|
|
73
|
4 |
|
}); |
|
74
|
|
|
|
|
75
|
4 |
|
return $user; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Unsecured way to get a user from its email. |
|
80
|
|
|
* |
|
81
|
|
|
* This should only be used in tests or controlled environment. |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getOneByEmail(?string $email, string $site): ?User |
|
84
|
|
|
{ |
|
85
|
|
|
$user = $this->getAclFilter()->runWithoutAcl(function () use ($email, $site) { |
|
86
|
|
|
return $this->findOneBy([ |
|
87
|
|
|
'email' => $email, |
|
88
|
|
|
'site' => $site, |
|
89
|
|
|
]); |
|
90
|
|
|
}); |
|
91
|
|
|
|
|
92
|
|
|
return $user; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Create new Shibboleth user. |
|
97
|
|
|
*/ |
|
98
|
|
|
public function createShibboleth(string $login, string $email, string $site): User |
|
99
|
|
|
{ |
|
100
|
|
|
$user = new User(); |
|
101
|
|
|
$user->setLogin($login); |
|
102
|
|
|
$user->setEmail($email); |
|
103
|
|
|
$user->setType(User::TYPE_AAI); |
|
104
|
|
|
$user->setRole(User::ROLE_STUDENT); |
|
105
|
|
|
$user->setSite($site); |
|
106
|
|
|
|
|
107
|
|
|
_em()->persist($user); |
|
108
|
|
|
_em()->flush(); |
|
109
|
|
|
|
|
110
|
|
|
return $user; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Returns pure SQL to get ID of all objects that are accessible to given user. |
|
115
|
|
|
*/ |
|
116
|
15 |
|
public function getAccessibleSubQuery(?User $user): string |
|
117
|
|
|
{ |
|
118
|
15 |
|
if ($user) { |
|
119
|
10 |
|
return $this->getAllIdsQuery(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
5 |
|
return '-1'; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|