2016-03-18 11:59:03 -04:00
|
|
|
request = require("./request")
|
2017-12-01 11:54:45 -05:00
|
|
|
_ = require("underscore")
|
2016-03-18 11:59:03 -04:00
|
|
|
settings = require("settings-sharelatex")
|
2016-03-21 13:03:31 -04:00
|
|
|
{db, ObjectId} = require("../../../../app/js/infrastructure/mongojs")
|
2017-12-01 11:54:45 -05:00
|
|
|
UserModel = require("../../../../app/js/models/User").User
|
2018-05-25 07:04:09 -04:00
|
|
|
UserUpdater = require("../../../../app/js/Features/User/UserUpdater")
|
2017-12-01 11:54:45 -05:00
|
|
|
AuthenticationManager = require("../../../../app/js/Features/Authentication/AuthenticationManager")
|
2016-03-18 11:59:03 -04:00
|
|
|
|
|
|
|
count = 0
|
|
|
|
|
|
|
|
class User
|
|
|
|
constructor: (options = {}) ->
|
2018-05-25 07:04:09 -04:00
|
|
|
@emails = [
|
|
|
|
email: "acceptance-test-#{count}@example.com"
|
|
|
|
createdAt: new Date()
|
|
|
|
]
|
|
|
|
@email = @emails[0].email
|
2016-03-18 11:59:03 -04:00
|
|
|
@password = "acceptance-test-#{count}-password"
|
|
|
|
count++
|
|
|
|
@jar = request.jar()
|
|
|
|
@request = request.defaults({
|
|
|
|
jar: @jar
|
|
|
|
})
|
2016-07-28 11:00:18 -04:00
|
|
|
|
2018-05-28 10:08:37 -04:00
|
|
|
setExtraAttributes: (user) ->
|
|
|
|
throw new Error("User does not exist") unless user?._id?
|
|
|
|
@id = user._id.toString()
|
|
|
|
@_id = user._id.toString()
|
|
|
|
@first_name = user.first_name
|
|
|
|
@referal_id = user.referal_id
|
|
|
|
|
2018-05-25 07:04:09 -04:00
|
|
|
get: (callback = (error, user)->) ->
|
|
|
|
db.users.findOne { _id: ObjectId(@_id) }, callback
|
|
|
|
|
2018-06-26 05:15:45 -04:00
|
|
|
mongoUpdate: (updateOp, callback=(error)->) ->
|
|
|
|
db.users.update {_id: ObjectId(@_id)}, updateOp, callback
|
|
|
|
|
2018-05-28 10:08:37 -04:00
|
|
|
register: (callback = (error, user) ->) ->
|
|
|
|
return callback(new Error('User already registered')) if @_id?
|
|
|
|
@getCsrfToken (error) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
@request.post {
|
|
|
|
url: '/register'
|
|
|
|
json: { @email, @password }
|
|
|
|
}, (error, response, body) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
db.users.findOne { email: @email }, (error, user) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
@setExtraAttributes user
|
|
|
|
callback(null, user)
|
|
|
|
|
2016-03-18 11:59:03 -04:00
|
|
|
login: (callback = (error) ->) ->
|
2018-05-25 07:04:09 -04:00
|
|
|
@loginWith(@email, callback)
|
|
|
|
|
|
|
|
loginWith: (email, callback = (error) ->) ->
|
2018-01-24 11:56:31 -05:00
|
|
|
@ensureUserExists (error) =>
|
2016-03-18 11:59:03 -04:00
|
|
|
return callback(error) if error?
|
2018-01-24 11:56:31 -05:00
|
|
|
@getCsrfToken (error) =>
|
2016-03-18 11:59:03 -04:00
|
|
|
return callback(error) if error?
|
2018-01-24 11:56:31 -05:00
|
|
|
@request.post {
|
|
|
|
url: "/login"
|
2018-05-25 07:04:09 -04:00
|
|
|
json: { email, @password }
|
2018-01-24 11:56:31 -05:00
|
|
|
}, callback
|
|
|
|
|
|
|
|
ensureUserExists: (callback = (error) ->) ->
|
|
|
|
filter = {@email}
|
|
|
|
options = {upsert: true, new: true, setDefaultsOnInsert: true}
|
|
|
|
UserModel.findOneAndUpdate filter, {}, options, (error, user) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
AuthenticationManager.setUserPassword user._id, @password, (error) =>
|
|
|
|
return callback(error) if error?
|
2018-05-25 07:04:09 -04:00
|
|
|
UserUpdater.updateUser user._id, $set: emails: @emails, (error) =>
|
|
|
|
return callback(error) if error?
|
2018-05-28 10:08:37 -04:00
|
|
|
@setExtraAttributes user
|
2018-05-25 07:04:09 -04:00
|
|
|
callback(null, @password)
|
2016-07-05 05:24:24 -04:00
|
|
|
|
2018-05-22 05:19:47 -04:00
|
|
|
setFeatures: (features, callback = (error) ->) ->
|
|
|
|
update = {}
|
|
|
|
for key, value of features
|
|
|
|
update["features.#{key}"] = value
|
|
|
|
UserModel.update { _id: @id }, update, callback
|
|
|
|
|
2018-08-22 09:20:12 -04:00
|
|
|
setOverleafId: (overleaf_id, callback = (error) ->) ->
|
|
|
|
UserModel.update { _id: @id }, { 'overleaf.id': overleaf_id }, callback
|
|
|
|
|
2016-07-05 05:24:24 -04:00
|
|
|
logout: (callback = (error) ->) ->
|
|
|
|
@getCsrfToken (error) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
@request.get {
|
2016-07-05 09:55:08 -04:00
|
|
|
url: "/logout"
|
2016-07-05 05:24:24 -04:00
|
|
|
json:
|
|
|
|
email: @email
|
|
|
|
password: @password
|
|
|
|
}, (error, response, body) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
db.users.findOne {email: @email}, (error, user) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
@id = user?._id?.toString()
|
|
|
|
@_id = user?._id?.toString()
|
|
|
|
callback()
|
|
|
|
|
2018-05-25 07:04:09 -04:00
|
|
|
addEmail: (email, callback = (error) ->) ->
|
|
|
|
@emails.push(email: email, createdAt: new Date())
|
|
|
|
UserUpdater.addEmailAddress @id, email, callback
|
|
|
|
|
2018-07-10 05:42:17 -04:00
|
|
|
confirmEmail: (email, callback = (error) ->) ->
|
|
|
|
for emailData, idx in @emails
|
|
|
|
@emails[idx].confirmedAt = new Date() if emailData.email == email
|
|
|
|
UserUpdater.confirmEmail @id, email, callback
|
|
|
|
|
2016-03-21 13:03:31 -04:00
|
|
|
ensure_admin: (callback = (error) ->) ->
|
|
|
|
db.users.update {_id: ObjectId(@id)}, { $set: { isAdmin: true }}, callback
|
2016-07-05 05:24:24 -04:00
|
|
|
|
2017-09-01 08:28:11 -04:00
|
|
|
upgradeFeatures: (callback = (error) -> ) ->
|
|
|
|
features = {
|
|
|
|
collaborators: -1 # Infinite
|
|
|
|
versioning: true
|
|
|
|
dropbox:true
|
|
|
|
compileTimeout: 60
|
|
|
|
compileGroup:"priority"
|
|
|
|
templates: true
|
|
|
|
references: true
|
|
|
|
trackChanges: true
|
|
|
|
trackChangesVisible: true
|
|
|
|
}
|
|
|
|
db.users.update {_id: ObjectId(@id)}, { $set: { features: features }}, callback
|
|
|
|
|
|
|
|
downgradeFeatures: (callback = (error) -> ) ->
|
|
|
|
features = {
|
|
|
|
collaborators: 1
|
|
|
|
versioning: false
|
|
|
|
dropbox:false
|
|
|
|
compileTimeout: 60
|
|
|
|
compileGroup:"standard"
|
|
|
|
templates: false
|
|
|
|
references: false
|
|
|
|
trackChanges: false
|
|
|
|
trackChangesVisible: false
|
|
|
|
}
|
|
|
|
db.users.update {_id: ObjectId(@id)}, { $set: { features: features }}, callback
|
|
|
|
|
2017-09-01 09:45:54 -04:00
|
|
|
defaultFeatures: (callback = (error) -> ) ->
|
|
|
|
features = settings.defaultFeatures
|
|
|
|
db.users.update {_id: ObjectId(@id)}, { $set: { features: features }}, callback
|
|
|
|
|
2017-08-29 12:45:16 -04:00
|
|
|
full_delete_user: (email, callback = (error) ->) ->
|
|
|
|
db.users.findOne {email: email}, (error, user) =>
|
|
|
|
if !user?
|
|
|
|
return callback()
|
|
|
|
user_id = user._id
|
|
|
|
db.projects.remove owner_ref:ObjectId(user_id), {multi:true}, (err)->
|
|
|
|
if err?
|
|
|
|
callback(err)
|
|
|
|
db.users.remove {_id: ObjectId(user_id)}, callback
|
|
|
|
|
2017-10-06 10:58:03 -04:00
|
|
|
getProject: (project_id, callback = (error, project)->) ->
|
|
|
|
db.projects.findOne {_id: ObjectId(project_id.toString())}, callback
|
|
|
|
|
2018-06-01 10:16:27 -04:00
|
|
|
saveProject: (project, callback=(error)->) ->
|
|
|
|
db.projects.update {_id: project._id}, project, callback
|
|
|
|
|
2017-11-27 12:09:51 -05:00
|
|
|
createProject: (name, options, callback = (error, oroject_id) ->) ->
|
|
|
|
if typeof options == "function"
|
|
|
|
callback = options
|
|
|
|
options = {}
|
|
|
|
|
2016-03-18 11:59:03 -04:00
|
|
|
@request.post {
|
|
|
|
url: "/project/new",
|
2017-11-27 12:09:51 -05:00
|
|
|
json: Object.assign({projectName: name}, options)
|
2016-03-18 11:59:03 -04:00
|
|
|
}, (error, response, body) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
if !body?.project_id?
|
2017-12-01 12:21:54 -05:00
|
|
|
error = new Error("SOMETHING WENT WRONG CREATING PROJECT", response.statusCode, response.headers["location"], body)
|
|
|
|
callback error
|
|
|
|
else
|
|
|
|
callback(null, body.project_id)
|
2016-07-28 11:00:18 -04:00
|
|
|
|
|
|
|
deleteProject: (project_id, callback=(error)) ->
|
|
|
|
@request.delete {
|
|
|
|
url: "/project/#{project_id}"
|
|
|
|
}, (error, response, body) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback(null)
|
|
|
|
|
2016-07-29 06:04:07 -04:00
|
|
|
openProject: (project_id, callback=(error)) ->
|
|
|
|
@request.get {
|
|
|
|
url: "/project/#{project_id}"
|
|
|
|
}, (error, response, body) ->
|
2016-07-29 06:08:24 -04:00
|
|
|
return callback(error) if error?
|
|
|
|
if response.statusCode != 200
|
|
|
|
err = new Error("Non-success response when opening project: #{response.statusCode}")
|
|
|
|
return callback(err)
|
2016-07-29 06:04:07 -04:00
|
|
|
callback(null)
|
|
|
|
|
2018-05-22 10:01:51 -04:00
|
|
|
createDocInProject: (project_id, parent_folder_id, name, callback=(error, doc_id)->) ->
|
|
|
|
@getCsrfToken (error) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
@request.post {
|
|
|
|
url: "/project/#{project_id}/doc",
|
|
|
|
json: {
|
|
|
|
name: name,
|
|
|
|
parent_folder_id: parent_folder_id
|
|
|
|
}
|
|
|
|
}, (error, response, body) =>
|
|
|
|
callback(null, body._id)
|
|
|
|
|
2017-09-21 06:43:16 -04:00
|
|
|
addUserToProject: (project_id, user, privileges, callback = (error, user) ->) ->
|
|
|
|
if privileges == 'readAndWrite'
|
|
|
|
updateOp = {$addToSet: {collaberator_refs: user._id.toString()}}
|
|
|
|
else if privileges == 'readOnly'
|
|
|
|
updateOp = {$addToSet: {readOnly_refs: user._id.toString()}}
|
|
|
|
db.projects.update {_id: db.ObjectId(project_id)}, updateOp, (err) ->
|
|
|
|
callback(err)
|
2016-07-28 11:00:18 -04:00
|
|
|
|
2016-03-18 11:59:03 -04:00
|
|
|
makePublic: (project_id, level, callback = (error) ->) ->
|
|
|
|
@request.post {
|
|
|
|
url: "/project/#{project_id}/settings/admin",
|
|
|
|
json:
|
|
|
|
publicAccessLevel: level
|
|
|
|
}, (error, response, body) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback(null)
|
|
|
|
|
2017-10-06 10:58:03 -04:00
|
|
|
makePrivate: (project_id, callback = (error) ->) ->
|
|
|
|
@request.post {
|
|
|
|
url: "/project/#{project_id}/settings/admin",
|
|
|
|
json:
|
|
|
|
publicAccessLevel: 'private'
|
|
|
|
}, (error, response, body) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback(null)
|
|
|
|
|
|
|
|
makeTokenBased: (project_id, callback = (error) ->) ->
|
|
|
|
@request.post {
|
|
|
|
url: "/project/#{project_id}/settings/admin",
|
|
|
|
json:
|
|
|
|
publicAccessLevel: 'tokenBased'
|
|
|
|
}, (error, response, body) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback(null)
|
|
|
|
|
2016-03-18 11:59:03 -04:00
|
|
|
getCsrfToken: (callback = (error) ->) ->
|
|
|
|
@request.get {
|
2017-12-01 11:47:38 -05:00
|
|
|
url: "/dev/csrf"
|
2016-03-18 11:59:03 -04:00
|
|
|
}, (err, response, body) =>
|
2017-11-21 12:04:09 -05:00
|
|
|
return callback(err) if err?
|
2017-12-01 11:47:38 -05:00
|
|
|
@csrfToken = body
|
2016-03-18 11:59:03 -04:00
|
|
|
@request = @request.defaults({
|
|
|
|
headers:
|
2017-08-29 12:45:16 -04:00
|
|
|
"x-csrf-token": @csrfToken
|
2016-03-18 11:59:03 -04:00
|
|
|
})
|
|
|
|
callback()
|
|
|
|
|
2016-07-05 09:55:08 -04:00
|
|
|
changePassword: (callback = (error) ->) ->
|
2016-07-05 09:21:38 -04:00
|
|
|
@getCsrfToken (error) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
@request.post {
|
2016-07-05 09:55:08 -04:00
|
|
|
url: "/user/password/update"
|
2016-07-05 09:21:38 -04:00
|
|
|
json:
|
2016-07-05 09:55:08 -04:00
|
|
|
currentPassword: @password
|
|
|
|
newPassword1: @password
|
|
|
|
newPassword2: @password
|
2016-07-05 09:21:38 -04:00
|
|
|
}, (error, response, body) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
db.users.findOne {email: @email}, (error, user) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
callback()
|
|
|
|
|
2016-07-06 07:14:01 -04:00
|
|
|
getUserSettingsPage: (callback = (error, statusCode) ->) ->
|
|
|
|
@getCsrfToken (error) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
@request.get {
|
|
|
|
url: "/user/settings"
|
|
|
|
}, (error, response, body) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
callback(null, response.statusCode)
|
|
|
|
|
2018-05-25 07:04:09 -04:00
|
|
|
activateSudoMode: (callback = (error)->) ->
|
|
|
|
@getCsrfToken (error) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
@request.post {
|
|
|
|
uri: '/confirm-password',
|
|
|
|
json:
|
|
|
|
password: @password
|
|
|
|
}, callback
|
|
|
|
|
|
|
|
updateSettings: (newSettings, callback = (error, response, body) ->) ->
|
|
|
|
@getCsrfToken (error) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
@request.post {
|
|
|
|
url: '/user/settings'
|
|
|
|
json: newSettings
|
|
|
|
}, callback
|
|
|
|
|
2017-05-10 08:45:53 -04:00
|
|
|
getProjectListPage: (callback=(error, statusCode)->) ->
|
|
|
|
@getCsrfToken (error) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
@request.get {
|
|
|
|
url: "/project"
|
|
|
|
}, (error, response, body) =>
|
|
|
|
return callback(error) if error?
|
|
|
|
callback(null, response.statusCode)
|
|
|
|
|
2018-01-24 11:56:31 -05:00
|
|
|
isLoggedIn: (callback = (error, loggedIn) ->) ->
|
|
|
|
@request.get "/user/personal_info", (error, response, body) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
if response.statusCode == 200
|
|
|
|
return callback(null, true)
|
|
|
|
else if response.statusCode == 302
|
|
|
|
return callback(null, false)
|
|
|
|
else
|
|
|
|
return callback(new Error("unexpected status code from /user/personal_info: #{response.statusCode}"))
|
2016-07-06 07:14:01 -04:00
|
|
|
|
2016-07-05 09:21:38 -04:00
|
|
|
module.exports = User
|