Skip to content

Commit f63963d

Browse files
committed
send invites as owner if added via an admin api key
1 parent 66f16f8 commit f63963d

File tree

3 files changed

+94
-5
lines changed

3 files changed

+94
-5
lines changed

core/server/api/invites.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ const invites = {
101101
.then((response) => {
102102
const adminUrl = urlService.utils.urlFor('admin', true);
103103

104-
// TODO: how to handle invitedBy for API Key requests
105104
emailData = {
106105
blogName: response.settings[0].value,
107106
invitedByName: loggedInUser.get('name'),
@@ -194,7 +193,10 @@ const invites = {
194193
const loggedInUserRole = loggedInUser.related('roles').models[0].get('name');
195194
let allowed = [];
196195

197-
if (loggedInUserRole === 'Owner' || loggedInUserRole === 'Administrator') {
196+
let userHasAdminRole = options.context.user && (loggedInUserRole === 'Owner' || loggedInUserRole === 'Administrator');
197+
198+
// admin api keys have an equivalent of the Adminstrator role
199+
if (options.context.api_key || userHasAdminRole) {
198200
allowed = ['Administrator', 'Editor', 'Author', 'Contributor'];
199201
} else if (loggedInUserRole === 'Editor') {
200202
allowed = ['Author', 'Contributor'];
@@ -235,11 +237,29 @@ const invites = {
235237
});
236238
}
237239

240+
function fetchOwner(options) {
241+
return models.User.getOwnerUser(merge({}, omit(options, 'data'), {withRelated: ['roles']}))
242+
.then((owner) => {
243+
loggedInUser = owner;
244+
return options;
245+
});
246+
}
247+
248+
// API Key requests are not tied to a user so send the invite from the
249+
// owner user instead
250+
function fetchLoggedInUserOrOwner(options) {
251+
if (options.context.api_key && !options.context.user) {
252+
return fetchOwner(options);
253+
}
254+
255+
return fetchLoggedInUser(options);
256+
}
257+
238258
tasks = [
239259
localUtils.validate(docName, {opts: ['email']}),
240260
localUtils.convertOptions(allowedIncludes),
241261
localUtils.handlePermissions(docName, 'add'),
242-
fetchLoggedInUser,
262+
fetchLoggedInUserOrOwner,
243263
validation,
244264
checkIfUserExists,
245265
destroyOldInvite,

core/test/integration/api/api_invites_spec.js

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@ var should = require('should'),
1313

1414
describe('Invites API', function () {
1515
before(testUtils.teardown);
16-
before(testUtils.setup('invites', 'settings', 'users:roles', 'perms:invite', 'perms:init'));
16+
before(testUtils.setup(
17+
'invites',
18+
'settings',
19+
'users:roles',
20+
'api_keys',
21+
'perms:invite',
22+
'perms:init'
23+
));
1724

1825
beforeEach(function () {
1926
sandbox.stub(mail, 'send').callsFake(function () {
@@ -440,5 +447,65 @@ describe('Invites API', function () {
440447
}).catch(done);
441448
});
442449
});
450+
451+
describe('Admin API Key', function () {
452+
it('CANNOT invite an Owner', function (done) {
453+
InvitesAPI.add({
454+
invites: [
455+
{
456+
457+
role_id: testUtils.roles.ids.owner
458+
}
459+
]
460+
}, testUtils.context.admin_api_key).then(function () {
461+
done(new Error('API Key should not be able to add an owner'));
462+
}).catch(checkForErrorType('NoPermissionError', done));
463+
});
464+
465+
it('Can invite an Admin', function (done) {
466+
InvitesAPI.add({
467+
invites: [
468+
{
469+
470+
role_id: testUtils.roles.ids.admin
471+
}
472+
]
473+
}, _.merge({}, {include: 'roles'}, testUtils.context.admin_api_key)).then(function (response) {
474+
checkAddResponse(response);
475+
response.invites[0].role_id.should.equal(testUtils.roles.ids.admin);
476+
done();
477+
}).catch(done);
478+
});
479+
480+
it('Can invite an Editor', function (done) {
481+
InvitesAPI.add({
482+
invites: [
483+
{
484+
485+
role_id: testUtils.roles.ids.editor
486+
}
487+
]
488+
}, testUtils.context.admin_api_key).then(function (response) {
489+
checkAddResponse(response);
490+
response.invites[0].role_id.should.equal(testUtils.roles.ids.editor);
491+
done();
492+
}).catch(done);
493+
});
494+
495+
it('Can invite an Author', function (done) {
496+
InvitesAPI.add({
497+
invites: [
498+
{
499+
500+
role_id: testUtils.roles.ids.author
501+
}
502+
]
503+
}, testUtils.context.admin_api_key).then(function (response) {
504+
checkAddResponse(response);
505+
response.invites[0].role_id.should.equal(testUtils.roles.ids.author);
506+
done();
507+
}).catch(done);
508+
});
509+
});
443510
});
444511
});

core/test/utils/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,9 @@ module.exports = {
11581158
admin: {context: {user: DataGenerator.Content.users[1].id}},
11591159
editor: {context: {user: DataGenerator.Content.users[2].id}},
11601160
author: {context: {user: DataGenerator.Content.users[3].id}},
1161-
contributor: {context: {user: DataGenerator.Content.users[7].id}}
1161+
contributor: {context: {user: DataGenerator.Content.users[7].id}},
1162+
admin_api_key: {context: {api_key: DataGenerator.Content.api_keys[0].id}},
1163+
content_api_key: {context: {api_key: DataGenerator.Content.api_keys[1].id}}
11621164
},
11631165
permissions: {
11641166
owner: {user: {roles: [DataGenerator.Content.roles[3]]}},

0 commit comments

Comments
 (0)