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.
This commit is contained in:
Shane Kilkelly 2017-09-20 15:26:03 +01:00
parent ceb7c509d0
commit 574baf386e
4 changed files with 63 additions and 17 deletions

View file

@ -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) ->

View file

@ -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
[

View file

@ -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

View file

@ -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", ->