User.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. max-len,
  5. no-return-assign,
  6. no-undef,
  7. no-unused-vars,
  8. */
  9. // TODO: This file was created by bulk-decaffeinate.
  10. // Fix any style issues and re-enable lint.
  11. /*
  12. * decaffeinate suggestions:
  13. * DS102: Remove unnecessary code created because of implicit returns
  14. * DS103: Rewrite code to no longer use __guard__
  15. * DS207: Consider shorter variations of null checks
  16. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  17. */
  18. const request = require('./request')
  19. const _ = require('underscore')
  20. const settings = require('settings-sharelatex')
  21. const { db, ObjectId } = require('../../../../app/src/infrastructure/mongojs')
  22. const UserModel = require('../../../../app/src/models/User').User
  23. const UserUpdater = require('../../../../app/src/Features/User/UserUpdater')
  24. const AuthenticationManager = require('../../../../app/src/Features/Authentication/AuthenticationManager')
  25. const { promisify } = require('util')
  26. let count = 0
  27. class User {
  28. constructor(options) {
  29. if (options == null) {
  30. options = {}
  31. }
  32. this.emails = [
  33. {
  34. email: options.email || `acceptance-test-${count}@example.com`,
  35. createdAt: new Date()
  36. }
  37. ]
  38. this.email = this.emails[0].email
  39. this.password = `acceptance-test-${count}-password`
  40. count++
  41. this.jar = request.jar()
  42. this.request = request.defaults({
  43. jar: this.jar
  44. })
  45. }
  46. setExtraAttributes(user) {
  47. if ((user != null ? user._id : undefined) == null) {
  48. throw new Error('User does not exist')
  49. }
  50. this.id = user._id.toString()
  51. this._id = user._id.toString()
  52. this.first_name = user.first_name
  53. return (this.referal_id = user.referal_id)
  54. }
  55. get(callback) {
  56. if (callback == null) {
  57. callback = function(error, user) {}
  58. }
  59. return db.users.findOne({ _id: ObjectId(this._id) }, callback)
  60. }
  61. mongoUpdate(updateOp, callback) {
  62. if (callback == null) {
  63. callback = function(error) {}
  64. }
  65. return db.users.update({ _id: ObjectId(this._id) }, updateOp, callback)
  66. }
  67. register(callback) {
  68. if (callback == null) {
  69. callback = function(error, user) {}
  70. }
  71. return this.registerWithQuery('', callback)
  72. }
  73. registerWithQuery(query, callback) {
  74. if (callback == null) {
  75. callback = function(error, user) {}
  76. }
  77. if (this._id != null) {
  78. return callback(new Error('User already registered'))
  79. }
  80. return this.getCsrfToken(error => {
  81. if (error != null) {
  82. return callback(error)
  83. }
  84. return this.request.post(
  85. {
  86. url: `/register${query}`,
  87. json: { email: this.email, password: this.password }
  88. },
  89. (error, response, body) => {
  90. if (error != null) {
  91. return callback(error)
  92. }
  93. return db.users.findOne({ email: this.email }, (error, user) => {
  94. if (error != null) {
  95. return callback(error)
  96. }
  97. this.setExtraAttributes(user)
  98. return callback(null, user)
  99. })
  100. }
  101. )
  102. })
  103. }
  104. login(callback) {
  105. if (callback == null) {
  106. callback = function(error) {}
  107. }
  108. return this.loginWith(this.email, callback)
  109. }
  110. loginWith(email, callback) {
  111. if (callback == null) {
  112. callback = function(error) {}
  113. }
  114. return this.ensureUserExists(error => {
  115. if (error != null) {
  116. return callback(error)
  117. }
  118. return this.getCsrfToken(error => {
  119. if (error != null) {
  120. return callback(error)
  121. }
  122. return this.request.post(
  123. {
  124. url: settings.enableLegacyLogin ? '/login/legacy' : '/login',
  125. json: { email, password: this.password }
  126. },
  127. callback
  128. )
  129. })
  130. })
  131. }
  132. ensureUserExists(callback) {
  133. if (callback == null) {
  134. callback = function(error) {}
  135. }
  136. const filter = { email: this.email }
  137. const options = { upsert: true, new: true, setDefaultsOnInsert: true }
  138. return UserModel.findOneAndUpdate(filter, {}, options, (error, user) => {
  139. if (error != null) {
  140. return callback(error)
  141. }
  142. return AuthenticationManager.setUserPasswordInV2(
  143. user._id,
  144. this.password,
  145. error => {
  146. if (error != null) {
  147. return callback(error)
  148. }
  149. return UserUpdater.updateUser(
  150. user._id,
  151. { $set: { emails: this.emails } },
  152. error => {
  153. if (error != null) {
  154. return callback(error)
  155. }
  156. this.setExtraAttributes(user)
  157. return callback(null, this.password)
  158. }
  159. )
  160. }
  161. )
  162. })
  163. }
  164. setFeatures(features, callback) {
  165. if (callback == null) {
  166. callback = function(error) {}
  167. }
  168. const update = {}
  169. for (let key in features) {
  170. const value = features[key]
  171. update[`features.${key}`] = value
  172. }
  173. return UserModel.update({ _id: this.id }, update, callback)
  174. }
  175. setOverleafId(overleaf_id, callback) {
  176. if (callback == null) {
  177. callback = function(error) {}
  178. }
  179. return UserModel.update(
  180. { _id: this.id },
  181. { 'overleaf.id': overleaf_id },
  182. callback
  183. )
  184. }
  185. logout(callback) {
  186. if (callback == null) {
  187. callback = function(error) {}
  188. }
  189. return this.getCsrfToken(error => {
  190. if (error != null) {
  191. return callback(error)
  192. }
  193. return this.request.post(
  194. {
  195. url: '/logout',
  196. json: {
  197. email: this.email,
  198. password: this.password
  199. }
  200. },
  201. (error, response, body) => {
  202. if (error != null) {
  203. return callback(error)
  204. }
  205. return db.users.findOne({ email: this.email }, (error, user) => {
  206. if (error != null) {
  207. return callback(error)
  208. }
  209. this.id = __guard__(user != null ? user._id : undefined, x =>
  210. x.toString()
  211. )
  212. this._id = __guard__(user != null ? user._id : undefined, x1 =>
  213. x1.toString()
  214. )
  215. return callback()
  216. })
  217. }
  218. )
  219. })
  220. }
  221. addEmail(email, callback) {
  222. if (callback == null) {
  223. callback = function(error) {}
  224. }
  225. this.emails.push({ email, createdAt: new Date() })
  226. return UserUpdater.addEmailAddress(this.id, email, callback)
  227. }
  228. confirmEmail(email, callback) {
  229. if (callback == null) {
  230. callback = function(error) {}
  231. }
  232. for (let idx = 0; idx < this.emails.length; idx++) {
  233. const emailData = this.emails[idx]
  234. if (emailData.email === email) {
  235. this.emails[idx].confirmedAt = new Date()
  236. }
  237. }
  238. return UserUpdater.confirmEmail(this.id, email, callback)
  239. }
  240. ensure_admin(callback) {
  241. if (callback == null) {
  242. callback = function(error) {}
  243. }
  244. return db.users.update(
  245. { _id: ObjectId(this.id) },
  246. { $set: { isAdmin: true } },
  247. callback
  248. )
  249. }
  250. ensureStaffAccess(flag, callback) {
  251. const update = { $set: {} }
  252. update.$set[`staffAccess.${flag}`] = true
  253. return db.users.update({ _id: ObjectId(this.id) }, update, callback)
  254. }
  255. upgradeFeatures(callback) {
  256. if (callback == null) {
  257. callback = function(error) {}
  258. }
  259. const features = {
  260. collaborators: -1, // Infinite
  261. versioning: true,
  262. dropbox: true,
  263. compileTimeout: 60,
  264. compileGroup: 'priority',
  265. templates: true,
  266. references: true,
  267. trackChanges: true,
  268. trackChangesVisible: true
  269. }
  270. return db.users.update(
  271. { _id: ObjectId(this.id) },
  272. { $set: { features } },
  273. callback
  274. )
  275. }
  276. downgradeFeatures(callback) {
  277. if (callback == null) {
  278. callback = function(error) {}
  279. }
  280. const features = {
  281. collaborators: 1,
  282. versioning: false,
  283. dropbox: false,
  284. compileTimeout: 60,
  285. compileGroup: 'standard',
  286. templates: false,
  287. references: false,
  288. trackChanges: false,
  289. trackChangesVisible: false
  290. }
  291. return db.users.update(
  292. { _id: ObjectId(this.id) },
  293. { $set: { features } },
  294. callback
  295. )
  296. }
  297. defaultFeatures(callback) {
  298. if (callback == null) {
  299. callback = function(error) {}
  300. }
  301. const features = settings.defaultFeatures
  302. return db.users.update(
  303. { _id: ObjectId(this.id) },
  304. { $set: { features } },
  305. callback
  306. )
  307. }
  308. full_delete_user(email, callback) {
  309. if (callback == null) {
  310. callback = function(error) {}
  311. }
  312. return db.users.findOne({ email }, (error, user) => {
  313. if (user == null) {
  314. return callback()
  315. }
  316. const user_id = user._id
  317. return db.projects.remove(
  318. { owner_ref: ObjectId(user_id) },
  319. { multi: true },
  320. err => {
  321. if (err != null) {
  322. callback(err)
  323. }
  324. return db.users.remove({ _id: ObjectId(user_id) }, callback)
  325. }
  326. )
  327. })
  328. }
  329. deleteUser(callback) {
  330. this.getCsrfToken(error => {
  331. if (error) {
  332. return callback(error)
  333. }
  334. this.request.post(
  335. {
  336. url: '/user/delete',
  337. json: { password: this.password }
  338. },
  339. (err, res) => {
  340. if (err) {
  341. return callback(err)
  342. }
  343. if (res.statusCode < 200 || res.statusCode >= 300) {
  344. return callback(
  345. new Error('Error received from API: ' + res.statusCode)
  346. )
  347. }
  348. callback()
  349. }
  350. )
  351. })
  352. }
  353. getProject(project_id, callback) {
  354. if (callback == null) {
  355. callback = function(error, project) {}
  356. }
  357. return db.projects.findOne(
  358. { _id: ObjectId(project_id.toString()) },
  359. callback
  360. )
  361. }
  362. saveProject(project, callback) {
  363. if (callback == null) {
  364. callback = function(error) {}
  365. }
  366. return db.projects.update({ _id: project._id }, project, callback)
  367. }
  368. createProject(name, options, callback) {
  369. if (callback == null) {
  370. callback = function(error, oroject_id) {}
  371. }
  372. if (typeof options === 'function') {
  373. callback = options
  374. options = {}
  375. }
  376. return this.request.post(
  377. {
  378. url: '/project/new',
  379. json: Object.assign({ projectName: name }, options)
  380. },
  381. (error, response, body) => {
  382. if (error != null) {
  383. return callback(error)
  384. }
  385. if ((body != null ? body.project_id : undefined) == null) {
  386. error = new Error(
  387. JSON.stringify([
  388. 'SOMETHING WENT WRONG CREATING PROJECT',
  389. name,
  390. options,
  391. response.statusCode,
  392. response.headers['location'],
  393. body
  394. ])
  395. )
  396. return callback(error)
  397. } else {
  398. return callback(null, body.project_id)
  399. }
  400. }
  401. )
  402. }
  403. deleteProject(project_id, callback) {
  404. if (callback == null) {
  405. callback = error
  406. }
  407. return this.request.delete(
  408. {
  409. url: `/project/${project_id}?forever=true`
  410. },
  411. (error, response, body) => {
  412. if (error != null) {
  413. return callback(error)
  414. }
  415. return callback(null)
  416. }
  417. )
  418. }
  419. deleteProjects(callback) {
  420. if (callback == null) {
  421. callback = error
  422. }
  423. return db.projects.remove(
  424. { owner_ref: ObjectId(this.id) },
  425. { multi: true },
  426. err => callback(err)
  427. )
  428. }
  429. openProject(project_id, callback) {
  430. if (callback == null) {
  431. callback = error
  432. }
  433. return this.request.get(
  434. {
  435. url: `/project/${project_id}`
  436. },
  437. (error, response, body) => {
  438. if (error != null) {
  439. return callback(error)
  440. }
  441. if (response.statusCode !== 200) {
  442. const err = new Error(
  443. `Non-success response when opening project: ${response.statusCode}`
  444. )
  445. return callback(err)
  446. }
  447. return callback(null)
  448. }
  449. )
  450. }
  451. createDocInProject(project_id, parent_folder_id, name, callback) {
  452. if (callback == null) {
  453. callback = function(error, doc_id) {}
  454. }
  455. return this.getCsrfToken(error => {
  456. if (error != null) {
  457. return callback(error)
  458. }
  459. return this.request.post(
  460. {
  461. url: `/project/${project_id}/doc`,
  462. json: {
  463. name,
  464. parent_folder_id
  465. }
  466. },
  467. (error, response, body) => {
  468. return callback(null, body._id)
  469. }
  470. )
  471. })
  472. }
  473. addUserToProject(project_id, user, privileges, callback) {
  474. let updateOp
  475. if (callback == null) {
  476. callback = function(error, user) {}
  477. }
  478. if (privileges === 'readAndWrite') {
  479. updateOp = { $addToSet: { collaberator_refs: user._id.toString() } }
  480. } else if (privileges === 'readOnly') {
  481. updateOp = { $addToSet: { readOnly_refs: user._id.toString() } }
  482. }
  483. return db.projects.update({ _id: db.ObjectId(project_id) }, updateOp, err =>
  484. callback(err)
  485. )
  486. }
  487. makePublic(project_id, level, callback) {
  488. if (callback == null) {
  489. callback = function(error) {}
  490. }
  491. return this.request.post(
  492. {
  493. url: `/project/${project_id}/settings/admin`,
  494. json: {
  495. publicAccessLevel: level
  496. }
  497. },
  498. (error, response, body) => {
  499. if (error != null) {
  500. return callback(error)
  501. }
  502. return callback(null)
  503. }
  504. )
  505. }
  506. makePrivate(project_id, callback) {
  507. if (callback == null) {
  508. callback = function(error) {}
  509. }
  510. return this.request.post(
  511. {
  512. url: `/project/${project_id}/settings/admin`,
  513. json: {
  514. publicAccessLevel: 'private'
  515. }
  516. },
  517. (error, response, body) => {
  518. if (error != null) {
  519. return callback(error)
  520. }
  521. return callback(null)
  522. }
  523. )
  524. }
  525. makeTokenBased(project_id, callback) {
  526. if (callback == null) {
  527. callback = function(error) {}
  528. }
  529. return this.request.post(
  530. {
  531. url: `/project/${project_id}/settings/admin`,
  532. json: {
  533. publicAccessLevel: 'tokenBased'
  534. }
  535. },
  536. (error, response, body) => {
  537. if (error != null) {
  538. return callback(error)
  539. }
  540. return callback(null)
  541. }
  542. )
  543. }
  544. getCsrfToken(callback) {
  545. if (callback == null) {
  546. callback = function(error) {}
  547. }
  548. return this.request.get(
  549. {
  550. url: '/dev/csrf'
  551. },
  552. (err, response, body) => {
  553. if (err != null) {
  554. return callback(err)
  555. }
  556. this.csrfToken = body
  557. this.request = this.request.defaults({
  558. headers: {
  559. 'x-csrf-token': this.csrfToken
  560. }
  561. })
  562. return callback()
  563. }
  564. )
  565. }
  566. changePassword(callback) {
  567. if (callback == null) {
  568. callback = function(error) {}
  569. }
  570. return this.getCsrfToken(error => {
  571. if (error != null) {
  572. return callback(error)
  573. }
  574. return this.request.post(
  575. {
  576. url: '/user/password/update',
  577. json: {
  578. currentPassword: this.password,
  579. newPassword1: this.password,
  580. newPassword2: this.password
  581. }
  582. },
  583. (error, response, body) => {
  584. if (error != null) {
  585. return callback(error)
  586. }
  587. return db.users.findOne({ email: this.email }, (error, user) => {
  588. if (error != null) {
  589. return callback(error)
  590. }
  591. return callback()
  592. })
  593. }
  594. )
  595. })
  596. }
  597. reconfirmAccountRequest(user_email, callback) {
  598. if (callback == null) {
  599. callback = function(error) {}
  600. }
  601. return this.getCsrfToken(error => {
  602. if (error != null) {
  603. return callback(error)
  604. }
  605. return this.request.post(
  606. {
  607. url: '/user/reconfirm',
  608. json: {
  609. email: user_email
  610. }
  611. },
  612. (error, response, body) => {
  613. return callback(error, response)
  614. }
  615. )
  616. })
  617. }
  618. getUserSettingsPage(callback) {
  619. if (callback == null) {
  620. callback = function(error, statusCode) {}
  621. }
  622. return this.getCsrfToken(error => {
  623. if (error != null) {
  624. return callback(error)
  625. }
  626. return this.request.get(
  627. {
  628. url: '/user/settings'
  629. },
  630. (error, response, body) => {
  631. if (error != null) {
  632. return callback(error)
  633. }
  634. return callback(null, response.statusCode)
  635. }
  636. )
  637. })
  638. }
  639. activateSudoMode(callback) {
  640. if (callback == null) {
  641. callback = function(error) {}
  642. }
  643. return this.getCsrfToken(error => {
  644. if (error != null) {
  645. return callback(error)
  646. }
  647. return this.request.post(
  648. {
  649. uri: '/confirm-password',
  650. json: {
  651. password: this.password
  652. }
  653. },
  654. callback
  655. )
  656. })
  657. }
  658. updateSettings(newSettings, callback) {
  659. if (callback == null) {
  660. callback = function(error, response, body) {}
  661. }
  662. return this.getCsrfToken(error => {
  663. if (error != null) {
  664. return callback(error)
  665. }
  666. return this.request.post(
  667. {
  668. url: '/user/settings',
  669. json: newSettings
  670. },
  671. callback
  672. )
  673. })
  674. }
  675. getProjectListPage(callback) {
  676. if (callback == null) {
  677. callback = function(error, statusCode) {}
  678. }
  679. return this.getCsrfToken(error => {
  680. if (error != null) {
  681. return callback(error)
  682. }
  683. return this.request.get(
  684. {
  685. url: '/project'
  686. },
  687. (error, response, body) => {
  688. if (error != null) {
  689. return callback(error)
  690. }
  691. return callback(null, response.statusCode)
  692. }
  693. )
  694. })
  695. }
  696. isLoggedIn(callback) {
  697. if (callback == null) {
  698. callback = function(error, loggedIn) {}
  699. }
  700. return this.request.get('/user/personal_info', (error, response, body) => {
  701. if (error != null) {
  702. return callback(error)
  703. }
  704. if (response.statusCode === 200) {
  705. return callback(null, true)
  706. } else if (response.statusCode === 302) {
  707. return callback(null, false)
  708. } else {
  709. return callback(
  710. new Error(
  711. `unexpected status code from /user/personal_info: ${
  712. response.statusCode
  713. }`
  714. )
  715. )
  716. }
  717. })
  718. }
  719. setV1Id(v1Id, callback) {
  720. return UserModel.update(
  721. {
  722. _id: this._id
  723. },
  724. {
  725. overleaf: {
  726. id: v1Id
  727. }
  728. },
  729. callback
  730. )
  731. }
  732. }
  733. User.promises = class extends User {
  734. doRequest(method, params) {
  735. return new Promise((resolve, reject) => {
  736. this.request[method.toLowerCase()](params, (err, response, body) => {
  737. if (err) {
  738. reject(err)
  739. } else {
  740. resolve({ response, body })
  741. }
  742. })
  743. })
  744. }
  745. }
  746. // promisify User class methods - works for methods with 0-1 output parameters,
  747. // otherwise we will need to implement the method manually instead
  748. const nonPromiseMethods = ['constructor', 'setExtraAttributes']
  749. Object.getOwnPropertyNames(User.prototype).forEach(methodName => {
  750. const method = User.prototype[methodName]
  751. if (typeof method === 'function' && !nonPromiseMethods.includes(methodName)) {
  752. User.promises.prototype[methodName] = promisify(method)
  753. }
  754. })
  755. module.exports = User
  756. function __guard__(value, transform) {
  757. return typeof value !== 'undefined' && value !== null
  758. ? transform(value)
  759. : undefined
  760. }