1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentsBundle\EventListener\Api; |
15
|
|
|
|
16
|
|
|
use Silverback\ApiComponentsBundle\Entity\User\AbstractUser; |
17
|
|
|
use Silverback\ApiComponentsBundle\Helper\User\UserMailer; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
use Symfony\Component\HttpKernel\Event\RequestEvent; |
20
|
|
|
use Symfony\Component\HttpKernel\Event\ViewEvent; |
21
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
22
|
|
|
use Symfony\Component\Security\Core\Security; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Daniel West <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class UserEventListener |
28
|
|
|
{ |
29
|
|
|
private UserMailer $userMailer; |
30
|
|
|
private Security $security; |
31
|
|
|
|
32
|
|
|
public function __construct(UserMailer $userMailer, Security $security) |
33
|
|
|
{ |
34
|
|
|
$this->userMailer = $userMailer; |
35
|
|
|
$this->security = $security; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function onPreRead(RequestEvent $event): void |
39
|
|
|
{ |
40
|
|
|
$request = $event->getRequest(); |
41
|
|
|
$resourceClass = $request->attributes->get('_api_resource_class'); |
42
|
|
|
if ( |
43
|
|
|
empty($resourceClass) || |
44
|
|
|
!is_a($resourceClass, AbstractUser::class, true) || |
45
|
|
|
'me' !== $request->attributes->get('_api_item_operation_name') |
46
|
|
|
) { |
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$user = $this->security->getUser(); |
51
|
|
|
if (!$user) { |
52
|
|
|
throw new AccessDeniedException('Access denied.'); |
53
|
|
|
} |
54
|
|
|
if (!$user instanceof AbstractUser) { |
55
|
|
|
throw new AccessDeniedException('Access denied. User not supported.'); |
56
|
|
|
} |
57
|
|
|
$request->attributes->set('id', $user->getId()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function onPostWrite(ViewEvent $event): void |
61
|
|
|
{ |
62
|
|
|
$request = $event->getRequest(); |
63
|
|
|
$data = $request->attributes->get('data'); |
64
|
|
|
$previousData = $request->attributes->get('previous_data'); |
65
|
|
|
if ( |
66
|
|
|
empty($data) || |
67
|
|
|
!$data instanceof AbstractUser || |
68
|
|
|
$request->isMethod(Request::METHOD_GET) || |
69
|
|
|
$request->isMethod(Request::METHOD_DELETE) |
70
|
|
|
) { |
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->postWrite($data, !$request->isMethod(Request::METHOD_POST) ? $previousData : null); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function postWrite(AbstractUser $user, ?AbstractUser $previousUser): void |
78
|
|
|
{ |
79
|
|
|
if (!$previousUser) { |
80
|
|
|
$this->userMailer->sendWelcomeEmail($user); |
81
|
|
|
|
82
|
|
|
return; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if (!$previousUser->isEnabled() && $user->isEnabled()) { |
86
|
|
|
$this->userMailer->sendUserEnabledEmail($user); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($previousUser->getUsername() !== $user->getUsername()) { |
90
|
|
|
$this->userMailer->sendUsernameChangedEmail($user); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if ($previousUser->getPassword() !== $user->getPassword()) { |
94
|
|
|
$this->userMailer->sendPasswordChangedEmail($user); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (($token = $user->getEmailAddressVerifyToken()) && $token !== $previousUser->getEmailAddressVerifyToken()) { |
98
|
|
|
$this->userMailer->sendEmailVerifyEmail($user); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (($token = $user->getNewEmailConfirmationToken()) && $token !== $previousUser->getNewEmailConfirmationToken()) { |
102
|
|
|
$this->userMailer->sendChangeEmailConfirmationEmail($user); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|