2014-04-12 13:51:02 -04:00
|
|
|
schema = require '../../app/schemas/models/user'
|
2014-02-17 18:19:19 -05:00
|
|
|
crypto = require 'crypto'
|
|
|
|
request = require 'request'
|
|
|
|
User = require './User'
|
|
|
|
Handler = require '../commons/Handler'
|
2014-01-03 13:32:13 -05:00
|
|
|
mongoose = require 'mongoose'
|
2014-01-03 17:28:00 -05:00
|
|
|
config = require '../../server_config'
|
2014-02-17 18:19:19 -05:00
|
|
|
errors = require '../commons/errors'
|
|
|
|
async = require 'async'
|
2014-03-31 16:56:13 -04:00
|
|
|
log = require 'winston'
|
2014-03-31 18:48:22 -04:00
|
|
|
LevelSession = require('../levels/sessions/LevelSession')
|
2014-04-09 19:46:44 -04:00
|
|
|
LevelSessionHandler = require '../levels/sessions/level_session_handler'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
serverProperties = ['passwordHash', 'emailLower', 'nameLower', 'passwordReset']
|
2014-03-23 12:30:01 -04:00
|
|
|
privateProperties = [
|
|
|
|
'permissions', 'email', 'firstName', 'lastName', 'gender', 'facebookID',
|
|
|
|
'gplusID', 'music', 'volume', 'aceConfig'
|
|
|
|
]
|
2014-04-07 18:21:05 -04:00
|
|
|
candidateProperties = [
|
2014-04-07 20:58:02 -04:00
|
|
|
'jobProfile', 'jobProfileApproved', 'jobProfileNotes'
|
2014-03-23 12:30:01 -04:00
|
|
|
]
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
UserHandler = class UserHandler extends Handler
|
|
|
|
modelClass: User
|
|
|
|
|
|
|
|
editableProperties: [
|
|
|
|
'name', 'photoURL', 'password', 'anonymous', 'wizardColor1', 'volume',
|
2014-03-23 12:30:01 -04:00
|
|
|
'firstName', 'lastName', 'gender', 'facebookID', 'gplusID', 'emailSubscriptions',
|
2014-01-12 14:54:50 -05:00
|
|
|
'testGroupNumber', 'music', 'hourOfCode', 'hourOfCodeComplete', 'preferredLanguage',
|
2014-04-05 20:05:03 -04:00
|
|
|
'wizard', 'aceConfig', 'autocastDelay', 'lastLevel', 'jobProfile'
|
2014-01-03 13:32:13 -05:00
|
|
|
]
|
|
|
|
|
|
|
|
jsonSchema: schema
|
2014-02-01 11:22:26 -05:00
|
|
|
|
2014-01-03 17:28:00 -05:00
|
|
|
constructor: ->
|
|
|
|
super(arguments...)
|
|
|
|
@editableProperties.push('permissions') unless config.isProduction
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-04-07 20:58:02 -04:00
|
|
|
getEditableProperties: (req, document) ->
|
|
|
|
props = super req, document
|
|
|
|
props.push 'jobProfileApproved', 'jobProfileNotes' if req.user.isAdmin()
|
|
|
|
props
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
formatEntity: (req, document) ->
|
|
|
|
return null unless document?
|
|
|
|
obj = document.toObject()
|
|
|
|
delete obj[prop] for prop in serverProperties
|
2014-04-07 18:21:05 -04:00
|
|
|
includePrivates = req.user and (req.user.isAdmin() or req.user._id.equals(document._id))
|
2014-01-03 13:32:13 -05:00
|
|
|
delete obj[prop] for prop in privateProperties unless includePrivates
|
2014-04-07 20:58:02 -04:00
|
|
|
includeCandidate = includePrivates or (obj.jobProfileApproved and req.user and ('employer' in (req.user.permissions ? [])))
|
2014-04-07 18:21:05 -04:00
|
|
|
delete obj[prop] for prop in candidateProperties unless includeCandidate
|
2014-01-03 13:32:13 -05:00
|
|
|
return obj
|
|
|
|
|
|
|
|
waterfallFunctions: [
|
|
|
|
# FB access token checking
|
|
|
|
# Check the email is the same as FB reports
|
|
|
|
(req, user, callback) ->
|
|
|
|
fbID = req.query.facebookID
|
|
|
|
fbAT = req.query.facebookAccessToken
|
|
|
|
return callback(null, req, user) unless fbID and fbAT
|
|
|
|
url = "https://graph.facebook.com/me?access_token=#{fbAT}"
|
2014-03-31 16:56:13 -04:00
|
|
|
request(url, (err, response, body) ->
|
|
|
|
log.warn "Error grabbing FB token: #{err}" if err
|
2014-01-03 13:32:13 -05:00
|
|
|
body = JSON.parse(body)
|
|
|
|
emailsMatch = req.body.email is body.email
|
|
|
|
return callback(res:'Invalid Facebook Access Token.', code:422) unless emailsMatch
|
|
|
|
callback(null, req, user)
|
|
|
|
)
|
|
|
|
|
|
|
|
# GPlus access token checking
|
|
|
|
(req, user, callback) ->
|
|
|
|
gpID = req.query.gplusID
|
|
|
|
gpAT = req.query.gplusAccessToken
|
|
|
|
return callback(null, req, user) unless gpID and gpAT
|
|
|
|
url = "https://www.googleapis.com/oauth2/v2/userinfo?access_token=#{gpAT}"
|
2014-03-31 16:56:13 -04:00
|
|
|
request(url, (err, response, body) ->
|
|
|
|
log.warn "Error grabbing G+ token: #{err}" if err
|
2014-01-03 13:32:13 -05:00
|
|
|
body = JSON.parse(body)
|
|
|
|
emailsMatch = req.body.email is body.email
|
|
|
|
return callback(res:'Invalid G+ Access Token.', code:422) unless emailsMatch
|
|
|
|
callback(null, req, user)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Email setting
|
|
|
|
(req, user, callback) ->
|
|
|
|
return callback(null, req, user) unless req.body.email?
|
|
|
|
emailLower = req.body.email.toLowerCase()
|
|
|
|
return callback(null, req, user) if emailLower is user.get('emailLower')
|
|
|
|
User.findOne({emailLower:emailLower}).exec (err, otherUser) ->
|
2014-03-31 16:56:13 -04:00
|
|
|
log.error "Database error setting user email: #{err}" if err
|
2014-01-03 13:32:13 -05:00
|
|
|
return callback(res:'Database error.', code:500) if err
|
|
|
|
|
|
|
|
if (req.query.gplusID or req.query.facebookID) and otherUser
|
|
|
|
# special case, log in as that user
|
|
|
|
return req.logIn(otherUser, (err) ->
|
|
|
|
return callback(res:'Facebook user login error.', code:500) if err
|
|
|
|
return callback(null, req, otherUser)
|
|
|
|
)
|
|
|
|
r = {message:'is already used by another account', property:'email'}
|
|
|
|
return callback({res:r, code:409}) if otherUser
|
|
|
|
user.set('email', req.body.email)
|
|
|
|
callback(null, req, user)
|
|
|
|
|
|
|
|
# Name setting
|
|
|
|
(req, user, callback) ->
|
|
|
|
return callback(null, req, user) unless req.body.name
|
|
|
|
nameLower = req.body.name?.toLowerCase()
|
|
|
|
return callback(null, req, user) if nameLower is user.get('nameLower')
|
|
|
|
User.findOne({nameLower:nameLower}).exec (err, otherUser) ->
|
2014-03-31 16:56:13 -04:00
|
|
|
log.error "Database error setting user name: #{err}" if err
|
2014-01-03 13:32:13 -05:00
|
|
|
return callback(res:'Database error.', code:500) if err
|
|
|
|
r = {message:'is already used by another account', property:'name'}
|
|
|
|
return callback({res:r, code:409}) if otherUser
|
|
|
|
user.set('name', req.body.name)
|
|
|
|
callback(null, req, user)
|
|
|
|
]
|
|
|
|
|
|
|
|
getById: (req, res, id) ->
|
2014-02-24 23:27:38 -05:00
|
|
|
if req.user?._id.equals(id)
|
2014-04-09 19:46:44 -04:00
|
|
|
return @sendSuccess(res, @formatEntity(req, req.user, 256))
|
2014-01-03 13:32:13 -05:00
|
|
|
super(req, res, id)
|
2014-03-20 18:40:02 -04:00
|
|
|
|
2014-02-17 18:19:19 -05:00
|
|
|
getNamesByIds: (req, res) ->
|
|
|
|
ids = req.query.ids or req.body.ids
|
|
|
|
ids = ids.split(',') if _.isString ids
|
|
|
|
ids = _.uniq ids
|
2014-03-20 18:40:02 -04:00
|
|
|
|
2014-03-03 13:21:51 -05:00
|
|
|
# TODO: Extend and repurpose this handler to return other public info about a user more flexibly,
|
|
|
|
# say by a query parameter that lists public properties to return.
|
|
|
|
returnWizard = req.query.wizard or req.body.wizard
|
|
|
|
query = if returnWizard then {name:1, wizard:1} else {name:1}
|
2014-03-20 18:40:02 -04:00
|
|
|
|
2014-02-17 18:19:19 -05:00
|
|
|
makeFunc = (id) ->
|
|
|
|
(callback) ->
|
2014-03-03 13:21:51 -05:00
|
|
|
User.findById(id, query).exec (err, document) ->
|
2014-02-17 18:19:19 -05:00
|
|
|
return done(err) if err
|
2014-03-03 13:21:51 -05:00
|
|
|
if document and returnWizard
|
|
|
|
callback(null, {name:document.get('name'), wizard:document.get('wizard') or {}})
|
|
|
|
else
|
|
|
|
callback(null, document?.get('name') or '')
|
2014-03-20 18:40:02 -04:00
|
|
|
|
2014-02-17 18:19:19 -05:00
|
|
|
funcs = {}
|
|
|
|
for id in ids
|
|
|
|
return errors.badInput(res, "Given an invalid id: #{id}") unless Handler.isID(id)
|
|
|
|
funcs[id] = makeFunc(id)
|
2014-03-20 18:40:02 -04:00
|
|
|
|
2014-02-17 18:19:19 -05:00
|
|
|
async.parallel funcs, (err, results) ->
|
|
|
|
return errors.serverError err if err
|
|
|
|
res.send results
|
|
|
|
res.end()
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-02-27 17:07:11 -05:00
|
|
|
nameToID: (req, res, name) ->
|
2014-02-27 17:42:11 -05:00
|
|
|
User.findOne({nameLower:name.toLowerCase()}).exec (err, otherUser) ->
|
|
|
|
res.send(if otherUser then otherUser._id else JSON.stringify(''))
|
2014-02-27 17:07:11 -05:00
|
|
|
res.end()
|
|
|
|
|
2014-04-12 17:13:26 -04:00
|
|
|
getSimulatorLeaderboard: (req, res) ->
|
2014-04-14 17:52:21 -04:00
|
|
|
queryParameters = @getSimulatorLeaderboardQueryParameters(req)
|
|
|
|
leaderboardQuery = User.find(queryParameters.query).select("name simulatedBy simulatedFor").sort({"simulatedBy":queryParameters.sortOrder}).limit(queryParameters.limit)
|
|
|
|
leaderboardQuery.exec (err, otherUsers) ->
|
|
|
|
otherUsers = _.reject otherUsers, _id: req.user._id if req.query.scoreOffset isnt -1
|
|
|
|
otherUsers ?= []
|
|
|
|
res.send(otherUsers)
|
|
|
|
res.end()
|
|
|
|
|
|
|
|
getMySimulatorLeaderboardRank: (req, res) ->
|
|
|
|
req.query.order = 1
|
|
|
|
queryParameters = @getSimulatorLeaderboardQueryParameters(req)
|
|
|
|
User.count queryParameters.query, (err, count) =>
|
|
|
|
return @sendDatabaseError(res, err) if err
|
|
|
|
res.send JSON.stringify(count + 1)
|
|
|
|
|
|
|
|
getSimulatorLeaderboardQueryParameters: (req) ->
|
2014-04-12 17:13:26 -04:00
|
|
|
@validateSimulateLeaderboardRequestParameters(req)
|
2014-04-15 13:45:54 -04:00
|
|
|
|
2014-04-12 17:13:26 -04:00
|
|
|
query = {}
|
2014-04-14 11:46:46 -04:00
|
|
|
sortOrder = -1
|
|
|
|
limit = if req.query.limit > 30 then 30 else req.query.limit
|
2014-04-13 18:28:16 -04:00
|
|
|
if req.query.scoreOffset isnt -1
|
2014-04-12 17:13:26 -04:00
|
|
|
simulatedByQuery = {}
|
2014-04-13 18:28:16 -04:00
|
|
|
simulatedByQuery[if req.query.order is 1 then "$gt" else "$lte"] = req.query.scoreOffset
|
2014-04-12 17:13:26 -04:00
|
|
|
query.simulatedBy = simulatedByQuery
|
2014-04-14 11:46:46 -04:00
|
|
|
sortOrder = 1 if req.query.order is 1
|
2014-04-14 14:39:30 -04:00
|
|
|
else
|
|
|
|
query.simulatedBy = {"$exists": true}
|
2014-04-14 17:52:21 -04:00
|
|
|
{query: query, sortOrder: sortOrder, limit: limit}
|
2014-04-12 17:13:26 -04:00
|
|
|
|
|
|
|
validateSimulateLeaderboardRequestParameters: (req) ->
|
|
|
|
req.query.order = parseInt(req.query.order) ? -1
|
|
|
|
req.query.scoreOffset = parseFloat(req.query.scoreOffset) ? 100000
|
|
|
|
req.query.limit = parseInt(req.query.limit) ? 20
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
post: (req, res) ->
|
|
|
|
return @sendBadInputError(res, 'No input.') if _.isEmpty(req.body)
|
2014-02-24 23:27:38 -05:00
|
|
|
return @sendBadInputError(res, 'Must have an anonymous user to post with.') unless req.user
|
2014-01-03 13:32:13 -05:00
|
|
|
return @sendBadInputError(res, 'Existing users cannot create new ones.') unless req.user.get('anonymous')
|
|
|
|
req.body._id = req.user._id if req.user.get('anonymous')
|
|
|
|
@put(req, res)
|
|
|
|
|
|
|
|
hasAccessToDocument: (req, document) ->
|
|
|
|
if req.route.method in ['put', 'post', 'patch']
|
2014-02-24 23:27:38 -05:00
|
|
|
return true if req.user?.isAdmin()
|
|
|
|
return req.user?._id.equals(document._id)
|
2014-01-03 13:32:13 -05:00
|
|
|
return true
|
|
|
|
|
|
|
|
getByRelationship: (req, res, args...) ->
|
|
|
|
return @agreeToCLA(req, res) if args[1] is 'agreeToCLA'
|
2014-02-07 19:11:07 -05:00
|
|
|
return @avatar(req, res, args[0]) if args[1] is 'avatar'
|
2014-02-17 18:19:19 -05:00
|
|
|
return @getNamesByIds(req, res) if args[1] is 'names'
|
2014-02-27 17:07:11 -05:00
|
|
|
return @nameToID(req, res, args[0]) if args[1] is 'nameToID'
|
2014-03-31 18:48:22 -04:00
|
|
|
return @getLevelSessions(req, res, args[0]) if args[1] is 'level.sessions'
|
2014-04-06 20:01:56 -04:00
|
|
|
return @getCandidates(req, res) if args[1] is 'candidates'
|
2014-04-12 17:13:26 -04:00
|
|
|
return @getSimulatorLeaderboard(req, res, args[0]) if args[1] is 'simulatorLeaderboard'
|
2014-04-14 17:52:21 -04:00
|
|
|
return @getMySimulatorLeaderboardRank(req, res, args[0]) if args[1] is 'simulator_leaderboard_rank'
|
2014-01-03 13:32:13 -05:00
|
|
|
return @sendNotFoundError(res)
|
2014-04-11 13:33:22 -04:00
|
|
|
super(arguments...)
|
2014-02-01 11:22:26 -05:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
agreeToCLA: (req, res) ->
|
2014-02-24 23:27:38 -05:00
|
|
|
return @sendUnauthorizedError(res) unless req.user
|
2014-01-03 13:32:13 -05:00
|
|
|
doc =
|
|
|
|
user: req.user._id+''
|
|
|
|
email: req.user.get 'email'
|
|
|
|
name: req.user.get 'name'
|
|
|
|
githubUsername: req.body.githubUsername
|
2014-01-05 19:02:50 -05:00
|
|
|
created: new Date()+''
|
2014-01-18 13:16:55 -05:00
|
|
|
collection = mongoose.connection.db.collection 'cla.submissions', (err, collection) =>
|
2014-01-03 13:32:13 -05:00
|
|
|
return @sendDatabaseError(res, err) if err
|
2014-01-18 13:16:55 -05:00
|
|
|
collection.insert doc, (err) =>
|
2014-01-03 13:32:13 -05:00
|
|
|
return @sendDatabaseError(res, err) if err
|
|
|
|
req.user.set('signedCLA', doc.created)
|
2014-01-18 13:16:55 -05:00
|
|
|
req.user.save (err) =>
|
2014-01-03 13:32:13 -05:00
|
|
|
return @sendDatabaseError(res, err) if err
|
2014-01-14 17:13:47 -05:00
|
|
|
@sendSuccess(res, {result:'success'})
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-02-07 19:11:07 -05:00
|
|
|
avatar: (req, res, id) ->
|
2014-04-09 19:46:44 -04:00
|
|
|
@modelClass.findById(id).exec (err, document) =>
|
2014-02-07 19:11:07 -05:00
|
|
|
return @sendDatabaseError(res, err) if err
|
2014-04-09 19:46:44 -04:00
|
|
|
photoURL = document?.get('photoURL')
|
2014-04-12 17:53:09 -04:00
|
|
|
if photoURL
|
|
|
|
photoURL = "/file/#{photoURL}"
|
|
|
|
else
|
|
|
|
photoURL = @buildGravatarURL document, req.query.s, req.query.fallback
|
2014-04-09 19:46:44 -04:00
|
|
|
res.redirect photoURL
|
2014-02-07 19:11:07 -05:00
|
|
|
res.end()
|
|
|
|
|
2014-03-31 18:48:22 -04:00
|
|
|
getLevelSessions: (req, res, userID) ->
|
|
|
|
return @sendUnauthorizedError(res) unless req.user._id+'' is userID or req.user.isAdmin()
|
|
|
|
query = {'creator': userID}
|
|
|
|
projection = null
|
|
|
|
if req.query.project
|
|
|
|
projection = {}
|
|
|
|
projection[field] = 1 for field in req.query.project.split(',')
|
|
|
|
LevelSession.find(query).select(projection).exec (err, documents) =>
|
|
|
|
return @sendDatabaseError(res, err) if err
|
2014-04-09 19:46:44 -04:00
|
|
|
documents = (LevelSessionHandler.formatEntity(req, doc) for doc in documents)
|
2014-03-31 18:48:22 -04:00
|
|
|
@sendSuccess(res, documents)
|
|
|
|
|
2014-04-06 20:01:56 -04:00
|
|
|
getCandidates: (req, res) ->
|
2014-04-07 18:21:05 -04:00
|
|
|
authorized = req.user.isAdmin() or ('employer' in req.user.get('permissions'))
|
|
|
|
since = (new Date((new Date()) - 2 * 30.4 * 86400 * 1000)).toISOString()
|
2014-04-15 13:45:54 -04:00
|
|
|
query = {'jobProfile.active': true, 'jobProfile.updated': {$gt: since}}
|
2014-04-07 20:58:02 -04:00
|
|
|
query.jobProfileApproved = true unless req.user.isAdmin()
|
2014-04-07 18:21:05 -04:00
|
|
|
selection = 'jobProfile'
|
2014-04-07 20:58:02 -04:00
|
|
|
selection += ' email' if authorized
|
|
|
|
selection += ' jobProfileApproved' if req.user.isAdmin()
|
2014-04-07 18:21:05 -04:00
|
|
|
User.find(query).select(selection).exec (err, documents) =>
|
2014-04-06 20:01:56 -04:00
|
|
|
return @sendDatabaseError(res, err) if err
|
2014-04-07 18:21:05 -04:00
|
|
|
candidates = (@formatCandidate(authorized, doc) for doc in documents)
|
|
|
|
@sendSuccess(res, candidates)
|
|
|
|
|
|
|
|
formatCandidate: (authorized, document) ->
|
2014-04-09 19:46:44 -04:00
|
|
|
fields = if authorized then ['jobProfile', 'jobProfileApproved', 'photoURL', '_id'] else ['jobProfile']
|
2014-04-07 18:21:05 -04:00
|
|
|
obj = _.pick document.toObject(), fields
|
2014-04-10 20:54:28 -04:00
|
|
|
obj.photoURL ||= obj.jobProfile.photoURL if authorized
|
2014-04-11 15:49:44 -04:00
|
|
|
subfields = ['country', 'city', 'lookingFor', 'jobTitle', 'skills', 'experience', 'updated']
|
2014-04-07 18:21:05 -04:00
|
|
|
if authorized
|
2014-04-11 15:49:44 -04:00
|
|
|
subfields = subfields.concat ['name']
|
2014-04-07 18:21:05 -04:00
|
|
|
obj.jobProfile = _.pick obj.jobProfile, subfields
|
|
|
|
obj
|
2014-03-31 18:48:22 -04:00
|
|
|
|
2014-04-12 17:53:09 -04:00
|
|
|
buildGravatarURL: (user, size, fallback) ->
|
2014-04-09 19:46:44 -04:00
|
|
|
emailHash = @buildEmailHash user
|
2014-04-12 17:53:09 -04:00
|
|
|
fallback ?= "http://codecombat.com/file/db/thang.type/52a00d55cf1818f2be00000b/portrait.png"
|
|
|
|
fallback = "http://codecombat.com#{fallback}" unless /^http/.test fallback
|
|
|
|
"https://www.gravatar.com/avatar/#{emailHash}?s=#{size}&default=#{fallback}"
|
2014-04-09 19:46:44 -04:00
|
|
|
|
|
|
|
buildEmailHash: (user) ->
|
|
|
|
# emailHash is used by gravatar
|
|
|
|
hash = crypto.createHash('md5')
|
|
|
|
if user.get('email')
|
|
|
|
hash.update(_.trim(user.get('email')).toLowerCase())
|
|
|
|
else
|
|
|
|
hash.update(user.get('_id') + '')
|
|
|
|
hash.digest('hex')
|
2014-03-31 18:48:22 -04:00
|
|
|
|
2014-03-16 11:11:55 -04:00
|
|
|
module.exports = new UserHandler()
|