Move email parsing code to Helpers/EmailHelpers

This commit is contained in:
Shane Kilkelly 2016-08-02 14:30:42 +01:00
parent abbd059eae
commit 13fe000176
5 changed files with 34 additions and 22 deletions

View file

@ -4,7 +4,8 @@ ProjectEditorHandler = require "../Project/ProjectEditorHandler"
EditorRealTimeController = require "../Editor/EditorRealTimeController" EditorRealTimeController = require "../Editor/EditorRealTimeController"
LimitationsManager = require "../Subscription/LimitationsManager" LimitationsManager = require "../Subscription/LimitationsManager"
UserGetter = require "../User/UserGetter" UserGetter = require "../User/UserGetter"
mimelib = require("mimelib") EmailHelpers = require "../Helpers/EmailHelpers"
module.exports = CollaboratorsController = module.exports = CollaboratorsController =
addUserToProject: (req, res, next) -> addUserToProject: (req, res, next) ->
@ -16,11 +17,11 @@ module.exports = CollaboratorsController =
return res.json { user: false } return res.json { user: false }
else else
{email, privileges} = req.body {email, privileges} = req.body
email = mimelib.parseAddresses(email or "")[0]?.address?.toLowerCase() email = EmailHelpers.parseEmail(email)
if !email? or email == "" if !email? or email == ""
return res.status(400).send("invalid email address") return res.status(400).send("invalid email address")
adding_user_id = req.session?.user?._id adding_user_id = req.session?.user?._id
CollaboratorsHandler.addEmailToProject project_id, adding_user_id, email, privileges, (error, user_id) => CollaboratorsHandler.addEmailToProject project_id, adding_user_id, email, privileges, (error, user_id) =>
return next(error) if error? return next(error) if error?
@ -36,7 +37,7 @@ module.exports = CollaboratorsController =
CollaboratorsController._removeUserIdFromProject project_id, user_id, (error) -> CollaboratorsController._removeUserIdFromProject project_id, user_id, (error) ->
return next(error) if error? return next(error) if error?
res.sendStatus 204 res.sendStatus 204
removeSelfFromProject: (req, res, next = (error) ->) -> removeSelfFromProject: (req, res, next = (error) ->) ->
project_id = req.params.Project_id project_id = req.params.Project_id
user_id = req.session?.user?._id user_id = req.session?.user?._id
@ -49,4 +50,3 @@ module.exports = CollaboratorsController =
return callback(error) if error? return callback(error) if error?
EditorRealTimeController.emitToRoom(project_id, 'userRemovedFromProject', user_id) EditorRealTimeController.emitToRoom(project_id, 'userRemovedFromProject', user_id)
callback() callback()

View file

