1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\EventSubscriber; |
4
|
|
|
|
5
|
|
|
use App\Entity\Course; |
6
|
|
|
use App\Event\NewCourseAddedEvent; |
7
|
|
|
use App\Event\UserCreateEvent; |
8
|
|
|
use App\Event\UserPasswordChangeRequestEvent; |
9
|
|
|
use App\Model\UserInterface; |
10
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
11
|
|
|
use Sentry\ClientInterface as SentryClient; |
12
|
|
|
use SWP\Bundle\SettingsBundle\Context\ScopeContext; |
13
|
|
|
use SWP\Bundle\SettingsBundle\Manager\SettingsManagerInterface; |
14
|
|
|
use Symfony\Bridge\Twig\Mime\TemplatedEmail; |
15
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
16
|
|
|
use Symfony\Component\Mailer\Exception\TransportExceptionInterface; |
17
|
|
|
use Symfony\Component\Mailer\MailerInterface; |
18
|
|
|
use Symfony\Component\Mime\Address; |
19
|
|
|
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; |
20
|
|
|
use Symfony\Component\Security\Http\SecurityEvents; |
21
|
|
|
use Twig\Environment; |
22
|
|
|
use Twig\Loader\ArrayLoader; |
23
|
|
|
|
24
|
|
|
class UserSubscriber implements EventSubscriberInterface |
25
|
|
|
{ |
26
|
|
|
private MailerInterface $mailer; |
27
|
|
|
|
28
|
|
|
private SettingsManagerInterface $settingsManager; |
29
|
|
|
|
30
|
|
|
private string $cacheDir; |
31
|
|
|
|
32
|
|
|
private string $studentsAppUrl; |
33
|
|
|
|
34
|
|
|
private SentryClient $sentryClient; |
35
|
|
|
|
36
|
|
|
private EntityManagerInterface $entityManager; |
37
|
|
|
|
38
|
|
|
public function __construct( |
39
|
|
|
MailerInterface $mailer, |
40
|
|
|
SettingsManagerInterface $settingsManager, |
41
|
|
|
string $cacheDir, |
42
|
|
|
string $studentsAppUrl, |
43
|
|
|
SentryClient $sentryClient, |
44
|
|
|
EntityManagerInterface $entityManager |
45
|
|
|
) { |
46
|
|
|
$this->mailer = $mailer; |
47
|
|
|
$this->settingsManager = $settingsManager; |
48
|
|
|
$this->cacheDir = $cacheDir; |
49
|
|
|
$this->studentsAppUrl = $studentsAppUrl; |
50
|
|
|
$this->sentryClient = $sentryClient; |
51
|
|
|
$this->entityManager = $entityManager; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public static function getSubscribedEvents() |
55
|
|
|
{ |
56
|
|
|
return [ |
57
|
|
|
UserCreateEvent::class => 'onUserCreated', |
58
|
|
|
UserPasswordChangeRequestEvent::class => 'onUserPasswordRequestReset', |
59
|
|
|
SecurityEvents::INTERACTIVE_LOGIN => 'onUserSuccessfulLogin', |
60
|
|
|
NewCourseAddedEvent::class => 'onCourseCreated', |
61
|
|
|
]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function onUserCreated(UserCreateEvent $event): void |
65
|
|
|
{ |
66
|
|
|
$user = $event->getUser(); |
67
|
|
|
$email = $this->createEmail($user); |
68
|
|
|
$email |
69
|
|
|
->subject($this->getSetting('new_user_email_title', 'Welcome in OwnCourses')) |
70
|
|
|
->html( |
71
|
|
|
$this->renderTemplateFromString($this->getSetting('new_user_email_template', 'CHANGE THIS EMAIL CONTENT IN OwnCourses SETTINGS! '), [ |
72
|
|
|
'firstName' => $user->getFirstName(), |
73
|
|
|
'lastName' => $user->getLastName(), |
74
|
|
|
'email' => $user->getEmail(), |
75
|
|
|
'temporaryPassword' => $user->getPlainPassword(), |
76
|
|
|
]) |
77
|
|
|
) |
78
|
|
|
; |
79
|
|
|
|
80
|
|
|
try { |
81
|
|
|
$this->mailer->send($email); |
82
|
|
|
} catch (TransportExceptionInterface $e) { |
83
|
|
|
$this->sentryClient->captureException($e); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function onCourseCreated(NewCourseAddedEvent $event): void |
88
|
|
|
{ |
89
|
|
|
$user = $event->user; |
90
|
|
|
/** @var Course $course */ |
91
|
|
|
$course = $event->course; |
92
|
|
|
|
93
|
|
|
$email = $this->createEmail($user); |
94
|
|
|
$email |
95
|
|
|
->subject($this->getSetting('new_course_email_title', 'New course was added to your account')) |
96
|
|
|
->html( |
97
|
|
|
$this->renderTemplateFromString($this->getSetting('new_course_email_template', 'CHANGE THIS EMAIL CONTENT IN OwnCourses SETTINGS! '), [ |
98
|
|
|
'firstName' => $user->getFirstName(), |
99
|
|
|
'lastName' => $user->getLastName(), |
100
|
|
|
'email' => $user->getEmail(), |
101
|
|
|
'course' => $course->getTitle(), |
102
|
|
|
]) |
103
|
|
|
) |
104
|
|
|
; |
105
|
|
|
|
106
|
|
|
try { |
107
|
|
|
$this->mailer->send($email); |
108
|
|
|
} catch (TransportExceptionInterface $e) { |
109
|
|
|
$this->sentryClient->captureException($e); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function onUserPasswordRequestReset(UserPasswordChangeRequestEvent $event): void |
114
|
|
|
{ |
115
|
|
|
$user = $event->getUser(); |
116
|
|
|
$email = $this->createEmail($user); |
117
|
|
|
$email |
118
|
|
|
->subject($this->getSetting('password_reset_email_title', 'Password reset in OwnCourses')) |
119
|
|
|
->html( |
120
|
|
|
$this->renderTemplateFromString($this->getSetting('password_reset_email_template', 'CHANGE THIS EMAIL CONTENT IN OwnCourses SETTINGS! '), [ |
121
|
|
|
'firstName' => $user->getFirstName(), |
122
|
|
|
'lastName' => $user->getLastName(), |
123
|
|
|
'email' => $user->getEmail(), |
124
|
|
|
'resetUrl' => $this->studentsAppUrl.'/reset?token='.$user->getPasswordResetToken(), |
125
|
|
|
]) |
126
|
|
|
) |
127
|
|
|
; |
128
|
|
|
|
129
|
|
|
try { |
130
|
|
|
$this->mailer->send($email); |
131
|
|
|
} catch (TransportExceptionInterface $e) { |
132
|
|
|
$this->sentryClient->captureException($e); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function onUserSuccessfulLogin(InteractiveLoginEvent $event): void |
137
|
|
|
{ |
138
|
|
|
$user = $event->getAuthenticationToken()->getUser(); |
139
|
|
|
if ($user instanceof UserInterface) { |
140
|
|
|
$user->setLastLoginDate(new \DateTime()); |
141
|
|
|
$this->entityManager->flush(); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
private function createEmail($user): TemplatedEmail |
146
|
|
|
{ |
147
|
|
|
$email = new TemplatedEmail(); |
148
|
|
|
$email |
149
|
|
|
->from(new Address( |
150
|
|
|
$this->getSetting('email_from_address', '[email protected]'), |
151
|
|
|
$this->getSetting('email_from_name', 'OwnCourses Team') |
152
|
|
|
)) |
153
|
|
|
->to($user->getEmail()); |
154
|
|
|
|
155
|
|
|
return $email; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
private function getSetting(string $name, string $default) |
159
|
|
|
{ |
160
|
|
|
return $this->settingsManager->get($name, ScopeContext::SCOPE_GLOBAL, null, $default); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
private function renderTemplateFromString(string $template, array $context): string |
164
|
|
|
{ |
165
|
|
|
$loader = new ArrayLoader(['template' => $template]); |
166
|
|
|
$twig = new Environment($loader, ['cache' => $this->cacheDir.DIRECTORY_SEPARATOR.'twig']); |
167
|
|
|
|
168
|
|
|
return $twig->render('template', $context); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|