mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-08-03 15:11:44 +00:00
fix case-insensitive local auth user lookup
This commit is contained in:
parent
1914b9d7e7
commit
4195e8800e
2 changed files with 50 additions and 4 deletions
|
|
@ -348,11 +348,13 @@ class User extends Model {
|
||||||
static async getUserByUsername(username) {
|
static async getUserByUsername(username) {
|
||||||
if (!username) return null
|
if (!username) return null
|
||||||
|
|
||||||
const cachedUser = userCache.getByUsername(username)
|
const normalizedUsername = username.toLowerCase()
|
||||||
|
|
||||||
|
const cachedUser = userCache.getByUsername(normalizedUsername)
|
||||||
if (cachedUser) return cachedUser
|
if (cachedUser) return cachedUser
|
||||||
|
|
||||||
const user = await this.findOne({
|
const user = await this.findOne({
|
||||||
where: sequelize.where(sequelize.fn('lower', sequelize.col('username')), username.toLowerCase()),
|
where: sequelize.where(sequelize.fn('LOWER', sequelize.col('username')), normalizedUsername),
|
||||||
include: this.sequelize.models.mediaProgress
|
include: this.sequelize.models.mediaProgress
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
@ -369,11 +371,13 @@ class User extends Model {
|
||||||
static async getUserByEmail(email) {
|
static async getUserByEmail(email) {
|
||||||
if (!email) return null
|
if (!email) return null
|
||||||
|
|
||||||
const cachedUser = userCache.getByEmail(email)
|
const normalizedEmail = email.toLowerCase()
|
||||||
|
|
||||||
|
const cachedUser = userCache.getByEmail(normalizedEmail)
|
||||||
if (cachedUser) return cachedUser
|
if (cachedUser) return cachedUser
|
||||||
|
|
||||||
const user = await this.findOne({
|
const user = await this.findOne({
|
||||||
where: sequelize.where(sequelize.fn('lower', sequelize.col('email')), email.toLowerCase()),
|
where: sequelize.where(sequelize.fn('LOWER', sequelize.col('email')), normalizedEmail),
|
||||||
include: this.sequelize.models.mediaProgress
|
include: this.sequelize.models.mediaProgress
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,48 @@ const sinon = require('sinon')
|
||||||
const User = require('../../../server/models/User')
|
const User = require('../../../server/models/User')
|
||||||
|
|
||||||
describe('User model', () => {
|
describe('User model', () => {
|
||||||
|
describe('case-insensitive lookup helpers', () => {
|
||||||
|
afterEach(() => {
|
||||||
|
sinon.restore()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should query usernames case-insensitively', async () => {
|
||||||
|
User.sequelize = {
|
||||||
|
models: {
|
||||||
|
mediaProgress: {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const findOneStub = sinon.stub(User, 'findOne').resolves(null)
|
||||||
|
|
||||||
|
await User.getUserByUsername('Madison')
|
||||||
|
|
||||||
|
expect(findOneStub.calledOnce).to.equal(true)
|
||||||
|
const options = findOneStub.firstCall.args[0]
|
||||||
|
expect(options.where.attribute.fn).to.equal('LOWER')
|
||||||
|
expect(options.where.attribute.args[0].col).to.equal('username')
|
||||||
|
expect(options.where.logic).to.equal('madison')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should query emails case-insensitively', async () => {
|
||||||
|
User.sequelize = {
|
||||||
|
models: {
|
||||||
|
mediaProgress: {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const findOneStub = sinon.stub(User, 'findOne').resolves(null)
|
||||||
|
|
||||||
|
await User.getUserByEmail('Example.User@Example.com')
|
||||||
|
|
||||||
|
expect(findOneStub.calledOnce).to.equal(true)
|
||||||
|
const options = findOneStub.firstCall.args[0]
|
||||||
|
expect(options.where.attribute.fn).to.equal('LOWER')
|
||||||
|
expect(options.where.attribute.args[0].col).to.equal('email')
|
||||||
|
expect(options.where.logic).to.equal('example.user@example.com')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe('getUserByIdOrOldId', () => {
|
describe('getUserByIdOrOldId', () => {
|
||||||
let originalSequelize
|
let originalSequelize
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue