Changeset View
Changeset View
Standalone View
Standalone View
lib/app/base-chips/access-strategy-types/when.js
"use strict"; | "use strict"; | ||||
const Promise = require("bluebird"); | const Promise = require("bluebird"); | ||||
const Query = require("../../../datastore/query.js"); | const Query = require("../../../datastore/query.js"); | ||||
async function construct_query( | async function construct_query( | ||||
app, | app, | ||||
context, | context, | ||||
collection_name, | collection_name, | ||||
special_filter_name, | special_filter_name, | ||||
when_true_name, | when_true_name, | ||||
when_false_name = "noone" | when_false_name = "noone" | ||||
) { | ) { | ||||
const collection = app.ChipManager.get_chip("collection", collection_name); | const collection = app.ChipManager.get_chip("collection", collection_name); | ||||
const special_filter = collection.get_named_filter(special_filter_name); | const special_filter = collection.get_named_filter(special_filter_name); | ||||
const when_true = new app.Sealious.AccessStrategy(app, when_true_name); | const when_true = new app.Sealious.AccessStrategy(app, when_true_name); | ||||
const when_false = new app.Sealious.AccessStrategy(app, when_false_name); | const when_false = new app.Sealious.AccessStrategy(app, when_false_name); | ||||
const filtering_query = await special_filter.getFilteringQuery(collection); | |||||
return new Query.Or( | return new Query.Or( | ||||
new Query.And( | new Query.And( | ||||
await special_filter.getFilteringQuery(collection), | filtering_query, | ||||
await when_true.getRestrictingQuery(context) | await when_true.getRestrictingQuery(context) | ||||
), | ), | ||||
new Query.And( | new Query.And( | ||||
new Query.Not(await special_filter.getFilteringQuery(collection)), | new Query.Not(filtering_query), | ||||
await when_false.getRestrictingQuery(context) | await when_false.getRestrictingQuery(context) | ||||
) | ) | ||||
); | ); | ||||
} | } | ||||
module.exports = app => ({ | module.exports = app => ({ | ||||
name: "when", | name: "when", | ||||
getRestrictingQuery: async function( | getRestrictingQuery: async function( | ||||
Show All 19 Lines | checker_function: async function( | ||||
[ | [ | ||||
collection_name, | collection_name, | ||||
special_filter_name, | special_filter_name, | ||||
when_true_name, | when_true_name, | ||||
when_false_name = "noone", | when_false_name = "noone", | ||||
], | ], | ||||
item | item | ||||
) { | ) { | ||||
const results = await app.Datastore.aggregate( | const query = await construct_query( | ||||
item.collection_name, | |||||
(await construct_query( | |||||
app, | app, | ||||
context, | context, | ||||
collection_name, | collection_name, | ||||
special_filter_name, | special_filter_name, | ||||
when_true_name, | when_true_name, | ||||
when_false_name | when_false_name | ||||
)) | |||||
.match({ sealious_id: item.id }) | |||||
.toPipeline() | |||||
); | ); | ||||
if (results.length) { | query.match({ sealious_id: item.id }); | ||||
return Promise.resolve(); | const results = await app.Datastore.aggregate( | ||||
} else { | item.collection_name, | ||||
query.toPipeline() | |||||
); | |||||
if (!results.length) { | |||||
return Promise.reject("No access"); | return Promise.reject("No access"); | ||||
} | } | ||||
}, | }, | ||||
item_sensitive: true, | item_sensitive: true, | ||||
}); | }); |