overleaf/services/web/app/coffee/Features/User/UserController.coffee

132 lines
4.3 KiB
CoffeeScript
Raw Normal View History

UserDeleter = require("./UserDeleter")
2014-04-09 15:59:28 +00:00
UserLocator = require("./UserLocator")
User = require("../../models/User").User
newsLetterManager = require('../Newsletter/NewsletterManager')
UserRegistrationHandler = require("./UserRegistrationHandler")
logger = require("logger-sharelatex")
2014-04-09 15:59:28 +00:00
metrics = require("../../infrastructure/Metrics")
Url = require("url")
AuthenticationController = require("../Authentication/AuthenticationController")
AuthenticationManager = require("../Authentication/AuthenticationManager")
ReferalAllocator = require("../Referal/ReferalAllocator")
2014-05-16 16:45:48 +00:00
UserUpdater = require("./UserUpdater")
module.exports =
deleteUser: (req, res)->
user_id = req.session.user._id
UserDeleter.deleteUser user_id, (err)->
if !err?
req.session.destroy()
res.send(200)
unsubscribe: (req, res)->
UserLocator.findById req.session.user._id, (err, user)->
newsLetterManager.unsubscribe user, ->
res.send()
updateUserSettings : (req, res)->
logger.log user: req.session.user, "updating account settings"
2014-05-19 10:50:32 +00:00
user_id = req.session.user._id
User.findById user_id, (err, user)->
if err? or !user?
2014-05-19 10:50:32 +00:00
logger.err err:err, user_id:user_id, "problem updaing user settings"
return res.send 500
2014-06-20 08:42:43 +00:00
if req.body.first_name?
user.first_name = req.body.first_name.trim()
if req.body.last_name?
user.last_name = req.body.last_name.trim()
2014-06-20 10:15:25 +00:00
if req.body.role?
user.role = req.body.role.trim()
if req.body.institution?
user.institution = req.body.institution.trim()
2014-06-20 08:42:43 +00:00
if req.body.mode?
user.ace.mode = req.body.mode
if req.body.theme?
user.ace.theme = req.body.theme
if req.body.fontSize?
user.ace.fontSize = req.body.fontSize
if req.body.autoComplete?
2014-06-24 20:09:20 +00:00
user.ace.autoComplete = req.body.autoComplete
2014-06-20 08:42:43 +00:00
if req.body.spellCheckLanguage?
user.ace.spellCheckLanguage = req.body.spellCheckLanguage
if req.body.pdfViewer?
user.ace.pdfViewer = req.body.pdfViewer
2014-05-19 10:50:32 +00:00
user.save (err)->
2014-06-20 08:42:43 +00:00
newEmail = req.body.email?.trim()
if !newEmail? or newEmail == user.email
2014-05-19 10:50:32 +00:00
return res.send 200
2014-06-20 08:42:43 +00:00
else if newEmail.indexOf("@") == -1
return res.send(400)
else
2014-05-19 10:50:32 +00:00
UserUpdater.changeEmailAddress user_id, newEmail, (err)->
if err?
logger.err err:err, user_id:user_id, newEmail:newEmail, "problem updaing users email address"
return res.send 500, {message:err?.message}
res.send(200)
2014-04-09 15:59:28 +00:00
logout : (req, res)->
metrics.inc "user.logout"
logger.log user: req?.session?.user, "logging out"
req.session.destroy (err)->
if err
logger.err err: err, 'error destorying session'
res.redirect '/login'
register : (req, res, next = (error) ->)->
logger.log email: req.body.email, "attempted register"
redir = Url.parse(req.body.redir or "/project").path
UserRegistrationHandler.registerNewUser req.body, (err, user)->
if err == "EmailAlreadyRegisterd"
return AuthenticationController.login req, res
else if err?
next(err)
else
metrics.inc "user.register.success"
req.session.user = user
req.session.justRegistered = true
ReferalAllocator.allocate req.session.referal_id, user._id, req.session.referal_source, req.session.referal_medium
res.send
redir:redir
id:user._id.toString()
first_name: user.first_name
last_name: user.last_name
email: user.email
created: Date.now()
changePassword : (req, res, next = (error) ->)->
metrics.inc "user.password-change"
oldPass = req.body.currentPassword
AuthenticationManager.authenticate {_id:req.session.user._id}, oldPass, (err, user)->
return next(err) if err?
if(user)
logger.log user: req.session.user, "changing password"
newPassword1 = req.body.newPassword1
newPassword2 = req.body.newPassword2
if newPassword1 != newPassword2
logger.log user: user, "passwords do not match"
res.send
message:
type:'error'
text:'Your passwords do not match'
else
logger.log user: user, "password changed"
AuthenticationManager.setUserPassword user._id, newPassword1, (error) ->
return next(error) if error?
res.send
message:
type:'success'
text:'Your password has been changed'
else
logger.log user: user, "current password wrong"
res.send
message:
type:'error'
2014-05-16 16:45:48 +00:00
text:'Your old password is wrong'
changeEmailAddress: (req, res)->