2020-12-11 05:40:38 -05:00
|
|
|
const { callbackify } = require('util')
|
2020-10-06 06:31:34 -04:00
|
|
|
const { db } = require('../../infrastructure/mongodb')
|
2020-10-30 04:10:50 -04:00
|
|
|
const metrics = require('@overleaf/metrics')
|
2019-05-29 05:21:06 -04:00
|
|
|
const logger = require('logger-sharelatex')
|
2019-09-09 07:52:25 -04:00
|
|
|
const { promisifyAll } = require('../../util/promises')
|
2020-12-11 05:40:38 -05:00
|
|
|
const {
|
|
|
|
promises: InstitutionsAPIPromises
|
|
|
|
} = require('../Institutions/InstitutionsAPI')
|
2020-05-22 07:16:52 -04:00
|
|
|
const InstitutionsHelper = require('../Institutions/InstitutionsHelper')
|
2019-05-29 05:21:06 -04:00
|
|
|
const Errors = require('../Errors/Errors')
|
2019-08-19 11:06:36 -04:00
|
|
|
const Features = require('../../infrastructure/Features')
|
2020-10-06 06:31:34 -04:00
|
|
|
const { normalizeQuery, normalizeMultiQuery } = require('../Helpers/Mongo')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-12-11 05:40:38 -05:00
|
|
|
async function getUserFullEmails(userId) {
|
|
|
|
const user = await UserGetter.promises.getUser(userId, {
|
|
|
|
email: 1,
|
|
|
|
emails: 1,
|
|
|
|
samlIdentifiers: 1
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
throw new Error('User not Found')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Features.hasFeature('affiliations')) {
|
|
|
|
return decorateFullEmails(user.email, user.emails, [], [])
|
|
|
|
}
|
|
|
|
|
|
|
|
const affiliationsData = await InstitutionsAPIPromises.getUserAffiliations(
|
|
|
|
userId
|
|
|
|
)
|
|
|
|
|
|
|
|
return decorateFullEmails(
|
|
|
|
user.email,
|
|
|
|
user.emails || [],
|
|
|
|
affiliationsData,
|
|
|
|
user.samlIdentifiers || []
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
const UserGetter = {
|
2019-05-29 05:21:06 -04:00
|
|
|
getUser(query, projection, callback) {
|
|
|
|
if (arguments.length === 2) {
|
|
|
|
callback = projection
|
|
|
|
projection = {}
|
|
|
|
}
|
2020-06-11 09:55:52 -04:00
|
|
|
try {
|
|
|
|
query = normalizeQuery(query)
|
2020-10-05 04:46:34 -04:00
|
|
|
db.users.findOne(query, { projection }, callback)
|
2020-06-11 09:55:52 -04:00
|
|
|
} catch (err) {
|
|
|
|
callback(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
getUserEmail(userId, callback) {
|
2019-09-24 04:43:43 -04:00
|
|
|
this.getUser(userId, { email: 1 }, (error, user) =>
|
|
|
|
callback(error, user && user.email)
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2020-12-11 05:40:38 -05:00
|
|
|
getUserFullEmails: callbackify(getUserFullEmails),
|
2019-05-29 05:21:06 -04:00
|
|
|
|
|
|
|
getUserByMainEmail(email, projection, callback) {
|
|
|
|
email = email.trim()
|
|
|
|
if (arguments.length === 2) {
|
|
|
|
callback = projection
|
|
|
|
projection = {}
|
|
|
|
}
|
2020-10-05 04:46:34 -04:00
|
|
|
db.users.findOne({ email }, { projection }, callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
getUserByAnyEmail(email, projection, callback) {
|
|
|
|
email = email.trim()
|
|
|
|
if (arguments.length === 2) {
|
|
|
|
callback = projection
|
|
|
|
projection = {}
|
|
|
|
}
|
|
|
|
// $exists: true MUST be set to use the partial index
|
|
|
|
const query = { emails: { $exists: true }, 'emails.email': email }
|
2020-10-05 04:46:34 -04:00
|
|
|
db.users.findOne(query, { projection }, (error, user) => {
|
2019-09-24 04:43:43 -04:00
|
|
|
if (error || user) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(error, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
// While multiple emails are being rolled out, check for the main email as
|
|
|
|
// well
|
2019-09-24 04:43:43 -04:00
|
|
|
this.getUserByMainEmail(email, projection, callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
getUsersByAnyConfirmedEmail(emails, projection, callback) {
|
|
|
|
if (arguments.length === 2) {
|
|
|
|
callback = projection
|
|
|
|
projection = {}
|
|
|
|
}
|
2020-10-12 06:25:33 -04:00
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
const query = {
|
2020-10-12 06:25:33 -04:00
|
|
|
'emails.email': { $in: emails }, // use the index on emails.email
|
2019-05-29 05:21:06 -04:00
|
|
|
emails: {
|
|
|
|
$exists: true,
|
2020-10-12 06:25:33 -04:00
|
|
|
$elemMatch: {
|
|
|
|
email: { $in: emails },
|
|
|
|
confirmedAt: { $exists: true }
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|
2020-10-12 06:25:33 -04:00
|
|
|
|
2020-10-05 04:46:34 -04:00
|
|
|
db.users.find(query, { projection }).toArray(callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
2019-07-29 05:47:44 -04:00
|
|
|
getUsersByV1Ids(v1Ids, projection, callback) {
|
|
|
|
if (arguments.length === 2) {
|
|
|
|
callback = projection
|
|
|
|
projection = {}
|
|
|
|
}
|
|
|
|
const query = { 'overleaf.id': { $in: v1Ids } }
|
2020-10-05 04:46:34 -04:00
|
|
|
db.users.find(query, { projection }).toArray(callback)
|
2019-07-29 05:47:44 -04:00
|
|
|
},
|
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
getUsersByHostname(hostname, projection, callback) {
|
|
|
|
const reversedHostname = hostname
|
|
|
|
.trim()
|
|
|
|
.split('')
|
|
|
|
.reverse()
|
|
|
|
.join('')
|
|
|
|
const query = {
|
|
|
|
emails: { $exists: true },
|
|
|
|
'emails.reversedHostname': reversedHostname
|
|
|
|
}
|
2020-10-05 04:46:34 -04:00
|
|
|
db.users.find(query, { projection }).toArray(callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
2020-06-02 10:02:06 -04:00
|
|
|
getUsers(query, projection, callback) {
|
2020-06-11 09:55:52 -04:00
|
|
|
try {
|
2020-10-06 06:31:34 -04:00
|
|
|
query = normalizeMultiQuery(query)
|
2020-10-05 04:46:34 -04:00
|
|
|
db.users.find(query, { projection }).toArray(callback)
|
2020-06-11 09:55:52 -04:00
|
|
|
} catch (err) {
|
|
|
|
callback(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// check for duplicate email address. This is also enforced at the DB level
|
|
|
|
ensureUniqueEmailAddress(newEmail, callback) {
|
2019-09-24 04:43:43 -04:00
|
|
|
this.getUserByAnyEmail(newEmail, function(error, user) {
|
|
|
|
if (user) {
|
2019-07-31 04:22:31 -04:00
|
|
|
return callback(new Errors.EmailExistsError())
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-09-24 04:43:43 -04:00
|
|
|
callback(error)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-22 07:16:52 -04:00
|
|
|
var decorateFullEmails = (
|
|
|
|
defaultEmail,
|
|
|
|
emailsData,
|
|
|
|
affiliationsData,
|
|
|
|
samlIdentifiers
|
|
|
|
) =>
|
2019-05-29 05:21:06 -04:00
|
|
|
emailsData.map(function(emailData) {
|
|
|
|
emailData.default = emailData.email === defaultEmail
|
|
|
|
|
|
|
|
const affiliation = affiliationsData.find(
|
|
|
|
aff => aff.email === emailData.email
|
|
|
|
)
|
2019-09-24 04:43:43 -04:00
|
|
|
if (affiliation) {
|
2020-02-18 06:36:53 -05:00
|
|
|
const { institution, inferred, role, department, licence } = affiliation
|
|
|
|
emailData.affiliation = {
|
|
|
|
institution,
|
|
|
|
inferred,
|
|
|
|
role,
|
|
|
|
department,
|
|
|
|
licence
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
|
|
|
emailsData.affiliation = null
|
|
|
|
}
|
|
|
|
|
2020-05-22 07:16:52 -04:00
|
|
|
if (emailData.samlProviderId) {
|
|
|
|
emailData.samlIdentifier = samlIdentifiers.find(
|
|
|
|
samlIdentifier => samlIdentifier.providerId === emailData.samlProviderId
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
emailsData.samlIdentifier = null
|
|
|
|
}
|
|
|
|
|
|
|
|
emailData.emailHasInstitutionLicence = InstitutionsHelper.emailHasLicence(
|
|
|
|
emailData
|
|
|
|
)
|
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
return emailData
|
|
|
|
})
|
|
|
|
;[
|
|
|
|
'getUser',
|
|
|
|
'getUserEmail',
|
|
|
|
'getUserByMainEmail',
|
|
|
|
'getUserByAnyEmail',
|
|
|
|
'getUsers',
|
|
|
|
'ensureUniqueEmailAddress'
|
|
|
|
].map(method =>
|
|
|
|
metrics.timeAsyncMethod(UserGetter, method, 'mongo.UserGetter', logger)
|
|
|
|
)
|
2019-09-09 07:52:25 -04:00
|
|
|
|
2020-12-11 05:40:38 -05:00
|
|
|
UserGetter.promises = promisifyAll(UserGetter, {
|
|
|
|
without: ['getUserFullEmails']
|
|
|
|
})
|
|
|
|
UserGetter.promises.getUserFullEmails = getUserFullEmails
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
module.exports = UserGetter
|