ProjectController.js 42 KB

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