mirror of
https://github.com/Unleash/unleash.git
synced 2025-08-04 13:48:56 +02:00
fix: make users emails case-insensitive (#804)
This commit is contained in:
parent
97d77d84cc
commit
c729f514cf
@ -28,10 +28,12 @@ const emptify = value => {
|
|||||||
return value;
|
return value;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const safeToLower = (s?: string) => (s ? s.toLowerCase() : s);
|
||||||
|
|
||||||
const mapUserToColumns = (user: ICreateUser) => ({
|
const mapUserToColumns = (user: ICreateUser) => ({
|
||||||
name: user.name,
|
name: user.name,
|
||||||
username: user.username,
|
username: user.username,
|
||||||
email: user.email,
|
email: safeToLower(user.email),
|
||||||
image_url: user.imageUrl,
|
image_url: user.imageUrl,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -114,7 +116,7 @@ class UserStore {
|
|||||||
return query.where('id', q.id);
|
return query.where('id', q.id);
|
||||||
}
|
}
|
||||||
if (q.email) {
|
if (q.email) {
|
||||||
return query.where('email', q.email);
|
return query.where('email', safeToLower(q.email));
|
||||||
}
|
}
|
||||||
if (q.username) {
|
if (q.username) {
|
||||||
return query.where('username', q.username);
|
return query.where('username', q.username);
|
||||||
|
15
src/migrations/20210423103647-lowercase-all-emails.js
Normal file
15
src/migrations/20210423103647-lowercase-all-emails.js
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
exports.up = function(db, cb) {
|
||||||
|
db.runSql(
|
||||||
|
`
|
||||||
|
DELETE FROM users WHERE id IN
|
||||||
|
(SELECT id FROM
|
||||||
|
(SELECT id, lower(email) as email, row_number() over (PARTITION BY lower(email) ORDER BY id desc) as Row FROM users) as dupes
|
||||||
|
WHERE email IS NOT NULL AND dupes.Row > 1);
|
||||||
|
UPDATE users SET email = LOWER(email);
|
||||||
|
|
||||||
|
`,
|
||||||
|
cb,
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.down = function() {};
|
@ -128,7 +128,7 @@ test.serial('should reset user after successful login', async t => {
|
|||||||
|
|
||||||
test.serial('should only update specified fields on user', async t => {
|
test.serial('should only update specified fields on user', async t => {
|
||||||
const store = stores.userStore;
|
const store = stores.userStore;
|
||||||
const email = 'userTobeUpdated@mail.com';
|
const email = 'usertobeupdated@mail.com';
|
||||||
const user = {
|
const user = {
|
||||||
email,
|
email,
|
||||||
username: 'test',
|
username: 'test',
|
||||||
@ -143,3 +143,40 @@ test.serial('should only update specified fields on user', async t => {
|
|||||||
t.deepEqual(storedUser.email, user.email);
|
t.deepEqual(storedUser.email, user.email);
|
||||||
t.deepEqual(storedUser.username, user.username);
|
t.deepEqual(storedUser.username, user.username);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test.serial('should always lowercase emails on inserts', async t => {
|
||||||
|
const store = stores.userStore;
|
||||||
|
const email = 'someCrazyCasingGoingOn@mail.com';
|
||||||
|
const user = {
|
||||||
|
email,
|
||||||
|
};
|
||||||
|
|
||||||
|
await store.upsert(user);
|
||||||
|
|
||||||
|
const storedUser = await store.get({ email });
|
||||||
|
|
||||||
|
t.deepEqual(storedUser.email, user.email.toLowerCase());
|
||||||
|
});
|
||||||
|
|
||||||
|
test.serial('should always lowercase emails on updates', async t => {
|
||||||
|
const store = stores.userStore;
|
||||||
|
const email = 'someCrazyCasingGoingOn@mail.com';
|
||||||
|
const user = {
|
||||||
|
email,
|
||||||
|
};
|
||||||
|
|
||||||
|
await store.upsert(user);
|
||||||
|
|
||||||
|
let storedUser = await store.get({ email });
|
||||||
|
|
||||||
|
t.deepEqual(storedUser.email, user.email.toLowerCase());
|
||||||
|
|
||||||
|
const updatedUser = {
|
||||||
|
id: storedUser.id,
|
||||||
|
email: 'SomeOtherCasing@hotmail.com',
|
||||||
|
};
|
||||||
|
await store.upsert(updatedUser);
|
||||||
|
|
||||||
|
storedUser = await store.get({ id: storedUser.id });
|
||||||
|
t.is(storedUser.email, updatedUser.email.toLowerCase());
|
||||||
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user