From b76cc7e31462999f8c3071beb4136eb9fee7d7b2 Mon Sep 17 00:00:00 2001 From: James Allen Date: Thu, 30 Jun 2016 14:21:44 +0100 Subject: [PATCH] Remove a user from being a collaborator when their account is deleted --- .../Collaborators/CollaboratorsHandler.coffee | 11 +++++++++++ .../Features/Project/ProjectDeleter.coffee | 8 +++++--- .../CollaboratorsHandlerTests.coffee | 19 ++++++++++++++++--- .../coffee/Project/ProjectDeleterTests.coffee | 10 +++++++++- 4 files changed, 41 insertions(+), 7 deletions(-) diff --git a/services/web/app/coffee/Features/Collaborators/CollaboratorsHandler.coffee b/services/web/app/coffee/Features/Collaborators/CollaboratorsHandler.coffee index b8e0f3e606..5f2580497d 100644 --- a/services/web/app/coffee/Features/Collaborators/CollaboratorsHandler.coffee +++ b/services/web/app/coffee/Features/Collaborators/CollaboratorsHandler.coffee @@ -87,6 +87,17 @@ module.exports = CollaboratorsHandler = logger.error err: err, "problem removing user from project collaberators" callback(err) + removeUserFromAllProjets: (user_id, callback = (error) ->) -> + CollaboratorsHandler.getProjectsUserIsCollaboratorOf user_id, { _id: 1 }, (error, readAndWriteProjects = [], readOnlyProjects = []) -> + return callback(error) if error? + allProjects = readAndWriteProjects.concat(readOnlyProjects) + jobs = [] + for project in allProjects + do (project) -> + jobs.push (cb) -> + CollaboratorsHandler.removeUserFromProject project._id, user_id, cb + async.series jobs, callback + addEmailToProject: (project_id, adding_user_id, unparsed_email, privilegeLevel, callback = (error, user) ->) -> emails = mimelib.parseAddresses(unparsed_email) email = emails[0]?.address?.toLowerCase() diff --git a/services/web/app/coffee/Features/Project/ProjectDeleter.coffee b/services/web/app/coffee/Features/Project/ProjectDeleter.coffee index 8ba8a65845..f398ea75b5 100644 --- a/services/web/app/coffee/Features/Project/ProjectDeleter.coffee +++ b/services/web/app/coffee/Features/Project/ProjectDeleter.coffee @@ -24,9 +24,11 @@ module.exports = ProjectDeleter = update = {deletedByExternalDataSource: false} Project.update conditions, update, {}, callback - deleteUsersProjects: (owner_id, callback)-> - logger.log owner_id:owner_id, "deleting users projects" - Project.remove owner_ref:owner_id, callback + deleteUsersProjects: (user_id, callback)-> + logger.log {user_id}, "deleting users projects" + Project.remove owner_ref:user_id, (error) -> + return callback(error) if error? + CollaboratorsHandler.removeUserFromAllProjets user_id, callback deleteProject: (project_id, callback = (error) ->) -> # archiveProject takes care of the clean-up diff --git a/services/web/test/UnitTests/coffee/Collaborators/CollaboratorsHandlerTests.coffee b/services/web/test/UnitTests/coffee/Collaborators/CollaboratorsHandlerTests.coffee index bb7e370044..645c39076c 100644 --- a/services/web/test/UnitTests/coffee/Collaborators/CollaboratorsHandlerTests.coffee +++ b/services/web/test/UnitTests/coffee/Collaborators/CollaboratorsHandlerTests.coffee @@ -276,6 +276,19 @@ describe "CollaboratorsHandler", -> it "should not add any users to the proejct", -> @CollaboratorHandler.addUserIdToProject.called.should.equal false - - - + describe "removeUserFromAllProjets", -> + beforeEach (done) -> + @CollaboratorHandler.getProjectsUserIsCollaboratorOf = sinon.stub() + @CollaboratorHandler.getProjectsUserIsCollaboratorOf.withArgs(@user_id, { _id: 1 }).yields( + null, + [ { _id: "read-and-write-0" }, { _id: "read-and-write-1" } ], + [ { _id: "read-only-0" }, { _id: "read-only-1" } ] + ) + @CollaboratorHandler.removeUserFromProject = sinon.stub().yields() + @CollaboratorHandler.removeUserFromAllProjets @user_id, done + + it "should remove the user from each project", -> + for project_id in ["read-and-write-0", "read-and-write-1", "read-only-0", "read-only-1"] + @CollaboratorHandler.removeUserFromProject + .calledWith(project_id, @user_id) + .should.equal true \ No newline at end of file diff --git a/services/web/test/UnitTests/coffee/Project/ProjectDeleterTests.coffee b/services/web/test/UnitTests/coffee/Project/ProjectDeleterTests.coffee index 601f7867b0..6a99ed8150 100644 --- a/services/web/test/UnitTests/coffee/Project/ProjectDeleterTests.coffee +++ b/services/web/test/UnitTests/coffee/Project/ProjectDeleterTests.coffee @@ -27,13 +27,15 @@ describe 'ProjectDeleter', -> removeProjectFromAllTags: sinon.stub().callsArgWith(2) @ProjectGetter = getProject:sinon.stub() + @CollaboratorsHandler = + removeUserFromAllProjets: sinon.stub().yields() @deleter = SandboxedModule.require modulePath, requires: "../Editor/EditorController": @editorController '../../models/Project':{Project:@Project} '../DocumentUpdater/DocumentUpdaterHandler': @documentUpdaterHandler "../Tags/TagsHandler":@TagsHandler "../FileStore/FileStoreHandler": @FileStoreHandler = {} - "../Collaborators/CollaboratorsHandler": @CollaboratorsHandler = {} + "../Collaborators/CollaboratorsHandler": @CollaboratorsHandler "./ProjectGetter": @ProjectGetter 'logger-sharelatex': log:-> @@ -74,6 +76,12 @@ describe 'ProjectDeleter', -> @Project.remove.calledWith(owner_ref:user_id).should.equal true done() + it "should remove all the projects the user is a collaborator of", (done)-> + user_id = 1234 + @deleter.deleteUsersProjects user_id, => + @CollaboratorsHandler.removeUserFromAllProjets.calledWith(user_id).should.equal true + done() + describe "deleteProject", -> beforeEach (done) -> @project_id = "mock-project-id-123"