Skip to content

Commit

Permalink
Normalize email address before saving (#2934)
Browse files Browse the repository at this point in the history
* normalize email before saving

* fix copy/paste error

* Run php-cs-fixer
  • Loading branch information
emmachughes authored Feb 13, 2025
1 parent f064e56 commit 14329d9
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 2 deletions.
14 changes: 14 additions & 0 deletions sourcecode/hub/app/Http/Requests/Api/UserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,22 @@
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

use function is_string;
use function strtolower;

class UserRequest extends FormRequest
{
protected function prepareForValidation(): void
{
$email = $this->input('email');

if (is_string($email)) {
$this->merge([
'email' => strtolower($email),
]);
}
}

/**
* @return array<string, mixed>
*/
Expand Down
14 changes: 14 additions & 0 deletions sourcecode/hub/app/Http/Requests/StoreUserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,22 @@
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Password;

use function is_string;
use function strtolower;

final class StoreUserRequest extends FormRequest
{
protected function prepareForValidation(): void
{
$email = $this->input('email');

if (is_string($email)) {
$this->merge([
'email' => strtolower($email),
]);
}
}

/**
* @return array<string, mixed>
*/
Expand Down
14 changes: 14 additions & 0 deletions sourcecode/hub/app/Http/Requests/UpdateUserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,22 @@
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rules\Password;

use function is_string;
use function strtolower;

class UpdateUserRequest extends FormRequest
{
protected function prepareForValidation(): void
{
$email = $this->input('email');

if (is_string($email)) {
$this->merge([
'email' => strtolower($email),
]);
}
}

/**
* @return array<string, mixed>
*/
Expand Down
4 changes: 2 additions & 2 deletions sourcecode/hub/app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ class User extends Model implements AuthenticatableContract
'email_verified' => true,
];

public function setEmailAttribute(string|null $email): void
public function setEmailAttribute(string $email): void
{
$this->attributes['email'] = $email;
$this->attributes['email'] = strtolower($email);

if ($this->exists && $email !== $this->getOriginal('email')) {
$this->attributes['email_verified'] = false;
Expand Down
34 changes: 34 additions & 0 deletions sourcecode/hub/tests/Browser/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ public function testUserCannotSignUpWithDuplicateEmail(): void
});
}

public function testEmailIsNormalizedUponRegistration(): void
{
$this->browse(function (Browser $browser) {
$browser->visit('/register')
->assertGuest()
->type('name', 'E. Mel')
->type('email', '[email protected]')
->type('password', 'my password')
->type('password_confirmation', 'my password')
->press('Sign up')
->assertAuthenticated()
->visit('/my-account')
->assertInputValue('email', '[email protected]');
});
}

public function testUserCanChangeLanguage(): void
{
User::factory()->create([
Expand Down Expand Up @@ -169,6 +185,24 @@ public function testUserCanChangeEmail(): void
});
}

public function testEmailIsNormalizedUponChanging(): void
{
User::factory()->withEmail('[email protected]')->create();

$this->browse(
fn(Browser $browser) => $browser
->loginAs('[email protected]')
->assertAuthenticated()
->visit('/my-account')
->type('email', '[email protected]')
->press('Save')
// The login should be invalid if the email didn't normalize.
// In that case, we wouldn't be able to see these.
->assertSee('Account updated successfully')
->assertInputValue('email', '[email protected]'),
);
}

public function testUserCanDisconnectFacebookAndGoogleIDWithPassword(): void
{
User::factory()->create([
Expand Down
22 changes: 22 additions & 0 deletions sourcecode/hub/tests/Feature/Api/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,26 @@ public function testCreatedAtIsOverrideable(): void
),
);
}

public function testEmailIsNormalizedBeforeSaving(): void
{
$user = User::factory()->admin()->create();

$this
->withBasicAuth($user->getApiKey(), $user->getApiSecret())
->postJson('/api/users', [
'name' => 'E. Mel',
'email' => '[email protected]',
])
->assertCreated()
->assertJson(
fn(AssertableJson $json) => $json
->has(
'data',
fn(AssertableJson $json) => $json
->where('email', '[email protected]')
->etc(),
),
);
}
}

0 comments on commit 14329d9

Please sign in to comment.