Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/15758 username as email validation #15769

Merged
merged 5 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Fixed a bug where it wasn’t possible to save nested entries via the `entries/save-entry` controller action. ([#15737](https://github.com/craftcms/cms/issues/15737))
- Fixed a bug where hyperlinks in Link field inputs could wrap unnecessarily. ([#15738](https://github.com/craftcms/cms/issues/15738))
- Fixed an error that occurred when running the `entrify/global-set` command. ([#15746](https://github.com/craftcms/cms/issues/15746))
- Fixed a bug where users’ `username` values weren’t getting updated based on email address changes when `useEmailAsUsername` was enabled. ([#15758](https://github.com/craftcms/cms/issues/15758))

## 5.4.4 - 2024-09-14

Expand Down
17 changes: 16 additions & 1 deletion src/elements/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -910,14 +910,25 @@ protected function defineRules(): array

if (Craft::$app->getIsInstalled()) {
$rules[] = [
['username', 'email'],
['email'],
UniqueValidator::class,
'targetClass' => UserRecord::class,
'caseInsensitive' => true,
'filter' => ['or', ['active' => true], ['pending' => true]],
'when' => $treatAsActive,
];

if (!Craft::$app->getConfig()->getGeneral()->useEmailAsUsername) {
$rules[] = [
['username'],
UniqueValidator::class,
'targetClass' => UserRecord::class,
'caseInsensitive' => true,
'filter' => ['or', ['active' => true], ['pending' => true]],
'when' => $treatAsActive,
];
}

$rules[] = [['unverifiedEmail'], 'validateUnverifiedEmail'];
}

Expand Down Expand Up @@ -2289,6 +2300,10 @@ final public function beforeSave(bool $isNew): bool
return false;
}

if (Craft::$app->getConfig()->getGeneral()->useEmailAsUsername) {
$this->username = $this->email;
}

return parent::beforeSave($isNew);
}

Expand Down
4 changes: 4 additions & 0 deletions tests/unit/services/UsersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ public function testUserActivationEmailAsUsernameWithAnUnverifedEmail(): void

self::assertSame(User::STATUS_ACTIVE, $user->getStatus());
self::assertSame('jsmith@gmail.com', $user->username);

Craft::$app->getConfig()->getGeneral()->useEmailAsUsername = false;
}

/**
Expand All @@ -139,6 +141,8 @@ public function testUserActivationEmailAsUsernameWithNoUnverifedEmail(): void

self::assertSame(User::STATUS_ACTIVE, $user->getStatus());
self::assertSame('jsmith', $user->username);

Craft::$app->getConfig()->getGeneral()->useEmailAsUsername = false;
}

/**
Expand Down