Refactor addUserToProject for better access by groups

This commit is contained in:
James Allen 2015-10-14 17:29:58 +01:00
parent c4e4f2c77a
commit d996ed6e47
8 changed files with 75 additions and 58 deletions

View file

@ -1,11 +1,9 @@
ProjectGetter = require "../Project/ProjectGetter" ProjectGetter = require "../Project/ProjectGetter"
CollaboratorsHandler = require "./CollaboratorsHandler" CollaboratorsHandler = require "./CollaboratorsHandler"
CollaboratorsEmailHandler = require "./CollaboratorsEmailHandler"
ProjectEditorHandler = require "../Project/ProjectEditorHandler" ProjectEditorHandler = require "../Project/ProjectEditorHandler"
EditorRealTimeController = require "../Editor/EditorRealTimeController" EditorRealTimeController = require "../Editor/EditorRealTimeController"
UserGetter = require "../User/UserGetter"
LimitationsManager = require "../Subscription/LimitationsManager" LimitationsManager = require "../Subscription/LimitationsManager"
ContactManager = require "../Contacts/ContactManager" UserGetter = require "../User/UserGetter"
mimelib = require("mimelib") mimelib = require("mimelib")
module.exports = CollaboratorsController = module.exports = CollaboratorsController =
@ -20,10 +18,10 @@ module.exports = CollaboratorsController =
addUserToProject: (req, res, next) -> addUserToProject: (req, res, next) ->
project_id = req.params.Project_id project_id = req.params.Project_id
LimitationsManager.isCollaboratorLimitReached project_id, (error, limit_reached) => LimitationsManager.canAddXCollaborators project_id, 1, (error, allowed) =>
return next(error) if error? return next(error) if error?
if limit_reached if !allowed
return res.json { user: false } return res.json { user: false }
else else
{email, privileges} = req.body {email, privileges} = req.body
@ -32,18 +30,13 @@ module.exports = CollaboratorsController =
if !email? or email == "" if !email? or email == ""
return res.status(400).send("invalid email address") return res.status(400).send("invalid email address")
CollaboratorsHandler.addEmailToProject project_id, email, privileges, (error, user_id) => adding_user_id = req.session?.user?._id
CollaboratorsHandler.addEmailToProject project_id, adding_user_id, email, privileges, (error, user_id) =>
return next(error) if error? return next(error) if error?
UserGetter.getUser user_id, (error, raw_user) -> UserGetter.getUser user_id, (error, raw_user) ->
return next(error) if error? return next(error) if error?
user = ProjectEditorHandler.buildUserModelView(raw_user, privileges) user = ProjectEditorHandler.buildUserModelView(raw_user, privileges)
# These things can all be done in the background
adding_user_id = req.session?.user?._id
CollaboratorsEmailHandler.notifyUserOfProjectShare project_id, user.email
EditorRealTimeController.emitToRoom(project_id, 'userAddedToProject', user, privileges) EditorRealTimeController.emitToRoom(project_id, 'userAddedToProject', user, privileges)
ContactManager.addContact adding_user_id, user_id
return res.json { user: user } return res.json { user: user }
removeUserFromProject: (req, res, next) -> removeUserFromProject: (req, res, next) ->

View file

