1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Superdesk Web Publisher User Bundle. |
7
|
|
|
* |
8
|
|
|
* Copyright 2021 Sourcefabric z.ú. and contributors. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please see the |
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* @Copyright 2021 Sourcefabric z.ú |
14
|
|
|
* @license http://www.superdesk.org/license |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace SWP\Bundle\UserBundle\Security; |
18
|
|
|
|
19
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
20
|
|
|
use Symfony\Bridge\Twig\Mime\TemplatedEmail; |
21
|
|
|
use Symfony\Component\HttpFoundation\Request; |
22
|
|
|
use Symfony\Component\Mailer\MailerInterface; |
23
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
24
|
|
|
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface; |
25
|
|
|
use SymfonyCasts\Bundle\VerifyEmail\Model\VerifyEmailSignatureComponents; |
26
|
|
|
use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface; |
27
|
|
|
|
28
|
|
|
class EmailVerifier |
29
|
|
|
{ |
30
|
|
|
private $verifyEmailHelper; |
31
|
|
|
private $mailer; |
32
|
|
|
private $entityManager; |
33
|
|
|
|
34
|
|
|
public function __construct( |
35
|
|
|
VerifyEmailHelperInterface $helper, |
36
|
|
|
MailerInterface $mailer, |
37
|
|
|
EntityManagerInterface $manager |
38
|
|
|
) { |
39
|
|
|
$this->verifyEmailHelper = $helper; |
40
|
|
|
$this->mailer = $mailer; |
41
|
|
|
$this->entityManager = $manager; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function sendEmailConfirmation( |
45
|
|
|
string $verifyEmailRouteName, |
46
|
|
|
UserInterface $user, |
47
|
|
|
TemplatedEmail $email |
48
|
|
|
): void { |
49
|
|
|
$signatureComponents = $this->getSignatureComponents($verifyEmailRouteName, $user); |
50
|
|
|
|
51
|
|
|
$context = $email->getContext(); |
52
|
|
|
$context['signedUrl'] = $signatureComponents->getSignedUrl(); |
53
|
|
|
$context['expiresAtMessageKey'] = $signatureComponents->getExpirationMessageKey(); |
54
|
|
|
$context['expiresAtMessageData'] = $signatureComponents->getExpirationMessageData(); |
55
|
|
|
|
56
|
|
|
$email->context($context); |
57
|
|
|
|
58
|
|
|
$this->mailer->send($email); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @throws VerifyEmailExceptionInterface |
63
|
|
|
*/ |
64
|
|
|
public function handleEmailConfirmation(Request $request, UserInterface $user): void |
65
|
|
|
{ |
66
|
|
|
$this->verifyEmailHelper->validateEmailConfirmation($request->getUri(), (string) $user->getId(), $user->getEmail()); |
|
|
|
|
67
|
|
|
|
68
|
|
|
$user->setIsVerified(true); |
|
|
|
|
69
|
|
|
|
70
|
|
|
$this->entityManager->persist($user); |
71
|
|
|
$this->entityManager->flush(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getSignatureComponents(string $verifyEmailRouteName, UserInterface $user): VerifyEmailSignatureComponents |
75
|
|
|
{ |
76
|
|
|
return $this->verifyEmailHelper->generateSignature( |
77
|
|
|
$verifyEmailRouteName, |
78
|
|
|
(string) $user->getId(), |
|
|
|
|
79
|
|
|
$user->getEmail(), |
|
|
|
|
80
|
|
|
['id' => $user->getId()] |
|
|
|
|
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: