@casl/vue
Version:
Vue plugin for CASL which makes it easy to add permissions in any Vue application
71 lines (64 loc) • 2.47 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, {
value: "Module"
});
let t = require("vue"), n = require("@casl/ability");
const o = Object.hasOwn || ((t, n) => Object.prototype.hasOwnProperty.call(t, n));
function i(n) {
if (o(n, "possibleRulesFor")) return n;
const i = (0, t.ref)(!0);
n.on("updated", () => {
i.value = !i.value;
});
const e = n.possibleRulesFor.bind(n);
return n.possibleRulesFor = (t, n) => (i.value = i.value, e(t, n)), n.can = n.can.bind(n),
n.cannot = n.cannot.bind(n), n;
}
const e = Symbol("ability");
function r() {
const n = (0, t.inject)(e);
if (!n) throw new Error("Cannot inject Ability instance because it was not provided. Did you forget to install abilitiesPlugin or call provideAbility in a parent component?");
return n;
}
const s = (0, t.defineComponent)({
name: "Can",
props: {
I: String,
do: String,
a: [ String, Function ],
an: [ String, Function ],
this: [ String, Function, Object ],
on: [ String, Function, Object ],
not: Boolean,
passThrough: Boolean,
field: String
},
setup(t, {slots: n}) {
const o = t;
let i = "do", e = "on";
if (void 0 === o[i] && (i = "I", e = function(t) {
if (void 0 !== t.a) return "a";
if (void 0 !== t.this) return "this";
if (void 0 !== t.an) return "an";
return "";
}(t)), !o[i]) throw new Error("Neither `I` nor `do` prop was passed in <Can>");
if (!n.default) throw new Error("Expects to receive default slot");
const s = r();
return () => {
const r = s.relevantRuleFor(o[i], o[e], o.field);
let l = !!r && !r.inverted;
return t.not && (l = !l), t.passThrough || l ? n.default({
allowed: l,
reason: r?.reason,
ability: s
}) : null;
};
}
});
exports.ABILITY_TOKEN = e, exports.Can = s, exports.abilitiesPlugin = function(t, o, r) {
if (!(o && o instanceof n.Ability)) throw new Error("Please, provide an Ability instance to abilitiesPlugin plugin");
t.provide(e, i(o)), r && r.useGlobalProperties && (t.config.globalProperties.$ability = o,
t.config.globalProperties.$can = o.can.bind(o));
}, exports.provideAbility = function(n) {
(0, t.provide)(e, i(n));
}, exports.useAbility = r;
//# sourceMappingURL=index.cjs.map