ProjectController.js 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  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 VISUAL_EDITOR_NAMING_SPLIT_TEST_MIN_SIGNUP_DATE = new Date('2023-04-17')
  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 } = 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. (err, project) => {
  241. if (err != null) {
  242. OError.tag(err, 'error cloning project', {
  243. projectId,
  244. userId: currentUser._id,
  245. })
  246. return next(err)
  247. }
  248. res.json({
  249. name: project.name,
  250. lastUpdated: project.lastUpdated,
  251. project_id: project._id,
  252. owner_ref: project.owner_ref,
  253. owner: {
  254. first_name: firstName,
  255. last_name: lastName,
  256. email,
  257. _id: currentUser._id,
  258. },
  259. })
  260. }
  261. )
  262. },
  263. newProject(req, res, next) {
  264. const currentUser = SessionManager.getSessionUser(req.session)
  265. const {
  266. first_name: firstName,
  267. last_name: lastName,
  268. email,
  269. _id: userId,
  270. } = currentUser
  271. const projectName =
  272. req.body.projectName != null ? req.body.projectName.trim() : undefined
  273. const { template } = req.body
  274. async.waterfall(
  275. [
  276. cb => {
  277. if (template === 'example') {
  278. ProjectCreationHandler.createExampleProject(userId, projectName, cb)
  279. } else {
  280. ProjectCreationHandler.createBasicProject(userId, projectName, cb)
  281. }
  282. },
  283. ],
  284. (err, project) => {
  285. if (err != null) {
  286. return next(err)
  287. }
  288. res.json({
  289. project_id: project._id,
  290. owner_ref: project.owner_ref,
  291. owner: {
  292. first_name: firstName,
  293. last_name: lastName,
  294. email,
  295. _id: userId,
  296. },
  297. })
  298. }
  299. )
  300. },
  301. renameProject(req, res, next) {
  302. const projectId = req.params.Project_id
  303. const newName = req.body.newProjectName
  304. EditorController.renameProject(projectId, newName, err => {
  305. if (err != null) {
  306. return next(err)
  307. }
  308. res.sendStatus(200)
  309. })
  310. },
  311. userProjectsJson(req, res, next) {
  312. const userId = SessionManager.getLoggedInUserId(req.session)
  313. ProjectGetter.findAllUsersProjects(
  314. userId,
  315. 'name lastUpdated publicAccesLevel archived trashed owner_ref tokens',
  316. (err, projects) => {
  317. if (err != null) {
  318. return next(err)
  319. }
  320. // _buildProjectList already converts archived/trashed to booleans so isArchivedOrTrashed should not be used here
  321. projects = ProjectController._buildProjectList(projects, userId)
  322. .filter(p => !(p.archived || p.trashed))
  323. .map(p => ({ _id: p.id, name: p.name, accessLevel: p.accessLevel }))
  324. res.json({ projects })
  325. }
  326. )
  327. },
  328. projectEntitiesJson(req, res, next) {
  329. const projectId = req.params.Project_id
  330. ProjectGetter.getProject(projectId, (err, project) => {
  331. if (err != null) {
  332. return next(err)
  333. }
  334. let docs, files
  335. try {
  336. ;({ docs, files } =
  337. ProjectEntityHandler.getAllEntitiesFromProject(project))
  338. } catch (err) {
  339. return next(err)
  340. }
  341. const entities = docs
  342. .concat(files)
  343. // Sort by path ascending
  344. .sort((a, b) => (a.path > b.path ? 1 : a.path < b.path ? -1 : 0))
  345. .map(e => ({
  346. path: e.path,
  347. type: e.doc != null ? 'doc' : 'file',
  348. }))
  349. res.json({ project_id: projectId, entities })
  350. })
  351. },
  352. loadEditor(req, res, next) {
  353. const timer = new metrics.Timer('load-editor')
  354. if (!Settings.editorIsOpen) {
  355. return res.render('general/closed', { title: 'updating_site' })
  356. }
  357. let anonymous, userId, sessionUser
  358. if (SessionManager.isUserLoggedIn(req.session)) {
  359. sessionUser = SessionManager.getSessionUser(req.session)
  360. userId = SessionManager.getLoggedInUserId(req.session)
  361. anonymous = false
  362. } else {
  363. sessionUser = null
  364. anonymous = true
  365. userId = null
  366. }
  367. const projectId = req.params.Project_id
  368. async.auto(
  369. {
  370. project(cb) {
  371. ProjectGetter.getProject(
  372. projectId,
  373. {
  374. name: 1,
  375. lastUpdated: 1,
  376. track_changes: 1,
  377. owner_ref: 1,
  378. brandVariationId: 1,
  379. overleaf: 1,
  380. tokens: 1,
  381. },
  382. (err, project) => {
  383. if (err != null) {
  384. return cb(err)
  385. }
  386. cb(null, project)
  387. }
  388. )
  389. },
  390. user(cb) {
  391. if (userId == 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 labsProgramGalileo',
  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. if (FeaturesUpdater.featuresEpochIsCurrent(user)) {
  414. return cb(null, user)
  415. }
  416. ProjectController._refreshFeatures(req, user, cb)
  417. }
  418. )
  419. }
  420. },
  421. userHasInstitutionLicence(cb) {
  422. if (!userId) {
  423. return cb(null, false)
  424. }
  425. InstitutionsFeatures.hasLicence(userId, (error, hasLicence) => {
  426. if (error) {
  427. // Don't fail if we can't get affiliation licences
  428. return cb(null, false)
  429. }
  430. cb(null, hasLicence)
  431. })
  432. },
  433. learnedWords(cb) {
  434. if (!userId) {
  435. return cb(null, [])
  436. }
  437. SpellingHandler.getUserDictionary(userId, cb)
  438. },
  439. subscription(cb) {
  440. if (userId == null) {
  441. return cb()
  442. }
  443. SubscriptionLocator.getUsersSubscription(userId, cb)
  444. },
  445. userIsMemberOfGroupSubscription(cb) {
  446. if (sessionUser == null) {
  447. return cb(null, false)
  448. }
  449. LimitationsManager.userIsMemberOfGroupSubscription(
  450. sessionUser,
  451. (error, isMember) => {
  452. cb(error, isMember)
  453. }
  454. )
  455. },
  456. activate(cb) {
  457. InactiveProjectManager.reactivateProjectIfRequired(projectId, cb)
  458. },
  459. markAsOpened(cb) {
  460. // don't need to wait for this to complete
  461. ProjectUpdateHandler.markAsOpened(projectId, () => {})
  462. cb()
  463. },
  464. isTokenMember(cb) {
  465. if (userId == null) {
  466. return cb()
  467. }
  468. CollaboratorsGetter.userIsTokenMember(userId, projectId, cb)
  469. },
  470. isInvitedMember(cb) {
  471. CollaboratorsGetter.isUserInvitedMemberOfProject(
  472. userId,
  473. projectId,
  474. cb
  475. )
  476. },
  477. brandVariation: [
  478. 'project',
  479. (results, cb) => {
  480. if (
  481. (results.project != null
  482. ? results.project.brandVariationId
  483. : undefined) == null
  484. ) {
  485. return cb()
  486. }
  487. BrandVariationsHandler.getBrandVariationById(
  488. results.project.brandVariationId,
  489. (error, brandVariationDetails) => cb(error, brandVariationDetails)
  490. )
  491. },
  492. ],
  493. flushToTpds: cb => {
  494. TpdsProjectFlusher.flushProjectToTpdsIfNeeded(projectId, cb)
  495. },
  496. sharingModalSplitTest(cb) {
  497. SplitTestHandler.getAssignment(
  498. req,
  499. res,
  500. 'project-share-modal-paywall',
  501. {},
  502. () => {
  503. // do not fail editor load if assignment fails
  504. cb()
  505. }
  506. )
  507. },
  508. sharingModalNullTest(cb) {
  509. // null test targeting logged in users, for front-end side
  510. SplitTestHandler.getAssignment(
  511. req,
  512. res,
  513. 'null-test-share-modal',
  514. {},
  515. () => {
  516. // do not fail editor load if assignment fails
  517. cb()
  518. }
  519. )
  520. },
  521. participatingInVisualEditorNamingTest: [
  522. 'user',
  523. (results, cb) => {
  524. const isNewUser =
  525. results.user.signUpDate >=
  526. VISUAL_EDITOR_NAMING_SPLIT_TEST_MIN_SIGNUP_DATE
  527. cb(null, isNewUser)
  528. },
  529. ],
  530. visualEditorNameAssignment: [
  531. 'participatingInVisualEditorNamingTest',
  532. (results, cb) => {
  533. if (!results.participatingInVisualEditorNamingTest) {
  534. cb(null, { variant: 'default' })
  535. } else {
  536. SplitTestHandler.getAssignment(
  537. req,
  538. res,
  539. 'visual-editor-name',
  540. (error, assignment) => {
  541. if (error) {
  542. cb(null, { variant: 'default' })
  543. } else {
  544. cb(null, assignment)
  545. }
  546. }
  547. )
  548. }
  549. },
  550. ],
  551. legacySourceEditorAssignment: [
  552. 'participatingInVisualEditorNamingTest',
  553. 'visualEditorNameAssignment',
  554. (results, cb) => {
  555. // Hide Ace for people in the Rich Text naming test
  556. if (results.participatingInVisualEditorNamingTest) {
  557. cb(null, { variant: 'true' })
  558. } else {
  559. SplitTestHandler.getAssignment(
  560. req,
  561. res,
  562. 'source-editor-legacy',
  563. (error, assignment) => {
  564. // do not fail editor load if assignment fails
  565. if (error) {
  566. cb(null, { variant: 'default' })
  567. } else {
  568. cb(null, assignment)
  569. }
  570. }
  571. )
  572. }
  573. },
  574. ],
  575. pdfjsAssignment(cb) {
  576. SplitTestHandler.getAssignment(
  577. req,
  578. res,
  579. 'pdfjs-36',
  580. {},
  581. (error, assignment) => {
  582. // do not fail editor load if assignment fails
  583. if (error) {
  584. cb(null, { variant: 'default' })
  585. } else {
  586. cb(null, assignment)
  587. }
  588. }
  589. )
  590. },
  591. latexLogParserAssignment(cb) {
  592. SplitTestHandler.getAssignment(
  593. req,
  594. res,
  595. 'latex-log-parser',
  596. (error, assignment) => {
  597. // do not fail editor load if assignment fails
  598. if (error) {
  599. cb(null, { variant: 'default' })
  600. } else {
  601. cb(null, assignment)
  602. }
  603. }
  604. )
  605. },
  606. trackPdfDownloadAssignment(cb) {
  607. SplitTestHandler.getAssignment(req, res, 'track-pdf-download', () => {
  608. // We'll pick up the assignment from the res.locals assignment.
  609. cb()
  610. })
  611. },
  612. pdfCachingModeAssignment(cb) {
  613. SplitTestHandler.getAssignment(req, res, 'pdf-caching-mode', () => {
  614. // We'll pick up the assignment from the res.locals assignment.
  615. cb()
  616. })
  617. },
  618. pdfCachingPrefetchingAssignment(cb) {
  619. SplitTestHandler.getAssignment(
  620. req,
  621. res,
  622. 'pdf-caching-prefetching',
  623. () => {
  624. // We'll pick up the assignment from the res.locals assignment.
  625. cb()
  626. }
  627. )
  628. },
  629. pdfCachingPrefetchLargeAssignment(cb) {
  630. SplitTestHandler.getAssignment(
  631. req,
  632. res,
  633. 'pdf-caching-prefetch-large',
  634. () => {
  635. // We'll pick up the assignment from the res.locals assignment.
  636. cb()
  637. }
  638. )
  639. },
  640. pdfCachingCachedUrlLookupAssignment(cb) {
  641. SplitTestHandler.getAssignment(
  642. req,
  643. res,
  644. 'pdf-caching-cached-url-lookup',
  645. () => {
  646. // We'll pick up the assignment from the res.locals assignment.
  647. cb()
  648. }
  649. )
  650. },
  651. editorLeftMenuAssignment(cb) {
  652. SplitTestHandler.getAssignment(
  653. req,
  654. res,
  655. 'editor-left-menu',
  656. (error, assignment) => {
  657. // do not fail editor load if assignment fails
  658. if (error) {
  659. cb(null, { variant: 'default' })
  660. } else {
  661. cb(null, assignment)
  662. }
  663. }
  664. )
  665. },
  666. editorDocumentationButton(cb) {
  667. SplitTestHandler.getAssignment(
  668. req,
  669. res,
  670. 'documentation-on-editor',
  671. (error, assignment) => {
  672. // do not fail editor load if assignment fails
  673. if (error) {
  674. cb(null, { variant: 'default' })
  675. } else {
  676. cb(null, assignment)
  677. }
  678. }
  679. )
  680. },
  681. richTextAssignment(cb) {
  682. SplitTestHandler.getAssignment(
  683. req,
  684. res,
  685. 'rich-text',
  686. (error, assignment) => {
  687. // do not fail editor load if assignment fails
  688. if (error) {
  689. cb(null, { variant: 'default' })
  690. } else {
  691. cb(null, assignment)
  692. }
  693. }
  694. )
  695. },
  696. figureModalAssignment(cb) {
  697. SplitTestHandler.getAssignment(req, res, 'figure-modal', () => {
  698. // We'll pick up the assignment from the res.locals assignment.
  699. cb()
  700. })
  701. },
  702. historyViewAssignment(cb) {
  703. SplitTestHandler.getAssignment(
  704. req,
  705. res,
  706. 'history-view',
  707. (error, assignment) => {
  708. // do not fail editor load if assignment fails
  709. if (error) {
  710. cb(null, { variant: 'default' })
  711. } else {
  712. cb(null, assignment)
  713. }
  714. }
  715. )
  716. },
  717. reviewPanelAssignment(cb) {
  718. SplitTestHandler.getAssignment(
  719. req,
  720. res,
  721. 'review-panel',
  722. (error, assignment) => {
  723. // do not fail editor load if assignment fails
  724. if (error) {
  725. cb(null, { variant: 'default' })
  726. } else {
  727. cb(null, assignment)
  728. }
  729. }
  730. )
  731. },
  732. accessCheckForOldCompileDomainAssigment(cb) {
  733. SplitTestHandler.getAssignment(
  734. req,
  735. res,
  736. 'access-check-for-old-compile-domain',
  737. () => {
  738. // We'll pick up the assignment from the res.locals assignment.
  739. cb()
  740. }
  741. )
  742. },
  743. forceNewDomainAssignment(cb) {
  744. SplitTestHandler.getAssignment(
  745. req,
  746. res,
  747. 'force-new-compile-domain',
  748. () => {
  749. // We'll pick up the assignment from the res.locals assignment.
  750. cb()
  751. }
  752. )
  753. },
  754. userContentDomainAccessCheckAssigment(cb) {
  755. SplitTestHandler.getAssignment(
  756. req,
  757. res,
  758. 'user-content-domain-access-check',
  759. () => {
  760. // We'll pick up the assignment from the res.locals assignment.
  761. cb()
  762. }
  763. )
  764. },
  765. userContentDomainAccessCheckDelayAssigment(cb) {
  766. SplitTestHandler.getAssignment(
  767. req,
  768. res,
  769. 'user-content-domain-access-check-delay',
  770. () => {
  771. // We'll pick up the assignment from the res.locals assignment.
  772. cb()
  773. }
  774. )
  775. },
  776. userContentDomainAccessCheckMaxChecksAssigment(cb) {
  777. SplitTestHandler.getAssignment(
  778. req,
  779. res,
  780. 'user-content-domain-access-check-max-checks',
  781. () => {
  782. // We'll pick up the assignment from the res.locals assignment.
  783. cb()
  784. }
  785. )
  786. },
  787. reportUserContentDomainAccessCheckErrorAssigment(cb) {
  788. SplitTestHandler.getAssignment(
  789. req,
  790. res,
  791. 'report-user-content-domain-access-check-error',
  792. () => {
  793. // We'll pick up the assignment from the res.locals assignment.
  794. cb()
  795. }
  796. )
  797. },
  798. },
  799. (
  800. err,
  801. {
  802. project,
  803. user,
  804. userHasInstitutionLicence,
  805. learnedWords,
  806. subscription,
  807. userIsMemberOfGroupSubscription,
  808. isTokenMember,
  809. isInvitedMember,
  810. brandVariation,
  811. visualEditorNameAssignment,
  812. participatingInVisualEditorNamingTest,
  813. legacySourceEditorAssignment,
  814. pdfjsAssignment,
  815. editorLeftMenuAssignment,
  816. richTextAssignment,
  817. historyViewAssignment,
  818. reviewPanelAssignment,
  819. }
  820. ) => {
  821. if (err != null) {
  822. OError.tag(err, 'error getting details for project page')
  823. return next(err)
  824. }
  825. const anonRequestToken = TokenAccessHandler.getRequestToken(
  826. req,
  827. projectId
  828. )
  829. const allowedImageNames = ProjectHelper.getAllowedImagesForUser(user)
  830. AuthorizationManager.getPrivilegeLevelForProject(
  831. userId,
  832. projectId,
  833. anonRequestToken,
  834. (error, privilegeLevel) => {
  835. let allowedFreeTrial = true
  836. if (error != null) {
  837. return next(error)
  838. }
  839. if (
  840. privilegeLevel == null ||
  841. privilegeLevel === PrivilegeLevels.NONE
  842. ) {
  843. return res.sendStatus(401)
  844. }
  845. if (subscription != null) {
  846. allowedFreeTrial = false
  847. }
  848. let wsUrl = Settings.wsUrl
  849. let metricName = 'load-editor-ws'
  850. if (user.betaProgram && Settings.wsUrlBeta !== undefined) {
  851. wsUrl = Settings.wsUrlBeta
  852. metricName += '-beta'
  853. } else if (
  854. Settings.wsUrlV2 &&
  855. Settings.wsUrlV2Percentage > 0 &&
  856. (ObjectId(projectId).getTimestamp() / 1000) % 100 <
  857. Settings.wsUrlV2Percentage
  858. ) {
  859. wsUrl = Settings.wsUrlV2
  860. metricName += '-v2'
  861. }
  862. if (req.query && req.query.ws === 'fallback') {
  863. // `?ws=fallback` will connect to the bare origin, and ignore
  864. // the custom wsUrl. Hence it must load the client side
  865. // javascript from there too.
  866. // Not resetting it here would possibly load a socket.io v2
  867. // client and connect to a v0 endpoint.
  868. wsUrl = undefined
  869. metricName += '-fallback'
  870. }
  871. metrics.inc(metricName)
  872. if (userId) {
  873. AnalyticsManager.recordEventForUser(userId, 'project-opened', {
  874. projectId: project._id,
  875. })
  876. }
  877. // should not be used in place of split tests query param overrides (?my-split-test-name=my-variant)
  878. function shouldDisplayFeature(name, variantFlag) {
  879. if (req.query && req.query[name]) {
  880. return req.query[name] === 'true'
  881. } else {
  882. return variantFlag === true
  883. }
  884. }
  885. const isAdminOrTemplateOwner =
  886. hasAdminAccess(user) || Settings.templates?.user_id === userId
  887. const showTemplatesServerPro =
  888. Features.hasFeature('templates-server-pro') &&
  889. isAdminOrTemplateOwner
  890. const debugPdfDetach = shouldDisplayFeature('debug_pdf_detach')
  891. const detachRole = req.params.detachRole
  892. const showLegacySourceEditor =
  893. !Features.hasFeature('saas') ||
  894. // Allow override via legacy_source_editor=true in query string
  895. shouldDisplayFeature('legacy_source_editor') ||
  896. // Hide Ace for beta users
  897. (!user.betaProgram &&
  898. legacySourceEditorAssignment.variant === 'default')
  899. const editorLeftMenuReact =
  900. editorLeftMenuAssignment?.variant === 'react'
  901. const showSymbolPalette =
  902. !Features.hasFeature('saas') ||
  903. (user.features && user.features.symbolPalette)
  904. // It would be nice if this could go in the Galileo module but
  905. // nothing else does that
  906. const galileoEnabled = req.query?.galileo || ''
  907. const galileoFeatures =
  908. req.query && 'galileoFeatures' in req.query
  909. ? req.query.galileoFeatures.split(',').map(f => f.trim())
  910. : ['all']
  911. const galileoPromptWords = req.query?.galileoPromptWords || ''
  912. // Persistent upgrade prompts
  913. // in header & in share project modal
  914. const showUpgradePrompt =
  915. Features.hasFeature('saas') &&
  916. userId &&
  917. !subscription &&
  918. !userIsMemberOfGroupSubscription &&
  919. !userHasInstitutionLicence
  920. const showPersonalAccessToken =
  921. !Features.hasFeature('saas') ||
  922. req.query?.personal_access_token === 'true'
  923. const template =
  924. detachRole === 'detached'
  925. ? 'project/editor_detached'
  926. : 'project/editor'
  927. const isParticipatingInVisualEditorNamingTest =
  928. Features.hasFeature('saas') &&
  929. participatingInVisualEditorNamingTest
  930. let richTextVariant
  931. if (!Features.hasFeature('saas')) {
  932. richTextVariant = 'cm6'
  933. } else {
  934. richTextVariant = richTextAssignment.variant
  935. }
  936. res.render(template, {
  937. title: project.name,
  938. priority_title: true,
  939. bodyClasses: ['editor'],
  940. project_id: project._id,
  941. projectName: project.name,
  942. editorLeftMenuReact,
  943. user: {
  944. id: userId,
  945. email: user.email,
  946. first_name: user.first_name,
  947. last_name: user.last_name,
  948. referal_id: user.referal_id,
  949. signUpDate: user.signUpDate,
  950. allowedFreeTrial,
  951. featureSwitches: user.featureSwitches,
  952. features: user.features,
  953. refProviders: _.mapValues(user.refProviders, Boolean),
  954. alphaProgram: user.alphaProgram,
  955. betaProgram: user.betaProgram,
  956. labsProgram: user.labsProgram,
  957. labsProgramGalileo: user.labsProgramGalileo,
  958. isAdmin: hasAdminAccess(user),
  959. },
  960. userSettings: {
  961. mode: user.ace.mode,
  962. editorTheme: user.ace.theme,
  963. fontSize: user.ace.fontSize,
  964. autoComplete: user.ace.autoComplete,
  965. autoPairDelimiters: user.ace.autoPairDelimiters,
  966. pdfViewer: user.ace.pdfViewer,
  967. syntaxValidation: user.ace.syntaxValidation,
  968. fontFamily: user.ace.fontFamily || 'lucida',
  969. lineHeight: user.ace.lineHeight || 'normal',
  970. overallTheme: user.ace.overallTheme,
  971. },
  972. privilegeLevel,
  973. anonymous,
  974. anonymousAccessToken: anonymous ? anonRequestToken : null,
  975. isTokenMember,
  976. isRestrictedTokenMember: AuthorizationManager.isRestrictedUser(
  977. userId,
  978. privilegeLevel,
  979. isTokenMember,
  980. isInvitedMember
  981. ),
  982. languages: Settings.languages,
  983. learnedWords,
  984. editorThemes: THEME_LIST,
  985. legacyEditorThemes: LEGACY_THEME_LIST,
  986. maxDocLength: Settings.max_doc_length,
  987. brandVariation,
  988. allowedImageNames,
  989. gitBridgePublicBaseUrl: Settings.gitBridgePublicBaseUrl,
  990. wsUrl,
  991. showSupport: Features.hasFeature('support'),
  992. showTemplatesServerPro,
  993. pdfjsVariant: pdfjsAssignment.variant,
  994. debugPdfDetach,
  995. isParticipatingInVisualEditorNamingTest,
  996. visualEditorNameVariant: visualEditorNameAssignment.variant,
  997. showLegacySourceEditor,
  998. showSymbolPalette,
  999. galileoEnabled,
  1000. galileoFeatures,
  1001. galileoPromptWords,
  1002. detachRole,
  1003. metadata: { viewport: false },
  1004. showUpgradePrompt,
  1005. fixedSizeDocument: true,
  1006. useOpenTelemetry: Settings.useOpenTelemetryClient,
  1007. showCM6SwitchAwaySurvey: Settings.showCM6SwitchAwaySurvey,
  1008. richTextVariant,
  1009. historyViewReact: historyViewAssignment.variant === 'react',
  1010. isReviewPanelReact: reviewPanelAssignment.variant === 'react',
  1011. showPersonalAccessToken,
  1012. })
  1013. timer.done()
  1014. }
  1015. )
  1016. }
  1017. )
  1018. },
  1019. _refreshFeatures(req, user, callback) {
  1020. // If the feature refresh has failed in this session, don't retry
  1021. // it - require the user to log in again.
  1022. if (req.session.feature_refresh_failed) {
  1023. metrics.inc('features-refresh', 1, {
  1024. path: 'load-editor',
  1025. status: 'skipped',
  1026. })
  1027. return callback(null, user)
  1028. }
  1029. // If the refresh takes too long then return the current
  1030. // features. Note that the user.features property may still be
  1031. // updated in the background after the callback is called.
  1032. callback = _.once(callback)
  1033. const refreshTimeoutHandler = setTimeout(() => {
  1034. req.session.feature_refresh_failed = { reason: 'timeout', at: new Date() }
  1035. metrics.inc('features-refresh', 1, {
  1036. path: 'load-editor',
  1037. status: 'timeout',
  1038. })
  1039. callback(null, user)
  1040. }, 5000)
  1041. // try to refresh user features now
  1042. const timer = new metrics.Timer('features-refresh-on-load-editor')
  1043. FeaturesUpdater.refreshFeatures(
  1044. user._id,
  1045. 'load-editor',
  1046. (err, features) => {
  1047. clearTimeout(refreshTimeoutHandler)
  1048. timer.done()
  1049. if (err) {
  1050. // keep a record to prevent unneceary retries and leave
  1051. // the original features unmodified if the refresh failed
  1052. req.session.feature_refresh_failed = {
  1053. reason: 'error',
  1054. at: new Date(),
  1055. }
  1056. metrics.inc('features-refresh', 1, {
  1057. path: 'load-editor',
  1058. status: 'error',
  1059. })
  1060. } else {
  1061. user.features = features
  1062. metrics.inc('features-refresh', 1, {
  1063. path: 'load-editor',
  1064. status: 'success',
  1065. })
  1066. }
  1067. callback(null, user)
  1068. }
  1069. )
  1070. },
  1071. _buildProjectList(allProjects, userId) {
  1072. let project
  1073. const { owned, readAndWrite, readOnly, tokenReadAndWrite, tokenReadOnly } =
  1074. allProjects
  1075. const projects = []
  1076. for (project of owned) {
  1077. projects.push(
  1078. ProjectController._buildProjectViewModel(
  1079. project,
  1080. 'owner',
  1081. Sources.OWNER,
  1082. userId
  1083. )
  1084. )
  1085. }
  1086. // Invite-access
  1087. for (project of readAndWrite) {
  1088. projects.push(
  1089. ProjectController._buildProjectViewModel(
  1090. project,
  1091. 'readWrite',
  1092. Sources.INVITE,
  1093. userId
  1094. )
  1095. )
  1096. }
  1097. for (project of readOnly) {
  1098. projects.push(
  1099. ProjectController._buildProjectViewModel(
  1100. project,
  1101. 'readOnly',
  1102. Sources.INVITE,
  1103. userId
  1104. )
  1105. )
  1106. }
  1107. // Token-access
  1108. // Only add these projects if they're not already present, this gives us cascading access
  1109. // from 'owner' => 'token-read-only'
  1110. for (project of tokenReadAndWrite) {
  1111. if (
  1112. projects.filter(p => p.id.toString() === project._id.toString())
  1113. .length === 0
  1114. ) {
  1115. projects.push(
  1116. ProjectController._buildProjectViewModel(
  1117. project,
  1118. 'readAndWrite',
  1119. Sources.TOKEN,
  1120. userId
  1121. )
  1122. )
  1123. }
  1124. }
  1125. for (project of tokenReadOnly) {
  1126. if (
  1127. projects.filter(p => p.id.toString() === project._id.toString())
  1128. .length === 0
  1129. ) {
  1130. projects.push(
  1131. ProjectController._buildProjectViewModel(
  1132. project,
  1133. 'readOnly',
  1134. Sources.TOKEN,
  1135. userId
  1136. )
  1137. )
  1138. }
  1139. }
  1140. return projects
  1141. },
  1142. _buildProjectViewModel(project, accessLevel, source, userId) {
  1143. const archived = ProjectHelper.isArchived(project, userId)
  1144. // If a project is simultaneously trashed and archived, we will consider it archived but not trashed.
  1145. const trashed = ProjectHelper.isTrashed(project, userId) && !archived
  1146. TokenAccessHandler.protectTokens(project, accessLevel)
  1147. const model = {
  1148. id: project._id,
  1149. name: project.name,
  1150. lastUpdated: project.lastUpdated,
  1151. lastUpdatedBy: project.lastUpdatedBy,
  1152. publicAccessLevel: project.publicAccesLevel,
  1153. accessLevel,
  1154. source,
  1155. archived,
  1156. trashed,
  1157. owner_ref: project.owner_ref,
  1158. isV1Project: false,
  1159. }
  1160. if (accessLevel === PrivilegeLevels.READ_ONLY && source === Sources.TOKEN) {
  1161. model.owner_ref = null
  1162. model.lastUpdatedBy = null
  1163. }
  1164. return model
  1165. },
  1166. _injectProjectUsers(projects, callback) {
  1167. const users = {}
  1168. for (const project of projects) {
  1169. if (project.owner_ref != null) {
  1170. users[project.owner_ref.toString()] = true
  1171. }
  1172. if (project.lastUpdatedBy != null) {
  1173. users[project.lastUpdatedBy.toString()] = true
  1174. }
  1175. }
  1176. const userIds = Object.keys(users)
  1177. async.eachSeries(
  1178. userIds,
  1179. (userId, cb) => {
  1180. UserGetter.getUser(
  1181. userId,
  1182. { first_name: 1, last_name: 1, email: 1 },
  1183. (error, user) => {
  1184. if (error != null) {
  1185. return cb(error)
  1186. }
  1187. users[userId] = user
  1188. cb()
  1189. }
  1190. )
  1191. },
  1192. error => {
  1193. if (error != null) {
  1194. return callback(error)
  1195. }
  1196. for (const project of projects) {
  1197. if (project.owner_ref != null) {
  1198. project.owner = users[project.owner_ref.toString()]
  1199. }
  1200. if (project.lastUpdatedBy != null) {
  1201. project.lastUpdatedBy =
  1202. users[project.lastUpdatedBy.toString()] || null
  1203. }
  1204. }
  1205. callback(null, projects)
  1206. }
  1207. )
  1208. },
  1209. _buildPortalTemplatesList(affiliations) {
  1210. if (affiliations == null) {
  1211. affiliations = []
  1212. }
  1213. const portalTemplates = []
  1214. for (const aff of affiliations) {
  1215. if (
  1216. aff.portal &&
  1217. aff.portal.slug &&
  1218. aff.portal.templates_count &&
  1219. aff.portal.templates_count > 0
  1220. ) {
  1221. const portalPath = aff.institution.isUniversity ? '/edu/' : '/org/'
  1222. portalTemplates.push({
  1223. name: aff.institution.name,
  1224. url: Settings.siteUrl + portalPath + aff.portal.slug,
  1225. })
  1226. }
  1227. }
  1228. return portalTemplates
  1229. },
  1230. }
  1231. const defaultSettingsForAnonymousUser = userId => ({
  1232. id: userId,
  1233. ace: {
  1234. mode: 'none',
  1235. theme: 'textmate',
  1236. fontSize: '12',
  1237. autoComplete: true,
  1238. spellCheckLanguage: '',
  1239. pdfViewer: '',
  1240. syntaxValidation: true,
  1241. },
  1242. subscription: {
  1243. freeTrial: {
  1244. allowed: true,
  1245. },
  1246. },
  1247. featureSwitches: {
  1248. github: false,
  1249. },
  1250. alphaProgram: false,
  1251. betaProgram: false,
  1252. })
  1253. const THEME_LIST = [
  1254. 'cobalt',
  1255. 'dracula',
  1256. 'eclipse',
  1257. 'monokai',
  1258. 'overleaf',
  1259. 'textmate',
  1260. ]
  1261. const LEGACY_THEME_LIST = [
  1262. 'ambiance',
  1263. 'chaos',
  1264. 'chrome',
  1265. 'clouds',
  1266. 'clouds_midnight',
  1267. 'crimson_editor',
  1268. 'dawn',
  1269. 'dreamweaver',
  1270. 'github',
  1271. 'gob',
  1272. 'gruvbox',
  1273. 'idle_fingers',
  1274. 'iplastic',
  1275. 'katzenmilch',
  1276. 'kr_theme',
  1277. 'kuroir',
  1278. 'merbivore',
  1279. 'merbivore_soft',
  1280. 'mono_industrial',
  1281. 'nord_dark',
  1282. 'pastel_on_dark',
  1283. 'solarized_dark',
  1284. 'solarized_light',
  1285. 'sqlserver',
  1286. 'terminal',
  1287. 'tomorrow',
  1288. 'tomorrow_night',
  1289. 'tomorrow_night_blue',
  1290. 'tomorrow_night_bright',
  1291. 'tomorrow_night_eighties',
  1292. 'twilight',
  1293. 'vibrant_ink',
  1294. 'xcode',
  1295. ]
  1296. module.exports = ProjectController