- Add case insensitive indexes for User.email and User.username - Update user queries by username or email so that they are case insensitive
This commit is contained in:
parent
16860d76dd
commit
15ad07d5ce
7 changed files with 110 additions and 107 deletions
|
@ -43,7 +43,7 @@ passport.use(new LocalStrategy({ usernameField: 'email' }, (email, password, don
|
||||||
* Authentificate using Basic Auth (Username + Api Key)
|
* Authentificate using Basic Auth (Username + Api Key)
|
||||||
*/
|
*/
|
||||||
passport.use(new BasicStrategy((userid, key, done) => {
|
passport.use(new BasicStrategy((userid, key, done) => {
|
||||||
User.findOne({ username: userid }, (err, user) => { // eslint-disable-line consistent-return
|
User.findOne({ username: userid }).collation({ locale: 'en', strength: 2 }).exec((err, user) => { // eslint-disable-line consistent-return
|
||||||
if (err) { return done(err); }
|
if (err) { return done(err); }
|
||||||
if (!user) { return done(null, false); }
|
if (!user) { return done(null, false); }
|
||||||
user.findMatchingKey(key, (innerErr, isMatch, keyDocument) => {
|
user.findMatchingKey(key, (innerErr, isMatch, keyDocument) => {
|
||||||
|
@ -100,7 +100,7 @@ passport.use(new GitHubStrategy({
|
||||||
|
|
||||||
User.findOne({
|
User.findOne({
|
||||||
email: { $in: emails },
|
email: { $in: emails },
|
||||||
}, (findByEmailErr, existingEmailUser) => {
|
}).collation({ locale: 'en', strength: 2 }).exec((findByEmailErr, existingEmailUser) => {
|
||||||
if (existingEmailUser) {
|
if (existingEmailUser) {
|
||||||
existingEmailUser.email = existingEmailUser.email || primaryEmail;
|
existingEmailUser.email = existingEmailUser.email || primaryEmail;
|
||||||
existingEmailUser.github = profile.id;
|
existingEmailUser.github = profile.id;
|
||||||
|
@ -143,9 +143,10 @@ passport.use(new GoogleStrategy({
|
||||||
|
|
||||||
User.findOne({
|
User.findOne({
|
||||||
email: primaryEmail,
|
email: primaryEmail,
|
||||||
}, (findByEmailErr, existingEmailUser) => {
|
}).collation({ locale: 'en', strength: 2 }).exec((findByEmailErr, existingEmailUser) => {
|
||||||
let username = profile._json.emails[0].value.split('@')[0];
|
let username = profile._json.emails[0].value.split('@')[0];
|
||||||
User.findOne({ username }, (findByUsernameErr, existingUsernameUser) => {
|
User.findOne({ username }).collation({ locale: 'en', strength: 2 })
|
||||||
|
.exec((findByUsernameErr, existingUsernameUser) => {
|
||||||
if (existingUsernameUser) {
|
if (existingUsernameUser) {
|
||||||
const adj = friendlyWords.predicates[Math.floor(Math.random() * friendlyWords.predicates.length)];
|
const adj = friendlyWords.predicates[Math.floor(Math.random() * friendlyWords.predicates.length)];
|
||||||
username = slugify(`${username} ${adj}`);
|
username = slugify(`${username} ${adj}`);
|
||||||
|
|
|
@ -11,7 +11,7 @@ export default function collectionForUserExists(username, collectionId, callback
|
||||||
}
|
}
|
||||||
|
|
||||||
function findUser() {
|
function findUser() {
|
||||||
return User.findOne({ username });
|
return User.findOne({ username }).collation({ locale: 'en', strength: 2 }).exec();
|
||||||
}
|
}
|
||||||
|
|
||||||
function findCollection(owner) {
|
function findCollection(owner) {
|
||||||
|
|
|
@ -3,7 +3,8 @@ import User from '../../models/user';
|
||||||
|
|
||||||
async function getOwnerUserId(req) {
|
async function getOwnerUserId(req) {
|
||||||
if (req.params.username) {
|
if (req.params.username) {
|
||||||
const user = await User.findOne({ username: req.params.username });
|
const user =
|
||||||
|
await User.findOne({ username: req.params.username }).collation({ locale: 'en', strength: 2 }).exec();
|
||||||
if (user && user._id) {
|
if (user && user._id) {
|
||||||
return user._id;
|
return user._id;
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,7 +64,7 @@ export function updateProject(req, res) {
|
||||||
|
|
||||||
export function getProject(req, res) {
|
export function getProject(req, res) {
|
||||||
const { project_id: projectId, username } = req.params;
|
const { project_id: projectId, username } = req.params;
|
||||||
User.findOne({ username }, (err, user) => { // eslint-disable-line
|
User.findOne({ username }).collation({ locale: "en", strength: 2 }).exec((err, user) => { // eslint-disable-line
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return res.status(404).send({ message: 'Project with that username does not exist' });
|
return res.status(404).send({ message: 'Project with that username does not exist' });
|
||||||
}
|
}
|
||||||
|
@ -141,7 +141,7 @@ export function projectExists(projectId, callback) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function projectForUserExists(username, projectId, callback) {
|
export function projectForUserExists(username, projectId, callback) {
|
||||||
User.findOne({ username }, (err, user) => {
|
User.findOne({ username }).collation({ locale: 'en', strength: 2 }).exec((err, user) => {
|
||||||
if (!user) {
|
if (!user) {
|
||||||
callback(false);
|
callback(false);
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -7,7 +7,7 @@ const UserNotFoundError = createApplicationErrorClass('UserNotFoundError');
|
||||||
|
|
||||||
function getProjectsForUserName(username) {
|
function getProjectsForUserName(username) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
User.findOne({ username }, (err, user) => {
|
User.findOne({ username }).collation({ locale: 'en', strength: 2 }).exec((err, user) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
reject(err);
|
reject(err);
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import crypto from 'crypto';
|
import crypto from 'crypto';
|
||||||
import async from 'async';
|
import async from 'async';
|
||||||
import escapeStringRegexp from 'escape-string-regexp';
|
|
||||||
|
|
||||||
import User from '../models/user';
|
import User from '../models/user';
|
||||||
import mail from '../utils/mail';
|
import mail from '../utils/mail';
|
||||||
|
@ -31,12 +30,9 @@ const random = (done) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
export function findUserByUsername(username, cb) {
|
export function findUserByUsername(username, cb) {
|
||||||
User.findOne(
|
User.findOne({ username }).collation({ locale: 'en', strength: 2 }).exec((err, user) => {
|
||||||
{ username },
|
|
||||||
(err, user) => {
|
|
||||||
cb(user);
|
cb(user);
|
||||||
}
|
});
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createUser(req, res, next) {
|
export function createUser(req, res, next) {
|
||||||
|
@ -54,14 +50,12 @@ export function createUser(req, res, next) {
|
||||||
verifiedTokenExpires: EMAIL_VERIFY_TOKEN_EXPIRY_TIME,
|
verifiedTokenExpires: EMAIL_VERIFY_TOKEN_EXPIRY_TIME,
|
||||||
});
|
});
|
||||||
|
|
||||||
User.findOne(
|
User.findOne({
|
||||||
{
|
|
||||||
$or: [
|
$or: [
|
||||||
{ email: new RegExp(`^${escapeStringRegexp(email)}$`, 'i') },
|
{ email },
|
||||||
{ username: new RegExp(`^${escapeStringRegexp(username)}$`, 'i') }
|
{ username }
|
||||||
]
|
]
|
||||||
},
|
}).collation({ locale: 'en', strength: 2 }).exec((err, existingUser) => {
|
||||||
(err, existingUser) => {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
res.status(404).send({ error: err });
|
res.status(404).send({ error: err });
|
||||||
return;
|
return;
|
||||||
|
@ -97,8 +91,7 @@ export function createUser(req, res, next) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,8 +99,8 @@ export function duplicateUserCheck(req, res) {
|
||||||
const checkType = req.query.check_type;
|
const checkType = req.query.check_type;
|
||||||
const value = req.query[checkType];
|
const value = req.query[checkType];
|
||||||
const query = {};
|
const query = {};
|
||||||
query[checkType] = new RegExp(`^${escapeStringRegexp(value)}$`, 'i');
|
query[checkType] = value;
|
||||||
User.findOne(query, (err, user) => {
|
User.findOne(query).collation({ locale: 'en', strength: 2 }).exec((err, user) => {
|
||||||
if (user) {
|
if (user) {
|
||||||
return res.json({
|
return res.json({
|
||||||
exists: true,
|
exists: true,
|
||||||
|
@ -151,7 +144,8 @@ export function resetPasswordInitiate(req, res) {
|
||||||
async.waterfall([
|
async.waterfall([
|
||||||
random,
|
random,
|
||||||
(token, done) => {
|
(token, done) => {
|
||||||
User.findOne({ email: req.body.email.toLowerCase() }, (err, user) => {
|
User.findOne({ email: req.body.email.toLowerCase() })
|
||||||
|
.collation({ locale: 'en', strength: 2 }).exec((err, user) => {
|
||||||
if (!user) {
|
if (!user) {
|
||||||
res.json({ success: true, message: 'If the email is registered with the editor, an email has been sent.' });
|
res.json({ success: true, message: 'If the email is registered with the editor, an email has been sent.' });
|
||||||
return;
|
return;
|
||||||
|
@ -281,7 +275,7 @@ export function updatePassword(req, res) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function userExists(username, callback) {
|
export function userExists(username, callback) {
|
||||||
User.findOne({ username: new RegExp(`^${escapeStringRegexp(username)}$`, 'i') }, (err, user) => (
|
User.findOne(username).collation({ locale: 'en', strength: 2 }).exec((err, user) => (
|
||||||
user ? callback(true) : callback(false)
|
user ? callback(true) : callback(false)
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
import mongoose from 'mongoose';
|
import mongoose from 'mongoose';
|
||||||
import escapeStringRegexp from 'escape-string-regexp';
|
|
||||||
|
|
||||||
const bcrypt = require('bcrypt-nodejs');
|
const bcrypt = require('bcrypt-nodejs');
|
||||||
|
|
||||||
|
@ -143,16 +142,24 @@ userSchema.methods.findMatchingKey = function findMatchingKey(candidateKey, cb)
|
||||||
};
|
};
|
||||||
|
|
||||||
userSchema.statics.findByMailOrName = function findByMailOrName(email) {
|
userSchema.statics.findByMailOrName = function findByMailOrName(email) {
|
||||||
|
const isEmail = email.indexOf('@') > -1;
|
||||||
|
if (isEmail) {
|
||||||
const query = {
|
const query = {
|
||||||
$or: [{
|
email: email.toLowerCase()
|
||||||
email: new RegExp(`^${escapeStringRegexp(email)}$`, 'i'),
|
|
||||||
}, {
|
|
||||||
username: new RegExp(`^${escapeStringRegexp(email)}$`, 'i'),
|
|
||||||
}],
|
|
||||||
};
|
};
|
||||||
return this.findOne(query).exec();
|
// once emails are all lowercase, won't need to do collation
|
||||||
|
// but maybe it's not even necessary to make all emails lowercase??
|
||||||
|
return this.findOne(query).collation({ locale: 'en', strength: 2 }).exec();
|
||||||
|
}
|
||||||
|
const query = {
|
||||||
|
username: email
|
||||||
|
};
|
||||||
|
return this.findOne(query).collation({ locale: 'en', strength: 2 }).exec();
|
||||||
};
|
};
|
||||||
|
|
||||||
userSchema.statics.EmailConfirmation = EmailConfirmationStates;
|
userSchema.statics.EmailConfirmation = EmailConfirmationStates;
|
||||||
|
|
||||||
|
userSchema.index({ username: 1 }, { collation: { locale: 'en', strength: 2 } });
|
||||||
|
userSchema.index({ email: 1 }, { collation: { locale: 'en', strength: 2 } });
|
||||||
|
|
||||||
export default mongoose.model('User', userSchema);
|
export default mongoose.model('User', userSchema);
|
||||||
|
|
Loading…
Reference in a new issue