Skip to content

Commit

Permalink
refactor: remove addGroupToEntry
Browse files Browse the repository at this point in the history
BREAKING CHANGE: it is now replaced by addOwnersToDoc
  • Loading branch information
targos committed Nov 15, 2016
1 parent 2262760 commit cee70f8
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 74 deletions.
2 changes: 1 addition & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const allowedFirstLevelKeys = [
module.exports = {
DESIGN_DOC_NAME: 'app',
DESIGN_DOC_ID: '_design/app',
DESIGN_DOC_VERSION: 15,
DESIGN_DOC_VERSION: 16,
RIGHTS_DOC_ID: 'rights',
DEFAULT_GROUPS_DOC_ID: 'defaultGroups',
globalRightTypes,
Expand Down
13 changes: 9 additions & 4 deletions src/couch/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ const methods = {
await this.open();

options = options || {};
options.groups = options.groups || [];
if (!entry.$content) throw new CouchError('entry has no content');
if (options.groups !== undefined && !Array.isArray(options.groups)) throw new CouchError('options.groups should be an array if defined', 'invalid argument');

Expand Down Expand Up @@ -187,7 +186,9 @@ const methods = {
}
const res = await createNew(this, entry, user);
action = 'created';
await util.addGroups(res, this, user, options.groups);
if (options.groups) {
await this.addOwnersToDoc(res.id, user, options.groups, 'entry');
}
result = res;
}

Expand All @@ -204,7 +205,9 @@ function onNotFound(ctx, entry, user, options) {
}

const res = await createNew(ctx, entry, user);
await util.addGroups(res, ctx, user, options.groups);
if (options.groups) {
await ctx.addOwnersToDoc(res.id, user, options.groups, 'entry');
}
return res;
} else {
throw error;
Expand Down Expand Up @@ -257,7 +260,9 @@ async function updateEntry(ctx, oldDoc, newDoc, user, options) {
// Doc validation will fail $kind changed
oldDoc.$kind = newDoc.$kind;
const res = await nanoMethods.saveEntry(ctx._db, oldDoc, user);
await util.addGroups(res, ctx, user, options.groups);
if (options.groups) {
await ctx.addOwnersToDoc(res.id, user, options.groups, 'entry');
}
return res;
}

Expand Down
5 changes: 0 additions & 5 deletions src/couch/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ const methods = {
return this.editDefaultGroup(group, type, 'remove');
},

addGroupToEntry(uuid, user, group) {
debug(`addGroupToEntry (${uuid}, ${user}, ${group})`);
return this._doUpdateOnEntry(uuid, user, 'addGroupToEntry', {group});
},

removeGroupFromEntry(uuid, user, group) {
debug(`removeGroupFromEntry (${uuid}, ${user}, ${group})`);
return this._doUpdateOnEntry(uuid, user, 'removeGroupFromEntry', {group});
Expand Down
7 changes: 0 additions & 7 deletions src/couch/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ function isAllowedFirstLevelKey(key) {
return includes(constants.allowedFirstLevelKeys, key);
}

async function addGroups(doc, ctx, user, groups) {
for (let i = 0; i < groups.length; i++) {
await ctx.addGroupToEntry(doc.id, user, groups[i]);
}
}

function isManagedDocumentType(type) {
return type === 'entry' || type === 'group';
}
Expand All @@ -53,6 +47,5 @@ module.exports = {
isValidGlobalRightUser,
isValidGlobalRightType,
isAllowedFirstLevelKey,
addGroups,
isManagedDocumentType
};
45 changes: 0 additions & 45 deletions src/design/updates.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,6 @@

const updates = module.exports;

updates.addGroupToEntry = function (doc, req) {
var group = JSON.parse(req.body).group || req.query.group;
var resp = {
headers: {
'Content-Type': 'application/json'
}
};

if (!doc) {
resp.code = 404;
resp.body = '{"error": "not found"}';
return [null, resp];
}
if (doc.$type !== 'entry') {
resp.code = 400;
resp.body = '{"error": "not an entry"}';
return [null, resp];
}
if (!group) {
resp.code = 400;
resp.body = '{"error": "no group in query"}';
return [null, resp];
}

if (!Array.isArray(group)) {
group = [group];
}

for (var i = 0; i < group.length; i++) {
if (typeof group[i] !== 'string') {
resp.code = 400;
resp.body = '{"error": "group must be a string or array"}';
return [null, resp];
}
var idx = doc.$owners.indexOf(group[i]);
if (idx === -1) {
doc.$owners.push(group[i]);
}
}

resp.code = 200;
resp.body = '{"ok": true}';
return [doc, resp];
};

updates.removeGroupFromEntry = function (doc, req) {
var group = JSON.parse(req.body).group || req.query.group;
var resp = {
Expand Down
3 changes: 2 additions & 1 deletion src/server/middleware/couch.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ exports.getOwners = composeWithError(function*() {
});

exports.addOwner = composeWithError(function*() {
yield this.state.couch.addGroupToEntry(this.params.uuid, this.state.userEmail, this.params.owner);
yield this.state.couch.addOwnersToDoc(this.params.uuid, this.state.userEmail, this.params.owner, 'entry');
this.body = OK;
});

Expand Down Expand Up @@ -261,6 +261,7 @@ function onGetError(ctx, e, secure) {
case 'forbidden':
ctx.status = 403;
ctx.body = e.message || statusMessages[403];
break;
default:
if (!handleCouchError(ctx, e, secure)) {
ctx.status = 500;
Expand Down
13 changes: 7 additions & 6 deletions test/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,16 @@ describe('entry creation and editions', function () {
});

it('should add group to entry', function () {
return couch.addGroupToEntry('A', 'b@b.com', 'groupD')
return couch.addOwnersToDoc('A', 'b@b.com', 'groupD', 'entry')
.then(() => couch.getEntry('A', 'b@b.com'))
.then(doc => {
doc.$owners.indexOf('groupD').should.be.above(0);
});
});

it('Add existing group to entry', function () {
return couch.addGroupToEntry('A', 'b@b.com', 'groupD')
.then(() => couch.addGroupToEntry('A', 'b@b.com', 'groupD'))
return couch.addOwnersToDoc('A', 'b@b.com', 'groupD', 'entry')
.then(() => couch.addOwnersToDoc('A', 'b@b.com', 'groupD', 'entry'))
.then(() => couch.getEntry('A', 'b@b.com'))
.then(entry => {
let count = 0;
Expand All @@ -176,8 +176,8 @@ describe('entry creation and editions', function () {
});

it('Add existing group to entry (2)', function () {
return couch.addGroupToEntry('A', 'b@b.com', 'anonymousRead')
.then(() => couch.addGroupToEntry('A', 'b@b.com', 'anonymousRead'))
return couch.addOwnersToDoc('A', 'b@b.com', 'anonymousRead', 'entry')
.then(() => couch.addOwnersToDoc('A', 'b@b.com', 'anonymousRead', 'entry'))
.then(() => couch.getEntry('A', 'b@b.com'))
.then(entry => {
let count = 0;
Expand All @@ -189,7 +189,8 @@ describe('entry creation and editions', function () {
});

it('should fail to add group to entry', function () {
return couch.addGroupToEntry('A', 'a@a.com', 'groupC').should.be.rejectedWith(/unauthorized/);
return couch.addOwnersToDoc('A', 'a@a.com', 'groupC', 'entry')
.should.be.rejectedWith(/user has no access/);
});

it('should remove group from entry', function () {
Expand Down
8 changes: 3 additions & 5 deletions test/rest-api/owners.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ const data = require('../data/data');
const authenticateAs = require('./authenticate');

describe('rest api - manage owners', function () {
var id;
const id = 'A';
before(function () {
return data().then(() => authenticateAs(request, 'b@b.com', '123')).then(() => {
return couch.getEntryById('A', 'b@b.com').then(entry => id = entry._id);
});
return data().then(() => authenticateAs(request, 'b@b.com', '123'));
});
it('get owners', function () {
return request.get(`/db/test/entry/${id}/_owner`)
Expand All @@ -27,7 +25,7 @@ describe('rest api - manage owners', function () {
});
});
it('remove owner', function () {
return couch.addGroupToEntry(id, 'b@b.com', 'testRemove')
return couch.addOwnersToDoc(id, 'b@b.com', 'testRemove', 'entry')
.then(() => {
return request.del(`/db/test/entry/${id}/_owner/testRemove`)
.expect(200)
Expand Down

0 comments on commit cee70f8

Please sign in to comment.