Skip to content

Commit e8fffcf

Browse files
committed
Add new database fields for user, change mis-named, fix tests - rate limiting not in play
1 parent b300c5c commit e8fffcf

File tree

4 files changed

+31
-18
lines changed

4 files changed

+31
-18
lines changed

features/bootstrap/DoctrineContext.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,13 +271,17 @@ public function theUserEmailIsNotVerified(?string $verificationToken = null): vo
271271
}
272272

273273
/**
274-
* @Given the user has a new email address :emailAddress and confirmation token :token
274+
* @Given /^the user has a new email address "([^" ]*)" and confirmation token "([^" ]*)"(?: and the email was sent at "([^"]*)"|)$/i
275275
*/
276-
public function theUserHasANewEmailAddress(string $emailAddress, string $verificationToken): void
276+
public function theUserHasANewEmailAddress(string $emailAddress, string $verificationToken, string $emailSentAt = 'now'): void
277277
{
278278
/** @var User $user */
279279
$user = $this->iriConverter->getResourceFromIri($this->restContext->resources['user']);
280-
$user->setNewEmailAddress($emailAddress)->setNewEmailConfirmationToken($this->passwordHasher->hashPassword($user, $verificationToken));
280+
$user
281+
->setNewEmailAddress($emailAddress)
282+
->setNewEmailConfirmationToken($this->passwordHasher->hashPassword($user, $verificationToken))
283+
->setNewEmailAddressChangeRequestedAt(new \DateTime($emailSentAt))
284+
;
281285
$this->manager->flush();
282286
}
283287

features/user/new_email_address.feature

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,15 @@ Feature: Register process via a form
165165
Scenario: I can resend a new email address confirmation email with a new token
166166
Given there is a "new_email" form
167167
And there is a user with the username "my_username" password "password" and role "ROLE_USER" and the email address "user@example.com"
168-
And the user has a new email address "new@example.com" and confirmation token abc123
168+
And the user has a new email address "new@example.com" and confirmation token "abc123" and the email was sent at "-1 day"
169169
And I add "referer" header equal to "http://www.website.com"
170170
When I send a "GET" request to "/resend-verify-new-email/my_username"
171171
Then the response status code should be 200
172172
And I should get a "change_email_confirmation" email sent to the email address "user@example.com"
173173

174174
Scenario: I can verify my new email address
175175
Given there is a user with the username "my_username" password "password" and role "ROLE_USER" and the email address "old@email.com"
176-
And the user has a new email address "new@email.com" and confirmation token abc123
176+
And the user has a new email address "new@email.com" and confirmation token "abc123"
177177
And I add "referer" header equal to "http://www.website.com"
178178
When I send a "GET" request to "/confirm-email/my_username/new@email.com/abc123"
179179
Then the response status code should be 200
@@ -184,7 +184,7 @@ Feature: Register process via a form
184184
Scenario: Email verification reset if another user now has confirmed email same as the one this user is trying to confirm
185185
Given there is a user with the username "new@email.com" password "password" and role "ROLE_USER" and the email address "new@email.com"
186186
And there is a user with the username "another_user" password "password" and role "ROLE_USER"
187-
And the user has a new email address "new@email.com" and confirmation token abc123
187+
And the user has a new email address "new@email.com" and confirmation token "abc123"
188188
When I send a "GET" request to "/confirm-email/another_user/new@email.com/abc123"
189189
Then the response status code should be 401
190190
And the new email address should be "test.user@example.com" for username "another_user"

src/Entity/User/AbstractUser.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ abstract class AbstractUser implements SymfonyUserInterface, PasswordAuthenticat
117117
#[ApiProperty(readable: false, writable: false)]
118118
protected ?\DateTime $emailLastUpdatedAt = null;
119119

