2019-09-09 07:52:25 -04:00
|
|
|
const _ = require('underscore')
|
2019-05-29 05:21:06 -04:00
|
|
|
const ProjectGetter = require('./ProjectGetter')
|
|
|
|
const UserGetter = require('../User/UserGetter')
|
|
|
|
const { Project } = require('../../models/Project')
|
2021-11-10 08:40:18 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2019-09-09 07:52:25 -04:00
|
|
|
const TpdsUpdateSender = require('../ThirdPartyDataStore/TpdsUpdateSender')
|
2019-05-29 05:21:06 -04:00
|
|
|
const PublicAccessLevels = require('../Authorization/PublicAccessLevels')
|
|
|
|
const Errors = require('../Errors/Errors')
|
2021-03-31 05:28:19 -04:00
|
|
|
const TokenGenerator = require('../TokenGenerator/TokenGenerator')
|
2019-05-29 05:21:06 -04:00
|
|
|
const ProjectHelper = require('./ProjectHelper')
|
2021-07-07 05:38:56 -04:00
|
|
|
const settings = require('@overleaf/settings')
|
2019-09-09 07:52:25 -04:00
|
|
|
const { callbackify } = require('util')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
const MAX_PROJECT_NAME_LENGTH = 150
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
module.exports = {
|
|
|
|
MAX_PROJECT_NAME_LENGTH,
|
|
|
|
getDetails: callbackify(getDetails),
|
|
|
|
getProjectDescription: callbackify(getProjectDescription),
|
|
|
|
setProjectDescription: callbackify(setProjectDescription),
|
|
|
|
renameProject: callbackify(renameProject),
|
|
|
|
validateProjectName: callbackify(validateProjectName),
|
|
|
|
generateUniqueName: callbackify(generateUniqueName),
|
|
|
|
setPublicAccessLevel: callbackify(setPublicAccessLevel),
|
|
|
|
ensureTokensArePresent: callbackify(ensureTokensArePresent),
|
2019-10-14 09:22:45 -04:00
|
|
|
clearTokens: callbackify(clearTokens),
|
2019-09-09 07:52:25 -04:00
|
|
|
fixProjectName,
|
|
|
|
promises: {
|
|
|
|
getDetails,
|
|
|
|
getProjectDescription,
|
|
|
|
setProjectDescription,
|
|
|
|
renameProject,
|
|
|
|
validateProjectName,
|
|
|
|
generateUniqueName,
|
|
|
|
setPublicAccessLevel,
|
2019-10-14 09:22:45 -04:00
|
|
|
ensureTokensArePresent,
|
2021-04-27 03:52:58 -04:00
|
|
|
clearTokens,
|
|
|
|
},
|
2019-09-09 07:52:25 -04:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
async function getDetails(projectId) {
|
|
|
|
let project
|
|
|
|
try {
|
|
|
|
project = await ProjectGetter.promises.getProject(projectId, {
|
|
|
|
name: true,
|
|
|
|
description: true,
|
|
|
|
compiler: true,
|
|
|
|
features: true,
|
|
|
|
owner_ref: true,
|
2021-04-27 03:52:58 -04:00
|
|
|
overleaf: true,
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
2019-09-09 07:52:25 -04:00
|
|
|
} catch (err) {
|
|
|
|
logger.warn({ err, projectId }, 'error getting project')
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
if (project == null) {
|
|
|
|
throw new Errors.NotFoundError('project not found')
|
|
|
|
}
|
|
|
|
const user = await UserGetter.promises.getUser(project.owner_ref)
|
|
|
|
const details = {
|
|
|
|
name: project.name,
|
|
|
|
description: project.description,
|
|
|
|
compiler: project.compiler,
|
|
|
|
features:
|
|
|
|
user != null && user.features != null
|
|
|
|
? user.features
|
2021-04-27 03:52:58 -04:00
|
|
|
: settings.defaultFeatures,
|
2019-09-09 07:52:25 -04:00
|
|
|
}
|
|
|
|
if (project.overleaf != null) {
|
|
|
|
details.overleaf = project.overleaf
|
|
|
|
}
|
|
|
|
return details
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
async function getProjectDescription(projectId) {
|
|
|
|
const project = await ProjectGetter.promises.getProject(projectId, {
|
2021-04-27 03:52:58 -04:00
|
|
|
description: true,
|
2019-09-09 07:52:25 -04:00
|
|
|
})
|
|
|
|
if (project == null) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
return project.description
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
async function setProjectDescription(projectId, description) {
|
|
|
|
const conditions = { _id: projectId }
|
|
|
|
const update = { description }
|
|
|
|
logger.log(
|
|
|
|
{ conditions, update, projectId, description },
|
|
|
|
'setting project description'
|
|
|
|
)
|
|
|
|
try {
|
2020-11-03 04:19:05 -05:00
|
|
|
await Project.updateOne(conditions, update).exec()
|
2019-09-09 07:52:25 -04:00
|
|
|
} catch (err) {
|
|
|
|
logger.warn({ err }, 'something went wrong setting project description')
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async function renameProject(projectId, newName) {
|
2022-03-24 05:18:49 -04:00
|
|
|
newName = newName.trim()
|
2019-09-09 07:52:25 -04:00
|
|
|
await validateProjectName(newName)
|
|
|
|
logger.log({ projectId, newName }, 'renaming project')
|
|
|
|
let project
|
|
|
|
try {
|
|
|
|
project = await ProjectGetter.promises.getProject(projectId, { name: true })
|
|
|
|
} catch (err) {
|
|
|
|
logger.warn({ err, projectId }, 'error getting project')
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
if (project == null) {
|
|
|
|
logger.warn({ projectId }, 'could not find project to rename')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const oldProjectName = project.name
|
2020-11-03 04:19:05 -05:00
|
|
|
await Project.updateOne({ _id: projectId }, { name: newName }).exec()
|
2019-09-09 07:52:25 -04:00
|
|
|
await TpdsUpdateSender.promises.moveEntity({
|
|
|
|
project_id: projectId,
|
|
|
|
project_name: oldProjectName,
|
2021-04-27 03:52:58 -04:00
|
|
|
newProjectName: newName,
|
2019-09-09 07:52:25 -04:00
|
|
|
})
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
async function validateProjectName(name) {
|
|
|
|
if (name == null || name.length === 0) {
|
|
|
|
throw new Errors.InvalidNameError('Project name cannot be blank')
|
|
|
|
}
|
|
|
|
if (name.length > MAX_PROJECT_NAME_LENGTH) {
|
|
|
|
throw new Errors.InvalidNameError('Project name is too long')
|
|
|
|
}
|
|
|
|
if (name.indexOf('/') > -1) {
|
|
|
|
throw new Errors.InvalidNameError(
|
|
|
|
'Project name cannot contain / characters'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if (name.indexOf('\\') > -1) {
|
|
|
|
throw new Errors.InvalidNameError(
|
|
|
|
'Project name cannot contain \\ characters'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
// FIXME: we should put a lock around this to make it completely safe, but we would need to do that at
|
|
|
|
// the point of project creation, rather than just checking the name at the start of the import.
|
|
|
|
// If we later move this check into ProjectCreationHandler we can ensure all new projects are created
|
|
|
|
// with a unique name. But that requires thinking through how we would handle incoming projects from
|
|
|
|
// dropbox for example.
|
|
|
|
async function generateUniqueName(userId, name, suffixes = []) {
|
2022-01-10 05:23:05 -05:00
|
|
|
const allUsersProjectNames =
|
|
|
|
await ProjectGetter.promises.findAllUsersProjects(userId, { name: 1 })
|
2019-09-09 07:52:25 -04:00
|
|
|
// allUsersProjectNames is returned as a hash {owned: [name1, name2, ...], readOnly: [....]}
|
|
|
|
// collect all of the names and flatten them into a single array
|
|
|
|
const projectNameList = _.pluck(
|
|
|
|
_.flatten(_.values(allUsersProjectNames)),
|
|
|
|
'name'
|
|
|
|
)
|
|
|
|
const uniqueName = await ProjectHelper.promises.ensureNameIsUnique(
|
|
|
|
projectNameList,
|
|
|
|
name,
|
|
|
|
suffixes,
|
|
|
|
MAX_PROJECT_NAME_LENGTH
|
|
|
|
)
|
|
|
|
return uniqueName
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
function fixProjectName(name) {
|
|
|
|
if (name === '' || !name) {
|
|
|
|
name = 'Untitled'
|
|
|
|
}
|
|
|
|
if (name.indexOf('/') > -1) {
|
|
|
|
// v2 does not allow / in a project name
|
|
|
|
name = name.replace(/\//g, '-')
|
|
|
|
}
|
|
|
|
if (name.indexOf('\\') > -1) {
|
|
|
|
// backslashes in project name will prevent syncing to dropbox
|
|
|
|
name = name.replace(/\\/g, '')
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-09-09 07:52:25 -04:00
|
|
|
if (name.length > MAX_PROJECT_NAME_LENGTH) {
|
|
|
|
name = name.substr(0, MAX_PROJECT_NAME_LENGTH)
|
|
|
|
}
|
|
|
|
return name
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
async function setPublicAccessLevel(projectId, newAccessLevel) {
|
|
|
|
// DEPRECATED: `READ_ONLY` and `READ_AND_WRITE` are still valid in, but should no longer
|
|
|
|
// be passed here. Remove after token-based access has been live for a while
|
|
|
|
if (
|
|
|
|
projectId != null &&
|
|
|
|
newAccessLevel != null &&
|
|
|
|
_.include(
|
|
|
|
[
|
|
|
|
PublicAccessLevels.READ_ONLY,
|
|
|
|
PublicAccessLevels.READ_AND_WRITE,
|
|
|
|
PublicAccessLevels.PRIVATE,
|
2021-04-27 03:52:58 -04:00
|
|
|
PublicAccessLevels.TOKEN_BASED,
|
2019-09-09 07:52:25 -04:00
|
|
|
],
|
|
|
|
newAccessLevel
|
|
|
|
)
|
|
|
|
) {
|
2020-11-03 04:19:05 -05:00
|
|
|
await Project.updateOne(
|
2019-09-09 07:52:25 -04:00
|
|
|
{ _id: projectId },
|
|
|
|
{ publicAccesLevel: newAccessLevel }
|
|
|
|
).exec()
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-06-24 09:18:14 -04:00
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
async function ensureTokensArePresent(projectId) {
|
|
|
|
const project = await ProjectGetter.promises.getProject(projectId, {
|
2021-04-27 03:52:58 -04:00
|
|
|
tokens: 1,
|
2019-09-09 07:52:25 -04:00
|
|
|
})
|
|
|
|
if (
|
|
|
|
project.tokens != null &&
|
|
|
|
project.tokens.readOnly != null &&
|
|
|
|
project.tokens.readAndWrite != null
|
|
|
|
) {
|
|
|
|
return project.tokens
|
|
|
|
}
|
|
|
|
await _generateTokens(project)
|
2020-11-03 04:19:05 -05:00
|
|
|
await Project.updateOne(
|
2019-09-09 07:52:25 -04:00
|
|
|
{ _id: projectId },
|
|
|
|
{ $set: { tokens: project.tokens } }
|
|
|
|
).exec()
|
|
|
|
return project.tokens
|
2019-06-24 09:18:14 -04:00
|
|
|
}
|
|
|
|
|
2019-10-14 09:22:45 -04:00
|
|
|
async function clearTokens(projectId) {
|
2020-11-03 04:19:05 -05:00
|
|
|
await Project.updateOne(
|
2019-10-14 09:22:45 -04:00
|
|
|
{ _id: projectId },
|
|
|
|
{ $unset: { tokens: 1 }, $set: { publicAccesLevel: 'private' } }
|
|
|
|
).exec()
|
|
|
|
}
|
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
async function _generateTokens(project, callback) {
|
|
|
|
if (!project.tokens) {
|
|
|
|
project.tokens = {}
|
|
|
|
}
|
|
|
|
const { tokens } = project
|
|
|
|
if (tokens.readAndWrite == null) {
|
2021-03-31 05:28:19 -04:00
|
|
|
const { token, numericPrefix } = TokenGenerator.readAndWriteToken()
|
2019-09-09 07:52:25 -04:00
|
|
|
tokens.readAndWrite = token
|
|
|
|
tokens.readAndWritePrefix = numericPrefix
|
|
|
|
}
|
|
|
|
if (tokens.readOnly == null) {
|
2022-01-10 05:23:05 -05:00
|
|
|
tokens.readOnly =
|
|
|
|
await TokenGenerator.promises.generateUniqueReadOnlyToken()
|
2019-09-09 07:52:25 -04:00
|
|
|
}
|
|
|
|
}
|