2014-02-12 10:23:40 +00:00
|
|
|
ProjectGetter = require("./ProjectGetter")
|
2014-03-28 12:47:15 +00:00
|
|
|
UserGetter = require("../User/UserGetter")
|
2014-02-12 10:23:40 +00:00
|
|
|
Project = require('../../models/Project').Project
|
|
|
|
logger = require("logger-sharelatex")
|
2014-04-07 14:37:40 +00:00
|
|
|
tpdsUpdateSender = require '../ThirdPartyDataStore/TpdsUpdateSender'
|
2014-04-07 15:07:44 +00:00
|
|
|
_ = require("underscore")
|
2016-03-15 14:35:01 +00:00
|
|
|
PublicAccessLevels = require("../Authorization/PublicAccessLevels")
|
2017-03-28 09:12:52 +00:00
|
|
|
Errors = require("../Errors/Errors")
|
2017-10-04 15:31:24 +00:00
|
|
|
ProjectTokenGenerator = require('./ProjectTokenGenerator')
|
2014-02-12 10:23:40 +00:00
|
|
|
|
2017-05-19 15:21:02 +00:00
|
|
|
module.exports = ProjectDetailsHandler =
|
2014-02-12 10:23:40 +00:00
|
|
|
getDetails: (project_id, callback)->
|
2017-09-26 09:19:30 +00:00
|
|
|
ProjectGetter.getProject project_id, {name:true, description:true, compiler:true, features:true, owner_ref:true, overleaf:true}, (err, project)->
|
2017-03-28 09:12:52 +00:00
|
|
|
if err?
|
2014-02-12 10:23:40 +00:00
|
|
|
logger.err err:err, project_id:project_id, "error getting project"
|
|
|
|
return callback(err)
|
2017-03-28 09:12:52 +00:00
|
|
|
return callback(new Errors.NotFoundError("project not found")) if !project?
|
2014-03-28 12:47:15 +00:00
|
|
|
UserGetter.getUser project.owner_ref, (err, user) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
details =
|
|
|
|
name : project.name
|
|
|
|
description: project.description
|
|
|
|
compiler: project.compiler
|
|
|
|
features: user.features
|
2017-09-26 09:19:30 +00:00
|
|
|
|
|
|
|
if project.overleaf?
|
|
|
|
details.overleaf = project.overleaf
|
|
|
|
|
|
|
|
logger.log project_id:project_id, details: details, "getting project details"
|
2014-03-28 12:47:15 +00:00
|
|
|
callback(err, details)
|
2014-02-12 10:23:40 +00:00
|
|
|
|
2014-07-16 10:27:47 +00:00
|
|
|
getProjectDescription: (project_id, callback)->
|
2018-02-15 12:18:43 +00:00
|
|
|
ProjectGetter.getProject project_id, description: true, (err, project)->
|
2014-07-16 10:56:22 +00:00
|
|
|
callback(err, project?.description)
|
2014-07-16 10:27:47 +00:00
|
|
|
|
2014-02-12 10:23:40 +00:00
|
|
|
setProjectDescription: (project_id, description, callback)->
|
|
|
|
conditions = _id:project_id
|
|
|
|
update = description:description
|
|
|
|
logger.log conditions:conditions, update:update, project_id:project_id, description:description, "setting project description"
|
|
|
|
Project.update conditions, update, (err)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, "something went wrong setting project description"
|
|
|
|
callback(err)
|
2014-04-07 14:37:40 +00:00
|
|
|
|
|
|
|
renameProject: (project_id, newName, callback = ->)->
|
2017-05-19 15:21:02 +00:00
|
|
|
ProjectDetailsHandler.validateProjectName newName, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
logger.log project_id: project_id, newName:newName, "renaming project"
|
|
|
|
ProjectGetter.getProject project_id, {name:true}, (err, project)->
|
|
|
|
if err? or !project?
|
|
|
|
logger.err err:err, project_id:project_id, "error getting project or could not find it todo project rename"
|
2014-04-07 14:37:40 +00:00
|
|
|
return callback(err)
|
2017-05-19 15:21:02 +00:00
|
|
|
oldProjectName = project.name
|
|
|
|
Project.update _id:project_id, {name: newName}, (err, project)=>
|
|
|
|
if err?
|
|
|
|
return callback(err)
|
|
|
|
tpdsUpdateSender.moveEntity {project_id:project_id, project_name:oldProjectName, newProjectName:newName}, callback
|
|
|
|
|
|
|
|
MAX_PROJECT_NAME_LENGTH: 150
|
|
|
|
validateProjectName: (name, callback = (error) ->) ->
|
2017-10-12 15:03:12 +00:00
|
|
|
if !name? or name.length == 0
|
2017-05-19 15:21:02 +00:00
|
|
|
return callback(new Errors.InvalidNameError("Project name cannot be blank"))
|
|
|
|
else if name.length > @MAX_PROJECT_NAME_LENGTH
|
|
|
|
return callback(new Errors.InvalidNameError("Project name is too long"))
|
|
|
|
else if name.indexOf("/") > -1
|
|
|
|
return callback(new Errors.InvalidNameError("Project name cannot not contain / characters"))
|
|
|
|
else
|
|
|
|
return callback()
|
2014-04-07 15:07:44 +00:00
|
|
|
|
|
|
|
setPublicAccessLevel : (project_id, newAccessLevel, callback = ->)->
|
|
|
|
logger.log project_id: project_id, level: newAccessLevel, "set public access level"
|
2017-10-12 10:00:39 +00:00
|
|
|
# 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
|
2017-10-04 15:31:24 +00:00
|
|
|
if project_id? && newAccessLevel? and _.include [
|
|
|
|
PublicAccessLevels.READ_ONLY,
|
|
|
|
PublicAccessLevels.READ_AND_WRITE,
|
|
|
|
PublicAccessLevels.PRIVATE,
|
|
|
|
PublicAccessLevels.TOKEN_BASED
|
|
|
|
], newAccessLevel
|
2014-04-07 15:07:44 +00:00
|
|
|
Project.update {_id:project_id},{publicAccesLevel:newAccessLevel}, (err)->
|
2017-10-26 15:39:24 +00:00
|
|
|
callback(err)
|
2017-10-04 15:31:24 +00:00
|
|
|
|
|
|
|
ensureTokensArePresent: (project_id, callback=(err, tokens)->) ->
|
|
|
|
ProjectGetter.getProject project_id, {tokens: 1}, (err, project) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
if project.tokens? and project.tokens.readOnly? and project.tokens.readAndWrite?
|
2017-10-12 10:25:16 +00:00
|
|
|
logger.log {project_id}, "project already has tokens"
|
2017-10-04 15:31:24 +00:00
|
|
|
return callback(null, project.tokens)
|
|
|
|
else
|
2017-10-12 10:25:16 +00:00
|
|
|
logger.log {
|
|
|
|
project_id,
|
|
|
|
has_tokens: project.tokens?,
|
|
|
|
has_readOnly: project?.tokens?.readOnly?,
|
|
|
|
has_readAndWrite: project?.tokens?.readAndWrite?
|
|
|
|
}, "generating tokens for project"
|
2017-10-12 10:19:26 +00:00
|
|
|
tokens = project.tokens || {}
|
|
|
|
if !tokens.readOnly?
|
|
|
|
tokens.readOnly = ProjectTokenGenerator.readOnlyToken()
|
|
|
|
if !tokens.readAndWrite?
|
|
|
|
tokens.readAndWrite = ProjectTokenGenerator.readAndWriteToken()
|
2017-10-04 15:31:24 +00:00
|
|
|
Project.update {_id: project_id}, {$set: {tokens: tokens}}, (err) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
callback(null, tokens)
|