Skip to content

Commit 080e76c

Browse files
committed
Respect user login case.
While creating new users with identical username is not possible, we still have some old ones in system that are not possible to retrieve. Fixes #651
1 parent f2a9833 commit 080e76c

File tree

4 files changed

+13
-8
lines changed

4 files changed

+13
-8
lines changed

controllers/__tests__/auth.test.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,18 @@ describe('authentication', () => {
8888
});
8989

9090
it('user login exists', async () => {
91-
expect.assertions(1);
91+
expect.assertions(2);
9292

9393
// Register user.
9494
await auth.register(data);
9595

9696
// Change email and register again.
97-
const testData = _.defaults({ 'email': '[email protected]' }, data);
97+
let testData = _.defaults({ 'email': '[email protected]' }, data);
98+
99+
await expect(auth.register(testData)).rejects.toThrow(new AuthenticationError(constants.AUTHENTICATION_USER_EXISTS));
100+
101+
// Change username to different case and register again.
102+
testData = _.defaults({ 'login': 'User1' }, testData);
98103

99104
await expect(auth.register(testData)).rejects.toThrow(new AuthenticationError(constants.AUTHENTICATION_USER_EXISTS));
100105
});

controllers/auth.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ async function recall({ login }) {
205205
}
206206

207207
const user = await User.findOne({
208-
$or: [{ login: new RegExp(`^${_.escapeRegExp(login)}$`, 'i') }, { email: login.toLowerCase() }],
208+
$or: [{ login: new RegExp(`^${_.escapeRegExp(login)}$`) }, { email: login.toLowerCase() }],
209209
}, null, { lean: true }).exec();
210210

211211
if (!user) {

controllers/profile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ async function giveUser({ login }) {
6767
user.online = Boolean(userObj);
6868
} else {
6969
user = await User.findOne(
70-
{ login: new RegExp(`^${_.escapeRegExp(login)}$`, 'i'), active: true },
70+
{ login: new RegExp(`^${_.escapeRegExp(login)}$`), active: true },
7171
{ _id: 0, cid: 0, pass: 0, activatedate: 0, loginAttempts: 0, active: 0, rules: 0 }, { lean: true }
7272
).populate([
7373
{

models/User.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ registerModel(db => {
153153
UserScheme.statics.getAuthenticated = async function (login, password) {
154154
const user = await this.findOne({
155155
$or: [
156-
{ login: new RegExp(`^${_.escapeRegExp(login)}$`, 'i') },
156+
{ login: new RegExp(`^${_.escapeRegExp(login)}$`) },
157157
{ email: login.toLowerCase() },
158158
], active: true, pass: { $ne: 'init' },
159159
});
@@ -213,7 +213,7 @@ registerModel(db => {
213213
cb(null, 'Login is not specified');
214214
}
215215

216-
this.findOne({ login: new RegExp(`^${_.escapeRegExp(login)}$`, 'i'), active: true }).select({
216+
this.findOne({ login: new RegExp(`^${_.escapeRegExp(login)}$`), active: true }).select({
217217
_id: 0,
218218
pass: 0,
219219
activatedate: 0,
@@ -230,7 +230,7 @@ registerModel(db => {
230230
cb(null, 'Login is not specified');
231231
}
232232

233-
this.findOne({ login: new RegExp(`^${_.escapeRegExp(login)}$`, 'i'), active: true }).exec(cb);
233+
this.findOne({ login: new RegExp(`^${_.escapeRegExp(login)}$`), active: true }).exec(cb);
234234
};
235235

236236
UserScheme.statics.getUserAllLoginMail = function (login, cb) {
@@ -242,7 +242,7 @@ registerModel(db => {
242242
$and: [
243243
{
244244
$or: [
245-
{ login: new RegExp(`^${_.escapeRegExp(login)}$`, 'i') },
245+
{ login: new RegExp(`^${_.escapeRegExp(login)}$`) },
246246
{ email: login.toLowerCase() },
247247
],
248248
},

0 commit comments

Comments
 (0)