@ -3,6 +3,9 @@ Project = require("../../models/Project").Project
ProjectEntityHandler = require("../Project/ProjectEntityHandler") ProjectEntityHandler = require("../Project/ProjectEntityHandler")
mimelib = require("mimelib") mimelib = require("mimelib")
logger = require('logger-sharelatex') logger = require('logger-sharelatex')
UserGetter = require "../User/UserGetter"
ContactManager = require "../Contacts/ContactManager"
CollaboratorsEmailHandler = require "./CollaboratorsEmailHandler"
module.exports = CollaboratorsHandler = module.exports = CollaboratorsHandler =
removeUserFromProject: (project_id, user_id, callback = (error) ->)-> removeUserFromProject: (project_id, user_id, callback = (error) ->)->
@ -15,18 +18,18 @@ module.exports = CollaboratorsHandler =
logger.error err: err, "problem removing user from project collaberators" logger.error err: err, "problem removing user from project collaberators"
callback(err) callback(err)
addEmailToProject: (project_id, unparsed_email, privilegeLevel, callback = (error, user) ->) -> addEmailToProject: (project_id, adding_user_id, unparsed_email, privilegeLevel, callback = (error, user) ->) ->
emails = mimelib.parseAddresses(unparsed_email) emails = mimelib.parseAddresses(unparsed_email)
email = emails[0]?.address?.toLowerCase() email = emails[0]?.address?.toLowerCase()
if !email? or email == "" if !email? or email == ""
return callback(new Error("no valid email provided: '#{unparsed_email}'")) return callback(new Error("no valid email provided: '#{unparsed_email}'"))
UserCreator.getUserOrCreateHoldingAccount email, (error, user) -> UserCreator.getUserOrCreateHoldingAccount email, (error, user) ->
return callback(error) if error? return callback(error) if error?
CollaboratorsHandler.addUserToProject project_id, user._id, privilegeLevel, (error) -> CollaboratorsHandler.addUserIdToProject project_id, adding_user_id, user._id, privilegeLevel, (error) ->
return callback(error) if error? return callback(error) if error?
return callback null, user._id return callback null, user._id
addUserToProject: (project_id, user_id, privilegeLevel, callback = (error) ->)-> addUserIdToProject: (project_id, adding_user_id, user_id, privilegeLevel, callback = (error) ->)->
if privilegeLevel == 'readAndWrite' if privilegeLevel == 'readAndWrite'
level = {"collaberator_refs":user_id} level = {"collaberator_refs":user_id}
logger.log {privileges: "readAndWrite", user_id, project_id}, "adding user" logger.log {privileges: "readAndWrite", user_id, project_id}, "adding user"
@ -35,6 +38,14 @@ module.exports = CollaboratorsHandler =
logger.log {privileges: "readOnly", user_id, project_id}, "adding user" logger.log {privileges: "readOnly", user_id, project_id}, "adding user"
else else
return callback(new Error("unknown privilegeLevel: #{privilegeLevel}")) return callback(new Error("unknown privilegeLevel: #{privilegeLevel}"))
# Do these in the background
UserGetter.getUser user_id, {email: 1}, (error, user) ->
if error?
logger.error {err: error, project_id, user_id}, "error getting user while adding to project"
CollaboratorsEmailHandler.notifyUserOfProjectShare project_id, user.email
ContactManager.addContact adding_user_id, user_id
Project.update { _id: project_id }, { $push:level }, (error) -> Project.update { _id: project_id }, { $push:level }, (error) ->
return callback(error) if error? return callback(error) if error?
# Flush to TPDS in background to add files to collaborator's Dropbox # Flush to TPDS in background to add files to collaborator's Dropbox

View file

@ -19,15 +19,15 @@ module.exports =
return callback(error) if error? return callback(error) if error?
callback null, (project.collaberator_refs.length + project.readOnly_refs.length) callback null, (project.collaberator_refs.length + project.readOnly_refs.length)
isCollaboratorLimitReached: (project_id, callback = (error, limit_reached)->) -> canAddXCollaborators: (project_id, x_collaborators, callback = (error, allowed)->) ->
@allowedNumberOfCollaboratorsInProject project_id, (error, allowed_number) => @allowedNumberOfCollaboratorsInProject project_id, (error, allowed_number) =>
return callback(error) if error? return callback(error) if error?
@currentNumberOfCollaboratorsInProject project_id, (error, current_number) => @currentNumberOfCollaboratorsInProject project_id, (error, current_number) =>
return callback(error) if error? return callback(error) if error?
if current_number < allowed_number or allowed_number < 0 if current_number + x_collaborators <= allowed_number or allowed_number < 0
callback null, false
else
callback null, true callback null, true
else
callback null, false
userHasSubscriptionOrIsGroupMember: (user, callback = (err, hasSubscriptionOrIsMember)->) -> userHasSubscriptionOrIsGroupMember: (user, callback = (err, hasSubscriptionOrIsMember)->) ->
@userHasSubscription user, (err, hasSubscription, subscription)=> @userHasSubscription user, (err, hasSubscription, subscription)=>

