diff --git a/packages/xpub-faraday/config/authsome-helpers.js b/packages/xpub-faraday/config/authsome-helpers.js index e30375101e82bc13c9d464ec8c4be74686c6c7d9..69f01bc6bcb311a244102ca243418242ef70fced 100644 --- a/packages/xpub-faraday/config/authsome-helpers.js +++ b/packages/xpub-faraday/config/authsome-helpers.js @@ -6,23 +6,43 @@ const statuses = config.get('statuses') const publicStatusesPermissions = ['author', 'reviewer'] +const parseAuthorsData = (coll, matchingCollPerm) => { + if (['reviewer'].includes(matchingCollPerm.permission)) { + coll.authors = coll.authors.map(a => omit(a, ['email'])) + } +} + +const setPublicStatuses = (coll, matchingCollPerm) => { + const status = get(coll, 'status') || 'draft' + coll.visibleStatus = statuses[status].public + if (!publicStatusesPermissions.includes(matchingCollPerm.permission)) { + coll.visibleStatus = statuses[coll.status].private + } +} + +const filterRefusedInvitations = (coll, user) => { + const matchingInv = coll.invitations.find(inv => inv.userId === user.id) + if (matchingInv === undefined) return null + if (matchingInv.hasAnswer === true && !matchingInv.isAccepted) return null + return coll +} + +const filterCollectionData = (collectionsPermissions, collection, user) => { + const matchingCollPerm = collectionsPermissions.find( + collPerm => collection.id === collPerm.id, + ) + setPublicStatuses(collection, matchingCollPerm) + parseAuthorsData(collection, matchingCollPerm) + if (['reviewer', 'handlingEditor'].includes(matchingCollPerm.permission)) { + return filterRefusedInvitations(collection, user) + } + + return collection +} + module.exports = { - parseAuthorsData: (coll, matchingCollPerm) => { - if (['reviewer'].includes(matchingCollPerm.permission)) { - coll.authors = coll.authors.map(a => omit(a, ['email'])) - } - }, - setPublicStatuses: (coll, matchingCollPerm) => { - const status = get(coll, 'status') || 'draft' - coll.visibleStatus = statuses[status].public - if (!publicStatusesPermissions.includes(matchingCollPerm.permission)) { - coll.visibleStatus = statuses[coll.status].private - } - }, - filterRefusedInvitations: (coll, user) => { - const matchingInv = coll.invitations.find(inv => inv.userId === user.id) - if (matchingInv === undefined) return null - if (matchingInv.hasAnswer === true && !matchingInv.isAccepted) return null - return coll - }, + parseAuthorsData, + setPublicStatuses, + filterRefusedInvitations, + filterCollectionData, } diff --git a/packages/xpub-faraday/config/authsome-mode.js b/packages/xpub-faraday/config/authsome-mode.js index 866460db79c6ccbcbc7140ef22b0583a2fea7489..878c15519c1370a1df4cc1bd3cfd458388b81f05 100644 --- a/packages/xpub-faraday/config/authsome-mode.js +++ b/packages/xpub-faraday/config/authsome-mode.js @@ -4,6 +4,8 @@ const omit = require('lodash/omit') const helpers = require('./authsome-helpers') async function teamPermissions(user, operation, object, context) { + if (object.type !== 'collection') return true + const permissions = ['handlingEditor', 'author', 'reviewer'] const teams = await Promise.all( user.teams.map(async teamId => { @@ -20,37 +22,26 @@ async function teamPermissions(user, operation, object, context) { permission: team.teamType.permissions, })) - if (collectionsPermissions.length > 0) { - return { - filter: filterParam => { - if (!filterParam.length) return filterParam - - const collections = filterParam - .map(coll => { - const matchingCollPerm = collectionsPermissions.find( - collPerm => coll.id === collPerm.id, - ) - if (matchingCollPerm === undefined) { - return null - } - helpers.setPublicStatuses(coll, matchingCollPerm) - helpers.parseAuthorsData(coll, matchingCollPerm) - if ( - ['reviewer', 'handlingEditor'].includes( - matchingCollPerm.permission, - ) - ) { - return helpers.filterRefusedInvitations(coll, user) - } - return coll - }) - .filter(Boolean) - return collections - }, - } - } + if (collectionsPermissions.length === 0) return {} - return {} + return { + filter: filterParam => { + if (!filterParam.length) { + return helpers.filterCollectionData( + collectionsPermissions, + filterParam, + user, + ) + } + + const collections = filterParam + .map(coll => + helpers.filterCollectionData(collectionsPermissions, coll, user), + ) + .filter(Boolean) + return collections + }, + } } function unauthenticatedUser(operation, object) {