@ -1,6 +1,5 @@
UserCreator = require('../User/UserCreator') UserCreator = require('../User/UserCreator')
Project = require("../../models/Project").Project Project = require("../../models/Project").Project
mimelib = require("mimelib")
logger = require('logger-sharelatex') logger = require('logger-sharelatex')
UserGetter = require "../User/UserGetter" UserGetter = require "../User/UserGetter"
ContactManager = require "../Contacts/ContactManager" ContactManager = require "../Contacts/ContactManager"
@ -8,8 +7,11 @@ CollaboratorsEmailHandler = require "./CollaboratorsEmailHandler"
async = require "async" async = require "async"
PrivilegeLevels = require "../Authorization/PrivilegeLevels" PrivilegeLevels = require "../Authorization/PrivilegeLevels"
Errors = require "../Errors/Errors" Errors = require "../Errors/Errors"
EmailHelpers = require "../Helpers/EmailHelpers"
module.exports = CollaboratorsHandler = module.exports = CollaboratorsHandler =
getMemberIdsWithPrivilegeLevels: (project_id, callback = (error, members) ->) -> getMemberIdsWithPrivilegeLevels: (project_id, callback = (error, members) ->) ->
Project.findOne { _id: project_id }, { owner_ref: 1, collaberator_refs: 1, readOnly_refs: 1 }, (error, project) -> Project.findOne { _id: project_id }, { owner_ref: 1, collaberator_refs: 1, readOnly_refs: 1 }, (error, project) ->
return callback(error) if error? return callback(error) if error?
@ -21,12 +23,12 @@ module.exports = CollaboratorsHandler =
for member_id in project.collaberator_refs or [] for member_id in project.collaberator_refs or []
members.push { id: member_id.toString(), privilegeLevel: PrivilegeLevels.READ_AND_WRITE } members.push { id: member_id.toString(), privilegeLevel: PrivilegeLevels.READ_AND_WRITE }
return callback null, members return callback null, members
getMemberIds: (project_id, callback = (error, member_ids) ->) -> getMemberIds: (project_id, callback = (error, member_ids) ->) ->
CollaboratorsHandler.getMemberIdsWithPrivilegeLevels project_id, (error, members) -> CollaboratorsHandler.getMemberIdsWithPrivilegeLevels project_id, (error, members) ->
return callback(error) if error? return callback(error) if error?
return callback null, members.map (m) -> m.id return callback null, members.map (m) -> m.id
getMembersWithPrivilegeLevels: (project_id, callback = (error, members) ->) -> getMembersWithPrivilegeLevels: (project_id, callback = (error, members) ->) ->
CollaboratorsHandler.getMemberIdsWithPrivilegeLevels project_id, (error, members = []) -> CollaboratorsHandler.getMemberIdsWithPrivilegeLevels project_id, (error, members = []) ->
return callback(error) if error? return callback(error) if error?
@ -41,7 +43,7 @@ module.exports = CollaboratorsHandler =
(error) -> (error) ->
return callback(error) if error? return callback(error) if error?
callback null, result callback null, result
getMemberIdPrivilegeLevel: (user_id, project_id, callback = (error, privilegeLevel) ->) -> getMemberIdPrivilegeLevel: (user_id, project_id, callback = (error, privilegeLevel) ->) ->
# In future if the schema changes and getting all member ids is more expensive (multiple documents) # In future if the schema changes and getting all member ids is more expensive (multiple documents)
# then optimise this. # then optimise this.
@ -51,12 +53,12 @@ module.exports = CollaboratorsHandler =
if member.id == user_id?.toString() if member.id == user_id?.toString()
return callback null, member.privilegeLevel return callback null, member.privilegeLevel
return callback null, PrivilegeLevels.NONE return callback null, PrivilegeLevels.NONE
getMemberCount: (project_id, callback = (error, count) ->) -> getMemberCount: (project_id, callback = (error, count) ->) ->
CollaboratorsHandler.getMemberIdsWithPrivilegeLevels project_id, (error, members) -> CollaboratorsHandler.getMemberIdsWithPrivilegeLevels project_id, (error, members) ->
return callback(error) if error? return callback(error) if error?
return callback null, (members or []).length return callback null, (members or []).length
getCollaboratorCount: (project_id, callback = (error, count) ->) -> getCollaboratorCount: (project_id, callback = (error, count) ->) ->
CollaboratorsHandler.getMemberCount project_id, (error, count) -> CollaboratorsHandler.getMemberCount project_id, (error, count) ->
return callback(error) if error? return callback(error) if error?
@ -69,14 +71,14 @@ module.exports = CollaboratorsHandler =
if member.id.toString() == user_id.toString() if member.id.toString() == user_id.toString()
return callback null, true, member.privilegeLevel return callback null, true, member.privilegeLevel
return callback null, false, null return callback null, false, null
getProjectsUserIsCollaboratorOf: (user_id, fields, callback = (error, readAndWriteProjects, readOnlyProjects) ->) -> getProjectsUserIsCollaboratorOf: (user_id, fields, callback = (error, readAndWriteProjects, readOnlyProjects) ->) ->
Project.find {collaberator_refs:user_id}, fields, (err, readAndWriteProjects)=> Project.find {collaberator_refs:user_id}, fields, (err, readAndWriteProjects)=>
return callback(err) if err? return callback(err) if err?
Project.find {readOnly_refs:user_id}, fields, (err, readOnlyProjects)=> Project.find {readOnly_refs:user_id}, fields, (err, readOnlyProjects)=>
return callback(err) if err? return callback(err) if err?
callback(null, readAndWriteProjects, readOnlyProjects) callback(null, readAndWriteProjects, readOnlyProjects)
removeUserFromProject: (project_id, user_id, callback = (error) ->)-> removeUserFromProject: (project_id, user_id, callback = (error) ->)->
logger.log user_id: user_id, project_id: project_id, "removing user" logger.log user_id: user_id, project_id: project_id, "removing user"
conditions = _id:project_id conditions = _id:project_id
@ -86,7 +88,7 @@ module.exports = CollaboratorsHandler =
if err? if err?
logger.error err: err, "problem removing user from project collaberators" logger.error err: err, "problem removing user from project collaberators"
callback(err) callback(err)
removeUserFromAllProjets: (user_id, callback = (error) ->) -> removeUserFromAllProjets: (user_id, callback = (error) ->) ->
CollaboratorsHandler.getProjectsUserIsCollaboratorOf user_id, { _id: 1 }, (error, readAndWriteProjects = [], readOnlyProjects = []) -> CollaboratorsHandler.getProjectsUserIsCollaboratorOf user_id, { _id: 1 }, (error, readAndWriteProjects = [], readOnlyProjects = []) ->
return callback(error) if error? return callback(error) if error?
@ -98,10 +100,9 @@ module.exports = CollaboratorsHandler =
return cb() if !project? return cb() if !project?
CollaboratorsHandler.removeUserFromProject project._id, user_id, cb CollaboratorsHandler.removeUserFromProject project._id, user_id, cb
async.series jobs, callback async.series jobs, callback
addEmailToProject: (project_id, adding_user_id, unparsed_email, privilegeLevel, callback = (error, user) ->) -> addEmailToProject: (project_id, adding_user_id, unparsed_email, privilegeLevel, callback = (error, user) ->) ->
emails = mimelib.parseAddresses(unparsed_email) email = EmailHelpers.parseEmail(unparsed_email)
email = emails[0]?.address?.toLowerCase()
if !email? or email == "" if !email? or email == ""
return callback(new Error("no valid email provided: '#{unparsed_email}'")) return callback(new Error("no valid email provided: '#{unparsed_email}'"))
UserCreator.getUserOrCreateHoldingAccount email, (error, user) -> UserCreator.getUserOrCreateHoldingAccount email, (error, user) ->
@ -118,7 +119,7 @@ module.exports = CollaboratorsHandler =
existing_users = existing_users.map (u) -> u.toString() existing_users = existing_users.map (u) -> u.toString()
if existing_users.indexOf(user_id.toString()) > -1 if existing_users.indexOf(user_id.toString()) > -1
return callback null # User already in Project return callback null # User already in Project
if privilegeLevel == PrivilegeLevels.READ_AND_WRITE if privilegeLevel == PrivilegeLevels.READ_AND_WRITE
level = {"collaberator_refs":user_id} level = {"collaberator_refs":user_id}
logger.log {privileges: "readAndWrite", user_id, project_id}, "adding user" logger.log {privileges: "readAndWrite", user_id, project_id}, "adding user"