View file

@ -47,7 +47,6 @@ module.exports = Modules =
hooks: hooks:
_hooks: {} _hooks: {}
attach: (name, method) -> attach: (name, method) ->
console.log "attaching hook", name, method
@_hooks[name] ?= [] @_hooks[name] ?= []
@_hooks[name].push method @_hooks[name].push method

View file

@ -146,12 +146,12 @@ script(type="text/ng-template", id="shareTagTemplate")
script(type="text/ng-template", id="shareAutocompleteTemplate") script(type="text/ng-template", id="shareAutocompleteTemplate")
.autocomplete-template .autocomplete-template
div(ng-if="data.type == 'user'") div(ng-if="data.type == 'user'")
i.fa.fa-user i.fa.fa-fw.fa-user
| |
span(ng-bind-html="$highlight(data.email)") span(ng-bind-html="$highlight(data.email)")
div.subdued.small(ng-show="data.name", ng-bind-html="$highlight(data.name)") div.subdued.small(ng-show="data.name", ng-bind-html="$highlight(data.name)")
div(ng-if="data.type == 'group'") div(ng-if="data.type == 'group'")
i.fa.fa-group i.fa.fa-fw.fa-group
| |
span(ng-bind-html="$highlight(data.name)") span(ng-bind-html="$highlight(data.name)")
div.subdued.small(ng-show="data.member_count") {{ data.memberCount }} members div.subdued.small(ng-show="data.member_count") {{ data.memberCount }} members

View file

