Changeset View
Changeset View
Standalone View
Standalone View
lib/chip-types/access-strategy-type.js
"use strict"; | |||||
const locreq = require("locreq")(__dirname); | const locreq = require("locreq")(__dirname); | ||||
const Promise = require("bluebird"); | const Promise = require("bluebird"); | ||||
const Errors = locreq("lib/response/error.js"); | const Errors = locreq("lib/response/error.js"); | ||||
const Chip = locreq("lib/chip-types/chip.js"); | |||||
function AccessStrategyType(app, declaration) { | function AccessStrategyType(app, declaration) { | ||||
if (declaration instanceof AccessStrategyType) { | if (declaration instanceof AccessStrategyType) { | ||||
return declaration; | return declaration; | ||||
} else if (typeof declaration === "string") { | } else if (typeof declaration === "string") { | ||||
return app.ChipManager.get_chip("access_strategy_type", declaration); | return app.ChipManager.get_chip("access_strategy_type", declaration); | ||||
} | } | ||||
this.declaration = declaration; | this.declaration = declaration; | ||||
this.checker_function = null; | this.checker_function = null; | ||||
this.name = declaration.name; | this.name = declaration.name; | ||||
this.app = app; | this.app = app; | ||||
if (declaration) { | if (declaration) { | ||||
this.checker_function = | this.checker_function = | ||||
declaration.checker_function === undefined | declaration.checker_function === undefined | ||||
? null | ? null | ||||
: declaration.checker_function.bind(this); | : declaration.checker_function.bind(this); | ||||
} | } | ||||
} | } | ||||
AccessStrategyType.type_name = "access_strategy_type"; | AccessStrategyType.type_name = "access_strategy_type"; | ||||
AccessStrategyType.pure = { | AccessStrategyType.pure = { | ||||
check: function(declaration, context, params, item) { | check(declaration, context, params, item) { | ||||
if (context.is_super) { | if (context.is_super) { | ||||
return Promise.resolve(); | return Promise.resolve(); | ||||
} | } | ||||
return AccessStrategyType.pure | return AccessStrategyType.pure | ||||
.is_item_sensitive(declaration, params) | .is_item_sensitive(declaration, params) | ||||
.then(function(is_item_sensitive) { | .then(is_item_sensitive => { | ||||
if (is_item_sensitive && item === undefined) { | if (is_item_sensitive && item === undefined) { | ||||
return Promise.resolve(undefined); | return Promise.resolve(undefined); | ||||
} else { | } | ||||
return Promise.try(function() { | return Promise.try(() => { | ||||
return Promise.method(declaration.checker_function)( | return Promise.method(declaration.checker_function)( | ||||
context, | context, | ||||
params, | params, | ||||
item | item | ||||
).then(function(result) { | ).then(result => { | ||||
if (result === false) { | if (result === false) { | ||||
return Promise.reject("Access denied"); | return Promise.reject("Access denied"); | ||||
} else { | |||||
return Promise.resolve(result); | |||||
} | } | ||||
return Promise.resolve(result); | |||||
}); | }); | ||||
}); | }); | ||||
} | |||||
}) | }) | ||||
.catch(function(error) { | .catch(error => { | ||||
if (typeof error === "string") { | if (typeof error === "string") { | ||||
return Promise.reject(new Errors.BadContext(error)); | return Promise.reject(new Errors.BadContext(error)); | ||||
} else { | |||||
return Promise.reject(error); | |||||
} | } | ||||
return Promise.reject(error); | |||||
}); | }); | ||||
}, | }, | ||||
is_item_sensitive: function(declaration, params) { | is_item_sensitive(declaration, params) { | ||||
if (typeof declaration.item_sensitive === "function") { | if (typeof declaration.item_sensitive === "function") { | ||||
return Promise.resolve(declaration.item_sensitive(params)); | return Promise.resolve(declaration.item_sensitive(params)); | ||||
} else { | |||||
return Promise.resolve(Boolean(declaration.item_sensitive)); | |||||
} | } | ||||
return Promise.resolve(Boolean(declaration.item_sensitive)); | |||||
}, | }, | ||||
getRestrictingQuery: function(declaration, context, params) { | getRestrictingQuery(declaration, context, params) { | ||||
if (declaration.getRestrictingQuery) { | if (declaration.getRestrictingQuery) { | ||||
return Promise.resolve( | return Promise.resolve( | ||||
declaration.getRestrictingQuery(context, params) | declaration.getRestrictingQuery(context, params) | ||||
).then(result => { | ).then(result => { | ||||
return result; | return result; | ||||
}); | }); | ||||
} else { | |||||
return Promise.resolve([]); | |||||
} | } | ||||
return Promise.resolve([]); | |||||
}, | }, | ||||
}; | }; | ||||
AccessStrategyType.prototype = { | AccessStrategyType.prototype = { | ||||
check(context, params, item) { | check(context, params, item) { | ||||
return AccessStrategyType.pure.check( | return AccessStrategyType.pure.check( | ||||
this.declaration, | this.declaration, | ||||
context, | context, | ||||
Show All 20 Lines |