pundit
Version:
Minimal and tiny authorisation library that uses a plain old JavaScript object (POJO)
510 lines (507 loc) • 17.3 kB
JavaScript
import G, { useMemo as je } from "react";
class H {
constructor(s, a) {
this.user = s, this.record = a, this.actions = /* @__PURE__ */ new Map();
}
can(s) {
const a = this.actions.get(s);
return a !== void 0 ? a(this.user, this.record) : !1;
}
add(s, a) {
this.actions.set(s, a);
}
copy(s, a) {
const v = new H(s || this.user, a || this.record);
return this.actions.forEach((c, d) => {
v.add(d, c);
}), v;
}
setup() {
const s = Object.getOwnPropertyNames(
this.constructor.prototype
).filter((a) => a !== "constructor");
this.actions = new Map(
s.map((a) => [
a,
() => this[a]()
])
);
}
}
var k = { exports: {} }, R = {};
/**
* @license React
* react-jsx-runtime.production.js
*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
var de;
function ke() {
if (de) return R;
de = 1;
var i = Symbol.for("react.transitional.element"), s = Symbol.for("react.fragment");
function a(v, c, d) {
var E = null;
if (d !== void 0 && (E = "" + d), c.key !== void 0 && (E = "" + c.key), "key" in c) {
d = {};
for (var h in c)
h !== "key" && (d[h] = c[h]);
} else d = c;
return c = d.ref, {
$$typeof: i,
type: v,
key: E,
ref: c !== void 0 ? c : null,
props: d
};
}
return R.Fragment = s, R.jsx = a, R.jsxs = a, R;
}
var C = {};
/**
* @license React
* react-jsx-runtime.development.js
*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
var ve;
function Oe() {
return ve || (ve = 1, process.env.NODE_ENV !== "production" && function() {
function i(e) {
if (e == null) return null;
if (typeof e == "function")
return e.$$typeof === Te ? null : e.displayName || e.name || null;
if (typeof e == "string") return e;
switch (e) {
case P:
return "Fragment";
case we:
return "Portal";
case Z:
return "Profiler";
case L:
return "StrictMode";
case M:
return "Suspense";
case W:
return "SuspenseList";
}
if (typeof e == "object")
switch (typeof e.tag == "number" && console.error(
"Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."
), e.$$typeof) {
case Q:
return (e.displayName || "Context") + ".Provider";
case I:
return (e._context.displayName || "Context") + ".Consumer";
case Y:
var r = e.render;
return e = e.displayName, e || (e = r.displayName || r.name || "", e = e !== "" ? "ForwardRef(" + e + ")" : "ForwardRef"), e;
case $:
return r = e.displayName || null, r !== null ? r : i(e.type) || "Memo";
case U:
r = e._payload, e = e._init;
try {
return i(e(r));
} catch {
}
}
return null;
}
function s(e) {
return "" + e;
}
function a(e) {
try {
s(e);
var r = !1;
} catch {
r = !0;
}
if (r) {
r = console;
var t = r.error, n = typeof Symbol == "function" && Symbol.toStringTag && e[Symbol.toStringTag] || e.constructor.name || "Object";
return t.call(
r,
"The provided key is an unsupported type %s. This value must be coerced to a string before using it here.",
n
), s(e);
}
}
function v() {
}
function c() {
if (x === 0) {
ee = console.log, re = console.info, te = console.warn, oe = console.error, ne = console.group, ae = console.groupCollapsed, ue = console.groupEnd;
var e = {
configurable: !0,
enumerable: !0,
value: v,
writable: !0
};
Object.defineProperties(console, {
info: e,
log: e,
warn: e,
error: e,
group: e,
groupCollapsed: e,
groupEnd: e
});
}
x++;
}
function d() {
if (x--, x === 0) {
var e = { configurable: !0, enumerable: !0, writable: !0 };
Object.defineProperties(console, {
log: _({}, e, { value: ee }),
info: _({}, e, { value: re }),
warn: _({}, e, { value: te }),
error: _({}, e, { value: oe }),
group: _({}, e, { value: ne }),
groupCollapsed: _({}, e, { value: ae }),
groupEnd: _({}, e, { value: ue })
});
}
0 > x && console.error(
"disabledDepth fell below zero. This is a bug in React. Please file an issue."
);
}
function E(e) {
if (q === void 0)
try {
throw Error();
} catch (t) {
var r = t.stack.trim().match(/\n( *(at )?)/);
q = r && r[1] || "", se = -1 < t.stack.indexOf(`
at`) ? " (<anonymous>)" : -1 < t.stack.indexOf("@") ? "@unknown:0:0" : "";
}
return `
` + q + e + se;
}
function h(e, r) {
if (!e || z) return "";
var t = V.get(e);
if (t !== void 0) return t;
z = !0, t = Error.prepareStackTrace, Error.prepareStackTrace = void 0;
var n = null;
n = p.H, p.H = null, c();
try {
var l = {
DetermineComponentFrameRoot: function() {
try {
if (r) {
var g = function() {
throw Error();
};
if (Object.defineProperty(g.prototype, "props", {
set: function() {
throw Error();
}
}), typeof Reflect == "object" && Reflect.construct) {
try {
Reflect.construct(g, []);
} catch (m) {
var j = m;
}
Reflect.construct(e, [], g);
} else {
try {
g.call();
} catch (m) {
j = m;
}
e.call(g.prototype);
}
} else {
try {
throw Error();
} catch (m) {
j = m;
}
(g = e()) && typeof g.catch == "function" && g.catch(function() {
});
}
} catch (m) {
if (m && j && typeof m.stack == "string")
return [m.stack, j.stack];
}
return [null, null];
}
};
l.DetermineComponentFrameRoot.displayName = "DetermineComponentFrameRoot";
var u = Object.getOwnPropertyDescriptor(
l.DetermineComponentFrameRoot,
"name"
);
u && u.configurable && Object.defineProperty(
l.DetermineComponentFrameRoot,
"name",
{ value: "DetermineComponentFrameRoot" }
);
var o = l.DetermineComponentFrameRoot(), b = o[0], w = o[1];
if (b && w) {
var f = b.split(`
`), y = w.split(`
`);
for (o = u = 0; u < f.length && !f[u].includes(
"DetermineComponentFrameRoot"
); )
u++;
for (; o < y.length && !y[o].includes(
"DetermineComponentFrameRoot"
); )
o++;
if (u === f.length || o === y.length)
for (u = f.length - 1, o = y.length - 1; 1 <= u && 0 <= o && f[u] !== y[o]; )
o--;
for (; 1 <= u && 0 <= o; u--, o--)
if (f[u] !== y[o]) {
if (u !== 1 || o !== 1)
do
if (u--, o--, 0 > o || f[u] !== y[o]) {
var T = `
` + f[u].replace(
" at new ",
" at "
);
return e.displayName && T.includes("<anonymous>") && (T = T.replace("<anonymous>", e.displayName)), typeof e == "function" && V.set(e, T), T;
}
while (1 <= u && 0 <= o);
break;
}
}
} finally {
z = !1, p.H = n, d(), Error.prepareStackTrace = t;
}
return f = (f = e ? e.displayName || e.name : "") ? E(f) : "", typeof e == "function" && V.set(e, f), f;
}
function O(e) {
if (e == null) return "";
if (typeof e == "function") {
var r = e.prototype;
return h(
e,
!(!r || !r.isReactComponent)
);
}
if (typeof e == "string") return E(e);
switch (e) {
case M:
return E("Suspense");
case W:
return E("SuspenseList");
}
if (typeof e == "object")
switch (e.$$typeof) {
case Y:
return e = h(e.render, !1), e;
case $:
return O(e.type);
case U:
r = e._payload, e = e._init;
try {
return O(e(r));
} catch {
}
}
return "";
}
function A() {
var e = p.A;
return e === null ? null : e.getOwner();
}
function me(e) {
if (D.call(e, "key")) {
var r = Object.getOwnPropertyDescriptor(e, "key").get;
if (r && r.isReactWarning) return !1;
}
return e.key !== void 0;
}
function ge(e, r) {
function t() {
ce || (ce = !0, console.error(
"%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://react.dev/link/special-props)",
r
));
}
t.isReactWarning = !0, Object.defineProperty(e, "key", {
get: t,
configurable: !0
});
}
function he() {
var e = i(this.type);
return ie[e] || (ie[e] = !0, console.error(
"Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release."
)), e = this.props.ref, e !== void 0 ? e : null;
}
function pe(e, r, t, n, l, u) {
return t = u.ref, e = {
$$typeof: N,
type: e,
key: r,
props: u,
_owner: l
}, (t !== void 0 ? t : null) !== null ? Object.defineProperty(e, "ref", {
enumerable: !1,
get: he
}) : Object.defineProperty(e, "ref", { enumerable: !1, value: null }), e._store = {}, Object.defineProperty(e._store, "validated", {
configurable: !1,
enumerable: !1,
writable: !0,
value: 0
}), Object.defineProperty(e, "_debugInfo", {
configurable: !1,
enumerable: !1,
writable: !0,
value: null
}), Object.freeze && (Object.freeze(e.props), Object.freeze(e)), e;
}
function X(e, r, t, n, l, u) {
if (typeof e == "string" || typeof e == "function" || e === P || e === Z || e === L || e === M || e === W || e === xe || typeof e == "object" && e !== null && (e.$$typeof === U || e.$$typeof === $ || e.$$typeof === Q || e.$$typeof === I || e.$$typeof === Y || e.$$typeof === Re || e.getModuleId !== void 0)) {
var o = r.children;
if (o !== void 0)
if (n)
if (J(o)) {
for (n = 0; n < o.length; n++)
B(o[n], e);
Object.freeze && Object.freeze(o);
} else
console.error(
"React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead."
);
else B(o, e);
} else
o = "", (e === void 0 || typeof e == "object" && e !== null && Object.keys(e).length === 0) && (o += " You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports."), e === null ? n = "null" : J(e) ? n = "array" : e !== void 0 && e.$$typeof === N ? (n = "<" + (i(e.type) || "Unknown") + " />", o = " Did you accidentally export a JSX literal instead of a component?") : n = typeof e, console.error(
"React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: %s.%s",
n,
o
);
if (D.call(r, "key")) {
o = i(e);
var b = Object.keys(r).filter(function(f) {
return f !== "key";
});
n = 0 < b.length ? "{key: someKey, " + b.join(": ..., ") + ": ...}" : "{key: someKey}", le[o + n] || (b = 0 < b.length ? "{" + b.join(": ..., ") + ": ...}" : "{}", console.error(
`A props object containing a "key" prop is being spread into JSX:
let props = %s;
<%s {...props} />
React keys must be passed directly to JSX without using spread:
let props = %s;
<%s key={someKey} {...props} />`,
n,
o,
b,
o
), le[o + n] = !0);
}
if (o = null, t !== void 0 && (a(t), o = "" + t), me(r) && (a(r.key), o = "" + r.key), "key" in r) {
t = {};
for (var w in r)
w !== "key" && (t[w] = r[w]);
} else t = r;
return o && ge(
t,
typeof e == "function" ? e.displayName || e.name || "Unknown" : e
), pe(e, o, u, l, A(), t);
}
function B(e, r) {
if (typeof e == "object" && e && e.$$typeof !== Ce) {
if (J(e))
for (var t = 0; t < e.length; t++) {
var n = e[t];
S(n) && F(n, r);
}
else if (S(e))
e._store && (e._store.validated = 1);
else if (e === null || typeof e != "object" ? t = null : (t = K && e[K] || e["@@iterator"], t = typeof t == "function" ? t : null), typeof t == "function" && t !== e.entries && (t = t.call(e), t !== e))
for (; !(e = t.next()).done; )
S(e.value) && F(e.value, r);
}
}
function S(e) {
return typeof e == "object" && e !== null && e.$$typeof === N;
}
function F(e, r) {
if (e._store && !e._store.validated && e.key == null && (e._store.validated = 1, r = _e(r), !fe[r])) {
fe[r] = !0;
var t = "";
e && e._owner != null && e._owner !== A() && (t = null, typeof e._owner.tag == "number" ? t = i(e._owner.type) : typeof e._owner.name == "string" && (t = e._owner.name), t = " It was passed a child from " + t + ".");
var n = p.getCurrentStack;
p.getCurrentStack = function() {
var l = O(e.type);
return n && (l += n() || ""), l;
}, console.error(
'Each child in a list should have a unique "key" prop.%s%s See https://react.dev/link/warning-keys for more information.',
r,
t
), p.getCurrentStack = n;
}
}
function _e(e) {
var r = "", t = A();
return t && (t = i(t.type)) && (r = `
Check the render method of \`` + t + "`."), r || (e = i(e)) && (r = `
Check the top-level render call using <` + e + ">."), r;
}
var ye = G, N = Symbol.for("react.transitional.element"), we = Symbol.for("react.portal"), P = Symbol.for("react.fragment"), L = Symbol.for("react.strict_mode"), Z = Symbol.for("react.profiler"), I = Symbol.for("react.consumer"), Q = Symbol.for("react.context"), Y = Symbol.for("react.forward_ref"), M = Symbol.for("react.suspense"), W = Symbol.for("react.suspense_list"), $ = Symbol.for("react.memo"), U = Symbol.for("react.lazy"), xe = Symbol.for("react.offscreen"), K = Symbol.iterator, Te = Symbol.for("react.client.reference"), p = ye.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, D = Object.prototype.hasOwnProperty, _ = Object.assign, Re = Symbol.for("react.client.reference"), J = Array.isArray, x = 0, ee, re, te, oe, ne, ae, ue;
v.__reactDisabledLog = !0;
var q, se, z = !1, V = new (typeof WeakMap == "function" ? WeakMap : Map)(), Ce = Symbol.for("react.client.reference"), ce, ie = {}, le = {}, fe = {};
C.Fragment = P, C.jsx = function(e, r, t, n, l) {
return X(e, r, t, !1, n, l);
}, C.jsxs = function(e, r, t, n, l) {
return X(e, r, t, !0, n, l);
};
}()), C;
}
var Ee;
function Ae() {
return Ee || (Ee = 1, process.env.NODE_ENV === "production" ? k.exports = ke() : k.exports = Oe()), k.exports;
}
var Se = Ae();
const be = G.createContext({ policy: new H(null, null) }), Ne = () => G.useContext(be);
function Pe({
policy: i,
user: s,
record: a,
children: v
}) {
const c = je(
() => ({ policy: i.copy(s, a) }),
[i, s, a]
);
return /* @__PURE__ */ Se.jsx(be.Provider, { value: c, children: v });
}
Pe.defaultProps = {
user: void 0,
record: void 0
};
function Ye({
children: i,
can: s,
policy: a,
user: v,
record: c
}) {
const { policy: d } = Ne(), E = a == null ? void 0 : a.copy(v, c);
return (E ? E.can(s) : d.can(s)) ? i : null;
}
Ye.defaultProps = {
policy: void 0,
user: void 0,
record: void 0
};
export {
H as Policy,
Pe as PunditProvider,
Ye as When,
Ne as usePundit
};