|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
//---------------------------------------------------------------------- |
|
4
|
|
|
// |
|
5
|
|
|
// Copyright (C) 2018-2020 Artem Rodygin |
|
6
|
|
|
// |
|
7
|
|
|
// You should have received a copy of the MIT License along with |
|
8
|
|
|
// this file. If not, see <http://opensource.org/licenses/MIT>. |
|
9
|
|
|
// |
|
10
|
|
|
//---------------------------------------------------------------------- |
|
11
|
|
|
|
|
12
|
|
|
namespace LazySec\Checker; |
|
13
|
|
|
|
|
14
|
|
|
use LazySec\Entity\DisableAccountTrait; |
|
15
|
|
|
use LazySec\Entity\ExpireAccountTrait; |
|
16
|
|
|
use LazySec\Entity\ExpirePasswordTrait; |
|
17
|
|
|
use LazySec\Entity\LockAccountTrait; |
|
18
|
|
|
use Symfony\Component\Security\Core\Exception\AccountExpiredException; |
|
19
|
|
|
use Symfony\Component\Security\Core\Exception\CredentialsExpiredException; |
|
20
|
|
|
use Symfony\Component\Security\Core\Exception\DisabledException; |
|
21
|
|
|
use Symfony\Component\Security\Core\Exception\LockedException; |
|
22
|
|
|
use Symfony\Component\Security\Core\User\UserCheckerInterface; |
|
23
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Generic user checker which autodetects included LazySec traits. |
|
27
|
|
|
*/ |
|
28
|
|
|
class GenericUserChecker implements UserCheckerInterface |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
4 |
|
public function checkPreAuth(UserInterface $user) |
|
34
|
|
|
{ |
|
35
|
4 |
|
if (in_array(LockAccountTrait::class, class_uses($user), true)) { |
|
36
|
|
|
/** @var LockAccountTrait|UserInterface $user */ |
|
37
|
4 |
|
if (!$user->isAccountNonLocked()) { |
|
38
|
1 |
|
$exception = new LockedException('User account is locked.'); |
|
39
|
1 |
|
$exception->setUser($user); |
|
|
|
|
|
|
40
|
1 |
|
throw $exception; |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
3 |
|
if (in_array(DisableAccountTrait::class, class_uses($user), true)) { |
|
45
|
|
|
/** @var DisableAccountTrait|UserInterface $user */ |
|
46
|
3 |
|
if (!$user->isEnabled()) { |
|
47
|
1 |
|
$exception = new DisabledException('User account is disabled.'); |
|
48
|
1 |
|
$exception->setUser($user); |
|
|
|
|
|
|
49
|
1 |
|
throw $exception; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
2 |
|
if (in_array(ExpireAccountTrait::class, class_uses($user), true)) { |
|
54
|
|
|
/** @var ExpireAccountTrait|UserInterface $user */ |
|
55
|
2 |
|
if (!$user->isAccountNonExpired()) { |
|
56
|
1 |
|
$exception = new AccountExpiredException('User account has expired.'); |
|
57
|
1 |
|
$exception->setUser($user); |
|
|
|
|
|
|
58
|
1 |
|
throw $exception; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
1 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritdoc} |
|
65
|
|
|
*/ |
|
66
|
2 |
|
public function checkPostAuth(UserInterface $user) |
|
67
|
|
|
{ |
|
68
|
2 |
|
if (in_array(ExpirePasswordTrait::class, class_uses($user), true)) { |
|
69
|
|
|
/** @var ExpirePasswordTrait|UserInterface $user */ |
|
70
|
2 |
|
if (!$user->isCredentialsNonExpired()) { |
|
71
|
1 |
|
$exception = new CredentialsExpiredException('User credentials have expired.'); |
|
72
|
1 |
|
$exception->setUser($user); |
|
|
|
|
|
|
73
|
1 |
|
throw $exception; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
1 |
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|