1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-22 09:23:13 +01:00
onion/js/utils/acl_utils.js

56 lines
1.8 KiB
JavaScript
Raw Permalink Normal View History

'use strict';
2015-11-03 10:39:01 +01:00
import { sanitize, intersectLists } from './general_utils';
2015-07-14 13:34:32 +02:00
export function getAvailableAcls(editions, filterFn) {
let availableAcls = [];
if (!editions || editions.constructor !== Array) {
2015-07-13 23:57:16 +02:00
return [];
}
// if you copy a javascript array of objects using slice, then
// the object reference is still there.
// Therefore we need to do this ugly copying
let editionsCopy = JSON.parse(JSON.stringify(editions));
// sanitize object acls in editions
// so that they don't contain any falsy key-value pairs anymore
editionsCopy = editionsCopy.map((edition) => {
// acl also returns the piece id and the edition id
// therefore, we're going to remove it
edition.acl.edition = false;
edition.acl.piece = false;
edition.acl = sanitize(edition.acl, (val) => !val);
edition.acl = Object.keys(edition.acl);
2015-07-14 13:34:32 +02:00
// additionally, the user can specify a filter function for
// an acl array
if(typeof filterFn === 'function') {
edition.acl = edition.acl.filter(filterFn);
}
return edition;
});
// If no edition has been selected, availableActions is empty
2015-11-03 10:39:01 +01:00
// If only one edition has been selected, its actions are available
// If more than one editions have been selected, intersect all their acl properties
if (editionsCopy.length >= 1) {
availableAcls = editionsCopy[0].acl;
2015-11-03 10:39:01 +01:00
if (editionsCopy.length >= 2) {
for (let i = 1; i < editionsCopy.length; i++) {
availableAcls = intersectLists(availableAcls, editionsCopy[i].acl);
}
}
}
// convert acls back to key-value object
let availableAclsObj = {};
for (let i = 0; i < availableAcls.length; i++) {
availableAclsObj[availableAcls[i]] = true;
}
return availableAclsObj;
}