2014-02-12 05:23:40 -05:00
|
|
|
UserGetter = require "./UserGetter"
|
|
|
|
logger = require("logger-sharelatex")
|
2014-04-09 09:43:08 -04:00
|
|
|
UserDeleter = require("./UserDeleter")
|
2014-06-10 17:26:43 -04:00
|
|
|
UserUpdater = require("./UserUpdater")
|
|
|
|
sanitize = require('sanitizer')
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
module.exports = UserController =
|
|
|
|
getLoggedInUsersPersonalInfo: (req, res, next = (error) ->) ->
|
|
|
|
# this is funcky as hell, we don't use the current session to get the user
|
|
|
|
# we use the auth token, actually destroying session from the chat api request
|
2014-06-11 12:45:09 -04:00
|
|
|
if req.query?.auth_token?
|
|
|
|
req.session.destroy()
|
2014-02-12 05:23:40 -05:00
|
|
|
logger.log user: req.user, "reciving request for getting logged in users personal info"
|
|
|
|
return next(new Error("User is not logged in")) if !req.user?
|
2014-06-11 12:45:09 -04:00
|
|
|
UserGetter.getUser req.session.user._id, { first_name: true, last_name: true, role:true, institution:true }, (error, user) ->
|
|
|
|
UserController.sendFormattedPersonalInfo(user, res, next)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
getPersonalInfo: (req, res, next = (error) ->) ->
|
2014-06-11 12:45:09 -04:00
|
|
|
UserGetter.getUser req.params.user_id, { _id: true, first_name: true, last_name: true, email: true}, (error, user) ->
|
|
|
|
logger.log user_id: req.params.user_id, "reciving request for getting users personal info"
|
|
|
|
console.log user
|
2014-02-12 05:23:40 -05:00
|
|
|
return next(error) if error?
|
|
|
|
return res.send(404) if !user?
|
|
|
|
UserController.sendFormattedPersonalInfo(user, res, next)
|
|
|
|
req.session.destroy()
|
|
|
|
|
2014-06-10 17:26:43 -04:00
|
|
|
updatePersonalInfo: (req, res, next = (error)->) ->
|
2014-06-11 07:46:28 -04:00
|
|
|
{first_name, last_name, role, institution} = req.body
|
2014-06-11 10:14:03 -04:00
|
|
|
user_id = req.session.user._id
|
|
|
|
logger.log data:req.body, user_id:user_id, "getting update for user personal info"
|
2014-06-10 17:26:43 -04:00
|
|
|
update =
|
|
|
|
first_name:sanitize.escape(first_name)
|
|
|
|
last_name:sanitize.escape(last_name)
|
|
|
|
role:sanitize.escape(role)
|
2014-06-11 07:46:28 -04:00
|
|
|
institution:sanitize.escape(institution)
|
2014-06-11 10:14:03 -04:00
|
|
|
UserUpdater.updatePersonalInfo user_id, update, (err)->
|
2014-06-10 17:26:43 -04:00
|
|
|
if err?
|
|
|
|
res.send 500
|
|
|
|
else
|
|
|
|
res.send 204
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
sendFormattedPersonalInfo: (user, res, next = (error) ->) ->
|
|
|
|
UserController._formatPersonalInfo user, (error, info) ->
|
|
|
|
return next(error) if error?
|
2014-06-11 12:45:09 -04:00
|
|
|
console.log info
|
2014-02-12 05:23:40 -05:00
|
|
|
res.send JSON.stringify(info)
|
|
|
|
|
|
|
|
_formatPersonalInfo: (user, callback = (error, info) ->) ->
|
|
|
|
callback null, {
|
|
|
|
id: user._id.toString()
|
|
|
|
first_name: user.first_name
|
|
|
|
last_name: user.last_name
|
|
|
|
email: user.email
|
|
|
|
signUpDate: user.signUpDate
|
2014-06-11 12:45:09 -04:00
|
|
|
role: user.role
|
|
|
|
institution: user.institution
|
2014-02-12 05:23:40 -05:00
|
|
|
}
|
|
|
|
|
2014-04-09 09:43:08 -04:00
|
|
|
|
|
|
|
|