View file

@ -3,8 +3,9 @@ LimitationsManager = require "../Subscription/LimitationsManager"
UserGetter = require "../User/UserGetter" UserGetter = require "../User/UserGetter"
Project = require("../../models/Project").Project Project = require("../../models/Project").Project
CollaboratorsInviteHandler = require('./CollaboratorsInviteHandler') CollaboratorsInviteHandler = require('./CollaboratorsInviteHandler')
mimelib = require("mimelib")
logger = require('logger-sharelatex') logger = require('logger-sharelatex')
EmailHelpers = require "../Helpers/EmailHelpers"
module.exports = CollaboratorsInviteController = module.exports = CollaboratorsInviteController =
@ -28,7 +29,7 @@ module.exports = CollaboratorsInviteController =
logger.log {projectId, email, sendingUserId}, "not allowed to invite more users to project" logger.log {projectId, email, sendingUserId}, "not allowed to invite more users to project"
return res.json {invite: null} return res.json {invite: null}
{email, privileges} = req.body {email, privileges} = req.body
email = mimelib.parseAddresses(email or "")[0]?.address?.toLowerCase() email = EmailHelpers.parseEmail(email)
if !email? or email == "" if !email? or email == ""
logger.log {projectId, email, sendingUserId}, "invalid email address" logger.log {projectId, email, sendingUserId}, "invalid email address"
return res.sendStatus(400) return res.sendStatus(400)

View file

@ -1,5 +1,4 @@
ProjectInvite = require("../../models/ProjectInvite").ProjectInvite ProjectInvite = require("../../models/ProjectInvite").ProjectInvite
mimelib = require("mimelib")
logger = require('logger-sharelatex') logger = require('logger-sharelatex')
CollaboratorsEmailHandler = require "./CollaboratorsEmailHandler" CollaboratorsEmailHandler = require "./CollaboratorsEmailHandler"
CollaboratorsHandler = require "./CollaboratorsHandler" CollaboratorsHandler = require "./CollaboratorsHandler"

View file

@ -0,0 +1,11 @@
mimelib = require("mimelib")
module.exports = EmailHelpers =
parseEmail: (email) ->
email = mimelib.parseAddresses(email or "")[0]?.address?.toLowerCase()
if !email? or email == ""
return null
else
return email