2014-06-05 11:18:25 -04:00
|
|
|
ProjectEntityHandler = require "../Project/ProjectEntityHandler"
|
2014-11-07 07:31:47 -05:00
|
|
|
ProjectDeleter = require "../Project/ProjectDeleter"
|
2014-06-05 11:18:25 -04:00
|
|
|
logger = require "logger-sharelatex"
|
|
|
|
EditorRealTimeController = require "./EditorRealTimeController"
|
2014-06-22 07:10:42 -04:00
|
|
|
EditorController = require "./EditorController"
|
2015-02-05 11:37:37 -05:00
|
|
|
ProjectGetter = require('../Project/ProjectGetter')
|
|
|
|
UserGetter = require('../User/UserGetter')
|
2016-03-10 12:17:26 -05:00
|
|
|
AuthorizationManager = require("../Authorization/AuthorizationManager")
|
2015-02-05 11:37:37 -05:00
|
|
|
ProjectEditorHandler = require('../Project/ProjectEditorHandler')
|
2017-04-03 11:18:30 -04:00
|
|
|
Metrics = require('metrics-sharelatex')
|
2016-03-07 10:25:10 -05:00
|
|
|
CollaboratorsHandler = require("../Collaborators/CollaboratorsHandler")
|
2016-08-01 12:05:37 -04:00
|
|
|
CollaboratorsInviteHandler = require("../Collaborators/CollaboratorsInviteHandler")
|
2016-03-15 10:39:27 -04:00
|
|
|
PrivilegeLevels = require "../Authorization/PrivilegeLevels"
|
2017-10-13 06:20:57 -04:00
|
|
|
TokenAccessHandler = require '../TokenAccess/TokenAccessHandler'
|
2017-11-01 14:21:05 -04:00
|
|
|
AuthenticationController = require "../Authentication/AuthenticationController"
|
2014-06-05 11:18:25 -04:00
|
|
|
|
|
|
|
module.exports = EditorHttpController =
|
2014-11-07 07:31:47 -05:00
|
|
|
joinProject: (req, res, next) ->
|
|
|
|
project_id = req.params.Project_id
|
|
|
|
user_id = req.query.user_id
|
2016-03-22 05:53:47 -04:00
|
|
|
if user_id == "anonymous-user"
|
|
|
|
user_id = null
|
2014-11-07 07:31:47 -05:00
|
|
|
logger.log {user_id, project_id}, "join project request"
|
|
|
|
Metrics.inc "editor.join-project"
|
2017-09-27 09:01:52 -04:00
|
|
|
EditorHttpController._buildJoinProjectView req, project_id, user_id, (error, project, privilegeLevel) ->
|
2014-11-07 07:31:47 -05:00
|
|
|
return next(error) if error?
|
2017-10-05 08:18:30 -04:00
|
|
|
# Hide access tokens if this is not the project owner
|
2017-10-19 11:26:01 -04:00
|
|
|
TokenAccessHandler.protectTokens(project, privilegeLevel)
|
2014-11-07 07:31:47 -05:00
|
|
|
res.json {
|
|
|
|
project: project
|
|
|
|
privilegeLevel: privilegeLevel
|
|
|
|
}
|
|
|
|
# Only show the 'renamed or deleted' message once
|
2014-12-12 05:27:14 -05:00
|
|
|
if project?.deletedByExternalDataSource
|
2014-11-07 07:31:47 -05:00
|
|
|
ProjectDeleter.unmarkAsDeletedByExternalSource project_id
|
|
|
|
|
2017-09-27 09:01:52 -04:00
|
|
|
_buildJoinProjectView: (req, project_id, user_id, callback = (error, project, privilegeLevel) ->) ->
|
2016-08-16 06:17:45 -04:00
|
|
|
logger.log {project_id, user_id}, "building the joinProject view"
|
2015-02-05 11:37:37 -05:00
|
|
|
ProjectGetter.getProjectWithoutDocLines project_id, (error, project) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
return callback(new Error("not found")) if !project?
|
2017-09-20 05:02:43 -04:00
|
|
|
CollaboratorsHandler.getInvitedMembersWithPrivilegeLevels project, (error, members) ->
|
2015-02-05 11:37:37 -05:00
|
|
|
return callback(error) if error?
|
2017-10-25 06:29:05 -04:00
|
|
|
UserGetter.getUser user_id, { isAdmin: true }, (error, user) ->
|
2015-02-05 11:37:37 -05:00
|
|
|
return callback(error) if error?
|
2017-10-25 06:29:05 -04:00
|
|
|
token = TokenAccessHandler.getRequestToken(req, project_id)
|
|
|
|
AuthorizationManager.getPrivilegeLevelForProject user_id, project_id, token, (error, privilegeLevel) ->
|
2015-02-05 11:37:37 -05:00
|
|
|
return callback(error) if error?
|
2017-10-25 06:29:05 -04:00
|
|
|
if !privilegeLevel? or privilegeLevel == PrivilegeLevels.NONE
|
|
|
|
logger.log {project_id, user_id, privilegeLevel}, "not an acceptable privilege level, returning null"
|
|
|
|
return callback null, null, false
|
|
|
|
CollaboratorsInviteHandler.getAllInvites project_id, (error, invites) ->
|
2016-08-01 12:05:37 -04:00
|
|
|
return callback(error) if error?
|
2017-10-25 06:29:05 -04:00
|
|
|
logger.log {project_id, user_id, memberCount: members.length, inviteCount: invites.length, privilegeLevel}, "returning project model view"
|
|
|
|
callback(null,
|
|
|
|
ProjectEditorHandler.buildProjectModelView(project, members, invites),
|
|
|
|
privilegeLevel
|
|
|
|
)
|
2015-02-05 11:37:37 -05:00
|
|
|
|
2014-06-05 11:18:25 -04:00
|
|
|
restoreDoc: (req, res, next) ->
|
|
|
|
project_id = req.params.Project_id
|
|
|
|
doc_id = req.params.doc_id
|
|
|
|
name = req.body.name
|
|
|
|
|
|
|
|
if !name?
|
2015-07-08 11:56:38 -04:00
|
|
|
return res.sendStatus 400 # Malformed request
|
2014-06-05 11:18:25 -04:00
|
|
|
|
|
|
|
logger.log project_id: project_id, doc_id: doc_id, "restoring doc"
|
|
|
|
ProjectEntityHandler.restoreDoc project_id, doc_id, name, (err, doc, folder_id) =>
|
|
|
|
return next(error) if error?
|
|
|
|
EditorRealTimeController.emitToRoom(project_id, 'reciveNewDoc', folder_id, doc)
|
|
|
|
res.json {
|
|
|
|
doc_id: doc._id
|
|
|
|
}
|
|
|
|
|
2015-03-02 07:46:02 -05:00
|
|
|
_nameIsAcceptableLength: (name)->
|
|
|
|
return name? and name.length < 150 and name.length != 0
|
|
|
|
|
2014-06-22 09:39:38 -04:00
|
|
|
|
2014-06-22 07:10:42 -04:00
|
|
|
addDoc: (req, res, next) ->
|
|
|
|
project_id = req.params.Project_id
|
|
|
|
name = req.body.name
|
|
|
|
parent_folder_id = req.body.parent_folder_id
|
2017-11-23 10:40:14 -05:00
|
|
|
user_id = AuthenticationController.getLoggedInUserId(req)
|
2016-02-29 14:01:46 -05:00
|
|
|
logger.log project_id:project_id, name:name, parent_folder_id:parent_folder_id, "getting request to add doc to project"
|
2015-03-02 07:46:02 -05:00
|
|
|
if !EditorHttpController._nameIsAcceptableLength(name)
|
2015-07-08 11:56:38 -04:00
|
|
|
return res.sendStatus 400
|
2017-11-23 10:40:14 -05:00
|
|
|
EditorController.addDoc project_id, parent_folder_id, name, [], "editor", user_id, (error, doc) ->
|
2016-02-29 08:05:17 -05:00
|
|
|
if error == "project_has_to_many_files"
|
|
|
|
res.status(400).json(req.i18n.translate("project_has_to_many_files"))
|
|
|
|
else if error?
|
|
|
|
next(error)
|
|
|
|
else
|
|
|
|
res.json doc
|
2014-06-22 09:39:38 -04:00
|
|
|
|
|
|
|
addFolder: (req, res, next) ->
|
|
|
|
project_id = req.params.Project_id
|
|
|
|
name = req.body.name
|
|
|
|
parent_folder_id = req.body.parent_folder_id
|
2015-03-02 07:46:02 -05:00
|
|
|
if !EditorHttpController._nameIsAcceptableLength(name)
|
2015-07-08 11:56:38 -04:00
|
|
|
return res.sendStatus 400
|
2014-10-16 06:26:57 -04:00
|
|
|
EditorController.addFolder project_id, parent_folder_id, name, "editor", (error, doc) ->
|
2016-02-29 08:05:17 -05:00
|
|
|
if error == "project_has_to_many_files"
|
|
|
|
res.status(400).json(req.i18n.translate("project_has_to_many_files"))
|
2017-03-31 05:46:13 -04:00
|
|
|
else if error?.message == 'invalid element name'
|
2017-03-31 05:31:03 -04:00
|
|
|
res.status(400).json(req.i18n.translate('invalid_file_name'))
|
2016-02-29 08:05:17 -05:00
|
|
|
else if error?
|
2017-03-31 05:31:03 -04:00
|
|
|
res.status(500).json(req.i18n.translate('generic_something_went_wrong'))
|
2016-02-29 08:05:17 -05:00
|
|
|
else
|
|
|
|
res.json doc
|
2014-06-22 15:08:56 -04:00
|
|
|
|
|
|
|
renameEntity: (req, res, next) ->
|
|
|
|
project_id = req.params.Project_id
|
|
|
|
entity_id = req.params.entity_id
|
|
|
|
entity_type = req.params.entity_type
|
|
|
|
name = req.body.name
|
2015-03-02 07:46:02 -05:00
|
|
|
if !EditorHttpController._nameIsAcceptableLength(name)
|
2015-07-08 11:56:38 -04:00
|
|
|
return res.sendStatus 400
|
2017-11-23 10:40:14 -05:00
|
|
|
user_id = AuthenticationController.getLoggedInUserId(req)
|
2017-11-01 14:21:05 -04:00
|
|
|
EditorController.renameEntity project_id, entity_id, entity_type, name, user_id, (error) ->
|
2014-06-22 15:08:56 -04:00
|
|
|
return next(error) if error?
|
2015-07-08 11:56:38 -04:00
|
|
|
res.sendStatus 204
|
2014-06-22 15:08:56 -04:00
|
|
|
|
|
|
|
moveEntity: (req, res, next) ->
|
|
|
|
project_id = req.params.Project_id
|
|
|
|
entity_id = req.params.entity_id
|
|
|
|
entity_type = req.params.entity_type
|
|
|
|
folder_id = req.body.folder_id
|
2017-11-02 05:44:23 -04:00
|
|
|
user_id = AuthenticationController.getLoggedInUserId(req)
|
|
|
|
EditorController.moveEntity project_id, entity_id, folder_id, entity_type, user_id, (error) ->
|
2014-06-22 15:08:56 -04:00
|
|
|
return next(error) if error?
|
2015-07-08 11:56:38 -04:00
|
|
|
res.sendStatus 204
|
2014-06-22 15:08:56 -04:00
|
|
|
|
2014-10-24 07:01:35 -04:00
|
|
|
deleteDoc: (req, res, next)->
|
|
|
|
req.params.entity_type = "doc"
|
|
|
|
EditorHttpController.deleteEntity(req, res, next)
|
|
|
|
|
|
|
|
deleteFile: (req, res, next)->
|
|
|
|
req.params.entity_type = "file"
|
|
|
|
EditorHttpController.deleteEntity(req, res, next)
|
|
|
|
|
|
|
|
deleteFolder: (req, res, next)->
|
|
|
|
req.params.entity_type = "folder"
|
|
|
|
EditorHttpController.deleteEntity(req, res, next)
|
|
|
|
|
2014-06-22 15:08:56 -04:00
|
|
|
deleteEntity: (req, res, next) ->
|
|
|
|
project_id = req.params.Project_id
|
|
|
|
entity_id = req.params.entity_id
|
|
|
|
entity_type = req.params.entity_type
|
2017-12-15 05:53:38 -05:00
|
|
|
user_id = AuthenticationController.getLoggedInUserId(req)
|
|
|
|
EditorController.deleteEntity project_id, entity_id, entity_type, "editor", user_id, (error) ->
|
2014-06-22 15:08:56 -04:00
|
|
|
return next(error) if error?
|
2015-07-08 11:56:38 -04:00
|
|
|
res.sendStatus 204
|