2014-02-12 05:23:40 -05:00
|
|
|
mongojs = require("../../infrastructure/mongojs")
|
2017-04-03 11:18:30 -04:00
|
|
|
metrics = require('metrics-sharelatex')
|
2017-03-16 06:59:18 -04:00
|
|
|
logger = require('logger-sharelatex')
|
2014-02-12 05:23:40 -05:00
|
|
|
db = mongojs.db
|
|
|
|
ObjectId = mongojs.ObjectId
|
2018-07-10 15:49:24 -04:00
|
|
|
{ getUserAffiliations } = require("../Institutions/InstitutionsAPI")
|
2018-07-17 06:12:09 -04:00
|
|
|
Errors = require("../Errors/Errors")
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
module.exports = UserGetter =
|
|
|
|
getUser: (query, projection, callback = (error, user) ->) ->
|
2018-09-13 12:31:35 -04:00
|
|
|
return callback(new Error("no query provided")) unless query?
|
2018-05-23 10:12:23 -04:00
|
|
|
if query?.email?
|
|
|
|
return callback(new Error("Don't use getUser to find user by email"), null)
|
2014-02-12 05:23:40 -05:00
|
|
|
if arguments.length == 2
|
|
|
|
callback = projection
|
|
|
|
projection = {}
|
|
|
|
if typeof query == "string"
|
2014-11-09 18:08:23 -05:00
|
|
|
try
|
|
|
|
query = _id: ObjectId(query)
|
|
|
|
catch e
|
|
|
|
return callback(null, null)
|
2014-02-12 05:23:40 -05:00
|
|
|
else if query instanceof ObjectId
|
|
|
|
query = _id: query
|
|
|
|
|
|
|
|
db.users.findOne query, projection, callback
|
2015-10-07 12:32:35 -04:00
|
|
|
|
2018-05-25 07:04:09 -04:00
|
|
|
getUserEmail: (userId, callback = (error, email) ->) ->
|
|
|
|
@getUser userId, { email: 1 }, (error, user) ->
|
|
|
|
callback(error, user?.email)
|
|
|
|
|
2018-06-08 13:05:19 -04:00
|
|
|
getUserFullEmails: (userId, callback = (error, emails) ->) ->
|
|
|
|
@getUser userId, { email: 1, emails: 1 }, (error, user) ->
|
|
|
|
return callback error if error?
|
|
|
|
return callback new Error('User not Found') unless user
|
|
|
|
|
2018-07-10 15:49:24 -04:00
|
|
|
getUserAffiliations userId, (error, affiliationsData) ->
|
2018-06-21 06:30:12 -04:00
|
|
|
return callback error if error?
|
2018-09-18 08:57:11 -04:00
|
|
|
callback null, decorateFullEmails(user.email, user.emails or [], affiliationsData)
|
2018-06-08 13:05:19 -04:00
|
|
|
|
2018-05-23 10:12:23 -04:00
|
|
|
getUserByMainEmail: (email, projection, callback = (error, user) ->) ->
|
|
|
|
email = email.trim()
|
|
|
|
if arguments.length == 2
|
|
|
|
callback = projection
|
|
|
|
projection = {}
|
|
|
|
db.users.findOne email: email, projection, callback
|
|
|
|
|
2018-05-25 07:04:09 -04:00
|
|
|
getUserByAnyEmail: (email, projection, callback = (error, user) ->) ->
|
|
|
|
email = email.trim()
|
|
|
|
if arguments.length == 2
|
|
|
|
callback = projection
|
|
|
|
projection = {}
|
2018-06-06 09:46:41 -04:00
|
|
|
# $exists: true MUST be set to use the partial index
|
2018-06-06 08:52:09 -04:00
|
|
|
query = emails: { $exists: true }, 'emails.email': email
|
|
|
|
db.users.findOne query, projection, (error, user) =>
|
2018-05-25 07:04:09 -04:00
|
|
|
return callback(error, user) if error? or user?
|
|
|
|
|
|
|
|
# While multiple emails are being rolled out, check for the main email as
|
|
|
|
# well
|
|
|
|
@getUserByMainEmail email, projection, callback
|
|
|
|
|
2015-10-07 12:32:35 -04:00
|
|
|
getUsers: (user_ids, projection, callback = (error, users) ->) ->
|
|
|
|
try
|
|
|
|
user_ids = user_ids.map (u) -> ObjectId(u.toString())
|
|
|
|
catch error
|
|
|
|
return callback error
|
|
|
|
|
2017-03-16 06:59:18 -04:00
|
|
|
db.users.find { _id: { $in: user_ids} }, projection, callback
|
|
|
|
|
2017-10-12 10:08:48 -04:00
|
|
|
getUserOrUserStubById: (user_id, projection, callback = (error, user) ->) ->
|
2017-08-24 11:48:47 -04:00
|
|
|
try
|
|
|
|
query = _id: ObjectId(user_id.toString())
|
|
|
|
catch e
|
|
|
|
return callback(new Error(e))
|
|
|
|
db.users.findOne query, projection, (error, user) ->
|
|
|
|
return callback(error) if error?
|
2017-10-12 10:08:48 -04:00
|
|
|
return callback(null, user) if user?
|
|
|
|
db.userstubs.findOne query, projection, callback
|
2017-03-17 10:55:41 -04:00
|
|
|
|
2018-05-28 10:08:37 -04:00
|
|
|
# check for duplicate email address. This is also enforced at the DB level
|
|
|
|
ensureUniqueEmailAddress: (newEmail, callback) ->
|
|
|
|
@getUserByAnyEmail newEmail, (error, user) ->
|
2018-07-17 06:12:09 -04:00
|
|
|
return callback(new Errors.EmailExistsError('alread_exists')) if user?
|
2018-05-28 10:08:37 -04:00
|
|
|
callback(error)
|
|
|
|
|
2018-06-21 06:30:12 -04:00
|
|
|
decorateFullEmails = (defaultEmail, emailsData, affiliationsData) ->
|
|
|
|
emailsData.map (emailData) ->
|
|
|
|
emailData.default = emailData.email == defaultEmail
|
|
|
|
|
|
|
|
affiliation = affiliationsData.find (aff) -> aff.email == emailData.email
|
|
|
|
if affiliation?
|
|
|
|
{ institution, inferred, role, department } = affiliation
|
|
|
|
emailData.affiliation = { institution, inferred, role, department }
|
|
|
|
else
|
|
|
|
emailsData.affiliation = null
|
|
|
|
|
|
|
|
emailData
|
|
|
|
|
2017-03-17 10:55:41 -04:00
|
|
|
[
|
|
|
|
'getUser',
|
2018-05-25 07:04:09 -04:00
|
|
|
'getUserEmail',
|
2018-05-23 10:12:23 -04:00
|
|
|
'getUserByMainEmail',
|
2018-05-25 07:04:09 -04:00
|
|
|
'getUserByAnyEmail',
|
2017-08-24 11:48:47 -04:00
|
|
|
'getUsers',
|
2018-05-28 10:08:37 -04:00
|
|
|
'getUserOrUserStubById',
|
|
|
|
'ensureUniqueEmailAddress',
|
2017-03-17 10:55:41 -04:00
|
|
|
].map (method) ->
|
|
|
|
metrics.timeAsyncMethod UserGetter, method, 'mongo.UserGetter', logger
|