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) ->
@ -17,7 +18,7 @@ module.exports = CollaboratorsController =
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")
@ -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?
@ -100,8 +102,7 @@ module.exports = CollaboratorsHandler =
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) ->

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