2018-10-11 13:56:14 -04:00
|
|
|
ObjectId = require('mongoose').Types.ObjectId
|
2018-09-25 09:10:06 -04:00
|
|
|
async = require("async")
|
|
|
|
Errors = require('../Errors/Errors')
|
2018-10-11 13:56:14 -04:00
|
|
|
EntityModels =
|
|
|
|
Institution: require('../../models/Institution').Institution
|
|
|
|
Subscription: require('../../models/Subscription').Subscription
|
2018-09-25 09:10:06 -04:00
|
|
|
UserMembershipViewModel = require('./UserMembershipViewModel')
|
|
|
|
UserGetter = require('../User/UserGetter')
|
|
|
|
logger = require('logger-sharelatex')
|
|
|
|
|
|
|
|
module.exports =
|
2018-10-11 13:56:14 -04:00
|
|
|
getEntity: (entityId, entityConfig, loggedInUser, callback = (error, entity) ->) ->
|
2018-10-24 09:50:34 -04:00
|
|
|
entityId = ObjectId(entityId) if ObjectId.isValid(entityId.toString())
|
2018-10-11 13:56:14 -04:00
|
|
|
query = Object.assign({}, entityConfig.baseQuery)
|
2018-10-24 09:50:34 -04:00
|
|
|
query[entityConfig.fields.primaryKey] = entityId
|
2018-10-11 13:56:14 -04:00
|
|
|
unless loggedInUser.isAdmin
|
|
|
|
query[entityConfig.fields.access] = ObjectId(loggedInUser._id)
|
|
|
|
EntityModels[entityConfig.modelName].findOne query, callback
|
|
|
|
|
|
|
|
getUsers: (entity, entityConfig, callback = (error, users) ->) ->
|
|
|
|
attributes = entityConfig.fields.read
|
2018-09-25 09:10:06 -04:00
|
|
|
getPopulatedListOfMembers(entity, attributes, callback)
|
|
|
|
|
2018-10-11 13:56:14 -04:00
|
|
|
addUser: (entity, entityConfig, email, callback = (error, user) ->) ->
|
|
|
|
attribute = entityConfig.fields.write
|
2018-09-25 09:10:06 -04:00
|
|
|
UserGetter.getUserByAnyEmail email, (error, user) ->
|
|
|
|
return callback(error) if error?
|
2018-10-30 10:17:53 -04:00
|
|
|
unless user
|
|
|
|
return callback(userNotFound: true)
|
|
|
|
if entity[attribute].some((managerId) -> managerId.equals(user._id))
|
|
|
|
return callback(alreadyAdded: true)
|
|
|
|
|
2018-09-25 09:10:06 -04:00
|
|
|
addUserToEntity entity, attribute, user, (error) ->
|
|
|
|
callback(error, UserMembershipViewModel.build(user))
|
|
|
|
|
2018-10-11 13:56:14 -04:00
|
|
|
removeUser: (entity, entityConfig, userId, callback = (error) ->) ->
|
|
|
|
attribute = entityConfig.fields.write
|
2018-10-30 10:17:53 -04:00
|
|
|
if entity.admin_id?.equals(userId)
|
|
|
|
return callback(isAdmin: true)
|
2018-09-25 09:10:06 -04:00
|
|
|
removeUserFromEntity entity, attribute, userId, callback
|
|
|
|
|
|
|
|
getPopulatedListOfMembers = (entity, attributes, callback = (error, users)->)->
|
|
|
|
userObjects = []
|
|
|
|
|
|
|
|
for attribute in attributes
|
|
|
|
for userObject in entity[attribute] or []
|
|
|
|
# userObject can be an email as String, a user id as ObjectId or an
|
|
|
|
# invite as Object with an email attribute as String. We want to pass to
|
|
|
|
# UserMembershipViewModel either an email as (String) or a user id (ObjectId)
|
|
|
|
userIdOrEmail = userObject.email || userObject
|
|
|
|
userObjects.push userIdOrEmail
|
|
|
|
|
|
|
|
async.map userObjects, UserMembershipViewModel.buildAsync, callback
|
|
|
|
|
|
|
|
addUserToEntity = (entity, attribute, user, callback = (error)->) ->
|
|
|
|
fieldUpdate = {}
|
|
|
|
fieldUpdate[attribute] = user._id
|
|
|
|
entity.update { $addToSet: fieldUpdate }, callback
|
|
|
|
|
|
|
|
removeUserFromEntity = (entity, attribute, userId, callback = (error)->) ->
|
|
|
|
fieldUpdate = {}
|
|
|
|
fieldUpdate[attribute] = userId
|
|
|
|
entity.update { $pull: fieldUpdate }, callback
|