vue-nuxt-permission
Version:
A unified permission system for Vue 3 and Nuxt 3 / Nuxt 4 with directives, guards, and async support.
3 lines (2 loc) • 7.38 kB
JavaScript
;/*! vue-nuxt-permission v2.2.1 | (c) 2026 Kerolos Zakaria | MIT License */Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("./plugin-BkYe8puG.cjs"),d=require("crypto-js"),j=require("vue");function G(r){if(!r||typeof r!="string")throw new Error("A crypto secret is required");const s=r.trim();return s.startsWith('"')&&s.endsWith('"')?s.slice(1,-1):s}function J(r){const s=d.enc.Utf8.parse(G(r));return s.sigBytes=32,s.words.length=8,s}function W(r){if(r&&typeof r=="object"){const s=r;if(typeof s.iv=="string"&&typeof s.data=="string")return s}if(typeof r=="string")try{return W(JSON.parse(r))}catch{return null}return null}function O(r){const s=J(r),u=o=>{const t=d.lib.WordArray.random(16),e=d.AES.encrypt(JSON.stringify(o),s,{iv:t,mode:d.mode.CBC,padding:d.pad.Pkcs7});return{iv:d.enc.Base64.stringify(t),data:d.enc.Base64.stringify(e.ciphertext)}},a=o=>{try{const t=W(o);if(!t)return null;const c=d.AES.decrypt({ciphertext:d.enc.Base64.parse(t.data)},s,{iv:d.enc.Base64.parse(t.iv),mode:d.mode.CBC,padding:d.pad.Pkcs7}).toString(d.enc.Utf8);return c?JSON.parse(c):null}catch{return null}};return{encrypt:u,decrypt:a,decryptAuthPayload:(o,t=["roles","permissions"])=>{const e={...o};for(const c of t){const n=o[c];Array.isArray(n)||(e[c]=a(n)??[])}return e}}}function _(r){const{decrypt:s}=O(r);return u=>{const a=s(u);return Array.isArray(a)?a:[]}}const T=50;function U(r,s,u="",a=0){var l;if(a>T)return null;for(const o of r){const t=u+o.path;if(t===s||o.path===s)return o;if((l=o.children)!=null&&l.length){const e=U(o.children,s,t,a+1);if(e)return e}}return null}const x=async(r,s,u,a={})=>{var m,y,A,S,b;const{authRoutes:l=[],protectedRoutes:o=[],getAuthState:t,loginPath:e="/login",homePath:c="/",onDenied:n,onAllowed:f}=a;try{const P=(t==null?void 0:t())??{isAuthenticated:!1},R=P.isAuthenticated,p=P.permissions??((m=P.user)==null?void 0:m.permissions)??i.getCurrentPermissions();Array.isArray(p)&&p.length>0&&i.getCurrentPermissions().length===0&&i.configurePermission(p);const v=l.some(C=>C.path===r.path),h=U(o,r.path),w=(((y=r.meta)==null?void 0:y.requiresAuth)??!1)||h!==null,g=((A=r.meta)==null?void 0:A.permissions)??((S=h==null?void 0:h.meta)==null?void 0:S.permissions),q=(((b=r.meta)==null?void 0:b.checkPermission)??!1)||h!==null&&g!==void 0;if(!R&&w)return n==null||n(r,s),u(e);if(R&&v)return f==null||f(r,s),u(c);if(q&&g!==void 0&&!await i.hasPermission(g,p)){n==null||n(r,s);const E=await B(o,p);return u(E||e)}f==null||f(r,s),u()}catch(P){console.error("[v-permission:globalGuard] Unexpected error:",P),u(e)}};async function B(r,s,u="",a=0){var l,o;if(a>T)return console.warn("[v-permission:globalGuard] Route nesting exceeds max depth"),null;for(const t of r){const e=u+t.path,c=((l=t.meta)==null?void 0:l.permissions)??"*";if(await i.hasPermission(c,s))return e;if((o=t.children)!=null&&o.length){const n=await B(t.children,s,e,a+1);if(n)return n}}return null}const D=50;function z(r,s){return!r||s.startsWith("/")?s:r.endsWith("/")?r+s:r+"/"+s}function F(r,s,u="",a=0){var l;if(a>D)return null;for(const o of r){const t=z(u,o.path);if(t===s)return o;if((l=o.children)!=null&&l.length){const e=F(o.children,s,t,a+1);if(e)return e}}return null}const N=(r={})=>{const{authRoutes:s=[],protectedRoutes:u=[],getAuthState:a,loginPath:l="/login",homePath:o="/",onDenied:t,onAllowed:e}=r;return async(c,n,f)=>{var m,y,A,S,b;try{const P=(a==null?void 0:a())??{isAuthenticated:!1},R=P.isAuthenticated,p=r.decrypt??r.transform??i.getDecryptHook(),v=P.permissions??((m=P.user)==null?void 0:m.permissions)??i.getCurrentPermissions();let h=[];if(p&&v&&!Array.isArray(v)){const k=await p(v);h=Array.isArray(k)?k:[]}else Array.isArray(v)?h=v:h=i.getCurrentPermissions();Array.isArray(h)&&h.length>0&&i.getCurrentPermissions().length===0&&i.configurePermission(h,{decrypt:p});const w=s.some(k=>k.path===c.path),g=F(u,c.path),q=(((y=c.meta)==null?void 0:y.requiresAuth)??!1)||g!==null,C=((A=c.meta)==null?void 0:A.permissions)??((S=g==null?void 0:g.meta)==null?void 0:S.permissions),E=(((b=c.meta)==null?void 0:b.checkPermission)??!1)||g!==null&&C!==void 0;if(!R&&q)return t==null||t(c,n),f(l);if(R&&w)return e==null||e(c,n),f(o);if(E&&C!==void 0&&!await i.hasPermission(C,h)){t==null||t(c,n);const M=await H(u,h);return f(M||l)}e==null||e(c,n),f()}catch(P){console.error("[v-permission:guard] Unexpected error:",P),f(l)}}};async function H(r,s,u="",a=0){var l,o;if(a>D)return console.warn("[v-permission:guard] Route nesting exceeds max depth"),null;for(const t of r){const e=u+t.path,c=((l=t.meta)==null?void 0:l.permissions)??"*";if(await i.hasPermission(c,s))return e;if((o=t.children)!=null&&o.length){const n=await H(t.children,s,e,a+1);if(n)return n}}return null}function $(){const r=i.getReactivePermissions();if((!r.value||r.value.length===0)&&i.getCurrentPermissions().length===0){const e=i.getPermissionsFromStorage();Array.isArray(e)&&e.length>0&&i.configurePermission(e,{persist:!1})}const s=async e=>await i.hasPermission(e,r.value),u=e=>{const c=r.value;if(e==="*")return!0;if(typeof e=="string")return c.includes(e);if(Array.isArray(e))return e.includes("*")?!0:e.some(n=>c.includes(n));if(typeof e=="object"&&e.permissions&&e.mode){const{permissions:n,mode:f}=e;if(!Array.isArray(n)||n.length===0)return!1;if(n.includes("*"))return!0;if(f==="and")return n.every(m=>c.includes(m));if(f==="or")return n.some(m=>c.includes(m));if(f==="not")return!n.some(m=>c.includes(m));if(f==="startWith")return n.some(m=>c.some(y=>y.startsWith(m)));if(f==="endWith")return n.some(m=>c.some(y=>y.endsWith(m)));if(f==="regex")return n.some(m=>{try{const y=new RegExp(m);return c.some(A=>y.test(A))}catch{return!1}})}return!1},a=()=>{i.clearPermissionCache();const e=i.getPermissionsFromStorage();Array.isArray(e)&&e.length>0&&i.configurePermission(e,{persist:!1})},l=e=>{if(!Array.isArray(e)){console.error("[v-permission] Permissions must be an array");return}i.clearPermissionCache(),i.configurePermission(e),i.savePermissionsToStorage(e)},o=async e=>!Array.isArray(e)||e.length===0?!0:await s({permissions:e,mode:"and"}),t=async e=>!Array.isArray(e)||e.length===0?!1:await s({permissions:e,mode:"or"});return{permissions:j.computed(()=>r.value),can:s,hasPermission:s,canSync:u,refresh:a,setPermissions:l,hasAll:o,hasAny:t}}exports.PermissionPlugin=i.PermissionPlugin;exports.clearPermissionCache=i.clearPermissionCache;exports.clearPermissionsFromStorage=i.clearPermissionsFromStorage;exports.configurePermission=i.configurePermission;exports.getCachedPermission=i.getCachedPermission;exports.getCurrentPermissions=i.getCurrentPermissions;exports.getDecryptHook=i.getDecryptHook;exports.getPermissionsFromStorage=i.getPermissionsFromStorage;exports.hasPermission=i.hasPermission;exports.invalidateCache=i.invalidateCache;exports.isDevMode=i.isDevMode;exports.isPermissionObject=i.isPermissionObject;exports.logDebug=i.logDebug;exports.normalizePermissions=i.normalizePermissions;exports.savePermissionsToStorage=i.savePermissionsToStorage;exports.setCachedPermission=i.setCachedPermission;exports.setDecryptHook=i.setDecryptHook;exports.sortUniq=i.sortUniq;exports.stableStringify=i.stableStringify;exports.vPermission=i.vPermission;exports.createPermissionCrypto=O;exports.createPermissionDecryptor=_;exports.createPermissionGuard=N;exports.globalGuard=x;exports.usePermission=$;
//# sourceMappingURL=index.cjs.map