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')
|
|
|
|
const logger = require('logger-sharelatex')
|
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')
|
|
|
|
const ProjectTokenGenerator = require('./ProjectTokenGenerator')
|
|
|
|
const ProjectHelper = require('./ProjectHelper')
|
|
|
|
const settings = require('settings-sharelatex')
|
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,
|
|
|
|
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,
|
|
|
|
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
|
|
|
|
: settings.defaultFeatures
|
|
|
|
}
|
|
|
|
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, {
|
|
|
|
description: true
|
|
|
|
})
|
|
|
|
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-02 05:53:57 -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) {
|
|
|
|
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-02 05:53:57 -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,
|
|
|
|
newProjectName: newName
|
|
|
|
})
|
|
|
|
}
|
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 = []) {
|
|
|
|
const allUsersProjectNames = await ProjectGetter.promises.findAllUsersProjects(
|
|
|
|
userId,
|
|
|
|
{ name: 1 }
|
|
|
|
)
|
|
|
|
// 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,
|
|
|
|
PublicAccessLevels.TOKEN_BASED
|
|
|
|
],
|
|
|
|
newAccessLevel
|
|
|
|
)
|
|
|
|
) {
|
2020-11-02 05:53:57 -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, {
|
|
|
|
tokens: 1
|
|
|
|
})
|
|
|
|
if (
|
|
|
|
project.tokens != null &&
|
|
|
|
project.tokens.readOnly != null &&
|
|
|
|
project.tokens.readAndWrite != null
|
|
|
|
) {
|
|
|
|
return project.tokens
|
|
|
|
}
|
|
|
|
await _generateTokens(project)
|
2020-11-02 05:53:57 -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-02 05:53:57 -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) {
|
|
|
|
const { token, numericPrefix } = ProjectTokenGenerator.readAndWriteToken()
|
|
|
|
tokens.readAndWrite = token
|
|
|
|
tokens.readAndWritePrefix = numericPrefix
|
|
|
|
}
|
|
|
|
if (tokens.readOnly == null) {
|
|
|
|
tokens.readOnly = await ProjectTokenGenerator.promises.generateUniqueReadOnlyToken()
|
|
|
|
}
|
|
|
|
}
|