120+
#[ApiProperty(readable: false, writable: false)]
121+
protected ?\DateTime $emailAddressVerificationRequestedAt = null;
122+
120123
/**
121124
* `final` to make `createFromPayload` safe. Could instead make an interface? Or abstract and force child to define constructor?
122125
*/
@@ -253,9 +256,6 @@ public function getNewEmailAddress(): ?string
253256
public function setNewEmailAddress(?string $newEmailAddress): self
254257
{
255258
$this->newEmailAddress = $newEmailAddress;
256-
if ($newEmailAddress) {
257-
$this->newEmailAddressChangeRequestedAt = new \DateTime();
258-
}
259259

260260
return $this;
261261
}
@@ -268,9 +268,6 @@ public function getNewEmailConfirmationToken(): ?string
268268
public function setNewEmailConfirmationToken(?string $newEmailConfirmationToken): self
269269
{
270270
$this->newEmailConfirmationToken = $newEmailConfirmationToken;
271-
if ($newEmailConfirmationToken) {
272-
$this->newEmailAddressChangeRequestedAt = new \DateTime();
273-
}
274271

275272
return $this;
276273
}
@@ -280,6 +277,11 @@ public function getNewEmailAddressChangeRequestedAt(): ?\DateTime
280277
return $this->newEmailAddressChangeRequestedAt;
281278
}
282279

280+
public function setNewEmailAddressChangeRequestedAt(?\DateTime $newEmailAddressChangeRequestedAt): void
281+
{
282+
$this->newEmailAddressChangeRequestedAt = $newEmailAddressChangeRequestedAt;
283+
}
284+
283285
public function isEmailAddressVerified(): bool
284286
{
285287
return $this->emailAddressVerified;
@@ -300,9 +302,16 @@ public function getEmailAddressVerifyToken(): ?string
300302
public function setEmailAddressVerifyToken(?string $emailAddressVerifyToken): void
301303
{
302304
$this->emailAddressVerifyToken = $emailAddressVerifyToken;
303-
if ($emailAddressVerifyToken) {
304-
$this->emailLastUpdatedAt = new \DateTime();
305-
}
305+
}
306+
307+
public function getEmailAddressVerificationRequestedAt(): ?\DateTime
308+
{
309+
return $this->emailAddressVerificationRequestedAt;
310+
}
311+
312+
public function setEmailAddressVerificationRequestedAt(?\DateTime $emailAddressVerificationRequestedAt): void
313+
{
314+
$this->emailAddressVerificationRequestedAt = $emailAddressVerificationRequestedAt;
306315
}
307316

308317
public function isPasswordRequestLimitReached($ttl): bool
@@ -323,7 +332,7 @@ public function isNewEmailVerifyRequestLimitReached($ttl): bool
323332

324333
public function isEmailVerifyRequestLimitReached($ttl): bool
325334
{
326-
$lastRequest = $this->emailLastUpdatedAt;
335+
$lastRequest = $this->getEmailAddressVerificationRequestedAt();
327336

328337
return $lastRequest instanceof \DateTime
329338
&& $lastRequest->getTimestamp() + $ttl > time();
@@ -349,7 +358,6 @@ public function serialize(): string
349358
*/
350359
public function unserialize(string $serialized): self
351360
{
352-
$id = null;
353361
[
354362
$id,
355363
$this->username,

src/Resources/config/doctrine-orm/User.AbstractUser.orm.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
<field name="newEmailConfirmationToken" column="new_email_verification_token" nullable="true"/>
1919
<field name="newEmailAddressChangeRequestedAt" column="new_email_address_change_requested_at" type="datetime" nullable="true"/>
2020
<field name="emailAddressVerified" column="email_address_verified" type="boolean"/>
21-
<field name="emailAddressVerifyToken" column="restore_access_token" length="255" nullable="true"/>
21+
<field name="emailAddressVerifyToken" column="email_address_verify_token" length="255" nullable="true"/>
22+
<field name="emailAddressVerificationRequestedAt" column="email_address_verification_requested_at" type="datetime" nullable="true"/>
2223
<field name="emailLastUpdatedAt" column="email_last_updated_at" type="datetime" nullable="true"/>
2324
</mapped-superclass>
2425
</doctrine-mapping>

0 commit comments

Comments
 (0)