vue-nuxt-permission
Version:
A lightweight, flexible permission directive and router guard for Vue 3
128 lines (127 loc) • 4.5 kB
JavaScript
import { isRef as N } from "vue";
const P = /* @__PURE__ */ new Map(), W = (s) => P.has(s) ? P.get(s) : null, k = (s, e) => P.set(s, e), E = () => P.clear(), I = "__v_permission__", F = (s) => btoa(JSON.stringify(s)), J = (s) => JSON.parse(atob(s)), U = (s) => {
try {
localStorage.setItem(I, F(s));
} catch (e) {
console.warn("[v-permission] Failed to save permissions to storage:", e);
}
}, G = () => {
try {
const s = localStorage.getItem(I);
return s ? J(s) : null;
} catch (s) {
return console.warn("[v-permission] Failed to read permissions from storage:", s), null;
}
};
let M = { permissions: null }, A = !1;
const q = (s, e) => {
M.permissions = s, A = (e == null ? void 0 : e.developmentMode) ?? !1, E(), U(s);
}, K = () => {
const s = G();
s && q(s);
}, T = () => {
const { permissions: s } = M;
return N(s) ? s.value : s ?? [];
}, p = async (s, e) => {
const i = e ?? T(), o = JSON.stringify({ permissionValue: s, currentPermissions: i }), a = W(o);
if (a !== null) return a;
const m = await (async (n) => {
var h;
if (n === "*") return !0;
if (typeof n == "string") return i.includes(n);
if (Array.isArray(n))
return n.includes("*") ? !0 : (await Promise.all(
n.map((r) => p(r, i))
)).some(Boolean);
if (typeof n == "object" && n.permissions && n.mode) {
const { permissions: r, mode: v } = n;
if (r.includes("*")) return !0;
const d = {
and: () => r.every((t) => i.includes(t)),
or: () => r.some((t) => i.includes(t)),
startWith: () => r.some(
(t) => i.some((c) => c.startsWith(t))
),
endWith: () => r.some(
(t) => i.some((c) => c.endsWith(t))
),
exact: () => r.some((t) => i.includes(t)),
regex: () => r.some((t) => {
try {
const c = new RegExp(t);
return i.some((w) => c.test(w));
} catch (c) {
return A && console.warn("[v-permission] Invalid regex:", t, c), !1;
}
})
};
return ((h = d[v]) == null ? void 0 : h.call(d)) ?? !1;
}
return A && console.warn("[v-permission] Invalid permission value:", n), !1;
})(s);
return k(o, m), m;
}, j = {
async mounted(s, e) {
const { value: i, modifiers: o, arg: a } = e, u = await p(i);
s._vPermissionOriginalDisplay = s.style.display, u || (a === "show" || o.show ? s.style.display = "none" : s.remove()), o.once && (s._vPermissionSkipUpdates = !0);
},
async updated(s, e) {
const { value: i, oldValue: o, modifiers: a, arg: u } = e;
if (s._vPermissionSkipUpdates || a.lazy && i === o) return;
await p(i) ? s.style.display = s._vPermissionOriginalDisplay || "" : u === "show" || a.show ? s.style.display = "none" : s.remove();
},
unmounted(s) {
s._vPermissionOriginalDisplay = void 0, s._vPermissionSkipUpdates = void 0;
}
}, z = {
install(s, e) {
e != null && e.permissions ? q(e.permissions, {
developmentMode: e.developmentMode
}) : K(), s.directive("permission", j);
}
};
async function B(s, e, i, o = {}) {
var O, R;
const {
authRoutes: a = [],
protectedRoutes: u = [],
getAuthState: m,
loginPath: n = "/login",
homePath: h = "/"
} = o, r = (m == null ? void 0 : m()) ?? { isAuthenticated: !1 }, { isAuthenticated: v } = r, d = (r == null ? void 0 : r.permissions) || ((O = r == null ? void 0 : r.user) == null ? void 0 : O.permissions) || [], t = a.some((l) => l.path === s.path), c = (R = s.meta) == null ? void 0 : R.requiresAuth, w = async () => {
var y, f;
if (!((y = s.meta) != null && y.checkPermission)) return !0;
const l = (f = s.meta) == null ? void 0 : f.permissions;
return !l || l === "*" ? !0 : await p(l, d);
}, b = async (l, y = "") => {
var f, S;
for (const g of l) {
const C = y + g.path, _ = (f = g.meta) == null ? void 0 : f.permissions;
if (!_ || _ === "*" || await p(_, d))
return C;
if ((S = g.children) != null && S.length) {
const D = await b(g.children, C);
if (D) return D;
}
}
return null;
};
if (!v)
return c ? i(n) : i();
if (t) return i(h);
if (!await w()) {
const l = await b(u);
return i(l || n);
}
i();
}
export {
z as PermissionPlugin,
E as clearPermissionCache,
q as configurePermission,
T as getCurrentPermissions,
B as globalGuard,
p as hasPermission,
K as initPermissionDirectiveIfNeeded,
j as vPermission
};