| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- /* eslint-disable
- camelcase,
- max-len,
- */
- // TODO: This file was created by bulk-decaffeinate.
- // Fix any style issues and re-enable lint.
- /*
- * decaffeinate suggestions:
- * DS102: Remove unnecessary code created because of implicit returns
- * DS207: Consider shorter variations of null checks
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- let TokenAccessController
- const ProjectController = require('../Project/ProjectController')
- const AuthenticationController = require('../Authentication/AuthenticationController')
- const TokenAccessHandler = require('./TokenAccessHandler')
- const Errors = require('../Errors/Errors')
- const logger = require('logger-sharelatex')
- const settings = require('settings-sharelatex')
- module.exports = TokenAccessController = {
- _loadEditor(projectId, req, res, next) {
- req.params.Project_id = projectId.toString()
- return ProjectController.loadEditor(req, res, next)
- },
- _tryHigherAccess(token, userId, req, res, next) {
- return TokenAccessHandler.findProjectWithHigherAccess(
- token,
- userId,
- function(err, project) {
- if (err != null) {
- logger.warn(
- { err, token, userId },
- '[TokenAccess] error finding project with higher access'
- )
- return next(err)
- }
- if (project == null) {
- logger.log(
- { token, userId },
- '[TokenAccess] no project with higher access found for this user and token'
- )
- return next(new Errors.NotFoundError())
- }
- logger.log(
- { token, userId, projectId: project._id },
- '[TokenAccess] user has higher access to project, redirecting'
- )
- return res.redirect(302, `/project/${project._id}`)
- }
- )
- },
- readAndWriteToken(req, res, next) {
- const userId = AuthenticationController.getLoggedInUserId(req)
- const token = req.params['read_and_write_token']
- logger.log(
- { userId, token },
- '[TokenAccess] requesting read-and-write token access'
- )
- return TokenAccessHandler.findProjectWithReadAndWriteToken(token, function(
- err,
- project,
- projectExists
- ) {
- if (err != null) {
- logger.warn(
- { err, token, userId },
- '[TokenAccess] error getting project by readAndWrite token'
- )
- return next(err)
- }
- if (!projectExists && settings.overleaf) {
- logger.log(
- { token, userId },
- '[TokenAccess] no project found for this token'
- )
- return TokenAccessController._handleV1Project(
- token,
- userId,
- `/${token}`,
- res,
- next
- )
- } else if (project == null) {
- logger.log(
- { token, userId },
- '[TokenAccess] no token-based project found for readAndWrite token'
- )
- if (userId == null) {
- logger.log(
- { token },
- '[TokenAccess] No project found with read-write token, anonymous user, deny'
- )
- return next(new Errors.NotFoundError())
- }
- return TokenAccessController._tryHigherAccess(
- token,
- userId,
- req,
- res,
- next
- )
- } else {
- if (userId == null) {
- if (TokenAccessHandler.ANONYMOUS_READ_AND_WRITE_ENABLED) {
- logger.log(
- { token, projectId: project._id },
- '[TokenAccess] allow anonymous read-and-write token access'
- )
- TokenAccessHandler.grantSessionTokenAccess(req, project._id, token)
- req._anonymousAccessToken = token
- return TokenAccessController._loadEditor(
- project._id,
- req,
- res,
- next
- )
- } else {
- logger.log(
- { token, projectId: project._id },
- '[TokenAccess] deny anonymous read-and-write token access'
- )
- AuthenticationController.setRedirectInSession(req)
- return res.redirect('/restricted')
- }
- }
- if (project.owner_ref.toString() === userId) {
- logger.log(
- { userId, projectId: project._id },
- '[TokenAccess] user is already project owner'
- )
- return TokenAccessController._loadEditor(project._id, req, res, next)
- }
- logger.log(
- { userId, projectId: project._id },
- '[TokenAccess] adding user to project with readAndWrite token'
- )
- return TokenAccessHandler.addReadAndWriteUserToProject(
- userId,
- project._id,
- function(err) {
- if (err != null) {
- logger.warn(
- { err, token, userId, projectId: project._id },
- '[TokenAccess] error adding user to project with readAndWrite token'
- )
- return next(err)
- }
- return TokenAccessController._loadEditor(
- project._id,
- req,
- res,
- next
- )
- }
- )
- }
- })
- },
- readOnlyToken(req, res, next) {
- const userId = AuthenticationController.getLoggedInUserId(req)
- const token = req.params['read_only_token']
- logger.log(
- { userId, token },
- '[TokenAccess] requesting read-only token access'
- )
- return TokenAccessHandler.getV1DocPublishedInfo(token, function(
- err,
- doc_published_info
- ) {
- if (err != null) {
- return next(err)
- }
- if (doc_published_info.allow === false) {
- return res.redirect(doc_published_info.published_path)
- }
- return TokenAccessHandler.findProjectWithReadOnlyToken(token, function(
- err,
- project,
- projectExists
- ) {
- if (err != null) {
- logger.warn(
- { err, token, userId },
- '[TokenAccess] error getting project by readOnly token'
- )
- return next(err)
- }
- if (!projectExists && settings.overleaf) {
- logger.log(
- { token, userId },
- '[TokenAccess] no project found for this token'
- )
- return TokenAccessController._handleV1Project(
- token,
- userId,
- `/read/${token}`,
- res,
- next
- )
- } else if (project == null) {
- logger.log(
- { token, userId },
- '[TokenAccess] no project found for readOnly token'
- )
- if (userId == null) {
- logger.log(
- { token },
- '[TokenAccess] No project found with readOnly token, anonymous user, deny'
- )
- return next(new Errors.NotFoundError())
- }
- return TokenAccessController._tryHigherAccess(
- token,
- userId,
- req,
- res,
- next
- )
- } else {
- if (userId == null) {
- logger.log(
- { userId, projectId: project._id },
- '[TokenAccess] adding anonymous user to project with readOnly token'
- )
- TokenAccessHandler.grantSessionTokenAccess(req, project._id, token)
- req._anonymousAccessToken = token
- return TokenAccessController._loadEditor(
- project._id,
- req,
- res,
- next
- )
- } else {
- if (project.owner_ref.toString() === userId) {
- logger.log(
- { userId, projectId: project._id },
- '[TokenAccess] user is already project owner'
- )
- return TokenAccessController._loadEditor(
- project._id,
- req,
- res,
- next
- )
- }
- logger.log(
- { userId, projectId: project._id },
- '[TokenAccess] adding user to project with readOnly token'
- )
- return TokenAccessHandler.addReadOnlyUserToProject(
- userId,
- project._id,
- function(err) {
- if (err != null) {
- logger.warn(
- { err, token, userId, projectId: project._id },
- '[TokenAccess] error adding user to project with readAndWrite token'
- )
- return next(err)
- }
- return TokenAccessController._loadEditor(
- project._id,
- req,
- res,
- next
- )
- }
- )
- }
- }
- })
- })
- },
- _handleV1Project(token, userId, redirectPath, res, next) {
- if (userId == null) {
- return res.render('project/v2-import', { loginRedirect: redirectPath })
- } else {
- return TokenAccessHandler.getV1DocInfo(token, userId, function(
- err,
- doc_info
- ) {
- if (err != null) {
- return next(err)
- }
- if (!doc_info.exists) {
- return next(new Errors.NotFoundError())
- }
- if (doc_info.exported) {
- return next(new Errors.NotFoundError())
- }
- return res.render('project/v2-import', {
- projectId: token,
- hasOwner: doc_info.has_owner,
- name: doc_info.name || 'Untitled',
- hasAssignment: doc_info.has_assignment,
- brandInfo: doc_info.brand_info
- })
- })
- }
- }
- }
|