@ -14,12 +14,10 @@ describe "CollaboratorsController", ->
@CollaboratorsController = SandboxedModule.require modulePath, requires: @CollaboratorsController = SandboxedModule.require modulePath, requires:
"../Project/ProjectGetter": @ProjectGetter = {} "../Project/ProjectGetter": @ProjectGetter = {}
"./CollaboratorsHandler": @CollaboratorsHandler = {} "./CollaboratorsHandler": @CollaboratorsHandler = {}
"./CollaboratorsEmailHandler": @CollaboratorsEmailHandler = {}
"../User/UserGetter": @UserGetter = {}
"../Editor/EditorRealTimeController": @EditorRealTimeController = {} "../Editor/EditorRealTimeController": @EditorRealTimeController = {}
'../Subscription/LimitationsManager' : @LimitationsManager = {} '../Subscription/LimitationsManager' : @LimitationsManager = {}
'../Project/ProjectEditorHandler' : @ProjectEditorHandler = {} '../Project/ProjectEditorHandler' : @ProjectEditorHandler = {}
"../Contacts/ContactManager": @ContactManager = {} '../User/UserGetter': @UserGetter = {}
@res = new MockResponse() @res = new MockResponse()
@req = new MockRequest() @req = new MockRequest()
@ -72,13 +70,11 @@ describe "CollaboratorsController", ->
@user_view = { @user_view = {
id: @user_id, first_name: "Joe", last_name: "Example", email: "joe@example.com" id: @user_id, first_name: "Joe", last_name: "Example", email: "joe@example.com"
} }
@LimitationsManager.isCollaboratorLimitReached = sinon.stub().callsArgWith(1, null, false) @LimitationsManager.canAddXCollaborators = sinon.stub().callsArgWith(2, null, true)
@ProjectEditorHandler.buildUserModelView = sinon.stub().returns(@user_view) @ProjectEditorHandler.buildUserModelView = sinon.stub().returns(@user_view)
@CollaboratorsHandler.addEmailToProject = sinon.stub().callsArgWith(3, null, @user_id) @CollaboratorsHandler.addEmailToProject = sinon.stub().callsArgWith(4, null, @user_id)
@UserGetter.getUser = sinon.stub().callsArgWith(1, null, @user) @UserGetter.getUser = sinon.stub().callsArgWith(1, null, @user)
@CollaboratorsEmailHandler.notifyUserOfProjectShare = sinon.stub()
@EditorRealTimeController.emitToRoom = sinon.stub() @EditorRealTimeController.emitToRoom = sinon.stub()
@ContactManager.addContact = sinon.stub()
@callback = sinon.stub() @callback = sinon.stub()
describe "when the project can accept more collaborators", -> describe "when the project can accept more collaborators", ->
@ -87,7 +83,7 @@ describe "CollaboratorsController", ->
it "should add the user to the project", -> it "should add the user to the project", ->
@CollaboratorsHandler.addEmailToProject @CollaboratorsHandler.addEmailToProject
.calledWith(@project_id, @email.toLowerCase(), @privileges) .calledWith(@project_id, @adding_user_id, @email.toLowerCase(), @privileges)
.should.equal true .should.equal true
it "should emit a userAddedToProject event", -> it "should emit a userAddedToProject event", ->
@ -95,16 +91,6 @@ describe "CollaboratorsController", ->
.calledWith(@project_id, "userAddedToProject", @user_view, @privileges) .calledWith(@project_id, "userAddedToProject", @user_view, @privileges)
.should.equal true .should.equal true
it "should send an email to the shared-with user", ->
@CollaboratorsEmailHandler.notifyUserOfProjectShare
.calledWith(@project_id, @email.toLowerCase())
.should.equal true
it "should add the user as a contact for the adding user", ->
@ContactManager.addContact
.calledWith(@adding_user_id, @user_id)
.should.equal true
it "should send the user as the response body", -> it "should send the user as the response body", ->
@res.json @res.json
.calledWith({ .calledWith({
@ -114,7 +100,7 @@ describe "CollaboratorsController", ->
describe "when the project cannot accept more collaborators", -> describe "when the project cannot accept more collaborators", ->
beforeEach -> beforeEach ->
@LimitationsManager.isCollaboratorLimitReached = sinon.stub().callsArgWith(1, null, true) @LimitationsManager.canAddXCollaborators = sinon.stub().callsArgWith(2, null, false)
@CollaboratorsController.addUserToProject @req, @res, @next @CollaboratorsController.addUserToProject @req, @res, @next
it "should not add the user to the project", -> it "should not add the user to the project", ->

View file

@ -11,11 +11,16 @@ describe "CollaboratorsHandler", ->
@CollaboratorHandler = SandboxedModule.require modulePath, requires: @CollaboratorHandler = SandboxedModule.require modulePath, requires:
"logger-sharelatex": @logger = { log: sinon.stub(), err: sinon.stub() } "logger-sharelatex": @logger = { log: sinon.stub(), err: sinon.stub() }
'../User/UserCreator': @UserCreator = {} '../User/UserCreator': @UserCreator = {}
'../User/UserGetter': @UserGetter = {}
"../Contacts/ContactManager": @ContactManager = {}
"../../models/Project": Project: @Project = {} "../../models/Project": Project: @Project = {}
"../Project/ProjectEntityHandler": @ProjectEntityHandler = {} "../Project/ProjectEntityHandler": @ProjectEntityHandler = {}
"./CollaboratorsEmailHandler": @CollaboratorsEmailHandler = {}
@project_id = "mock-project-id" @project_id = "mock-project-id"
@user_id = "mock-user-id" @user_id = "mock-user-id"
@adding_user_id = "adding-user-id"
@email = "joe@sharelatex.com"
@callback = sinon.stub() @callback = sinon.stub()
describe "removeUserFromProject", -> describe "removeUserFromProject", ->
@ -36,10 +41,14 @@ describe "CollaboratorsHandler", ->
beforeEach -> beforeEach ->
@Project.update = sinon.stub().callsArg(2) @Project.update = sinon.stub().callsArg(2)
@ProjectEntityHandler.flushProjectToThirdPartyDataStore = sinon.stub().callsArg(1) @ProjectEntityHandler.flushProjectToThirdPartyDataStore = sinon.stub().callsArg(1)
@CollaboratorHandler.addEmailToProject = sinon.stub().callsArgWith(4, null, @user_id)
@UserGetter.getUser = sinon.stub().callsArgWith(2, null, @user = { _id: @user_id, email: @email })
@CollaboratorsEmailHandler.notifyUserOfProjectShare = sinon.stub()
@ContactManager.addContact = sinon.stub()
describe "as readOnly", -> describe "as readOnly", ->
beforeEach -> beforeEach ->
@CollaboratorHandler.addUserToProject @project_id, @user_id, "readOnly", @callback @CollaboratorHandler.addUserIdToProject @project_id, @adding_user_id, @user_id, "readOnly", @callback
it "should add the user to the readOnly_refs", -> it "should add the user to the readOnly_refs", ->
@Project.update @Project.update
@ -55,9 +64,19 @@ describe "CollaboratorsHandler", ->
.calledWith(@project_id) .calledWith(@project_id)
.should.equal true .should.equal true
it "should send an email to the shared-with user", ->
@CollaboratorsEmailHandler.notifyUserOfProjectShare
.calledWith(@project_id, @email)
.should.equal true
it "should add the user as a contact for the adding user", ->
@ContactManager.addContact
.calledWith(@adding_user_id, @user_id)
.should.equal true
describe "as readAndWrite", -> describe "as readAndWrite", ->
beforeEach -> beforeEach ->
@CollaboratorHandler.addUserToProject @project_id, @user_id, "readAndWrite", @callback @CollaboratorHandler.addUserIdToProject @project_id, @adding_user_id, @user_id, "readAndWrite", @callback
it "should add the user to the collaberator_refs", -> it "should add the user to the collaberator_refs", ->
@Project.update @Project.update
@ -75,7 +94,7 @@ describe "CollaboratorsHandler", ->
describe "with invalid privilegeLevel", -> describe "with invalid privilegeLevel", ->
beforeEach -> beforeEach ->
@CollaboratorHandler.addUserToProject @project_id, @user_id, "notValid", @callback @CollaboratorHandler.addUserIdToProject @project_id, @adding_user_id, @user_id, "notValid", @callback
it "should call the callback with an error", -> it "should call the callback with an error", ->
@callback.calledWith(new Error()).should.equal true @callback.calledWith(new Error()).should.equal true
@ -83,11 +102,11 @@ describe "CollaboratorsHandler", ->
describe "addEmailToProject", -> describe "addEmailToProject", ->
beforeEach -> beforeEach ->
@UserCreator.getUserOrCreateHoldingAccount = sinon.stub().callsArgWith(1, null, @user = {_id: @user_id}) @UserCreator.getUserOrCreateHoldingAccount = sinon.stub().callsArgWith(1, null, @user = {_id: @user_id})
@CollaboratorHandler.addUserToProject = sinon.stub().callsArg(3) @CollaboratorHandler.addUserIdToProject = sinon.stub().callsArg(4)
describe "with a valid email", -> describe "with a valid email", ->
beforeEach -> beforeEach ->
@CollaboratorHandler.addEmailToProject @project_id, (@email = "Joe@example.com"), (@privilegeLevel = "readAndWrite"), @callback @CollaboratorHandler.addEmailToProject @project_id, @adding_user_id, (@email = "Joe@example.com"), (@privilegeLevel = "readAndWrite"), @callback
it "should get the user with the lowercased email", -> it "should get the user with the lowercased email", ->
@UserCreator.getUserOrCreateHoldingAccount @UserCreator.getUserOrCreateHoldingAccount
@ -95,8 +114,8 @@ describe "CollaboratorsHandler", ->
.should.equal true .should.equal true
it "should add the user to the project by id", -> it "should add the user to the project by id", ->
@CollaboratorHandler.addUserToProject @CollaboratorHandler.addUserIdToProject
.calledWith(@project_id, @user_id, @privilegeLevel) .calledWith(@project_id, @adding_user_id, @user_id, @privilegeLevel)
.should.equal true .should.equal true
it "should return the callback with the user_id", -> it "should return the callback with the user_id", ->
@ -104,13 +123,13 @@ describe "CollaboratorsHandler", ->
describe "with an invalid email", -> describe "with an invalid email", ->
beforeEach -> beforeEach ->
@CollaboratorHandler.addEmailToProject @project_id, "not-and-email", (@privilegeLevel = "readAndWrite"), @callback @CollaboratorHandler.addEmailToProject @project_id, @adding_user_id, "not-and-email", (@privilegeLevel = "readAndWrite"), @callback
it "should call the callback with an error", -> it "should call the callback with an error", ->
@callback.calledWith(new Error()).should.equal true @callback.calledWith(new Error()).should.equal true
it "should not add any users to the proejct", -> it "should not add any users to the proejct", ->
@CollaboratorHandler.addUserToProject.called.should.equal false @CollaboratorHandler.addUserIdToProject.called.should.equal false

View file

@ -62,7 +62,7 @@ describe "LimitationsManager", ->
it "should return the total number of collaborators", -> it "should return the total number of collaborators", ->
@callback.calledWith(null, 3).should.equal true @callback.calledWith(null, 3).should.equal true
describe "isCollaboratorLimitReached", -> describe "canAddXCollaborators", ->
beforeEach -> beforeEach ->
sinon.stub @LimitationsManager, sinon.stub @LimitationsManager,
"currentNumberOfCollaboratorsInProject", "currentNumberOfCollaboratorsInProject",
@ -76,7 +76,16 @@ describe "LimitationsManager", ->
beforeEach -> beforeEach ->
@current_number = 1 @current_number = 1
@allowed_number = 2 @allowed_number = 2
@LimitationsManager.isCollaboratorLimitReached(@project_id, @callback) @LimitationsManager.canAddXCollaborators(@project_id, 1, @callback)
it "should return true", ->
@callback.calledWith(null, true).should.equal true
describe "when the project has fewer collaborators than allowed but I want to add more than allowed", ->
beforeEach ->
@current_number = 1
@allowed_number = 2
@LimitationsManager.canAddXCollaborators(@project_id, 2, @callback)
it "should return false", -> it "should return false", ->
@callback.calledWith(null, false).should.equal true @callback.calledWith(null, false).should.equal true
@ -85,19 +94,19 @@ describe "LimitationsManager", ->
beforeEach -> beforeEach ->
@current_number = 3 @current_number = 3
@allowed_number = 2 @allowed_number = 2
@LimitationsManager.isCollaboratorLimitReached(@project_id, @callback) @LimitationsManager.canAddXCollaborators(@project_id, 1, @callback)
it "should return true", -> it "should return false", ->
@callback.calledWith(null, true).should.equal true @callback.calledWith(null, false).should.equal true
describe "when the project has infinite collaborators", -> describe "when the project has infinite collaborators", ->
beforeEach -> beforeEach ->
@current_number = 100 @current_number = 100
@allowed_number = -1 @allowed_number = -1
@LimitationsManager.isCollaboratorLimitReached(@project_id, @callback) @LimitationsManager.canAddXCollaborators(@project_id, 1, @callback)
it "should return false", -> it "should return true", ->
@callback.calledWith(null, false).should.equal true @callback.calledWith(null, true).should.equal true
describe "userHasSubscription", -> describe "userHasSubscription", ->