Changeset View
Changeset View
Standalone View
Standalone View
lib/app/base-chips/access-strategy-types/or.js
"use strict"; | |||||
const locreq = require("locreq")(__dirname); | const locreq = require("locreq")(__dirname); | ||||
const Promise = require("bluebird"); | const Promise = require("bluebird"); | ||||
const Error = locreq("lib/response/error.js").Error; | const Error = locreq("lib/response/error.js").Error; | ||||
const AccessStrategy = locreq("lib/chip-types/access-strategy.js"); | const AccessStrategy = locreq("lib/chip-types/access-strategy.js"); | ||||
const Query = locreq("lib/datastore/query.js"); | const Query = locreq("lib/datastore/query.js"); | ||||
function parse_params(app, params) { | function parse_params(app, params) { | ||||
return Object.keys(params).map(i => new AccessStrategy(app, params[i])); | return Object.keys(params).map(i => new AccessStrategy(app, params[i])); | ||||
} | } | ||||
module.exports = function(app) { | module.exports = function(app) { | ||||
const or = { | const or = { | ||||
name: "or", | name: "or", | ||||
getRestrictingQuery: async function(context, params) { | async getRestrictingQuery(context, params) { | ||||
const access_strategies = parse_params(app, params); | const access_strategies = parse_params(app, params); | ||||
const queries = await Promise.map(access_strategies, strategy => | const queries = await Promise.map(access_strategies, strategy => | ||||
strategy.getRestrictingQuery(context) | strategy.getRestrictingQuery(context) | ||||
); | ); | ||||
if (queries.some(query => query instanceof Query.AllowAll)) { | if (queries.some(query => query instanceof Query.AllowAll)) { | ||||
return new Query.AllowAll(); | return new Query.AllowAll(); | ||||
} | } | ||||
return new Query.Or(...queries); | return new Query.Or(...queries); | ||||
}, | }, | ||||
item_sensitive: function(params) { | item_sensitive(params) { | ||||
const access_strategies = parse_params(app, params); | const access_strategies = parse_params(app, params); | ||||
return Promise.map(access_strategies, function(access_strategy) { | return Promise.map(access_strategies, access_strategy => | ||||
return access_strategy.is_item_sensitive(); | access_strategy.is_item_sensitive() | ||||
}).reduce((a, b) => a || b); | ).reduce((a, b) => a || b); | ||||
}, | }, | ||||
checker_function: function(context, params, item) { | checker_function(context, params, item) { | ||||
if (context.is_super) { | if (context.is_super) { | ||||
return Promise.resolve(); | return Promise.resolve(); | ||||
} | } | ||||
return or.item_sensitive(params).then(function(item_sensitive) { | return or.item_sensitive(params).then(item_sensitive => { | ||||
if (item_sensitive && item === undefined) { | if (item_sensitive && item === undefined) { | ||||
return undefined; | return undefined; | ||||
} else { | } | ||||
const access_strategies = parse_params(app, params); | const access_strategies = parse_params(app, params); | ||||
const results = access_strategies.map(function(strategy) { | const results = access_strategies.map(strategy => | ||||
return strategy.check(context, item); | strategy.check(context, item) | ||||
}); | ); | ||||
return Promise.any(results).catch( | return Promise.any(results).catch( | ||||
Promise.AggregateError, | Promise.AggregateError, | ||||
function(aggregated_errors) { | aggregated_errors => { | ||||
aggregated_errors.forEach(function(error) { | aggregated_errors.forEach(error => { | ||||
if (!(error instanceof Error)) { | if (!(error instanceof Error)) { | ||||
throw error; | throw error; | ||||
} | } | ||||
}); | }); | ||||
const error_message = aggregated_errors | const error_message = aggregated_errors | ||||
.map( | .map( | ||||
aggregated_errors => | _aggregated_errors => _aggregated_errors.message | ||||
aggregated_errors.message | |||||
) | ) | ||||
.reduce((a, b) => `${a} ${b}`); | .reduce((a, b) => `${a} ${b}`); | ||||
return Promise.reject(error_message); | return Promise.reject(error_message); | ||||
} | } | ||||
); | ); | ||||
} | |||||
}); | }); | ||||
}, | }, | ||||
}; | }; | ||||
return or; | return or; | ||||
}; | }; |