ProjectController.js 37 KB

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