Skip to content

Commit

Permalink
🐛 invite existing users
Browse files Browse the repository at this point in the history
refs #8692

- protect invite endpoint
  • Loading branch information
kirrg001 authored and kevinansfield committed Jul 18, 2017
1 parent d4c74e7 commit 91f36fc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
14 changes: 14 additions & 0 deletions core/server/api/invites.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,19 @@ invites = {
});
}

function checkIfUserExists(options) {
return dataProvider.User.findOne({email: options.data.invites[0].email}, options)
.then(function (user) {
if (user) {
return Promise.reject(new errors.ValidationError({
message: i18n.t('errors.api.users.userAlreadyRegistered')
}));
}

return options;
});
}

function fetchLoggedInUser(options) {
return dataProvider.User.findOne({id: loggedInUser}, _.merge({}, options, {include: ['roles']}))
.then(function (user) {
Expand All @@ -219,6 +232,7 @@ invites = {
utils.convertOptions(allowedIncludes),
fetchLoggedInUser,
validation,
checkIfUserExists,
destroyOldInvite,
addInvite
];
Expand Down
18 changes: 17 additions & 1 deletion core/test/integration/api/api_invites_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var should = require('should'),

describe('Invites API', function () {
beforeEach(testUtils.teardown);
beforeEach(testUtils.setup('invites', 'users:roles', 'perms:invite', 'perms:init'));
beforeEach(testUtils.setup('invites', 'settings', 'users:roles', 'perms:invite', 'perms:init'));

beforeEach(function () {
sandbox.stub(mail, 'send', function () {
Expand Down Expand Up @@ -72,6 +72,22 @@ describe('Invites API', function () {
done();
});
});

it('add invite: invite existing user', function (done) {
InvitesAPI.add({
invites: [{
email: testUtils.DataGenerator.Content.users[0].email,
role_id: testUtils.roles.ids.author
}]
}, testUtils.context.owner)
.then(function () {
throw new Error('expected validation error');
})
.catch(function (err) {
(err instanceof errors.ValidationError).should.eql(true);
done();
});
});
});

describe('Browse', function () {
Expand Down

0 comments on commit 91f36fc

Please sign in to comment.