fix: Match existing users by case-insensitive email on invite (#14762)

* fix: Match existing users by case-insensitive email on invite

* test: Cover case-insensitive User.findByEmail lookup
This commit is contained in:
Tom Moor authored and GitHub committed 2026-11-27 00:03:43 +00:00
1 parent d2ab9e7e3f
commit fbe06d0624
4 files changed
+50 -5

No files matched your search

+18
View File
@@ -231,4 +231,22 @@ describe("userInviter", () => {
);
expect(response.sent.length).toEqual(0);
});
it("should not send invites to existing team members with different email case", async () => {
const email = faker.internet.email().toLowerCase();
const user = await buildUser({ email: email.toUpperCase() });
const response = await withContext(user, (ctx) =>
userInviter(ctx, {
invites: [
{
role: UserRole.Member,
email,
name: user.name,
},
],
})
);
expect(response.sent.length).toEqual(0);
expect(response.unsent.length).toEqual(1);
});
});
+8 -3
View File
@@ -1,4 +1,5 @@
import { partition, uniqBy } from "es-toolkit/compat";
import { Op, Sequelize } from "sequelize";
import { UserRole } from "@shared/types";
import InviteEmail from "@server/emails/templates/InviteEmail";
import env from "@server/env";
@@ -61,11 +62,15 @@ export default async function userInviter(
const existingUsers = await User.findAll({
where: {
teamId: user.teamId,
email: emails,
[Op.and]: [
Sequelize.where(Sequelize.fn("lower", Sequelize.col("email")), {
[Op.in]: emails,
}),
],
},
});
const existingEmails = existingUsers.map(
(existingUser) => existingUser.email
const existingEmails = existingUsers.map((existingUser) =>
existingUser.email?.toLowerCase()
);
const [existingInvites, filteredInvites] = partition(
normalizedInvites,
+17
View File
@@ -55,6 +55,23 @@ describe("user model", () => {
});
});
describe("findByEmail", () => {
it("should match email case-insensitively within the workspace only", async () => {
const email = faker.internet.email().toLowerCase();
const user = await buildUser({ email: email.toUpperCase() });
const otherTeamUser = await buildUser();
const found = await User.findByEmail(createContext({ user }), email);
expect(found?.id).toEqual(user.id);
const notFound = await User.findByEmail(
createContext({ user: otherTeamUser }),
email
);
expect(notFound).toBeNull();
});
});
describe("create", () => {
it("should not allow URLs in name", async () => {
await expect(
+7 -2
View File
@@ -8,7 +8,7 @@ import type {
InferAttributes,
InferCreationAttributes,
} from "sequelize";
import { QueryTypes, Op } from "sequelize";
import { QueryTypes, Op, Sequelize } from "sequelize";
import { type InstanceUpdateOptions } from "sequelize";
import {
Table,
@@ -996,7 +996,12 @@ class User extends ParanoidModel<
return this.findOne({
where: {
teamId: ctx.auth.user.teamId,
email: email.trim().toLowerCase(),
[Op.and]: [
Sequelize.where(
Sequelize.fn("lower", Sequelize.col("email")),
email.trim().toLowerCase()
),
],
},
...toHookContext(ctx),
});