ProjectController.js 36 KB

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