ProjectController.js 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  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 ProjectHistoryHandler = require('./ProjectHistoryHandler')
  13. const EditorController = require('../Editor/EditorController')
  14. const ProjectHelper = require('./ProjectHelper')
  15. const metrics = require('@overleaf/metrics')
  16. const { User } = require('../../models/User')
  17. const TagsHandler = require('../Tags/TagsHandler')
  18. const SubscriptionLocator = require('../Subscription/SubscriptionLocator')
  19. const NotificationsHandler = require('../Notifications/NotificationsHandler')
  20. const LimitationsManager = require('../Subscription/LimitationsManager')
  21. const Settings = require('@overleaf/settings')
  22. const AuthorizationManager = require('../Authorization/AuthorizationManager')
  23. const InactiveProjectManager = require('../InactiveData/InactiveProjectManager')
  24. const ProjectUpdateHandler = require('./ProjectUpdateHandler')
  25. const ProjectGetter = require('./ProjectGetter')
  26. const PrivilegeLevels = require('../Authorization/PrivilegeLevels')
  27. const SessionManager = require('../Authentication/SessionManager')
  28. const Sources = require('../Authorization/Sources')
  29. const TokenAccessHandler = require('../TokenAccess/TokenAccessHandler')
  30. const CollaboratorsGetter = require('../Collaborators/CollaboratorsGetter')
  31. const ProjectEntityHandler = require('./ProjectEntityHandler')
  32. const TpdsProjectFlusher = require('../ThirdPartyDataStore/TpdsProjectFlusher')
  33. const UserGetter = require('../User/UserGetter')
  34. const NotificationsBuilder = require('../Notifications/NotificationsBuilder')
  35. const { V1ConnectionError } = require('../Errors/Errors')
  36. const Features = require('../../infrastructure/Features')
  37. const BrandVariationsHandler = require('../BrandVariations/BrandVariationsHandler')
  38. const UserController = require('../User/UserController')
  39. const AnalyticsManager = require('../Analytics/AnalyticsManager')
  40. const Modules = require('../../infrastructure/Modules')
  41. const SplitTestHandler = require('../SplitTests/SplitTestHandler')
  42. const { getNewLogsUIVariantForUser } = require('../Helpers/NewLogsUI')
  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. // Module.hooks.fire accepts multiple methods
  395. // and does async.series
  396. const allInReconfirmNotificationPeriods =
  397. (results && results[0]) || []
  398. return cb(null, {
  399. list: fullEmails,
  400. allInReconfirmNotificationPeriods,
  401. })
  402. }
  403. )
  404. })
  405. },
  406. },
  407. (err, results) => {
  408. if (err != null) {
  409. OError.tag(err, 'error getting data for project list page')
  410. return next(err)
  411. }
  412. const { notifications, user, userEmailsData } = results
  413. const userEmails = userEmailsData.list || []
  414. const userAffiliations = userEmails
  415. .filter(emailData => !!emailData.affiliation)
  416. .map(emailData => {
  417. const result = emailData.affiliation
  418. result.email = emailData.email
  419. return result
  420. })
  421. const { allInReconfirmNotificationPeriods } = userEmailsData
  422. // Handle case of deleted user
  423. if (user == null) {
  424. UserController.logout(req, res, next)
  425. return
  426. }
  427. const tags = results.tags
  428. const notificationsInstitution = []
  429. for (const notification of notifications) {
  430. notification.html = req.i18n.translate(
  431. notification.templateKey,
  432. notification.messageOpts
  433. )
  434. }
  435. // Institution SSO Notifications
  436. let reconfirmedViaSAML
  437. if (Features.hasFeature('saml')) {
  438. reconfirmedViaSAML = _.get(req.session, ['saml', 'reconfirmed'])
  439. const samlSession = req.session.saml
  440. // Notification: SSO Available
  441. const linkedInstitutionIds = []
  442. user.emails.forEach(email => {
  443. if (email.samlProviderId) {
  444. linkedInstitutionIds.push(email.samlProviderId)
  445. }
  446. })
  447. if (Array.isArray(userAffiliations)) {
  448. userAffiliations.forEach(affiliation => {
  449. if (
  450. _ssoAvailable(affiliation, req.session, linkedInstitutionIds)
  451. ) {
  452. notificationsInstitution.push({
  453. email: affiliation.email,
  454. institutionId: affiliation.institution.id,
  455. institutionName: affiliation.institution.name,
  456. templateKey: 'notification_institution_sso_available',
  457. })
  458. }
  459. })
  460. }
  461. if (samlSession) {
  462. // Notification: After SSO Linked
  463. if (samlSession.linked) {
  464. notificationsInstitution.push({
  465. email: samlSession.institutionEmail,
  466. institutionName: samlSession.linked.universityName,
  467. templateKey: 'notification_institution_sso_linked',
  468. })
  469. }
  470. // Notification: After SSO Linked or Logging in
  471. // The requested email does not match primary email returned from
  472. // the institution
  473. if (
  474. samlSession.requestedEmail &&
  475. samlSession.emailNonCanonical &&
  476. !samlSession.error
  477. ) {
  478. notificationsInstitution.push({
  479. institutionEmail: samlSession.emailNonCanonical,
  480. requestedEmail: samlSession.requestedEmail,
  481. templateKey: 'notification_institution_sso_non_canonical',
  482. })
  483. }
  484. // Notification: Tried to register, but account already existed
  485. // registerIntercept is set before the institution callback.
  486. // institutionEmail is set after institution callback.
  487. // Check for both in case SSO flow was abandoned
  488. if (
  489. samlSession.registerIntercept &&
  490. samlSession.institutionEmail &&
  491. !samlSession.error
  492. ) {
  493. notificationsInstitution.push({
  494. email: samlSession.institutionEmail,
  495. templateKey: 'notification_institution_sso_already_registered',
  496. })
  497. }
  498. // Notification: When there is a session error
  499. if (samlSession.error) {
  500. notificationsInstitution.push({
  501. templateKey: 'notification_institution_sso_error',
  502. error: samlSession.error,
  503. })
  504. }
  505. }
  506. delete req.session.saml
  507. }
  508. const portalTemplates = ProjectController._buildPortalTemplatesList(
  509. userAffiliations
  510. )
  511. const projects = ProjectController._buildProjectList(
  512. results.projects,
  513. userId
  514. )
  515. // in v2 add notifications for matching university IPs
  516. if (Settings.overleaf != null && req.ip !== user.lastLoginIp) {
  517. NotificationsBuilder.ipMatcherAffiliation(user._id).create(req.ip)
  518. }
  519. ProjectController._injectProjectUsers(projects, (error, projects) => {
  520. if (error != null) {
  521. return next(error)
  522. }
  523. const viewModel = {
  524. title: 'your_projects',
  525. priority_title: true,
  526. projects,
  527. tags,
  528. notifications: notifications || [],
  529. notificationsInstitution,
  530. allInReconfirmNotificationPeriods,
  531. portalTemplates,
  532. user,
  533. userAffiliations,
  534. userEmails,
  535. hasSubscription: results.hasSubscription,
  536. reconfirmedViaSAML,
  537. zipFileSizeLimit: Settings.maxUploadSize,
  538. isOverleaf: !!Settings.overleaf,
  539. }
  540. const paidUser =
  541. (user.features != null ? user.features.github : undefined) &&
  542. (user.features != null ? user.features.dropbox : undefined) // use a heuristic for paid account
  543. const freeUserProportion = 0.1
  544. const sampleFreeUser =
  545. parseInt(user._id.toString().slice(-2), 16) <
  546. freeUserProportion * 255
  547. const showFrontWidget = paidUser || sampleFreeUser
  548. if (showFrontWidget) {
  549. viewModel.frontChatWidgetRoomId =
  550. Settings.overleaf != null
  551. ? Settings.overleaf.front_chat_widget_room_id
  552. : undefined
  553. }
  554. res.render('project/list', viewModel)
  555. timer.done()
  556. })
  557. }
  558. )
  559. },
  560. loadEditor(req, res, next) {
  561. const timer = new metrics.Timer('load-editor')
  562. if (!Settings.editorIsOpen) {
  563. return res.render('general/closed', { title: 'updating_site' })
  564. }
  565. let anonymous, userId, sessionUser
  566. if (SessionManager.isUserLoggedIn(req.session)) {
  567. sessionUser = SessionManager.getSessionUser(req.session)
  568. userId = SessionManager.getLoggedInUserId(req.session)
  569. anonymous = false
  570. } else {
  571. sessionUser = null
  572. anonymous = true
  573. userId = null
  574. }
  575. const projectId = req.params.Project_id
  576. async.auto(
  577. {
  578. project(cb) {
  579. ProjectGetter.getProject(
  580. projectId,
  581. {
  582. name: 1,
  583. lastUpdated: 1,
  584. track_changes: 1,
  585. owner_ref: 1,
  586. brandVariationId: 1,
  587. overleaf: 1,
  588. tokens: 1,
  589. },
  590. (err, project) => {
  591. if (err != null) {
  592. return cb(err)
  593. }
  594. cb(null, project)
  595. }
  596. )
  597. },
  598. user(cb) {
  599. if (userId == null) {
  600. cb(null, defaultSettingsForAnonymousUser(userId))
  601. } else {
  602. User.findById(
  603. userId,
  604. 'email first_name last_name referal_id signUpDate featureSwitches features refProviders alphaProgram betaProgram isAdmin ace',
  605. (err, user) => {
  606. // Handle case of deleted user
  607. if (user == null) {
  608. UserController.logout(req, res, next)
  609. return
  610. }
  611. logger.log({ projectId, userId }, 'got user')
  612. cb(err, user)
  613. }
  614. )
  615. }
  616. },
  617. subscription(cb) {
  618. if (userId == null) {
  619. return cb()
  620. }
  621. SubscriptionLocator.getUsersSubscription(userId, cb)
  622. },
  623. activate(cb) {
  624. InactiveProjectManager.reactivateProjectIfRequired(projectId, cb)
  625. },
  626. ensureHistoryExists(cb) {
  627. // enable full project history in background for older projects
  628. if (!Settings.apis.project_history || !Features.hasFeature('saas')) {
  629. return cb()
  630. }
  631. ProjectHistoryHandler.ensureHistoryExistsForProject(
  632. projectId,
  633. err => {
  634. if (err) {
  635. logger.error(
  636. { err, projectId },
  637. 'error ensuring history exists for project'
  638. )
  639. }
  640. cb()
  641. }
  642. )
  643. },
  644. markAsOpened(cb) {
  645. // don't need to wait for this to complete
  646. ProjectUpdateHandler.markAsOpened(projectId, () => {})
  647. cb()
  648. },
  649. isTokenMember(cb) {
  650. if (userId == null) {
  651. return cb()
  652. }
  653. CollaboratorsGetter.userIsTokenMember(userId, projectId, cb)
  654. },
  655. brandVariation: [
  656. 'project',
  657. (cb, results) => {
  658. if (
  659. (results.project != null
  660. ? results.project.brandVariationId
  661. : undefined) == null
  662. ) {
  663. return cb()
  664. }
  665. BrandVariationsHandler.getBrandVariationById(
  666. results.project.brandVariationId,
  667. (error, brandVariationDetails) => cb(error, brandVariationDetails)
  668. )
  669. },
  670. ],
  671. flushToTpds: cb => {
  672. TpdsProjectFlusher.flushProjectToTpdsIfNeeded(projectId, cb)
  673. },
  674. pdfCachingFeatureFlag(cb) {
  675. if (!Settings.enablePdfCaching) return cb(null, '')
  676. if (!userId) return cb(null, 'enable-caching-only')
  677. SplitTestHandler.getTestSegmentation(
  678. userId,
  679. 'pdf_caching_full',
  680. (err, segmentation) => {
  681. if (err) {
  682. // Do not fail loading the editor.
  683. return cb(null, '')
  684. }
  685. cb(null, (segmentation && segmentation.variant) || '')
  686. }
  687. )
  688. },
  689. },
  690. (err, results) => {
  691. if (err != null) {
  692. OError.tag(err, 'error getting details for project page')
  693. return next(err)
  694. }
  695. const { project } = results
  696. const { user } = results
  697. const { subscription } = results
  698. const { brandVariation } = results
  699. const { pdfCachingFeatureFlag } = results
  700. const anonRequestToken = TokenAccessHandler.getRequestToken(
  701. req,
  702. projectId
  703. )
  704. const { isTokenMember } = results
  705. const allowedImageNames = ProjectHelper.getAllowedImagesForUser(
  706. sessionUser
  707. )
  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.recordEvent(userId, 'project-opened', {
  752. projectId: project._id,
  753. })
  754. }
  755. const logsUIVariant = getNewLogsUIVariantForUser(user)
  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. function partOfPdfCachingRollout(flag) {
  764. if (!Settings.enablePdfCaching) {
  765. // The feature is disabled globally.
  766. return false
  767. }
  768. const canSeeFeaturePreview = pdfCachingFeatureFlag.includes(flag)
  769. if (!canSeeFeaturePreview) {
  770. // The user is not in the target group.
  771. return false
  772. }
  773. // Optionally let the user opt-out.
  774. // The will opt-out of both caching and metrics collection,
  775. // as if this editing session never happened.
  776. return shouldDisplayFeature('enable_pdf_caching', true)
  777. }
  778. res.render('project/editor', {
  779. title: project.name,
  780. priority_title: true,
  781. bodyClasses: ['editor'],
  782. project_id: project._id,
  783. user: {
  784. id: userId,
  785. email: user.email,
  786. first_name: user.first_name,
  787. last_name: user.last_name,
  788. referal_id: user.referal_id,
  789. signUpDate: user.signUpDate,
  790. allowedFreeTrial: allowedFreeTrial,
  791. featureSwitches: user.featureSwitches,
  792. features: user.features,
  793. refProviders: _.mapValues(user.refProviders, Boolean),
  794. alphaProgram: user.alphaProgram,
  795. betaProgram: user.betaProgram,
  796. isAdmin: user.isAdmin,
  797. },
  798. userSettings: {
  799. mode: user.ace.mode,
  800. editorTheme: user.ace.theme,
  801. fontSize: user.ace.fontSize,
  802. autoComplete: user.ace.autoComplete,
  803. autoPairDelimiters: user.ace.autoPairDelimiters,
  804. pdfViewer: user.ace.pdfViewer,
  805. syntaxValidation: user.ace.syntaxValidation,
  806. fontFamily: user.ace.fontFamily || 'lucida',
  807. lineHeight: user.ace.lineHeight || 'normal',
  808. overallTheme: user.ace.overallTheme,
  809. },
  810. privilegeLevel,
  811. anonymous,
  812. anonymousAccessToken: anonymous ? anonRequestToken : null,
  813. isTokenMember,
  814. isRestrictedTokenMember: AuthorizationManager.isRestrictedUser(
  815. userId,
  816. privilegeLevel,
  817. isTokenMember
  818. ),
  819. languages: Settings.languages,
  820. editorThemes: THEME_LIST,
  821. maxDocLength: Settings.max_doc_length,
  822. useV2History:
  823. project.overleaf &&
  824. project.overleaf.history &&
  825. Boolean(project.overleaf.history.display),
  826. brandVariation,
  827. allowedImageNames,
  828. gitBridgePublicBaseUrl: Settings.gitBridgePublicBaseUrl,
  829. wsUrl,
  830. showSupport: Features.hasFeature('support'),
  831. showNewLogsUI: shouldDisplayFeature(
  832. 'new_logs_ui',
  833. logsUIVariant.newLogsUI
  834. ),
  835. logsUISubvariant: logsUIVariant.subvariant,
  836. showNewNavigationUI: shouldDisplayFeature(
  837. 'new_navigation_ui',
  838. true
  839. ),
  840. showNewFileViewUI: shouldDisplayFeature(
  841. 'new_file_view',
  842. user.alphaProgram || user.betaProgram
  843. ),
  844. showSymbolPalette: shouldDisplayFeature(
  845. 'symbol_palette',
  846. user.alphaProgram || user.betaProgram
  847. ),
  848. trackPdfDownload: partOfPdfCachingRollout('collect-metrics'),
  849. enablePdfCaching: partOfPdfCachingRollout('enable-caching'),
  850. resetServiceWorker: Boolean(Settings.resetServiceWorker),
  851. })
  852. timer.done()
  853. }
  854. )
  855. }
  856. )
  857. },
  858. _buildProjectList(allProjects, userId) {
  859. let project
  860. const {
  861. owned,
  862. readAndWrite,
  863. readOnly,
  864. tokenReadAndWrite,
  865. tokenReadOnly,
  866. } = allProjects
  867. const projects = []
  868. for (project of owned) {
  869. projects.push(
  870. ProjectController._buildProjectViewModel(
  871. project,
  872. 'owner',
  873. Sources.OWNER,
  874. userId
  875. )
  876. )
  877. }
  878. // Invite-access
  879. for (project of readAndWrite) {
  880. projects.push(
  881. ProjectController._buildProjectViewModel(
  882. project,
  883. 'readWrite',
  884. Sources.INVITE,
  885. userId
  886. )
  887. )
  888. }
  889. for (project of readOnly) {
  890. projects.push(
  891. ProjectController._buildProjectViewModel(
  892. project,
  893. 'readOnly',
  894. Sources.INVITE,
  895. userId
  896. )
  897. )
  898. }
  899. // Token-access
  900. // Only add these projects if they're not already present, this gives us cascading access
  901. // from 'owner' => 'token-read-only'
  902. for (project of tokenReadAndWrite) {
  903. if (
  904. projects.filter(p => p.id.toString() === project._id.toString())
  905. .length === 0
  906. ) {
  907. projects.push(
  908. ProjectController._buildProjectViewModel(
  909. project,
  910. 'readAndWrite',
  911. Sources.TOKEN,
  912. userId
  913. )
  914. )
  915. }
  916. }
  917. for (project of tokenReadOnly) {
  918. if (
  919. projects.filter(p => p.id.toString() === project._id.toString())
  920. .length === 0
  921. ) {
  922. projects.push(
  923. ProjectController._buildProjectViewModel(
  924. project,
  925. 'readOnly',
  926. Sources.TOKEN,
  927. userId
  928. )
  929. )
  930. }
  931. }
  932. return projects
  933. },
  934. _buildProjectViewModel(project, accessLevel, source, userId) {
  935. const archived = ProjectHelper.isArchived(project, userId)
  936. // If a project is simultaneously trashed and archived, we will consider it archived but not trashed.
  937. const trashed = ProjectHelper.isTrashed(project, userId) && !archived
  938. TokenAccessHandler.protectTokens(project, accessLevel)
  939. const model = {
  940. id: project._id,
  941. name: project.name,
  942. lastUpdated: project.lastUpdated,
  943. lastUpdatedBy: project.lastUpdatedBy,
  944. publicAccessLevel: project.publicAccesLevel,
  945. accessLevel,
  946. source,
  947. archived,
  948. trashed,
  949. owner_ref: project.owner_ref,
  950. isV1Project: false,
  951. }
  952. if (accessLevel === PrivilegeLevels.READ_ONLY && source === Sources.TOKEN) {
  953. model.owner_ref = null
  954. model.lastUpdatedBy = null
  955. }
  956. return model
  957. },
  958. _injectProjectUsers(projects, callback) {
  959. const users = {}
  960. for (const project of projects) {
  961. if (project.owner_ref != null) {
  962. users[project.owner_ref.toString()] = true
  963. }
  964. if (project.lastUpdatedBy != null) {
  965. users[project.lastUpdatedBy.toString()] = true
  966. }
  967. }
  968. const userIds = Object.keys(users)
  969. async.eachSeries(
  970. userIds,
  971. (userId, cb) => {
  972. UserGetter.getUser(
  973. userId,
  974. { first_name: 1, last_name: 1, email: 1 },
  975. (error, user) => {
  976. if (error != null) {
  977. return cb(error)
  978. }
  979. users[userId] = user
  980. cb()
  981. }
  982. )
  983. },
  984. error => {
  985. if (error != null) {
  986. return callback(error)
  987. }
  988. for (const project of projects) {
  989. if (project.owner_ref != null) {
  990. project.owner = users[project.owner_ref.toString()]
  991. }
  992. if (project.lastUpdatedBy != null) {
  993. project.lastUpdatedBy =
  994. users[project.lastUpdatedBy.toString()] || null
  995. }
  996. }
  997. callback(null, projects)
  998. }
  999. )
  1000. },
  1001. _buildPortalTemplatesList(affiliations) {
  1002. if (affiliations == null) {
  1003. affiliations = []
  1004. }
  1005. const portalTemplates = []
  1006. for (const aff of affiliations) {
  1007. if (
  1008. aff.portal &&
  1009. aff.portal.slug &&
  1010. aff.portal.templates_count &&
  1011. aff.portal.templates_count > 0
  1012. ) {
  1013. const portalPath = aff.institution.isUniversity ? '/edu/' : '/org/'
  1014. portalTemplates.push({
  1015. name: aff.institution.name,
  1016. url: Settings.siteUrl + portalPath + aff.portal.slug,
  1017. })
  1018. }
  1019. }
  1020. return portalTemplates
  1021. },
  1022. }
  1023. var defaultSettingsForAnonymousUser = userId => ({
  1024. id: userId,
  1025. ace: {
  1026. mode: 'none',
  1027. theme: 'textmate',
  1028. fontSize: '12',
  1029. autoComplete: true,
  1030. spellCheckLanguage: '',
  1031. pdfViewer: '',
  1032. syntaxValidation: true,
  1033. },
  1034. subscription: {
  1035. freeTrial: {
  1036. allowed: true,
  1037. },
  1038. },
  1039. featureSwitches: {
  1040. github: false,
  1041. },
  1042. alphaProgram: false,
  1043. betaProgram: false,
  1044. })
  1045. var THEME_LIST = []
  1046. function generateThemeList() {
  1047. const files = fs.readdirSync(
  1048. Path.join(__dirname, '/../../../../node_modules/ace-builds/src-noconflict')
  1049. )
  1050. const result = []
  1051. for (const file of files) {
  1052. if (file.slice(-2) === 'js' && /^theme-/.test(file)) {
  1053. const cleanName = file.slice(0, -3).slice(6)
  1054. result.push(THEME_LIST.push(cleanName))
  1055. } else {
  1056. result.push(undefined)
  1057. }
  1058. }
  1059. }
  1060. generateThemeList()
  1061. module.exports = ProjectController