ProjectController.js 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  1. const _ = require('lodash')
  2. const Path = require('path')
  3. const OError = require('@overleaf/o-error')
  4. const fs = require('fs')
  5. const crypto = require('crypto')
  6. const async = require('async')
  7. const logger = require('@overleaf/logger')
  8. const { ObjectId } = require('mongodb')
  9. const ProjectDeleter = require('./ProjectDeleter')
  10. const ProjectDuplicator = require('./ProjectDuplicator')
  11. const ProjectCreationHandler = require('./ProjectCreationHandler')
  12. const EditorController = require('../Editor/EditorController')
  13. const ProjectHelper = require('./ProjectHelper')
  14. const metrics = require('@overleaf/metrics')
  15. const { User } = require('../../models/User')
  16. const TagsHandler = require('../Tags/TagsHandler')
  17. const SubscriptionLocator = require('../Subscription/SubscriptionLocator')
  18. const NotificationsHandler = require('../Notifications/NotificationsHandler')
  19. const LimitationsManager = require('../Subscription/LimitationsManager')
  20. const Settings = require('@overleaf/settings')
  21. const AuthorizationManager = require('../Authorization/AuthorizationManager')
  22. const InactiveProjectManager = require('../InactiveData/InactiveProjectManager')
  23. const ProjectUpdateHandler = require('./ProjectUpdateHandler')
  24. const ProjectGetter = require('./ProjectGetter')
  25. const PrivilegeLevels = require('../Authorization/PrivilegeLevels')
  26. const SessionManager = require('../Authentication/SessionManager')
  27. const Sources = require('../Authorization/Sources')
  28. const TokenAccessHandler = require('../TokenAccess/TokenAccessHandler')
  29. const CollaboratorsGetter = require('../Collaborators/CollaboratorsGetter')
  30. const ProjectEntityHandler = require('./ProjectEntityHandler')
  31. const TpdsProjectFlusher = require('../ThirdPartyDataStore/TpdsProjectFlusher')
  32. const UserGetter = require('../User/UserGetter')
  33. const NotificationsBuilder = require('../Notifications/NotificationsBuilder')
  34. const { V1ConnectionError } = require('../Errors/Errors')
  35. const Features = require('../../infrastructure/Features')
  36. const BrandVariationsHandler = require('../BrandVariations/BrandVariationsHandler')
  37. const UserController = require('../User/UserController')
  38. const AnalyticsManager = require('../Analytics/AnalyticsManager')
  39. const Modules = require('../../infrastructure/Modules')
  40. const SplitTestV2Handler = require('../SplitTests/SplitTestV2Handler')
  41. const { getNewLogsUIVariantForUser } = require('../Helpers/NewLogsUI')
  42. const FeaturesUpdater = require('../Subscription/FeaturesUpdater')
  43. const _ssoAvailable = (affiliation, session, linkedInstitutionIds) => {
  44. if (!affiliation.institution) return false
  45. // institution.confirmed is for the domain being confirmed, not the email
  46. // Do not show SSO UI for unconfirmed domains
  47. if (!affiliation.institution.confirmed) return false
  48. // Could have multiple emails at the same institution, and if any are
  49. // linked to the institution then do not show notification for others
  50. if (
  51. linkedInstitutionIds.indexOf(affiliation.institution.id.toString()) === -1
  52. ) {
  53. if (affiliation.institution.ssoEnabled) return true
  54. if (affiliation.institution.ssoBeta && session.samlBeta) return true
  55. return false
  56. }
  57. return false
  58. }
  59. const ProjectController = {
  60. _isInPercentageRollout(rolloutName, objectId, percentage) {
  61. if (Settings.bypassPercentageRollouts === true) {
  62. return true
  63. }
  64. const data = `${rolloutName}:${objectId.toString()}`
  65. const md5hash = crypto.createHash('md5').update(data).digest('hex')
  66. const counter = parseInt(md5hash.slice(26, 32), 16)
  67. return counter % 100 < percentage
  68. },
  69. updateProjectSettings(req, res, next) {
  70. const projectId = req.params.Project_id
  71. const jobs = []
  72. if (req.body.compiler != null) {
  73. jobs.push(callback =>
  74. EditorController.setCompiler(projectId, req.body.compiler, callback)
  75. )
  76. }
  77. if (req.body.imageName != null) {
  78. jobs.push(callback =>
  79. EditorController.setImageName(projectId, req.body.imageName, callback)
  80. )
  81. }
  82. if (req.body.name != null) {
  83. jobs.push(callback =>
  84. EditorController.renameProject(projectId, req.body.name, callback)
  85. )
  86. }
  87. if (req.body.spellCheckLanguage != null) {
  88. jobs.push(callback =>
  89. EditorController.setSpellCheckLanguage(
  90. projectId,
  91. req.body.spellCheckLanguage,
  92. callback
  93. )
  94. )
  95. }
  96. if (req.body.rootDocId != null) {
  97. jobs.push(callback =>
  98. EditorController.setRootDoc(projectId, req.body.rootDocId, callback)
  99. )
  100. }
  101. async.series(jobs, error => {
  102. if (error != null) {
  103. return next(error)
  104. }
  105. res.sendStatus(204)
  106. })
  107. },
  108. updateProjectAdminSettings(req, res, next) {
  109. const projectId = req.params.Project_id
  110. const jobs = []
  111. if (req.body.publicAccessLevel != null) {
  112. jobs.push(callback =>
  113. EditorController.setPublicAccessLevel(
  114. projectId,
  115. req.body.publicAccessLevel,
  116. callback
  117. )
  118. )
  119. }
  120. async.series(jobs, error => {
  121. if (error != null) {
  122. return next(error)
  123. }
  124. res.sendStatus(204)
  125. })
  126. },
  127. deleteProject(req, res) {
  128. const projectId = req.params.Project_id
  129. const user = SessionManager.getSessionUser(req.session)
  130. const cb = err => {
  131. if (err != null) {
  132. res.sendStatus(500)
  133. } else {
  134. res.sendStatus(200)
  135. }
  136. }
  137. ProjectDeleter.deleteProject(
  138. projectId,
  139. { deleterUser: user, ipAddress: req.ip },
  140. cb
  141. )
  142. },
  143. archiveProject(req, res, next) {
  144. const projectId = req.params.Project_id
  145. const userId = SessionManager.getLoggedInUserId(req.session)
  146. ProjectDeleter.archiveProject(projectId, userId, function (err) {
  147. if (err != null) {
  148. return next(err)
  149. } else {
  150. return res.sendStatus(200)
  151. }
  152. })
  153. },
  154. unarchiveProject(req, res, next) {
  155. const projectId = req.params.Project_id
  156. const userId = SessionManager.getLoggedInUserId(req.session)
  157. ProjectDeleter.unarchiveProject(projectId, userId, function (err) {
  158. if (err != null) {
  159. return next(err)
  160. } else {
  161. return res.sendStatus(200)
  162. }
  163. })
  164. },
  165. trashProject(req, res, next) {
  166. const projectId = req.params.project_id
  167. const userId = SessionManager.getLoggedInUserId(req.session)
  168. ProjectDeleter.trashProject(projectId, userId, function (err) {
  169. if (err != null) {
  170. return next(err)
  171. } else {
  172. return res.sendStatus(200)
  173. }
  174. })
  175. },
  176. untrashProject(req, res, next) {
  177. const projectId = req.params.project_id
  178. const userId = SessionManager.getLoggedInUserId(req.session)
  179. ProjectDeleter.untrashProject(projectId, userId, function (err) {
  180. if (err != null) {
  181. return next(err)
  182. } else {
  183. return res.sendStatus(200)
  184. }
  185. })
  186. },
  187. expireDeletedProjectsAfterDuration(req, res) {
  188. ProjectDeleter.expireDeletedProjectsAfterDuration(err => {
  189. if (err != null) {
  190. res.sendStatus(500)
  191. } else {
  192. res.sendStatus(200)
  193. }
  194. })
  195. },
  196. expireDeletedProject(req, res, next) {
  197. const { projectId } = req.params
  198. ProjectDeleter.expireDeletedProject(projectId, err => {
  199. if (err != null) {
  200. next(err)
  201. } else {
  202. res.sendStatus(200)
  203. }
  204. })
  205. },
  206. restoreProject(req, res) {
  207. const projectId = req.params.Project_id
  208. ProjectDeleter.restoreProject(projectId, err => {
  209. if (err != null) {
  210. res.sendStatus(500)
  211. } else {
  212. res.sendStatus(200)
  213. }
  214. })
  215. },
  216. cloneProject(req, res, next) {
  217. res.setTimeout(5 * 60 * 1000) // allow extra time for the copy to complete
  218. metrics.inc('cloned-project')
  219. const projectId = req.params.Project_id
  220. const { projectName } = req.body
  221. logger.log({ projectId, projectName }, 'cloning project')
  222. if (!SessionManager.isUserLoggedIn(req.session)) {
  223. return res.send({ redir: '/register' })
  224. }
  225. const currentUser = SessionManager.getSessionUser(req.session)
  226. const { first_name: firstName, last_name: lastName, email } = currentUser
  227. ProjectDuplicator.duplicate(
  228. currentUser,
  229. projectId,
  230. projectName,
  231. (err, project) => {
  232. if (err != null) {
  233. OError.tag(err, 'error cloning project', {
  234. projectId,
  235. userId: currentUser._id,
  236. })
  237. return next(err)
  238. }
  239. res.send({
  240. name: project.name,
  241. project_id: project._id,
  242. owner_ref: project.owner_ref,
  243. owner: {
  244. first_name: firstName,
  245. last_name: lastName,
  246. email,
  247. _id: currentUser._id,
  248. },
  249. })
  250. }
  251. )
  252. },
  253. newProject(req, res, next) {
  254. const currentUser = SessionManager.getSessionUser(req.session)
  255. const {
  256. first_name: firstName,
  257. last_name: lastName,
  258. email,
  259. _id: userId,
  260. } = currentUser
  261. const projectName =
  262. req.body.projectName != null ? req.body.projectName.trim() : undefined
  263. const { template } = req.body
  264. async.waterfall(
  265. [
  266. cb => {
  267. if (template === 'example') {
  268. ProjectCreationHandler.createExampleProject(userId, projectName, cb)
  269. } else {
  270. ProjectCreationHandler.createBasicProject(userId, projectName, cb)
  271. }
  272. },
  273. ],
  274. (err, project) => {
  275. if (err != null) {
  276. return next(err)
  277. }
  278. res.send({
  279. project_id: project._id,
  280. owner_ref: project.owner_ref,
  281. owner: {
  282. first_name: firstName,
  283. last_name: lastName,
  284. email,
  285. _id: userId,
  286. },
  287. })
  288. }
  289. )
  290. },
  291. renameProject(req, res, next) {
  292. const projectId = req.params.Project_id
  293. const newName = req.body.newProjectName
  294. EditorController.renameProject(projectId, newName, err => {
  295. if (err != null) {
  296. return next(err)
  297. }
  298. res.sendStatus(200)
  299. })
  300. },
  301. userProjectsJson(req, res, next) {
  302. const userId = SessionManager.getLoggedInUserId(req.session)
  303. ProjectGetter.findAllUsersProjects(
  304. userId,
  305. 'name lastUpdated publicAccesLevel archived trashed owner_ref tokens',
  306. (err, projects) => {
  307. if (err != null) {
  308. return next(err)
  309. }
  310. // _buildProjectList already converts archived/trashed to booleans so isArchivedOrTrashed should not be used here
  311. projects = ProjectController._buildProjectList(projects, userId)
  312. .filter(p => !(p.archived || p.trashed))
  313. .map(p => ({ _id: p.id, name: p.name, accessLevel: p.accessLevel }))
  314. res.json({ projects })
  315. }
  316. )
  317. },
  318. projectEntitiesJson(req, res, next) {
  319. const projectId = req.params.Project_id
  320. ProjectGetter.getProject(projectId, (err, project) => {
  321. if (err != null) {
  322. return next(err)
  323. }
  324. ProjectEntityHandler.getAllEntitiesFromProject(
  325. project,
  326. (err, docs, files) => {
  327. if (err != null) {
  328. return next(err)
  329. }
  330. const entities = docs
  331. .concat(files)
  332. // Sort by path ascending
  333. .sort((a, b) => (a.path > b.path ? 1 : a.path < b.path ? -1 : 0))
  334. .map(e => ({
  335. path: e.path,
  336. type: e.doc != null ? 'doc' : 'file',
  337. }))
  338. res.json({ project_id: projectId, entities })
  339. }
  340. )
  341. })
  342. },
  343. projectListPage(req, res, next) {
  344. const timer = new metrics.Timer('project-list')
  345. const userId = SessionManager.getLoggedInUserId(req.session)
  346. const currentUser = SessionManager.getSessionUser(req.session)
  347. async.parallel(
  348. {
  349. tags(cb) {
  350. TagsHandler.getAllTags(userId, cb)
  351. },
  352. notifications(cb) {
  353. NotificationsHandler.getUserNotifications(userId, cb)
  354. },
  355. projects(cb) {
  356. ProjectGetter.findAllUsersProjects(
  357. userId,
  358. 'name lastUpdated lastUpdatedBy publicAccesLevel archived trashed owner_ref tokens',
  359. cb
  360. )
  361. },
  362. hasSubscription(cb) {
  363. LimitationsManager.hasPaidSubscription(
  364. currentUser,
  365. (error, hasPaidSubscription) => {
  366. if (error != null && error instanceof V1ConnectionError) {
  367. return cb(null, true)
  368. }
  369. cb(error, hasPaidSubscription)
  370. }
  371. )
  372. },
  373. user(cb) {
  374. User.findById(
  375. userId,
  376. 'emails featureSwitches overleaf awareOfV2 features lastLoginIp',
  377. cb
  378. )
  379. },
  380. userEmailsData(cb) {
  381. const result = { list: [], allInReconfirmNotificationPeriods: [] }
  382. UserGetter.getUserFullEmails(userId, (error, fullEmails) => {
  383. if (error && error instanceof V1ConnectionError) {
  384. return cb(null, result)
  385. }
  386. if (!Features.hasFeature('affiliations')) {
  387. result.list = fullEmails
  388. return cb(null, result)
  389. }
  390. Modules.hooks.fire(
  391. 'allInReconfirmNotificationPeriodsForUser',
  392. fullEmails,
  393. (error, results) => {
  394. if (error != null) {
  395. return cb(error)
  396. }
  397. // Module.hooks.fire accepts multiple methods
  398. // and does async.series
  399. const allInReconfirmNotificationPeriods =
  400. (results && results[0]) || []
  401. return cb(null, {
  402. list: fullEmails,
  403. allInReconfirmNotificationPeriods,
  404. })
  405. }
  406. )
  407. })
  408. },
  409. },
  410. (err, results) => {
  411. if (err != null) {
  412. OError.tag(err, 'error getting data for project list page')
  413. return next(err)
  414. }
  415. const { notifications, user, userEmailsData } = results
  416. const userEmails = userEmailsData.list || []
  417. const userAffiliations = userEmails
  418. .filter(emailData => !!emailData.affiliation)
  419. .map(emailData => {
  420. const result = emailData.affiliation
  421. result.email = emailData.email
  422. return result
  423. })
  424. const { allInReconfirmNotificationPeriods } = userEmailsData
  425. // Handle case of deleted user
  426. if (user == null) {
  427. UserController.logout(req, res, next)
  428. return
  429. }
  430. const tags = results.tags
  431. const notificationsInstitution = []
  432. for (const notification of notifications) {
  433. notification.html = req.i18n.translate(
  434. notification.templateKey,
  435. notification.messageOpts
  436. )
  437. }
  438. // Institution SSO Notifications
  439. let reconfirmedViaSAML
  440. if (Features.hasFeature('saml')) {
  441. reconfirmedViaSAML = _.get(req.session, ['saml', 'reconfirmed'])
  442. const samlSession = req.session.saml
  443. // Notification: SSO Available
  444. const linkedInstitutionIds = []
  445. user.emails.forEach(email => {
  446. if (email.samlProviderId) {
  447. linkedInstitutionIds.push(email.samlProviderId)
  448. }
  449. })
  450. if (Array.isArray(userAffiliations)) {
  451. userAffiliations.forEach(affiliation => {
  452. if (
  453. _ssoAvailable(affiliation, req.session, linkedInstitutionIds)
  454. ) {
  455. notificationsInstitution.push({
  456. email: affiliation.email,
  457. institutionId: affiliation.institution.id,
  458. institutionName: affiliation.institution.name,
  459. templateKey: 'notification_institution_sso_available',
  460. })
  461. }
  462. })
  463. }
  464. if (samlSession) {
  465. // Notification: After SSO Linked
  466. if (samlSession.linked) {
  467. notificationsInstitution.push({
  468. email: samlSession.institutionEmail,
  469. institutionName: samlSession.linked.universityName,
  470. templateKey: 'notification_institution_sso_linked',
  471. })
  472. }
  473. // Notification: After SSO Linked or Logging in
  474. // The requested email does not match primary email returned from
  475. // the institution
  476. if (
  477. samlSession.requestedEmail &&
  478. samlSession.emailNonCanonical &&
  479. !samlSession.error
  480. ) {
  481. notificationsInstitution.push({
  482. institutionEmail: samlSession.emailNonCanonical,
  483. requestedEmail: samlSession.requestedEmail,
  484. templateKey: 'notification_institution_sso_non_canonical',
  485. })
  486. }
  487. // Notification: Tried to register, but account already existed
  488. // registerIntercept is set before the institution callback.
  489. // institutionEmail is set after institution callback.
  490. // Check for both in case SSO flow was abandoned
  491. if (
  492. samlSession.registerIntercept &&
  493. samlSession.institutionEmail &&
  494. !samlSession.error
  495. ) {
  496. notificationsInstitution.push({
  497. email: samlSession.institutionEmail,
  498. templateKey: 'notification_institution_sso_already_registered',
  499. })
  500. }
  501. // Notification: When there is a session error
  502. if (samlSession.error) {
  503. notificationsInstitution.push({
  504. templateKey: 'notification_institution_sso_error',
  505. error: samlSession.error,
  506. })
  507. }
  508. }
  509. delete req.session.saml
  510. }
  511. const portalTemplates = ProjectController._buildPortalTemplatesList(
  512. userAffiliations
  513. )
  514. const projects = ProjectController._buildProjectList(
  515. results.projects,
  516. userId
  517. )
  518. // in v2 add notifications for matching university IPs
  519. if (Settings.overleaf != null && req.ip !== user.lastLoginIp) {
  520. NotificationsBuilder.ipMatcherAffiliation(user._id).create(
  521. req.ip,
  522. () => {}
  523. )
  524. }
  525. ProjectController._injectProjectUsers(projects, (error, projects) => {
  526. if (error != null) {
  527. return next(error)
  528. }
  529. const viewModel = {
  530. title: 'your_projects',
  531. priority_title: true,
  532. projects,
  533. tags,
  534. notifications: notifications || [],
  535. notificationsInstitution,
  536. allInReconfirmNotificationPeriods,
  537. portalTemplates,
  538. user,
  539. userAffiliations,
  540. userEmails,
  541. hasSubscription: results.hasSubscription,
  542. reconfirmedViaSAML,
  543. zipFileSizeLimit: Settings.maxUploadSize,
  544. isOverleaf: !!Settings.overleaf,
  545. }
  546. const paidUser =
  547. (user.features != null ? user.features.github : undefined) &&
  548. (user.features != null ? user.features.dropbox : undefined) // use a heuristic for paid account
  549. const freeUserProportion = 0.1
  550. const sampleFreeUser =
  551. parseInt(user._id.toString().slice(-2), 16) <
  552. freeUserProportion * 255
  553. const showFrontWidget = paidUser || sampleFreeUser
  554. if (showFrontWidget) {
  555. viewModel.frontChatWidgetRoomId =
  556. Settings.overleaf != null
  557. ? Settings.overleaf.front_chat_widget_room_id
  558. : undefined
  559. }
  560. res.render('project/list', viewModel)
  561. timer.done()
  562. })
  563. }
  564. )
  565. },
  566. loadEditor(req, res, next) {
  567. const timer = new metrics.Timer('load-editor')
  568. if (!Settings.editorIsOpen) {
  569. return res.render('general/closed', { title: 'updating_site' })
  570. }
  571. let anonymous, userId, sessionUser
  572. if (SessionManager.isUserLoggedIn(req.session)) {
  573. sessionUser = SessionManager.getSessionUser(req.session)
  574. userId = SessionManager.getLoggedInUserId(req.session)
  575. anonymous = false
  576. } else {
  577. sessionUser = null
  578. anonymous = true
  579. userId = null
  580. }
  581. const projectId = req.params.Project_id
  582. async.auto(
  583. {
  584. project(cb) {
  585. ProjectGetter.getProject(
  586. projectId,
  587. {
  588. name: 1,
  589. lastUpdated: 1,
  590. track_changes: 1,
  591. owner_ref: 1,
  592. brandVariationId: 1,
  593. overleaf: 1,
  594. tokens: 1,
  595. },
  596. (err, project) => {
  597. if (err != null) {
  598. return cb(err)
  599. }
  600. cb(null, project)
  601. }
  602. )
  603. },
  604. user(cb) {
  605. if (userId == null) {
  606. cb(null, defaultSettingsForAnonymousUser(userId))
  607. } else {
  608. User.findById(
  609. userId,
  610. 'email first_name last_name referal_id signUpDate featureSwitches features featuresEpoch refProviders alphaProgram betaProgram isAdmin ace',
  611. (err, user) => {
  612. // Handle case of deleted user
  613. if (user == null) {
  614. UserController.logout(req, res, next)
  615. return
  616. }
  617. if (err) {
  618. return cb(err)
  619. }
  620. logger.log({ projectId, userId }, 'got user')
  621. if (FeaturesUpdater.featuresEpochIsCurrent(user)) {
  622. return cb(null, user)
  623. }
  624. ProjectController._refreshFeatures(req, user, cb)
  625. }
  626. )
  627. }
  628. },
  629. subscription(cb) {
  630. if (userId == null) {
  631. return cb()
  632. }
  633. SubscriptionLocator.getUsersSubscription(userId, cb)
  634. },
  635. activate(cb) {
  636. InactiveProjectManager.reactivateProjectIfRequired(projectId, cb)
  637. },
  638. markAsOpened(cb) {
  639. // don't need to wait for this to complete
  640. ProjectUpdateHandler.markAsOpened(projectId, () => {})
  641. cb()
  642. },
  643. isTokenMember(cb) {
  644. if (userId == null) {
  645. return cb()
  646. }
  647. CollaboratorsGetter.userIsTokenMember(userId, projectId, cb)
  648. },
  649. brandVariation: [
  650. 'project',
  651. (cb, results) => {
  652. if (
  653. (results.project != null
  654. ? results.project.brandVariationId
  655. : undefined) == null
  656. ) {
  657. return cb()
  658. }
  659. BrandVariationsHandler.getBrandVariationById(
  660. results.project.brandVariationId,
  661. (error, brandVariationDetails) => cb(error, brandVariationDetails)
  662. )
  663. },
  664. ],
  665. flushToTpds: cb => {
  666. TpdsProjectFlusher.flushProjectToTpdsIfNeeded(projectId, cb)
  667. },
  668. sharingModalSplitTest(cb) {
  669. SplitTestV2Handler.assignInLocalsContext(
  670. res,
  671. userId,
  672. 'project-share-modal-paywall',
  673. err => {
  674. cb(err, null)
  675. }
  676. )
  677. },
  678. newPdfPreviewAssignment(cb) {
  679. SplitTestV2Handler.getAssignmentForSession(
  680. req.session,
  681. 'react-pdf-preview-rollout',
  682. (err, assignment) => {
  683. cb(err, assignment)
  684. }
  685. )
  686. },
  687. },
  688. (
  689. err,
  690. {
  691. project,
  692. user,
  693. subscription,
  694. isTokenMember,
  695. brandVariation,
  696. newPdfPreviewAssignment,
  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(
  708. sessionUser
  709. )
  710. AuthorizationManager.getPrivilegeLevelForProject(
  711. userId,
  712. projectId,
  713. anonRequestToken,
  714. (error, privilegeLevel) => {
  715. let allowedFreeTrial = true
  716. if (error != null) {
  717. return next(error)
  718. }
  719. if (
  720. privilegeLevel == null ||
  721. privilegeLevel === PrivilegeLevels.NONE
  722. ) {
  723. return res.sendStatus(401)
  724. }
  725. if (subscription != null) {
  726. allowedFreeTrial = false
  727. }
  728. let wsUrl = Settings.wsUrl
  729. let metricName = 'load-editor-ws'
  730. if (user.betaProgram && Settings.wsUrlBeta !== undefined) {
  731. wsUrl = Settings.wsUrlBeta
  732. metricName += '-beta'
  733. } else if (
  734. Settings.wsUrlV2 &&
  735. Settings.wsUrlV2Percentage > 0 &&
  736. (ObjectId(projectId).getTimestamp() / 1000) % 100 <
  737. Settings.wsUrlV2Percentage
  738. ) {
  739. wsUrl = Settings.wsUrlV2
  740. metricName += '-v2'
  741. }
  742. if (req.query && req.query.ws === 'fallback') {
  743. // `?ws=fallback` will connect to the bare origin, and ignore
  744. // the custom wsUrl. Hence it must load the client side
  745. // javascript from there too.
  746. // Not resetting it here would possibly load a socket.io v2
  747. // client and connect to a v0 endpoint.
  748. wsUrl = undefined
  749. metricName += '-fallback'
  750. }
  751. metrics.inc(metricName)
  752. if (userId) {
  753. AnalyticsManager.recordEventForUser(userId, 'project-opened', {
  754. projectId: project._id,
  755. })
  756. }
  757. const logsUIVariant = getNewLogsUIVariantForUser(user)
  758. function shouldDisplayFeature(name, variantFlag) {
  759. if (req.query && req.query[name]) {
  760. return req.query[name] === 'true'
  761. } else {
  762. return variantFlag === true
  763. }
  764. }
  765. function partOfPdfCachingRollout(flag) {
  766. if (!Settings.enablePdfCaching) {
  767. // The feature is disabled globally.
  768. return false
  769. }
  770. // Let the user opt-in only.
  771. // The flag will opt-out of both caching and metrics collection,
  772. // as if this editing session never happened.
  773. return shouldDisplayFeature('enable_pdf_caching', false)
  774. }
  775. let showNewPdfPreview = shouldDisplayFeature(
  776. 'new_pdf_preview',
  777. newPdfPreviewAssignment.variant === 'react-pdf-preview'
  778. )
  779. const showPdfDetach = shouldDisplayFeature(
  780. 'pdf_detach',
  781. user.alphaProgram
  782. )
  783. if (showPdfDetach) {
  784. showNewPdfPreview = true
  785. }
  786. res.render('project/editor', {
  787. title: project.name,
  788. priority_title: true,
  789. bodyClasses: ['editor'],
  790. project_id: project._id,
  791. user: {
  792. id: userId,
  793. email: user.email,
  794. first_name: user.first_name,
  795. last_name: user.last_name,
  796. referal_id: user.referal_id,
  797. signUpDate: user.signUpDate,
  798. allowedFreeTrial: allowedFreeTrial,
  799. featureSwitches: user.featureSwitches,
  800. features: user.features,
  801. refProviders: _.mapValues(user.refProviders, Boolean),
  802. alphaProgram: user.alphaProgram,
  803. betaProgram: user.betaProgram,
  804. isAdmin: user.isAdmin,
  805. },
  806. userSettings: {
  807. mode: user.ace.mode,
  808. editorTheme: user.ace.theme,
  809. fontSize: user.ace.fontSize,
  810. autoComplete: user.ace.autoComplete,
  811. autoPairDelimiters: user.ace.autoPairDelimiters,
  812. pdfViewer: user.ace.pdfViewer,
  813. syntaxValidation: user.ace.syntaxValidation,
  814. fontFamily: user.ace.fontFamily || 'lucida',
  815. lineHeight: user.ace.lineHeight || 'normal',
  816. overallTheme: user.ace.overallTheme,
  817. },
  818. privilegeLevel,
  819. anonymous,
  820. anonymousAccessToken: anonymous ? anonRequestToken : null,
  821. isTokenMember,
  822. isRestrictedTokenMember: AuthorizationManager.isRestrictedUser(
  823. userId,
  824. privilegeLevel,
  825. isTokenMember
  826. ),
  827. languages: Settings.languages,
  828. editorThemes: THEME_LIST,
  829. maxDocLength: Settings.max_doc_length,
  830. useV2History:
  831. project.overleaf &&
  832. project.overleaf.history &&
  833. Boolean(project.overleaf.history.display),
  834. brandVariation,
  835. allowedImageNames,
  836. gitBridgePublicBaseUrl: Settings.gitBridgePublicBaseUrl,
  837. wsUrl,
  838. showSupport: Features.hasFeature('support'),
  839. showNewLogsUI: shouldDisplayFeature(
  840. 'new_logs_ui',
  841. logsUIVariant.newLogsUI
  842. ),
  843. logsUISubvariant: logsUIVariant.subvariant,
  844. showPdfDetach,
  845. showNewPdfPreview,
  846. showNewSourceEditor: shouldDisplayFeature(
  847. 'new_source_editor',
  848. false
  849. ),
  850. showSymbolPalette: shouldDisplayFeature(
  851. 'symbol_palette',
  852. user.alphaProgram || user.betaProgram
  853. ),
  854. trackPdfDownload: partOfPdfCachingRollout('collect-metrics'),
  855. enablePdfCaching: partOfPdfCachingRollout('enable-caching'),
  856. resetServiceWorker:
  857. Boolean(Settings.resetServiceWorker) &&
  858. !shouldDisplayFeature('enable_pdf_caching', false),
  859. })
  860. timer.done()
  861. }
  862. )
  863. }
  864. )
  865. },
  866. _refreshFeatures(req, user, callback) {
  867. // If the feature refresh has failed in this session, don't retry
  868. // it - require the user to log in again.
  869. if (req.session.feature_refresh_failed) {
  870. metrics.inc('features-refresh', 1, {
  871. path: 'load-editor',
  872. status: 'skipped',
  873. })
  874. return callback(null, user)
  875. }
  876. // If the refresh takes too long then return the current
  877. // features. Note that the user.features property may still be
  878. // updated in the background after the callback is called.
  879. callback = _.once(callback)
  880. const refreshTimeoutHandler = setTimeout(() => {
  881. req.session.feature_refresh_failed = { reason: 'timeout', at: new Date() }
  882. metrics.inc('features-refresh', 1, {
  883. path: 'load-editor',
  884. status: 'timeout',
  885. })
  886. callback(null, user)
  887. }, 5000)
  888. // try to refresh user features now
  889. const timer = new metrics.Timer('features-refresh-on-load-editor')
  890. FeaturesUpdater.refreshFeatures(
  891. user._id,
  892. 'load-editor',
  893. (err, features) => {
  894. clearTimeout(refreshTimeoutHandler)
  895. timer.done()
  896. if (err) {
  897. // keep a record to prevent unneceary retries and leave
  898. // the original features unmodified if the refresh failed
  899. req.session.feature_refresh_failed = {
  900. reason: 'error',
  901. at: new Date(),
  902. }
  903. metrics.inc('features-refresh', 1, {
  904. path: 'load-editor',
  905. status: 'error',
  906. })
  907. } else {
  908. user.features = features
  909. metrics.inc('features-refresh', 1, {
  910. path: 'load-editor',
  911. status: 'success',
  912. })
  913. }
  914. return callback(null, user)
  915. }
  916. )
  917. },
  918. _buildProjectList(allProjects, userId) {
  919. let project
  920. const {
  921. owned,
  922. readAndWrite,
  923. readOnly,
  924. tokenReadAndWrite,
  925. tokenReadOnly,
  926. } = allProjects
  927. const projects = []
  928. for (project of owned) {
  929. projects.push(
  930. ProjectController._buildProjectViewModel(
  931. project,
  932. 'owner',
  933. Sources.OWNER,
  934. userId
  935. )
  936. )
  937. }
  938. // Invite-access
  939. for (project of readAndWrite) {
  940. projects.push(
  941. ProjectController._buildProjectViewModel(
  942. project,
  943. 'readWrite',
  944. Sources.INVITE,
  945. userId
  946. )
  947. )
  948. }
  949. for (project of readOnly) {
  950. projects.push(
  951. ProjectController._buildProjectViewModel(
  952. project,
  953. 'readOnly',
  954. Sources.INVITE,
  955. userId
  956. )
  957. )
  958. }
  959. // Token-access
  960. // Only add these projects if they're not already present, this gives us cascading access
  961. // from 'owner' => 'token-read-only'
  962. for (project of tokenReadAndWrite) {
  963. if (
  964. projects.filter(p => p.id.toString() === project._id.toString())
  965. .length === 0
  966. ) {
  967. projects.push(
  968. ProjectController._buildProjectViewModel(
  969. project,
  970. 'readAndWrite',
  971. Sources.TOKEN,
  972. userId
  973. )
  974. )
  975. }
  976. }
  977. for (project of tokenReadOnly) {
  978. if (
  979. projects.filter(p => p.id.toString() === project._id.toString())
  980. .length === 0
  981. ) {
  982. projects.push(
  983. ProjectController._buildProjectViewModel(
  984. project,
  985. 'readOnly',
  986. Sources.TOKEN,
  987. userId
  988. )
  989. )
  990. }
  991. }
  992. return projects
  993. },
  994. _buildProjectViewModel(project, accessLevel, source, userId) {
  995. const archived = ProjectHelper.isArchived(project, userId)
  996. // If a project is simultaneously trashed and archived, we will consider it archived but not trashed.
  997. const trashed = ProjectHelper.isTrashed(project, userId) && !archived
  998. TokenAccessHandler.protectTokens(project, accessLevel)
  999. const model = {
  1000. id: project._id,
  1001. name: project.name,
  1002. lastUpdated: project.lastUpdated,
  1003. lastUpdatedBy: project.lastUpdatedBy,
  1004. publicAccessLevel: project.publicAccesLevel,
  1005. accessLevel,
  1006. source,
  1007. archived,
  1008. trashed,
  1009. owner_ref: project.owner_ref,
  1010. isV1Project: false,
  1011. }
  1012. if (accessLevel === PrivilegeLevels.READ_ONLY && source === Sources.TOKEN) {
  1013. model.owner_ref = null
  1014. model.lastUpdatedBy = null
  1015. }
  1016. return model
  1017. },
  1018. _injectProjectUsers(projects, callback) {
  1019. const users = {}
  1020. for (const project of projects) {
  1021. if (project.owner_ref != null) {
  1022. users[project.owner_ref.toString()] = true
  1023. }
  1024. if (project.lastUpdatedBy != null) {
  1025. users[project.lastUpdatedBy.toString()] = true
  1026. }
  1027. }
  1028. const userIds = Object.keys(users)
  1029. async.eachSeries(
  1030. userIds,
  1031. (userId, cb) => {
  1032. UserGetter.getUser(
  1033. userId,
  1034. { first_name: 1, last_name: 1, email: 1 },
  1035. (error, user) => {
  1036. if (error != null) {
  1037. return cb(error)
  1038. }
  1039. users[userId] = user
  1040. cb()
  1041. }
  1042. )
  1043. },
  1044. error => {
  1045. if (error != null) {
  1046. return callback(error)
  1047. }
  1048. for (const project of projects) {
  1049. if (project.owner_ref != null) {
  1050. project.owner = users[project.owner_ref.toString()]
  1051. }
  1052. if (project.lastUpdatedBy != null) {
  1053. project.lastUpdatedBy =
  1054. users[project.lastUpdatedBy.toString()] || null
  1055. }
  1056. }
  1057. callback(null, projects)
  1058. }
  1059. )
  1060. },
  1061. _buildPortalTemplatesList(affiliations) {
  1062. if (affiliations == null) {
  1063. affiliations = []
  1064. }
  1065. const portalTemplates = []
  1066. for (const aff of affiliations) {
  1067. if (
  1068. aff.portal &&
  1069. aff.portal.slug &&
  1070. aff.portal.templates_count &&
  1071. aff.portal.templates_count > 0
  1072. ) {
  1073. const portalPath = aff.institution.isUniversity ? '/edu/' : '/org/'
  1074. portalTemplates.push({
  1075. name: aff.institution.name,
  1076. url: Settings.siteUrl + portalPath + aff.portal.slug,
  1077. })
  1078. }
  1079. }
  1080. return portalTemplates
  1081. },
  1082. }
  1083. const defaultSettingsForAnonymousUser = userId => ({
  1084. id: userId,
  1085. ace: {
  1086. mode: 'none',
  1087. theme: 'textmate',
  1088. fontSize: '12',
  1089. autoComplete: true,
  1090. spellCheckLanguage: '',
  1091. pdfViewer: '',
  1092. syntaxValidation: true,
  1093. },
  1094. subscription: {
  1095. freeTrial: {
  1096. allowed: true,
  1097. },
  1098. },
  1099. featureSwitches: {
  1100. github: false,
  1101. },
  1102. alphaProgram: false,
  1103. betaProgram: false,
  1104. })
  1105. const THEME_LIST = []
  1106. function generateThemeList() {
  1107. const files = fs.readdirSync(
  1108. Path.join(__dirname, '/../../../../node_modules/ace-builds/src-noconflict')
  1109. )
  1110. const result = []
  1111. for (const file of files) {
  1112. if (file.slice(-2) === 'js' && /^theme-/.test(file)) {
  1113. const cleanName = file.slice(0, -3).slice(6)
  1114. result.push(THEME_LIST.push(cleanName))
  1115. } else {
  1116. result.push(undefined)
  1117. }
  1118. }
  1119. }
  1120. generateThemeList()
  1121. module.exports = ProjectController