litejs
Version:
Single-page application framework
54 lines (40 loc) • 1.11 kB
JavaScript
!function(exports) {
exports.createAcl = createAcl
function createAcl(fn, modelCache) {
return {
check: check
}
function check(aclGroups, acl, _inherit) {
if (!acl || !Array.isArray(aclGroups)) return false
var tmp, inherit
, fields = ["id"]
, arr = acl.inherit
, i = Array.isArray(arr) && arr.length
, sum = i
if (i) for (inherit = _inherit || {}; i--; ) {
tmp = acl.inherit[i]
tmp = tmp && !inherit[tmp] && modelCache[inherit[tmp] = tmp]
tmp = tmp && tmp.data.enabled && check(aclGroups, tmp.data.acl, inherit)
if (tmp === true) return true
if (tmp) fields.push.apply(fields, tmp)
}
arr = acl.policy
i = Array.isArray(arr) && arr.length
sum += i
if (i) for (i = acl.policy.length; i--; ) {
tmp = acl.policy[i]
if (
tmp &&
Array.isArray(tmp.who) &&
tmp.acl & (_inherit ? 2 : 1) &&
tmp.who.some(inArray, aclGroups) &&
fn(tmp.acl, fields) === true
) return true
}
return sum ? fields.length > 1 && fields : null
}
}
function inArray(value) {
return this.indexOf(value) > -1
}
}(this)