2015-06-11 15:03:55 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-07-13 21:19:45 +02:00
|
|
|
import { sanitize } from './general_utils';
|
|
|
|
|
|
|
|
function intersectAcls(a, b) {
|
|
|
|
return a.filter((val) => b.indexOf(val) > -1);
|
|
|
|
}
|
|
|
|
|
2015-07-14 13:34:32 +02:00
|
|
|
export function getAvailableAcls(editions, filterFn) {
|
2015-06-11 15:03:55 +02:00
|
|
|
let availableAcls = [];
|
2015-07-14 16:29:01 +02:00
|
|
|
if (!editions || editions.constructor !== Array){
|
2015-07-13 23:57:16 +02:00
|
|
|
return [];
|
|
|
|
}
|
2015-07-13 21:19:45 +02:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2015-07-13 21:19:45 +02:00
|
|
|
return edition;
|
|
|
|
});
|
|
|
|
|
2015-06-11 15:03:55 +02:00
|
|
|
// If no edition has been selected, availableActions is empty
|
|
|
|
// If only one edition has been selected, their actions are available
|
|
|
|
// If more than one editions have been selected, their acl properties are intersected
|
2015-07-13 21:19:45 +02:00
|
|
|
if(editionsCopy.length >= 1) {
|
|
|
|
availableAcls = editionsCopy[0].acl;
|
2015-06-11 15:03:55 +02:00
|
|
|
}
|
2015-07-13 21:19:45 +02:00
|
|
|
if(editionsCopy.length >= 2) {
|
|
|
|
for(let i = 1; i < editionsCopy.length; i++) {
|
|
|
|
availableAcls = intersectAcls(availableAcls, editionsCopy[i].acl);
|
2015-06-11 15:03:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-13 21:19:45 +02:00
|
|
|
// convert acls back to key-value object
|
|
|
|
let availableAclsObj = {};
|
|
|
|
for(let i = 0; i < availableAcls.length; i++) {
|
|
|
|
availableAclsObj[availableAcls[i]] = true;
|
|
|
|
}
|
2015-06-11 15:03:55 +02:00
|
|
|
|
2015-07-13 21:19:45 +02:00
|
|
|
|
|
|
|
return availableAclsObj;
|
2015-06-11 15:03:55 +02:00
|
|
|
}
|