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

fix: Lowercase user email before insert and update #1342

Merged
merged 3 commits into from
Oct 14, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Knex } from 'knex';

export async function up(knex: Knex): Promise<void> {
await knex('users')
.whereRaw('email != LOWER(email)')
.update({
email: knex.raw('LOWER(email)'),
});
}

export async function down(): Promise<void> {
// void
}
10 changes: 8 additions & 2 deletions packages/backend/src/graphql/mutations/create-user.ee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@ type Params = {
};
};

const createUser = async (_parent: unknown, params: Params, context: Context) => {
const createUser = async (
_parent: unknown,
params: Params,
context: Context
) => {
context.currentUser.can('create', 'User');

const { fullName, email, password } = params.input;

const existingUser = await User.query().findOne({ email });
const existingUser = await User.query().findOne({
email: email.toLowerCase(),
});

if (existingUser) {
throw new Error('User already exists!');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Params = {
const forgotPassword = async (_parent: unknown, params: Params) => {
const { email } = params.input;

const user = await User.query().findOne({ email });
const user = await User.query().findOne({ email: email.toLowerCase() });

if (!user) {
throw new Error('Email address not found!');
Expand Down
4 changes: 3 additions & 1 deletion packages/backend/src/graphql/mutations/register-user.ee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ type Params = {
const registerUser = async (_parent: unknown, params: Params) => {
const { fullName, email, password } = params.input;

const existingUser = await User.query().findOne({ email });
const existingUser = await User.query().findOne({
email: email.toLowerCase(),
});

if (existingUser) {
throw new Error('User already exists!');
Expand Down
3 changes: 3 additions & 0 deletions packages/backend/src/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ class User extends Base {

async $beforeInsert(queryContext: QueryContext) {
await super.$beforeInsert(queryContext);

this.email = this.email.toLowerCase();
await this.generateHash();

if (appConfig.isCloud) {
Expand All @@ -273,6 +275,7 @@ class User extends Base {
async $beforeUpdate(opt: ModelOptions, queryContext: QueryContext) {
await super.$beforeUpdate(opt, queryContext);

this.email = this.email.toLowerCase();
await this.generateHash();
}

Expand Down