From 574baf386ed40b04c3ffcf774e97d17927d74081 Mon Sep 17 00:00:00 2001 From: Shane Kilkelly Date: Wed, 20 Sep 2017 15:26:03 +0100 Subject: [PATCH] Alter `getProjectsUserIsMemberOf` to include token-access projects. Also change the api to produce an object with the different project lists attached, rather than a pair of lists. --- .../Collaborators/CollaboratorsHandler.coffee | 37 +++++++++++++++---- .../Features/Project/ProjectGetter.coffee | 8 ++-- .../CollaboratorsHandlerTests.coffee | 28 ++++++++++++-- .../coffee/Project/ProjectGetterTests.coffee | 7 +++- 4 files changed, 63 insertions(+), 17 deletions(-) diff --git a/services/web/app/coffee/Features/Collaborators/CollaboratorsHandler.coffee b/services/web/app/coffee/Features/Collaborators/CollaboratorsHandler.coffee index f7db3953eb..3624f2a51c 100644 --- a/services/web/app/coffee/Features/Collaborators/CollaboratorsHandler.coffee +++ b/services/web/app/coffee/Features/Collaborators/CollaboratorsHandler.coffee @@ -107,12 +107,29 @@ module.exports = CollaboratorsHandler = return callback null, true, member.privilegeLevel return callback null, false, null - getProjectsUserIsMemberOf: (user_id, fields, callback = (error, readAndWriteProjects, readOnlyProjects) ->) -> - Project.find {collaberator_refs:user_id}, fields, (err, readAndWriteProjects)=> - return callback(err) if err? - Project.find {readOnly_refs:user_id}, fields, (err, readOnlyProjects)=> - return callback(err) if err? - callback(null, readAndWriteProjects, readOnlyProjects) + getProjectsUserIsMemberOf: (user_id, fields, callback = (error, {readAndWrite:[],readOnly:[],tokenReadAndWrite:[],tokenReadOnly:[]}) ->) -> + async.mapLimit( + [ + 'collaberator_refs', + 'readOnly_refs', + 'tokenAccessReadAndWrite_refs', + 'tokenAccessReadOnly_refs' + ] + , 2 + , (key, cb) -> + query = {} + query[key] = user_id + Project.find query, fields, cb + , (error, results) -> + return callback(error) if error? + projects = { + readAndWrite: results[0] + readOnly: results[1] + tokenReadAndWrite: results[2] + tokenReadOnly: results[3] + } + callback(null, projects) + ) removeUserFromProject: (project_id, user_id, callback = (error) ->)-> logger.log user_id: user_id, project_id: project_id, "removing user" @@ -125,9 +142,13 @@ module.exports = CollaboratorsHandler = callback(err) removeUserFromAllProjets: (user_id, callback = (error) ->) -> - CollaboratorsHandler.getProjectsUserIsMemberOf user_id, { _id: 1 }, (error, readAndWriteProjects = [], readOnlyProjects = []) -> + CollaboratorsHandler.getProjectsUserIsMemberOf user_id, { _id: 1 }, (error, {readAndWrite, readOnly, tokenReadAndWrite, tokenReadOnly}) -> return callback(error) if error? - allProjects = readAndWriteProjects.concat(readOnlyProjects) + allProjects = + readAndWrite + .concat(readOnly) + .concat(tokenReadAndWrite) + .concat(tokenReadOnly) jobs = [] for project in allProjects do (project) -> diff --git a/services/web/app/coffee/Features/Project/ProjectGetter.coffee b/services/web/app/coffee/Features/Project/ProjectGetter.coffee index afa1d0be02..4e354495a5 100644 --- a/services/web/app/coffee/Features/Project/ProjectGetter.coffee +++ b/services/web/app/coffee/Features/Project/ProjectGetter.coffee @@ -50,14 +50,14 @@ module.exports = ProjectGetter = return callback(err) callback(null, project?[0]) - findAllUsersProjects: (user_id, fields, callback = (error, ownedProjects, readAndWriteProjects, readOnlyProjects) ->) -> CollaboratorsHandler = require "../Collaborators/CollaboratorsHandler" - Project.find {owner_ref: user_id}, fields, (error, projects) -> + Project.find {owner_ref: user_id}, fields, (error, ownedProjects) -> return callback(error) if error? - CollaboratorsHandler.getProjectsUserIsMemberOf user_id, fields, (error, readAndWriteProjects, readOnlyProjects) -> + CollaboratorsHandler.getProjectsUserIsMemberOf user_id, fields, (error, projects) -> return callback(error) if error? - callback null, projects, readAndWriteProjects, readOnlyProjects + # TODO: Add token projects too + callback null, ownedProjects, projects.readAndWrite, projects.readOnly [ diff --git a/services/web/test/UnitTests/coffee/Collaborators/CollaboratorsHandlerTests.coffee b/services/web/test/UnitTests/coffee/Collaborators/CollaboratorsHandlerTests.coffee index 529558554f..2bdc4deb33 100644 --- a/services/web/test/UnitTests/coffee/Collaborators/CollaboratorsHandlerTests.coffee +++ b/services/web/test/UnitTests/coffee/Collaborators/CollaboratorsHandlerTests.coffee @@ -192,11 +192,21 @@ describe "CollaboratorsHandler", -> @Project.find = sinon.stub() @Project.find.withArgs({collaberator_refs:@user_id}, @fields).yields(null, ["mock-read-write-project-1", "mock-read-write-project-2"]) @Project.find.withArgs({readOnly_refs:@user_id}, @fields).yields(null, ["mock-read-only-project-1", "mock-read-only-project-2"]) + @Project.find.withArgs({tokenAccessReadAndWrite_refs:@user_id}, @fields).yields(null, ["mock-token-read-write-project-1", "mock-token-read-write-project-2"]) + @Project.find.withArgs({tokenAccessReadOnly_refs:@user_id}, @fields).yields(null, ["mock-token-read-only-project-1", "mock-token-read-only-project-2"]) @CollaboratorHandler.getProjectsUserIsMemberOf @user_id, @fields, @callback it "should call the callback with the projects", -> @callback - .calledWith(null, ["mock-read-write-project-1", "mock-read-write-project-2"], ["mock-read-only-project-1", "mock-read-only-project-2"]) + .calledWith( + null, + { + readAndWrite: ["mock-read-write-project-1", "mock-read-write-project-2"], + readOnly: ["mock-read-only-project-1", "mock-read-only-project-2"], + tokenReadAndWrite: ["mock-token-read-write-project-1", "mock-token-read-write-project-2"], + tokenReadOnly: ["mock-token-read-only-project-1", "mock-token-read-only-project-2"] + } + ) .should.equal true describe "removeUserFromProject", -> @@ -282,14 +292,24 @@ describe "CollaboratorsHandler", -> @CollaboratorHandler.getProjectsUserIsMemberOf = sinon.stub() @CollaboratorHandler.getProjectsUserIsMemberOf.withArgs(@user_id, { _id: 1 }).yields( null, - [ { _id: "read-and-write-0" }, { _id: "read-and-write-1" }, null ], - [ { _id: "read-only-0" }, { _id: "read-only-1" }, null ] + { + readAndWrite: [ { _id: "read-and-write-0" }, { _id: "read-and-write-1" }, null ], + readOnly: [ { _id: "read-only-0" }, { _id: "read-only-1" }, null ] + tokenReadAndWrite: [ { _id: "token-read-and-write-0" }, { _id: "token-read-and-write-1" }, null ] + tokenReadOnly: [ { _id: "token-read-only-0" }, { _id: "token-read-only-1" }, null ] + } ) @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"] + expectedProjects = [ + "read-and-write-0", "read-and-write-1", + "read-only-0", "read-only-1", + "token-read-and-write-0", "token-read-and-write-1", + "token-read-only-0", "token-read-only-1", + ] + for project_id in expectedProjects @CollaboratorHandler.removeUserFromProject .calledWith(project_id, @user_id) .should.equal true diff --git a/services/web/test/UnitTests/coffee/Project/ProjectGetterTests.coffee b/services/web/test/UnitTests/coffee/Project/ProjectGetterTests.coffee index ef8e7ef1ed..256de181a9 100644 --- a/services/web/test/UnitTests/coffee/Project/ProjectGetterTests.coffee +++ b/services/web/test/UnitTests/coffee/Project/ProjectGetterTests.coffee @@ -149,7 +149,12 @@ describe "ProjectGetter", -> @Project.find = sinon.stub() @Project.find.withArgs({owner_ref: @user_id}, @fields).yields(null, ["mock-owned-projects"]) @CollaboratorsHandler.getProjectsUserIsMemberOf = sinon.stub() - @CollaboratorsHandler.getProjectsUserIsMemberOf.withArgs(@user_id, @fields).yields(null, ["mock-rw-projects"], ["mock-ro-projects"]) + @CollaboratorsHandler.getProjectsUserIsMemberOf.withArgs(@user_id, @fields).yields( + null, + { + readAndWrite: ["mock-rw-projects"], readOnly: ["mock-ro-projects"] + } + ) @ProjectGetter.findAllUsersProjects @user_id, @fields, @callback it "should call the callback with all the projects", ->