ProjectController.js 34 KB

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