ProjectController.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  1. const _ = require('lodash')
  2. const OError = require('@overleaf/o-error')
  3. const crypto = require('crypto')
  4. const async = require('async')
  5. const logger = require('@overleaf/logger')
  6. const { ObjectId } = require('mongodb')
  7. const ProjectDeleter = require('./ProjectDeleter')
  8. const ProjectDuplicator = require('./ProjectDuplicator')
  9. const ProjectCreationHandler = require('./ProjectCreationHandler')
  10. const EditorController = require('../Editor/EditorController')
  11. const ProjectHelper = require('./ProjectHelper')
  12. const metrics = require('@overleaf/metrics')
  13. const { User } = require('../../models/User')
  14. const SubscriptionLocator = require('../Subscription/SubscriptionLocator')
  15. const LimitationsManager = require('../Subscription/LimitationsManager')
  16. const Settings = require('@overleaf/settings')
  17. const AuthorizationManager = require('../Authorization/AuthorizationManager')
  18. const InactiveProjectManager = require('../InactiveData/InactiveProjectManager')
  19. const ProjectUpdateHandler = require('./ProjectUpdateHandler')
  20. const ProjectGetter = require('./ProjectGetter')
  21. const PrivilegeLevels = require('../Authorization/PrivilegeLevels')
  22. const SessionManager = require('../Authentication/SessionManager')
  23. const Sources = require('../Authorization/Sources')
  24. const TokenAccessHandler = require('../TokenAccess/TokenAccessHandler')
  25. const CollaboratorsGetter = require('../Collaborators/CollaboratorsGetter')
  26. const ProjectEntityHandler = require('./ProjectEntityHandler')
  27. const TpdsProjectFlusher = require('../ThirdPartyDataStore/TpdsProjectFlusher')
  28. const UserGetter = require('../User/UserGetter')
  29. const Features = require('../../infrastructure/Features')
  30. const BrandVariationsHandler = require('../BrandVariations/BrandVariationsHandler')
  31. const UserController = require('../User/UserController')
  32. const AnalyticsManager = require('../Analytics/AnalyticsManager')
  33. const SplitTestHandler = require('../SplitTests/SplitTestHandler')
  34. const FeaturesUpdater = require('../Subscription/FeaturesUpdater')
  35. const SpellingHandler = require('../Spelling/SpellingHandler')
  36. const { hasAdminAccess } = require('../Helpers/AdminAuthorizationHelper')
  37. const InstitutionsFeatures = require('../Institutions/InstitutionsFeatures')
  38. const ProjectAuditLogHandler = require('./ProjectAuditLogHandler')
  39. const PublicAccessLevels = require('../Authorization/PublicAccessLevels')
  40. const TagsHandler = require('../Tags/TagsHandler')
  41. /**
  42. * @typedef {import("./types").GetProjectsRequest} GetProjectsRequest
  43. * @typedef {import("./types").GetProjectsResponse} GetProjectsResponse
  44. * @typedef {import("./types").Project} Project
  45. */
  46. const ProjectController = {
  47. _isInPercentageRollout(rolloutName, objectId, percentage) {
  48. if (Settings.bypassPercentageRollouts === true) {
  49. return true
  50. }
  51. const data = `${rolloutName}:${objectId.toString()}`
  52. const md5hash = crypto.createHash('md5').update(data).digest('hex')
  53. const counter = parseInt(md5hash.slice(26, 32), 16)
  54. return counter % 100 < percentage
  55. },
  56. updateProjectSettings(req, res, next) {
  57. const projectId = req.params.Project_id
  58. const jobs = []
  59. if (req.body.compiler != null) {
  60. jobs.push(callback =>
  61. EditorController.setCompiler(projectId, req.body.compiler, callback)
  62. )
  63. }
  64. if (req.body.imageName != null) {
  65. jobs.push(callback =>
  66. EditorController.setImageName(projectId, req.body.imageName, callback)
  67. )
  68. }
  69. if (req.body.name != null) {
  70. jobs.push(callback =>
  71. EditorController.renameProject(projectId, req.body.name, callback)
  72. )
  73. }
  74. if (req.body.spellCheckLanguage != null) {
  75. jobs.push(callback =>
  76. EditorController.setSpellCheckLanguage(
  77. projectId,
  78. req.body.spellCheckLanguage,
  79. callback
  80. )
  81. )
  82. }
  83. if (req.body.rootDocId != null) {
  84. jobs.push(callback =>
  85. EditorController.setRootDoc(projectId, req.body.rootDocId, callback)
  86. )
  87. }
  88. async.series(jobs, error => {
  89. if (error != null) {
  90. return next(error)
  91. }
  92. res.sendStatus(204)
  93. })
  94. },
  95. updateProjectAdminSettings(req, res, next) {
  96. const projectId = req.params.Project_id
  97. const user = SessionManager.getSessionUser(req.session)
  98. const publicAccessLevel = req.body.publicAccessLevel
  99. const publicAccessLevels = [
  100. PublicAccessLevels.READ_ONLY,
  101. PublicAccessLevels.READ_AND_WRITE,
  102. PublicAccessLevels.PRIVATE,
  103. PublicAccessLevels.TOKEN_BASED,
  104. ]
  105. if (
  106. req.body.publicAccessLevel != null &&
  107. publicAccessLevels.includes(publicAccessLevel)
  108. ) {
  109. const jobs = []
  110. jobs.push(callback =>
  111. EditorController.setPublicAccessLevel(
  112. projectId,
  113. req.body.publicAccessLevel,
  114. callback
  115. )
  116. )
  117. jobs.push(callback =>
  118. ProjectAuditLogHandler.addEntry(
  119. projectId,
  120. 'toggle-access-level',
  121. user._id,
  122. { publicAccessLevel: req.body.publicAccessLevel, status: 'OK' },
  123. callback
  124. )
  125. )
  126. async.series(jobs, error => {
  127. if (error != null) {
  128. return next(error)
  129. }
  130. res.sendStatus(204)
  131. })
  132. } else {
  133. res.sendStatus(500)
  134. }
  135. },
  136. deleteProject(req, res) {
  137. const projectId = req.params.Project_id
  138. const user = SessionManager.getSessionUser(req.session)
  139. const cb = err => {
  140. if (err != null) {
  141. res.sendStatus(500)
  142. } else {
  143. res.sendStatus(200)
  144. }
  145. }
  146. ProjectDeleter.deleteProject(
  147. projectId,
  148. { deleterUser: user, ipAddress: req.ip },
  149. cb
  150. )
  151. },
  152. archiveProject(req, res, next) {
  153. const projectId = req.params.Project_id
  154. const userId = SessionManager.getLoggedInUserId(req.session)
  155. ProjectDeleter.archiveProject(projectId, userId, function (err) {
  156. if (err != null) {
  157. next(err)
  158. } else {
  159. res.sendStatus(200)
  160. }
  161. })
  162. },
  163. unarchiveProject(req, res, next) {
  164. const projectId = req.params.Project_id
  165. const userId = SessionManager.getLoggedInUserId(req.session)
  166. ProjectDeleter.unarchiveProject(projectId, userId, function (err) {
  167. if (err != null) {
  168. next(err)
  169. } else {
  170. res.sendStatus(200)
  171. }
  172. })
  173. },
  174. trashProject(req, res, next) {
  175. const projectId = req.params.project_id
  176. const userId = SessionManager.getLoggedInUserId(req.session)
  177. ProjectDeleter.trashProject(projectId, userId, function (err) {
  178. if (err != null) {
  179. next(err)
  180. } else {
  181. res.sendStatus(200)
  182. }
  183. })
  184. },
  185. untrashProject(req, res, next) {
  186. const projectId = req.params.project_id
  187. const userId = SessionManager.getLoggedInUserId(req.session)
  188. ProjectDeleter.untrashProject(projectId, userId, function (err) {
  189. if (err != null) {
  190. next(err)
  191. } else {
  192. res.sendStatus(200)
  193. }
  194. })
  195. },
  196. expireDeletedProjectsAfterDuration(req, res) {
  197. ProjectDeleter.expireDeletedProjectsAfterDuration(err => {
  198. if (err != null) {
  199. res.sendStatus(500)
  200. } else {
  201. res.sendStatus(200)
  202. }
  203. })
  204. },
  205. expireDeletedProject(req, res, next) {
  206. const { projectId } = req.params
  207. ProjectDeleter.expireDeletedProject(projectId, err => {
  208. if (err != null) {
  209. next(err)
  210. } else {
  211. res.sendStatus(200)
  212. }
  213. })
  214. },
  215. restoreProject(req, res) {
  216. const projectId = req.params.Project_id
  217. ProjectDeleter.restoreProject(projectId, err => {
  218. if (err != null) {
  219. res.sendStatus(500)
  220. } else {
  221. res.sendStatus(200)
  222. }
  223. })
  224. },
  225. cloneProject(req, res, next) {
  226. res.setTimeout(5 * 60 * 1000) // allow extra time for the copy to complete
  227. metrics.inc('cloned-project')
  228. const projectId = req.params.Project_id
  229. const { projectName, tags } = req.body
  230. logger.debug({ projectId, projectName }, 'cloning project')
  231. if (!SessionManager.isUserLoggedIn(req.session)) {
  232. return res.json({ redir: '/register' })
  233. }
  234. const currentUser = SessionManager.getSessionUser(req.session)
  235. const { first_name: firstName, last_name: lastName, email } = currentUser
  236. ProjectDuplicator.duplicate(
  237. currentUser,
  238. projectId,
  239. projectName,
  240. tags,
  241. (err, project) => {
  242. if (err != null) {
  243. OError.tag(err, 'error cloning project', {
  244. projectId,
  245. userId: currentUser._id,
  246. })
  247. return next(err)
  248. }
  249. res.json({
  250. name: project.name,
  251. lastUpdated: project.lastUpdated,
  252. project_id: project._id,
  253. owner_ref: project.owner_ref,
  254. owner: {
  255. first_name: firstName,
  256. last_name: lastName,
  257. email,
  258. _id: currentUser._id,
  259. },
  260. })
  261. }
  262. )
  263. },
  264. newProject(req, res, next) {
  265. const currentUser = SessionManager.getSessionUser(req.session)
  266. const {
  267. first_name: firstName,
  268. last_name: lastName,
  269. email,
  270. _id: userId,
  271. } = currentUser
  272. const projectName =
  273. req.body.projectName != null ? req.body.projectName.trim() : undefined
  274. const { template } = req.body
  275. async.waterfall(
  276. [
  277. cb => {
  278. if (template === 'example') {
  279. ProjectCreationHandler.createExampleProject(userId, projectName, cb)
  280. } else {
  281. ProjectCreationHandler.createBasicProject(userId, projectName, cb)
  282. }
  283. },
  284. ],
  285. (err, project) => {
  286. if (err != null) {
  287. return next(err)
  288. }
  289. res.json({
  290. project_id: project._id,
  291. owner_ref: project.owner_ref,
  292. owner: {
  293. first_name: firstName,
  294. last_name: lastName,
  295. email,
  296. _id: userId,
  297. },
  298. })
  299. }
  300. )
  301. },
  302. renameProject(req, res, next) {
  303. const projectId = req.params.Project_id
  304. const newName = req.body.newProjectName
  305. EditorController.renameProject(projectId, newName, err => {
  306. if (err != null) {
  307. return next(err)
  308. }
  309. res.sendStatus(200)
  310. })
  311. },
  312. userProjectsJson(req, res, next) {
  313. const userId = SessionManager.getLoggedInUserId(req.session)
  314. ProjectGetter.findAllUsersProjects(
  315. userId,
  316. 'name lastUpdated publicAccesLevel archived trashed owner_ref',
  317. (err, projects) => {
  318. if (err != null) {
  319. return next(err)
  320. }
  321. // _buildProjectList already converts archived/trashed to booleans so isArchivedOrTrashed should not be used here
  322. projects = ProjectController._buildProjectList(projects, userId)
  323. .filter(p => !(p.archived || p.trashed))
  324. .map(p => ({ _id: p.id, name: p.name, accessLevel: p.accessLevel }))
  325. res.json({ projects })
  326. }
  327. )
  328. },
  329. projectEntitiesJson(req, res, next) {
  330. const projectId = req.params.Project_id
  331. ProjectGetter.getProject(projectId, (err, project) => {
  332. if (err != null) {
  333. return next(err)
  334. }
  335. let docs, files
  336. try {
  337. ;({ docs, files } =
  338. ProjectEntityHandler.getAllEntitiesFromProject(project))
  339. } catch (err) {
  340. return next(err)
  341. }
  342. const entities = docs
  343. .concat(files)
  344. // Sort by path ascending
  345. .sort((a, b) => (a.path > b.path ? 1 : a.path < b.path ? -1 : 0))
  346. .map(e => ({
  347. path: e.path,
  348. type: e.doc != null ? 'doc' : 'file',
  349. }))
  350. res.json({ project_id: projectId, entities })
  351. })
  352. },
  353. loadEditor(req, res, next) {
  354. const timer = new metrics.Timer('load-editor')
  355. if (!Settings.editorIsOpen) {
  356. return res.render('general/closed', { title: 'updating_site' })
  357. }
  358. let anonymous, userId, sessionUser
  359. if (SessionManager.isUserLoggedIn(req.session)) {
  360. sessionUser = SessionManager.getSessionUser(req.session)
  361. userId = SessionManager.getLoggedInUserId(req.session)
  362. anonymous = false
  363. } else {
  364. sessionUser = null
  365. anonymous = true
  366. userId = null
  367. }
  368. const projectId = req.params.Project_id
  369. async.auto(
  370. {
  371. project(cb) {
  372. ProjectGetter.getProject(
  373. projectId,
  374. {
  375. name: 1,
  376. lastUpdated: 1,
  377. track_changes: 1,
  378. owner_ref: 1,
  379. brandVariationId: 1,
  380. overleaf: 1,
  381. tokens: 1,
  382. },
  383. (err, project) => {
  384. if (err != null) {
  385. return cb(err)
  386. }
  387. cb(null, project)
  388. }
  389. )
  390. },
  391. user(cb) {
  392. if (userId == null) {
  393. SplitTestHandler.sessionMaintenance(req, null, () => {})
  394. cb(null, defaultSettingsForAnonymousUser(userId))
  395. } else {
  396. User.updateOne(
  397. { _id: ObjectId(userId) },
  398. { $set: { lastActive: new Date() } },
  399. {},
  400. () => {}
  401. )
  402. User.findById(
  403. userId,
  404. 'email first_name last_name referal_id signUpDate featureSwitches features featuresEpoch refProviders alphaProgram betaProgram isAdmin ace labsProgram completedTutorials',
  405. (err, user) => {
  406. // Handle case of deleted user
  407. if (user == null) {
  408. UserController.logout(req, res, next)
  409. return
  410. }
  411. if (err) {
  412. return cb(err)
  413. }
  414. logger.debug({ projectId, userId }, 'got user')
  415. SplitTestHandler.sessionMaintenance(req, user, () => {})
  416. if (FeaturesUpdater.featuresEpochIsCurrent(user)) {
  417. return cb(null, user)
  418. }
  419. ProjectController._refreshFeatures(req, user, cb)
  420. }
  421. )
  422. }
  423. },
  424. userHasInstitutionLicence(cb) {
  425. if (!userId) {
  426. return cb(null, false)
  427. }
  428. InstitutionsFeatures.hasLicence(userId, (error, hasLicence) => {
  429. if (error) {
  430. // Don't fail if we can't get affiliation licences
  431. return cb(null, false)
  432. }
  433. cb(null, hasLicence)
  434. })
  435. },
  436. learnedWords(cb) {
  437. if (!userId) {
  438. return cb(null, [])
  439. }
  440. SpellingHandler.getUserDictionary(userId, cb)
  441. },
  442. subscription(cb) {
  443. if (userId == null) {
  444. return cb()
  445. }
  446. SubscriptionLocator.getUsersSubscription(userId, cb)
  447. },
  448. userIsMemberOfGroupSubscription(cb) {
  449. if (sessionUser == null) {
  450. return cb(null, false)
  451. }
  452. LimitationsManager.userIsMemberOfGroupSubscription(
  453. sessionUser,
  454. (error, isMember) => {
  455. cb(error, isMember)
  456. }
  457. )
  458. },
  459. activate(cb) {
  460. InactiveProjectManager.reactivateProjectIfRequired(projectId, cb)
  461. },
  462. markAsOpened(cb) {
  463. // don't need to wait for this to complete
  464. ProjectUpdateHandler.markAsOpened(projectId, () => {})
  465. cb()
  466. },
  467. isTokenMember(cb) {
  468. if (userId == null) {
  469. return cb()
  470. }
  471. CollaboratorsGetter.userIsTokenMember(userId, projectId, cb)
  472. },
  473. isInvitedMember(cb) {
  474. CollaboratorsGetter.isUserInvitedMemberOfProject(
  475. userId,
  476. projectId,
  477. cb
  478. )
  479. },
  480. brandVariation: [
  481. 'project',
  482. (results, cb) => {
  483. if (
  484. (results.project != null
  485. ? results.project.brandVariationId
  486. : undefined) == null
  487. ) {
  488. return cb()
  489. }
  490. BrandVariationsHandler.getBrandVariationById(
  491. results.project.brandVariationId,
  492. (error, brandVariationDetails) => cb(error, brandVariationDetails)
  493. )
  494. },
  495. ],
  496. flushToTpds: cb => {
  497. TpdsProjectFlusher.flushProjectToTpdsIfNeeded(projectId, cb)
  498. },
  499. sharingModalSplitTest(cb) {
  500. SplitTestHandler.getAssignment(
  501. req,
  502. res,
  503. 'project-share-modal-paywall',
  504. {},
  505. () => {
  506. // do not fail editor load if assignment fails
  507. cb()
  508. }
  509. )
  510. },
  511. sharingModalNullTest(cb) {
  512. // null test targeting logged in users, for front-end side
  513. SplitTestHandler.getAssignment(
  514. req,
  515. res,
  516. 'null-test-share-modal',
  517. {},
  518. () => {
  519. // do not fail editor load if assignment fails
  520. cb()
  521. }
  522. )
  523. },
  524. pdfjsAssignment(cb) {
  525. SplitTestHandler.getAssignment(
  526. req,
  527. res,
  528. 'pdfjs-36',
  529. {},
  530. (error, assignment) => {
  531. // do not fail editor load if assignment fails
  532. if (error) {
  533. cb(null, { variant: 'default' })
  534. } else {
  535. cb(null, assignment)
  536. }
  537. }
  538. )
  539. },
  540. latexLogParserAssignment(cb) {
  541. SplitTestHandler.getAssignment(
  542. req,
  543. res,
  544. 'latex-log-parser',
  545. (error, assignment) => {
  546. // do not fail editor load if assignment fails
  547. if (error) {
  548. cb(null, { variant: 'default' })
  549. } else {
  550. cb(null, assignment)
  551. }
  552. }
  553. )
  554. },
  555. trackPdfDownloadAssignment(cb) {
  556. SplitTestHandler.getAssignment(req, res, 'track-pdf-download', () => {
  557. // We'll pick up the assignment from the res.locals assignment.
  558. cb()
  559. })
  560. },
  561. pdfCachingModeAssignment(cb) {
  562. SplitTestHandler.getAssignment(req, res, 'pdf-caching-mode', () => {
  563. // We'll pick up the assignment from the res.locals assignment.
  564. cb()
  565. })
  566. },
  567. pdfCachingPrefetchingAssignment(cb) {
  568. SplitTestHandler.getAssignment(
  569. req,
  570. res,
  571. 'pdf-caching-prefetching',
  572. () => {
  573. // We'll pick up the assignment from the res.locals assignment.
  574. cb()
  575. }
  576. )
  577. },
  578. pdfCachingPrefetchLargeAssignment(cb) {
  579. SplitTestHandler.getAssignment(
  580. req,
  581. res,
  582. 'pdf-caching-prefetch-large',
  583. () => {
  584. // We'll pick up the assignment from the res.locals assignment.
  585. cb()
  586. }
  587. )
  588. },
  589. pdfCachingCachedUrlLookupAssignment(cb) {
  590. SplitTestHandler.getAssignment(
  591. req,
  592. res,
  593. 'pdf-caching-cached-url-lookup',
  594. () => {
  595. // We'll pick up the assignment from the res.locals assignment.
  596. cb()
  597. }
  598. )
  599. },
  600. editorLeftMenuAssignment(cb) {
  601. SplitTestHandler.getAssignment(
  602. req,
  603. res,
  604. 'editor-left-menu',
  605. (error, assignment) => {
  606. // do not fail editor load if assignment fails
  607. if (error) {
  608. cb(null, { variant: 'default' })
  609. } else {
  610. cb(null, assignment)
  611. }
  612. }
  613. )
  614. },
  615. // this is only needed until the survey link is removed from the toolbar
  616. richTextAssignment(cb) {
  617. SplitTestHandler.getAssignment(
  618. req,
  619. res,
  620. 'rich-text',
  621. (error, assignment) => {
  622. // do not fail editor load if assignment fails
  623. if (error) {
  624. cb(null, { variant: 'default' })
  625. } else {
  626. cb(null, assignment)
  627. }
  628. }
  629. )
  630. },
  631. figureModalAssignment(cb) {
  632. SplitTestHandler.getAssignment(req, res, 'figure-modal', () => {
  633. // We'll pick up the assignment from the res.locals assignment.
  634. cb()
  635. })
  636. },
  637. tableGeneratorAssignment(cb) {
  638. SplitTestHandler.getAssignment(req, res, 'table-generator', () => {
  639. // We'll pick up the assignment from the res.locals assignment.
  640. cb()
  641. })
  642. },
  643. tableGeneratorPromotionAssignment(cb) {
  644. SplitTestHandler.getAssignment(
  645. req,
  646. res,
  647. 'table-generator-promotion',
  648. () => {
  649. // We'll pick up the assignment from the res.locals assignment.
  650. cb()
  651. }
  652. )
  653. },
  654. pasteHtmlAssignment(cb) {
  655. SplitTestHandler.getAssignment(req, res, 'paste-html', () => {
  656. // We'll pick up the assignment from the res.locals assignment.
  657. cb()
  658. })
  659. },
  660. sourceEditorToolbarAssigment(cb) {
  661. SplitTestHandler.getAssignment(
  662. req,
  663. res,
  664. 'source-editor-toolbar',
  665. (error, assignment) => {
  666. // do not fail editor load if assignment fails
  667. if (error) {
  668. cb(null, { variant: 'default' })
  669. } else {
  670. cb(null, assignment)
  671. }
  672. }
  673. )
  674. },
  675. historyViewAssignment(cb) {
  676. SplitTestHandler.getAssignment(
  677. req,
  678. res,
  679. 'history-view-version',
  680. (error, assignment) => {
  681. // do not fail editor load if assignment fails
  682. if (error) {
  683. cb(null, { variant: 'default' })
  684. } else {
  685. cb(null, assignment)
  686. }
  687. }
  688. )
  689. },
  690. reviewPanelAssignment(cb) {
  691. SplitTestHandler.getAssignment(
  692. req,
  693. res,
  694. 'review-panel',
  695. (error, assignment) => {
  696. // do not fail editor load if assignment fails
  697. if (error) {
  698. cb(null, { variant: 'default' })
  699. } else {
  700. cb(null, assignment)
  701. }
  702. }
  703. )
  704. },
  705. projectTags(cb) {
  706. if (!userId) {
  707. return cb(null, [])
  708. }
  709. TagsHandler.getTagsForProject(userId, projectId, cb)
  710. },
  711. },
  712. (
  713. err,
  714. {
  715. project,
  716. user,
  717. userHasInstitutionLicence,
  718. learnedWords,
  719. subscription,
  720. userIsMemberOfGroupSubscription,
  721. isTokenMember,
  722. isInvitedMember,
  723. brandVariation,
  724. pdfjsAssignment,
  725. editorLeftMenuAssignment,
  726. sourceEditorToolbarAssigment,
  727. historyViewAssignment,
  728. reviewPanelAssignment,
  729. projectTags,
  730. }
  731. ) => {
  732. if (err != null) {
  733. OError.tag(err, 'error getting details for project page')
  734. return next(err)
  735. }
  736. const anonRequestToken = TokenAccessHandler.getRequestToken(
  737. req,
  738. projectId
  739. )
  740. const allowedImageNames = ProjectHelper.getAllowedImagesForUser(user)
  741. AuthorizationManager.getPrivilegeLevelForProject(
  742. userId,
  743. projectId,
  744. anonRequestToken,
  745. (error, privilegeLevel) => {
  746. let allowedFreeTrial = true
  747. if (error != null) {
  748. return next(error)
  749. }
  750. if (
  751. privilegeLevel == null ||
  752. privilegeLevel === PrivilegeLevels.NONE
  753. ) {
  754. return res.sendStatus(401)
  755. }
  756. if (subscription != null) {
  757. allowedFreeTrial = false
  758. }
  759. let wsUrl = Settings.wsUrl
  760. let metricName = 'load-editor-ws'
  761. if (user.betaProgram && Settings.wsUrlBeta !== undefined) {
  762. wsUrl = Settings.wsUrlBeta
  763. metricName += '-beta'
  764. } else if (
  765. Settings.wsUrlV2 &&
  766. Settings.wsUrlV2Percentage > 0 &&
  767. (ObjectId(projectId).getTimestamp() / 1000) % 100 <
  768. Settings.wsUrlV2Percentage
  769. ) {
  770. wsUrl = Settings.wsUrlV2
  771. metricName += '-v2'
  772. }
  773. if (req.query && req.query.ws === 'fallback') {
  774. // `?ws=fallback` will connect to the bare origin, and ignore
  775. // the custom wsUrl. Hence it must load the client side
  776. // javascript from there too.
  777. // Not resetting it here would possibly load a socket.io v2
  778. // client and connect to a v0 endpoint.
  779. wsUrl = undefined
  780. metricName += '-fallback'
  781. }
  782. metrics.inc(metricName)
  783. if (userId) {
  784. AnalyticsManager.recordEventForUser(userId, 'project-opened', {
  785. projectId: project._id,
  786. })
  787. }
  788. // should not be used in place of split tests query param overrides (?my-split-test-name=my-variant)
  789. function shouldDisplayFeature(name, variantFlag) {
  790. if (req.query && req.query[name]) {
  791. return req.query[name] === 'true'
  792. } else {
  793. return variantFlag === true
  794. }
  795. }
  796. const isAdminOrTemplateOwner =
  797. hasAdminAccess(user) || Settings.templates?.user_id === userId
  798. const showTemplatesServerPro =
  799. Features.hasFeature('templates-server-pro') &&
  800. isAdminOrTemplateOwner
  801. const debugPdfDetach = shouldDisplayFeature('debug_pdf_detach')
  802. const detachRole = req.params.detachRole
  803. const showLegacySourceEditor =
  804. !Features.hasFeature('saas') ||
  805. // Allow override via legacy_source_editor=true in query string
  806. shouldDisplayFeature('legacy_source_editor')
  807. const editorLeftMenuReact =
  808. editorLeftMenuAssignment?.variant === 'react'
  809. const showSymbolPalette =
  810. !Features.hasFeature('saas') ||
  811. (user.features && user.features.symbolPalette)
  812. // Persistent upgrade prompts
  813. // in header & in share project modal
  814. const showUpgradePrompt =
  815. Features.hasFeature('saas') &&
  816. userId &&
  817. !subscription &&
  818. !userIsMemberOfGroupSubscription &&
  819. !userHasInstitutionLicence
  820. const showPersonalAccessToken =
  821. !Features.hasFeature('saas') ||
  822. req.query?.personal_access_token === 'true'
  823. const idePageReact = req.query?.['ide-page'] === 'react'
  824. const template =
  825. detachRole === 'detached'
  826. ? // TODO: Create React version of detached page
  827. 'project/editor_detached'
  828. : idePageReact
  829. ? 'project/ide-react'
  830. : 'project/editor'
  831. res.render(template, {
  832. title: project.name,
  833. priority_title: true,
  834. bodyClasses: ['editor'],
  835. project_id: project._id,
  836. projectName: project.name,
  837. editorLeftMenuReact,
  838. user: {
  839. id: userId,
  840. email: user.email,
  841. first_name: user.first_name,
  842. last_name: user.last_name,
  843. referal_id: user.referal_id,
  844. signUpDate: user.signUpDate,
  845. allowedFreeTrial,
  846. featureSwitches: user.featureSwitches,
  847. features: user.features,
  848. refProviders: _.mapValues(user.refProviders, Boolean),
  849. alphaProgram: user.alphaProgram,
  850. betaProgram: user.betaProgram,
  851. labsProgram: user.labsProgram,
  852. completedTutorials: user.completedTutorials,
  853. isAdmin: hasAdminAccess(user),
  854. },
  855. userSettings: {
  856. mode: user.ace.mode,
  857. editorTheme: user.ace.theme,
  858. fontSize: user.ace.fontSize,
  859. autoComplete: user.ace.autoComplete,
  860. autoPairDelimiters: user.ace.autoPairDelimiters,
  861. pdfViewer: user.ace.pdfViewer,
  862. syntaxValidation: user.ace.syntaxValidation,
  863. fontFamily: user.ace.fontFamily || 'lucida',
  864. lineHeight: user.ace.lineHeight || 'normal',
  865. overallTheme: user.ace.overallTheme,
  866. },
  867. privilegeLevel,
  868. anonymous,
  869. anonymousAccessToken: anonymous ? anonRequestToken : null,
  870. isTokenMember,
  871. isRestrictedTokenMember: AuthorizationManager.isRestrictedUser(
  872. userId,
  873. privilegeLevel,
  874. isTokenMember,
  875. isInvitedMember
  876. ),
  877. languages: Settings.languages,
  878. learnedWords,
  879. editorThemes: THEME_LIST,
  880. legacyEditorThemes: LEGACY_THEME_LIST,
  881. maxDocLength: Settings.max_doc_length,
  882. brandVariation,
  883. allowedImageNames,
  884. gitBridgePublicBaseUrl: Settings.gitBridgePublicBaseUrl,
  885. wsUrl,
  886. showSupport: Features.hasFeature('support'),
  887. showTemplatesServerPro,
  888. pdfjsVariant: pdfjsAssignment.variant,
  889. debugPdfDetach,
  890. showLegacySourceEditor,
  891. showSourceToolbar:
  892. !showLegacySourceEditor &&
  893. sourceEditorToolbarAssigment.variant === 'enabled',
  894. showSymbolPalette,
  895. symbolPaletteAvailable: Features.hasFeature('symbol-palette'),
  896. detachRole,
  897. metadata: { viewport: false },
  898. showUpgradePrompt,
  899. fixedSizeDocument: true,
  900. useOpenTelemetry: Settings.useOpenTelemetryClient,
  901. showCM6SwitchAwaySurvey: Settings.showCM6SwitchAwaySurvey,
  902. historyViewReact: historyViewAssignment.variant === 'react',
  903. isReviewPanelReact: reviewPanelAssignment.variant === 'react',
  904. showPersonalAccessToken,
  905. hasTrackChangesFeature: Features.hasFeature('track-changes'),
  906. projectTags,
  907. })
  908. timer.done()
  909. }
  910. )
  911. }
  912. )
  913. },
  914. _refreshFeatures(req, user, callback) {
  915. // If the feature refresh has failed in this session, don't retry
  916. // it - require the user to log in again.
  917. if (req.session.feature_refresh_failed) {
  918. metrics.inc('features-refresh', 1, {
  919. path: 'load-editor',
  920. status: 'skipped',
  921. })
  922. return callback(null, user)
  923. }
  924. // If the refresh takes too long then return the current
  925. // features. Note that the user.features property may still be
  926. // updated in the background after the callback is called.
  927. callback = _.once(callback)
  928. const refreshTimeoutHandler = setTimeout(() => {
  929. req.session.feature_refresh_failed = { reason: 'timeout', at: new Date() }
  930. metrics.inc('features-refresh', 1, {
  931. path: 'load-editor',
  932. status: 'timeout',
  933. })
  934. callback(null, user)
  935. }, 5000)
  936. // try to refresh user features now
  937. const timer = new metrics.Timer('features-refresh-on-load-editor')
  938. FeaturesUpdater.refreshFeatures(
  939. user._id,
  940. 'load-editor',
  941. (err, features) => {
  942. clearTimeout(refreshTimeoutHandler)
  943. timer.done()
  944. if (err) {
  945. // keep a record to prevent unneceary retries and leave
  946. // the original features unmodified if the refresh failed
  947. req.session.feature_refresh_failed = {
  948. reason: 'error',
  949. at: new Date(),
  950. }
  951. metrics.inc('features-refresh', 1, {
  952. path: 'load-editor',
  953. status: 'error',
  954. })
  955. } else {
  956. user.features = features
  957. metrics.inc('features-refresh', 1, {
  958. path: 'load-editor',
  959. status: 'success',
  960. })
  961. }
  962. callback(null, user)
  963. }
  964. )
  965. },
  966. _buildProjectList(allProjects, userId) {
  967. let project
  968. const { owned, readAndWrite, readOnly, tokenReadAndWrite, tokenReadOnly } =
  969. allProjects
  970. const projects = []
  971. for (project of owned) {
  972. projects.push(
  973. ProjectController._buildProjectViewModel(
  974. project,
  975. 'owner',
  976. Sources.OWNER,
  977. userId
  978. )
  979. )
  980. }
  981. // Invite-access
  982. for (project of readAndWrite) {
  983. projects.push(
  984. ProjectController._buildProjectViewModel(
  985. project,
  986. 'readWrite',
  987. Sources.INVITE,
  988. userId
  989. )
  990. )
  991. }
  992. for (project of readOnly) {
  993. projects.push(
  994. ProjectController._buildProjectViewModel(
  995. project,
  996. 'readOnly',
  997. Sources.INVITE,
  998. userId
  999. )
  1000. )
  1001. }
  1002. // Token-access
  1003. // Only add these projects if they're not already present, this gives us cascading access
  1004. // from 'owner' => 'token-read-only'
  1005. for (project of tokenReadAndWrite) {
  1006. if (
  1007. projects.filter(p => p.id.toString() === project._id.toString())
  1008. .length === 0
  1009. ) {
  1010. projects.push(
  1011. ProjectController._buildProjectViewModel(
  1012. project,
  1013. 'readAndWrite',
  1014. Sources.TOKEN,
  1015. userId
  1016. )
  1017. )
  1018. }
  1019. }
  1020. for (project of tokenReadOnly) {
  1021. if (
  1022. projects.filter(p => p.id.toString() === project._id.toString())
  1023. .length === 0
  1024. ) {
  1025. projects.push(
  1026. ProjectController._buildProjectViewModel(
  1027. project,
  1028. 'readOnly',
  1029. Sources.TOKEN,
  1030. userId
  1031. )
  1032. )
  1033. }
  1034. }
  1035. return projects
  1036. },
  1037. _buildProjectViewModel(project, accessLevel, source, userId) {
  1038. const archived = ProjectHelper.isArchived(project, userId)
  1039. // If a project is simultaneously trashed and archived, we will consider it archived but not trashed.
  1040. const trashed = ProjectHelper.isTrashed(project, userId) && !archived
  1041. const model = {
  1042. id: project._id,
  1043. name: project.name,
  1044. lastUpdated: project.lastUpdated,
  1045. lastUpdatedBy: project.lastUpdatedBy,
  1046. publicAccessLevel: project.publicAccesLevel,
  1047. accessLevel,
  1048. source,
  1049. archived,
  1050. trashed,
  1051. owner_ref: project.owner_ref,
  1052. isV1Project: false,
  1053. }
  1054. if (accessLevel === PrivilegeLevels.READ_ONLY && source === Sources.TOKEN) {
  1055. model.owner_ref = null
  1056. model.lastUpdatedBy = null
  1057. }
  1058. return model
  1059. },
  1060. _injectProjectUsers(projects, callback) {
  1061. const users = {}
  1062. for (const project of projects) {
  1063. if (project.owner_ref != null) {
  1064. users[project.owner_ref.toString()] = true
  1065. }
  1066. if (project.lastUpdatedBy != null) {
  1067. users[project.lastUpdatedBy.toString()] = true
  1068. }
  1069. }
  1070. const userIds = Object.keys(users)
  1071. async.eachSeries(
  1072. userIds,
  1073. (userId, cb) => {
  1074. UserGetter.getUser(
  1075. userId,
  1076. { first_name: 1, last_name: 1, email: 1 },
  1077. (error, user) => {
  1078. if (error != null) {
  1079. return cb(error)
  1080. }
  1081. users[userId] = user
  1082. cb()
  1083. }
  1084. )
  1085. },
  1086. error => {
  1087. if (error != null) {
  1088. return callback(error)
  1089. }
  1090. for (const project of projects) {
  1091. if (project.owner_ref != null) {
  1092. project.owner = users[project.owner_ref.toString()]
  1093. }
  1094. if (project.lastUpdatedBy != null) {
  1095. project.lastUpdatedBy =
  1096. users[project.lastUpdatedBy.toString()] || null
  1097. }
  1098. }
  1099. callback(null, projects)
  1100. }
  1101. )
  1102. },
  1103. _buildPortalTemplatesList(affiliations) {
  1104. if (affiliations == null) {
  1105. affiliations = []
  1106. }
  1107. const portalTemplates = []
  1108. for (const aff of affiliations) {
  1109. if (
  1110. aff.portal &&
  1111. aff.portal.slug &&
  1112. aff.portal.templates_count &&
  1113. aff.portal.templates_count > 0
  1114. ) {
  1115. const portalPath = aff.institution.isUniversity ? '/edu/' : '/org/'
  1116. portalTemplates.push({
  1117. name: aff.institution.name,
  1118. url: Settings.siteUrl + portalPath + aff.portal.slug,
  1119. })
  1120. }
  1121. }
  1122. return portalTemplates
  1123. },
  1124. }
  1125. const defaultSettingsForAnonymousUser = userId => ({
  1126. id: userId,
  1127. ace: {
  1128. mode: 'none',
  1129. theme: 'textmate',
  1130. fontSize: '12',
  1131. autoComplete: true,
  1132. spellCheckLanguage: '',
  1133. pdfViewer: '',
  1134. syntaxValidation: true,
  1135. },
  1136. subscription: {
  1137. freeTrial: {
  1138. allowed: true,
  1139. },
  1140. },
  1141. featureSwitches: {
  1142. github: false,
  1143. },
  1144. alphaProgram: false,
  1145. betaProgram: false,
  1146. })
  1147. const THEME_LIST = [
  1148. 'cobalt',
  1149. 'dracula',
  1150. 'eclipse',
  1151. 'monokai',
  1152. 'overleaf',
  1153. 'textmate',
  1154. ]
  1155. const LEGACY_THEME_LIST = [
  1156. 'ambiance',
  1157. 'chaos',
  1158. 'chrome',
  1159. 'clouds',
  1160. 'clouds_midnight',
  1161. 'crimson_editor',
  1162. 'dawn',
  1163. 'dreamweaver',
  1164. 'github',
  1165. 'gob',
  1166. 'gruvbox',
  1167. 'idle_fingers',
  1168. 'iplastic',
  1169. 'katzenmilch',
  1170. 'kr_theme',
  1171. 'kuroir',
  1172. 'merbivore',
  1173. 'merbivore_soft',
  1174. 'mono_industrial',
  1175. 'nord_dark',
  1176. 'pastel_on_dark',
  1177. 'solarized_dark',
  1178. 'solarized_light',
  1179. 'sqlserver',
  1180. 'terminal',
  1181. 'tomorrow',
  1182. 'tomorrow_night',
  1183. 'tomorrow_night_blue',
  1184. 'tomorrow_night_bright',
  1185. 'tomorrow_night_eighties',
  1186. 'twilight',
  1187. 'vibrant_ink',
  1188. 'xcode',
  1189. ]
  1190. module.exports = ProjectController