1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use BZIon\Form\Creator\ProfileFormCreator; |
4
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
|
7
|
|
|
class ProfileController extends HTMLController |
8
|
|
|
{ |
9
|
1 |
|
public function setup() |
10
|
|
|
{ |
11
|
1 |
|
$this->requireLogin(); |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Edit a profile |
16
|
|
|
* |
17
|
|
|
* @param Player $me The player's profile to edit |
18
|
|
|
* @param Request $request |
19
|
|
|
* @param bool $self Whether a player is editing their own profile, |
20
|
|
|
* instead of an admin editing another player's |
21
|
|
|
* profile |
22
|
|
|
* @return array |
23
|
|
|
*/ |
24
|
|
|
public function editAction(Player $me, Request $request, $self = true) |
25
|
|
|
{ |
26
|
|
|
$creator = new ProfileFormCreator($me); |
27
|
|
|
$creator->setEditingSelf($self); |
28
|
|
|
$form = $creator->create()->handleRequest($request); |
29
|
|
|
|
30
|
|
|
if ($form->isValid()) { |
31
|
|
|
if (!$self && $form->has('verify_email') && $form->get('verify_email')->isClicked()) { |
|
|
|
|
32
|
|
|
// An admin is editing a form and has chosen to verify a |
33
|
|
|
// player's e-mail address |
34
|
|
|
$me->setVerified(true); |
35
|
|
|
|
36
|
|
|
// Reset the form so that the "verify email" button gets hidden |
37
|
|
|
$form = $creator->create()->handleRequest($request); |
38
|
|
|
} else { |
39
|
|
|
$creator->update($form, $me); |
40
|
|
|
|
41
|
|
|
$email = $form->get('email')->getData(); |
42
|
|
|
if ($email !== $me->getEmailAddress()) { |
43
|
|
|
// User has changed their address, send a confirmation mail |
44
|
|
|
$me->setEmailAddress($email); |
45
|
|
|
|
46
|
|
|
if ($self) { |
47
|
|
|
$this->sendConfirmationMessage($me); |
48
|
|
|
} else { |
49
|
|
|
// Admins can set users' e-mail addresses at will, without |
50
|
|
|
// having to send them confirmation messages |
51
|
|
|
$me->setVerified(true); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$message = ($self) ? "Your profile has been updated." : $me->getUsername() . "'s profile has been updated."; |
57
|
|
|
$this->getFlashBag()->add("success", $message); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this->render('Profile/edit.html.twig', array( |
61
|
|
|
"editingSelf" => $self, |
62
|
|
|
"player" => $me, |
63
|
|
|
"form" => $form->createView() |
64
|
|
|
)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @todo Expire verification codes |
69
|
|
|
*/ |
70
|
|
|
public function confirmAction(Player $me, $token) |
71
|
|
|
{ |
72
|
|
|
if (!$me->getEmailAddress()) { |
73
|
|
|
throw new ForbiddenException("You need to have an e-mail address to confirm!"); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if ($me->isVerified()) { |
77
|
|
|
throw new ForbiddenException("You have already been verified"); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (!$me->isCorrectConfirmCode($token)) { |
81
|
|
|
throw new ForbiddenException("Invalid verification code"); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$me->setVerified(true); |
85
|
|
|
|
86
|
|
|
$this->getFlashBag()->add('success', "Your e-mail address has been successfully verified"); |
87
|
|
|
|
88
|
|
|
return new RedirectResponse($me->getUrl()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function showAction(Player $me) |
92
|
|
|
{ |
93
|
|
|
return new RedirectResponse($me->getUrl()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Send a confirmation e-mail to a player |
98
|
|
|
* @param Player $player The receiving player |
99
|
|
|
*/ |
100
|
|
|
private function sendConfirmationMessage($player) |
101
|
|
|
{ |
102
|
|
|
if ($player->getConfirmCode() === null) { |
103
|
|
|
// The player has no confirmation code, don't send them a message |
104
|
|
|
return; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$from = $this->container->getParameter('bzion.email.from'); |
108
|
|
|
$title = $this->container->getParameter('bzion.site.name'); |
109
|
|
|
|
110
|
|
|
if (!$from) { |
111
|
|
|
$this->getLogger()->addError('Unable to send verification e-mail message to player due to the "From:" address not being specified', array( |
112
|
|
|
'player' => array('id' => $player->getId(), 'username' => $player->getUsername()) |
113
|
|
|
)); |
114
|
|
|
return; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$message = Swift_Message::newInstance() |
118
|
|
|
->setSubject($title . ' Email Confirmation') |
119
|
|
|
->setFrom(array($from => $title)) |
120
|
|
|
->setTo($player->getEmailAddress()) |
121
|
|
|
->setBody($this->render('Email/confirm.txt.twig', array('player' => $player))) |
122
|
|
|
->addPart($this->render('Email/confirm.html.twig', array('player' => $player)), 'text/html'); |
123
|
|
|
|
124
|
|
|
$this->container->get('mailer')->send($message); |
125
|
|
|
|
126
|
|
|
$this->getFlashBag()->add('info', |
127
|
|
|
'Please check your inbox in order to confirm your email address.'); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
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: