Ignore current users email on update

Fixes #5538
This commit is contained in:
rijkvanzanten
2021-05-12 16:39:53 -04:00
parent d8caf221ed
commit 9bd5fc1f54

View File

@@ -39,13 +39,19 @@ export class UsersService extends ItemsService {
* User email has to be unique case-insensitive. This is an additional check to make sure that
* the email is unique regardless of casing
*/
private async checkUniqueEmails(emails: string[]) {
private async checkUniqueEmails(emails: string[], excludeKey?: PrimaryKey) {
if (emails.length > 0) {
const results = await this.knex
const query = this.knex
.select('email')
.from('directus_users')
.whereRaw(`LOWER(??) IN (${emails.map(() => '?')})`, ['email', ...emails]);
if (excludeKey) {
query.whereNot('id', excludeKey);
}
const results = await query;
if (results.length > 0) {
throw new RecordNotUniqueException('email', {
collection: 'directus_users',
@@ -125,7 +131,7 @@ export class UsersService extends ItemsService {
const email = data.email?.toLowerCase();
if (email) {
await this.checkUniqueEmails([email]);
await this.checkUniqueEmails([email], key);
}
if (data.password) {