1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the login-cidadao project or it's bundles. |
4
|
|
|
* |
5
|
|
|
* (c) Guilherme Donato <guilhermednt on github> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace LoginCidadao\PhoneVerificationBundle\Service; |
12
|
|
|
|
13
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
14
|
|
|
use libphonenumber\PhoneNumber; |
15
|
|
|
use LoginCidadao\CoreBundle\Entity\Person; |
16
|
|
|
use LoginCidadao\CoreBundle\Entity\PersonRepository; |
17
|
|
|
use LoginCidadao\CoreBundle\Mailer\TwigSwiftMailer; |
18
|
|
|
use LoginCidadao\CoreBundle\Model\PersonInterface; |
19
|
|
|
use LoginCidadao\CoreBundle\Security\User\Manager\UserManager; |
20
|
|
|
use LoginCidadao\PhoneVerificationBundle\Entity\BlockedPhoneNumber; |
21
|
|
|
use LoginCidadao\PhoneVerificationBundle\Entity\BlockedPhoneNumberRepository; |
22
|
|
|
use LoginCidadao\PhoneVerificationBundle\Entity\PhoneVerification; |
23
|
|
|
use LoginCidadao\PhoneVerificationBundle\Entity\PhoneVerificationRepository; |
24
|
|
|
use LoginCidadao\PhoneVerificationBundle\Model\BlockedPhoneNumberInterface; |
25
|
|
|
|
26
|
|
|
class Blocklist implements BlocklistInterface |
27
|
|
|
{ |
28
|
|
|
/** @var UserManager */ |
29
|
|
|
private $userManager; |
30
|
|
|
|
31
|
|
|
/** @var TwigSwiftMailer */ |
32
|
|
|
private $mailer; |
33
|
|
|
|
34
|
|
|
/** @var BlockedPhoneNumberRepository */ |
35
|
|
|
private $blockedPhoneRepository; |
36
|
|
|
|
37
|
|
|
/** @var PhoneVerificationServiceInterface */ |
38
|
|
|
private $phoneVerificationService; |
39
|
|
|
|
40
|
|
|
/** @var EntityManagerInterface */ |
41
|
|
|
private $em; |
42
|
|
|
|
43
|
|
|
/** @var BlocklistOptions */ |
44
|
|
|
private $options; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Blocklist constructor. |
48
|
|
|
* @param UserManager $userManager |
49
|
|
|
* @param TwigSwiftMailer $mailer |
50
|
|
|
* @param EntityManagerInterface $em |
51
|
|
|
* @param PhoneVerificationServiceInterface $phoneVerificationService |
52
|
|
|
* @param BlocklistOptions $options |
53
|
|
|
*/ |
54
|
6 |
|
public function __construct( |
55
|
|
|
UserManager $userManager, |
56
|
|
|
TwigSwiftMailer $mailer, |
57
|
|
|
EntityManagerInterface $em, |
58
|
|
|
PhoneVerificationServiceInterface $phoneVerificationService, |
59
|
|
|
BlocklistOptions $options |
60
|
|
|
) { |
61
|
6 |
|
$this->userManager = $userManager; |
62
|
6 |
|
$this->mailer = $mailer; |
63
|
6 |
|
$this->em = $em; |
64
|
6 |
|
$this->options = $options; |
65
|
6 |
|
$this->blockedPhoneRepository = $this->em->getRepository(BlockedPhoneNumber::class); |
66
|
6 |
|
$this->phoneVerificationService = $phoneVerificationService; |
67
|
6 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritDoc |
71
|
|
|
*/ |
72
|
4 |
|
public function isPhoneBlocked(PhoneNumber $phoneNumber): bool |
73
|
|
|
{ |
74
|
4 |
|
return $this->isManuallyBlocked($phoneNumber) || $this->isBlockedAutomatically($phoneNumber); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @inheritDoc |
79
|
|
|
*/ |
80
|
2 |
|
public function blockByPhone(PhoneNumber $phoneNumber): array |
81
|
|
|
{ |
82
|
2 |
|
$blockedUsers = $this->userManager->blockUsersByPhone($phoneNumber, UserManager::FLUSH_STRATEGY_ONCE); |
83
|
2 |
|
$this->notifyBlockedUsers($blockedUsers); |
84
|
|
|
|
85
|
2 |
|
return $blockedUsers; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @inheritDoc |
90
|
|
|
*/ |
91
|
1 |
|
public function checkPhoneNumber(PhoneNumber $phoneNumber): array |
92
|
|
|
{ |
93
|
1 |
|
$blocked = []; |
94
|
1 |
|
if ($this->isPhoneBlocked($phoneNumber)) { |
95
|
1 |
|
$blocked = $this->blockByPhone($phoneNumber); |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
return $blocked; |
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
public function addBlockedPhoneNumber( |
102
|
|
|
PhoneNumber $phoneNumber, |
103
|
|
|
PersonInterface $blockedBy |
104
|
|
|
): BlockedPhoneNumberInterface { |
105
|
1 |
|
$blockedPhoneNumber = new BlockedPhoneNumber($phoneNumber, $blockedBy, new \DateTime()); |
106
|
1 |
|
$this->em->persist($blockedPhoneNumber); |
107
|
1 |
|
$this->em->flush(); |
108
|
|
|
|
109
|
1 |
|
return $blockedPhoneNumber; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param PersonInterface[] $blockedUsers |
114
|
|
|
*/ |
115
|
2 |
|
private function notifyBlockedUsers(array $blockedUsers) |
116
|
|
|
{ |
117
|
2 |
|
foreach ($blockedUsers as $person) { |
118
|
2 |
|
$this->mailer->sendAccountAutoBlockedMessage($person); |
119
|
|
|
} |
120
|
2 |
|
} |
121
|
|
|
|
122
|
4 |
|
private function isManuallyBlocked(PhoneNumber $phoneNumber): bool |
123
|
|
|
{ |
124
|
4 |
|
return $this->blockedPhoneRepository->findByPhone($phoneNumber) instanceof BlockedPhoneNumberInterface; |
125
|
|
|
} |
126
|
|
|
|
127
|
2 |
|
private function isBlockedAutomatically(PhoneNumber $phoneNumber): bool |
128
|
|
|
{ |
129
|
2 |
|
if ($this->options->isAutoBlockEnabled()) { |
130
|
1 |
|
$autoBlockLimit = $this->options->getAutoBlockPhoneLimit(); |
131
|
|
|
|
132
|
1 |
|
return $this->phoneVerificationService->countVerified($phoneNumber) >= $autoBlockLimit; |
133
|
|
|
} |
134
|
|
|
|
135
|
1 |
|
return false; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|