@agendize/vue-acl
Version:
Vue agendize's ACL management
382 lines (381 loc) • 15.3 kB
JavaScript
import { reactive as H } from "vue";
class y {
static rightToAbilities(e) {
const i = [];
return e && (e.scheduling && i.push(...this.getSchedulingAbilities(e.scheduling)), e.settings && i.push(...this.getSettingsAbilities(e.settings)), e.report && i.push(...this.getReportAbilities(e.report)), e.queue && i.push(...this.getQueueAbilities(e.queue)), e.form && i.push(...this.getFormAbilities(e.form)), e.client && i.push(...this.getClientAbilities(e.client)), e.workflow && i.push(...this.getWorkflowAbilities(e.workflow)), e.planning && i.push(...this.getPlanningAbilities(e.planning)), e.workingPlanning && i.push(...this.getWorkingPlanningAbilities(e.workingPlanning)), e.conference && i.push(...this.getConferenceAbilities(e.conference)), e.dashboard && i.push(...this.getDashboardAbilities(e.dashboard)), e.whiteLabel && i.push(...this.getWhiteLabelAbilities(e.whiteLabel))), i;
}
static getSchedulingAbilities(e) {
const i = [];
return e.appointment && i.push(...this.rightLettersToAbilities("appointment", e.appointment)), e.staffAppointment && i.push(...this.rightLettersToAbilities("staffAppointment", e.staffAppointment)), i;
}
static getSettingsAbilities(e) {
const i = [];
return e.company && i.push(...this.rightLettersToAbilities("company", e.company)), e.service && i.push(...this.rightLettersToAbilities("service", e.service)), e.staff && i.push(...this.rightLettersToAbilities("staff", e.staff)), i;
}
static getReportAbilities(e) {
const i = [];
return e.report && i.push(...this.rightLettersToAbilities("report", e.report)), i;
}
static getQueueAbilities(e) {
const i = [];
return e.queue && i.push(...this.rightLettersToAbilities("queue", e.queue)), i;
}
static getFormAbilities(e) {
const i = [];
return e.form && i.push(...this.rightLettersToAbilities("form", e.form)), i;
}
static getClientAbilities(e) {
const i = [];
return e.client && i.push(...this.rightLettersToAbilities("client", e.client)), i;
}
static getWorkflowAbilities(e) {
const i = [];
return e.workflow && i.push(...this.rightLettersToAbilities("workflow", e.workflow)), i;
}
static getPlanningAbilities(e) {
const i = [];
return e.planning && i.push(...this.rightLettersToAbilities("planning", e.planning)), i;
}
static getWorkingPlanningAbilities(e) {
const i = [];
return e.workingPlanning && i.push(...this.rightLettersToAbilities("workingPlanning", e.workingPlanning)), i;
}
static getConferenceAbilities(e) {
const i = [];
return e.conference && i.push(...this.rightLettersToAbilities("conference", e.conference)), e.forms && i.push(...this.rightLettersToAbilities("conferenceForms", e.forms)), e.registrations && i.push(...this.rightLettersToAbilities("conferenceRegistrations", e.registrations)), e.sessions && i.push(...this.rightLettersToAbilities("conferenceSessions", e.sessions)), e.speakers && i.push(...this.rightLettersToAbilities("conferenceSpeakers", e.speakers)), e.tags && i.push(...this.rightLettersToAbilities("conferenceTags", e.tags)), i;
}
static getDashboardAbilities(e) {
const i = [];
return e.dashboard && i.push(...this.rightLettersToAbilities("dashboard", e.dashboard)), i;
}
static getWhiteLabelAbilities(e) {
const i = [];
return e.name && i.push(...this.rightLettersToAbilities("whiteLabelName", e.name)), e.logo && i.push(...this.rightLettersToAbilities("whiteLabelLogo", e.logo)), e.favicon && i.push(...this.rightLettersToAbilities("whiteLabelFavicon", e.favicon)), i;
}
static rightLettersToAbilities(e, i) {
const n = [];
return i.indexOf("r") > -1 && n.push("read" + e.charAt(0).toUpperCase() + e.slice(1)), i.indexOf("w") > -1 && n.push("create" + e.charAt(0).toUpperCase() + e.slice(1)), i.indexOf("u") > -1 && n.push("update" + e.charAt(0).toUpperCase() + e.slice(1)), i.indexOf("d") > -1 && n.push("delete" + e.charAt(0).toUpperCase() + e.slice(1)), (e === "appointment" || e === "staffAppointment") && (i.indexOf("a") > -1 && n.push("accept" + e.charAt(0).toUpperCase() + e.slice(1)), i.indexOf("c") > -1 && n.push("statusChange" + e.charAt(0).toUpperCase() + e.slice(1)), i.indexOf("f") > -1 && n.push("freemode" + e.charAt(0).toUpperCase() + e.slice(1)), i.indexOf("e") > -1 && n.push("event" + e.charAt(0).toUpperCase() + e.slice(1))), n;
}
}
const r = H({
options: {}
}), U = (t, e) => {
r.options = e;
function i(A, o, u, p) {
switch (A) {
case "can":
f(o, u);
break;
case "role":
h(o, u);
break;
}
}
function n(A, o, u, p, s, c) {
A ? u ? o.style.display = "none" : o.style.display = "" : u ? o.style.display = "" : p ? o.disabled = !0 : s ? o.readOnly = !0 : o.style.display = "none";
}
const f = (A, o, u) => {
const p = o.arg, s = o.value, c = o.modifiers, a = !!c.any, l = !!c.not, m = !!c.readonly, g = !!(c.disable || c.disabled), C = !!(c.hide || c.hidden), N = D({ ability: p, args: s, any: a });
n(N, A, l, g, m, C);
}, h = (A, o, u) => {
const p = o.arg, s = o.modifiers, c = !!s.any, a = !!s.not, l = !!s.readonly, m = !!(s.disable || s.disabled), g = !!(s.hide || s.hidden), C = I({ roles: [p], any: c });
n(C, A, a, m, l, g);
}, d = (A, o) => {
A.directive(`${o}`, {
mounted(u, p, s) {
i(o, u, p);
},
updated(u, p, s) {
i(o, u, p);
}
});
};
if (t && (d(t, "can"), d(t, "role")), e.router) {
let A = function(s, c) {
let a;
if (s.meta.can) {
const l = s.meta.can;
typeof l == "function" ? a = l(s, c, S) : a = S(l);
} else if (s.meta.canNot) {
const l = s.meta.canNot;
typeof l == "function" ? (q(l), a = l(s, c, k)) : a = k(l);
} else if (s.meta.canAny) {
const l = s.meta.canAny;
typeof l == "function" ? a = l(s, c, b) : a = b(l);
} else if (s.meta.canNone) {
const l = s.meta.canNone;
typeof l == "function" ? a = !l(s, c, b) : a = !b(l);
}
return a;
}, o = function(s) {
let c;
if (s.meta.role) {
const a = s.meta.role;
c = O(a);
} else if (s.meta.roleNot) {
const a = s.meta.roleNot;
c = P(a);
} else if (s.meta.roleAny) {
const a = s.meta.roleAny;
c = M(a);
}
return c;
};
const u = (s, c, a, l) => {
if (l)
a();
else {
let m = e.onDeniedRoute;
s.meta && s.meta.onDeniedRoute && (m = s.meta.onDeniedRoute), a(typeof m == "object" ? m : m === "$from" ? c : { path: `${m}`, replace: !0 });
}
}, p = (s, c, a) => {
if (!s.meta) {
a();
return;
}
let l = A(s, c);
if (l === !1) {
u(s, c, a, l);
return;
}
l = o(s), l === void 0 ? a() : u(s, c, a, l);
};
r.options.router.beforeEach((s, c, a) => {
W().then(() => {
p(s, c, a);
});
});
}
};
function W() {
return new Promise(t);
function t(e, i) {
v ? e() : setTimeout(function() {
t(e);
}, 30);
}
}
const K = (t) => ({
install: (e, i = {}) => {
U(e, t);
}
});
let v = !1;
function R(t) {
var i;
return (((i = t.rightIds) == null ? void 0 : i.map((n) => E(n))) ?? []).filter((n) => !!n);
}
function E(t) {
let e = t;
switch (t.includes("-") && (e = t.split("-")[0]), e) {
case "0":
return "owner";
case "1":
return "accountAdmin";
case "2":
return "accountAnalytics";
case "3":
return "chatAdmin";
case "4":
return "chatOperator";
case "5":
return "schedulingCompanyAdmin";
case "6":
return "schedulingCompanyScheduler";
case "7":
return "testimnonialAdmin";
case "8":
return "testimonialModerator";
case "9":
return "accountBilling";
case "10":
return "buttonAccountManager";
case "11":
return "accountEmail";
case "12":
return "accountCalls";
case "13":
return "accountForms";
case "14":
return "accountCRM";
case "15":
return "schedulingAccountAdmin";
case "16":
return "schedulingAccountScheduler";
case "17":
return "schedulingAccountReader";
case "18":
return "accountQueue";
case "19":
return "schedulingCompanyReader";
case "20":
return "accountDataOfficer";
case "21":
return "accountWorkflow";
case "22:":
return "accountWorkingPlanning";
case "23":
return "schedulingCompanyTeamManager";
case "24":
return "accountConferenceAdmin";
case "26":
return "accountConferenceScheduler";
case "27":
return "accountConferenceReader";
case "28":
return "conferenceAdmin";
case "29":
return "conferenceScheduler";
case "30":
return "conferenceReader";
}
}
const V = () => {
const t = {};
return t.can = S, t.can.not = k, t.can.any = b, t.role = O, t.role.not = P, t.role.any = M, t.init = async (e, i) => {
var n, f;
return r.organisationEmail = e.organisation, r.userId = i, r.rolesType = R(e), (n = r.options.logger) == null || n.info("vue-acl", "User roles", r.rolesType), r.abilities = [], r.companiesAbilities = /* @__PURE__ */ new Map(), e && e.rightIds && e.rightIds.length > 0 && (await r.options.api.getRights(e.organisation, e.rightIds)).forEach((d) => {
var A, o, u;
if (d.object && d.object.type) {
if (d.object.type === "company") {
let p = (A = r.companiesAbilities) == null ? void 0 : A.get(d.object.id);
p ? p.push(...y.rightToAbilities(d)) : p = y.rightToAbilities(d), (o = r.companiesAbilities) == null || o.set(d.object.id, p);
}
} else
(u = r.abilities) == null || u.push(...y.rightToAbilities(d));
}), Z(), (f = r.options.logger) == null || f.info("vue-acl", "User abilities", r.abilities, r.companiesAbilities), v = !0, Promise.resolve();
}, t.initStaffIds = (e) => {
r.staffIds = e;
}, t.ready = () => v, H(t);
}, S = (t, e, i) => w({ ability: t, args: e, args2: i, any: !1 }), k = (t, e, i) => !w({ ability: t, args: e, args2: i, any: !1 }), b = (t, e, i) => w({ ability: t, args: e, args2: i, any: !0 }), O = (t) => T({ roles: t, any: !1 }), P = (t) => T({ roles: t, any: !1 }), M = (t) => T({ roles: t, any: !0 }), w = ({
ability: t,
args: e,
args2: i,
any: n = !1
}) => {
var f;
return t ? D({
ability: t,
args: e,
args2: i,
any: n
}) : ((f = r.options.logger) == null || f.warn("vue-acl", "Invalid ACL arguments specified: ", t, e, i, n), !1);
}, T = ({ roles: t, any: e }) => {
var i;
return t ? I({
roles: t,
any: e
}) : ((i = r.options.logger) == null || i.warn("vue-acl", "Invalid ACL arguments specified: ", t), !1);
}, D = ({ ability: t, args: e, args2: i, any: n = !1 }) => {
const f = t, h = e;
return F(h ? { ability: f, args: h, args2: i, any: n } : { ability: f, any: n });
}, I = ({ roles: t, any: e }) => x({ roles: t, any: e }), F = ({ ability: t, args: e, args2: i, any: n = !1 }) => {
if (t) {
if (n && Array.isArray(t)) {
for (const f in t)
if (L(t[f], e, i))
return !0;
return !1;
} else if (typeof t == "string")
return L(t, e, i);
}
return Array.isArray(e) && n ? _(Object.values(e)) : !1;
}, x = ({ roles: t, any: e }) => t ? Q(t, e) : !1, _ = (t) => {
let e = !1;
for (const i of t) {
const [n, f] = i;
if (f) {
const h = L(n, f);
if (h) {
e = h;
break;
}
} else if (n === !0) {
e = !0;
break;
}
}
return e;
}, q = (t) => {
let e = null;
const i = /\(([a-z0-9 ,$_+*\-/]+)\)/im, f = t.toString().match(i);
if (f && Array.isArray(f) && f[0]) {
let h = f[0];
h = h.replace(/[() ]/g, "");
const d = h.split(",");
d.length > 0 && (e = d);
}
return e;
}, L = (t, e, i) => {
var f, h, d, A, o;
const n = (f = r.abilities) == null ? void 0 : f.includes(t);
if (n)
return n;
if (e) {
if (typeof e == "string") {
const u = B(t);
if (u === "company" || u === "conference") {
const p = (h = r.companiesAbilities) == null ? void 0 : h.get(e);
if (p)
return (t === "updateStaffAppointment" || t === "acceptStaffAppointment" || t === "readStaffAppointment" || t === "deleteStaffAppointment" || t === "statusChangeStaffAppointment") && p.includes(t) && i && typeof i == "string" ? ((d = r.staffIds) == null ? void 0 : d.includes(i)) ?? !1 : p.includes(t);
}
} else if (e.type === "company" || e.type === "conference") {
const u = (A = r.companiesAbilities) == null ? void 0 : A.get(e.id);
if (u)
return u.includes(t);
}
} else {
let u = !1;
return (o = r.companiesAbilities) == null || o.forEach((p, s) => {
p.includes(t) && (u = !0);
}), u;
}
return !1;
}, Q = (t, e = !1) => e ? t.find(
(i) => {
var n;
return (n = r.rolesType) == null ? void 0 : n.includes(i);
}
) != null : t.filter(
(i) => {
var n;
return (n = r.rolesType) == null ? void 0 : n.includes(i);
}
).length === t.length, $ = (t) => {
const i = /[A-Z]/g.exec(t);
if (i)
return t.substring(i.index);
}, B = (t) => {
const e = $(t);
if (e) {
if (e === "Company" || e === "Service" || e === "Staff" || e === "Resource" || e === "Client" || e === "Appointment" || e === "StaffAppointment" || e === "StaffCustomPeriod" || e === "ChangeAppointment" || e === "ChangeStaffAppointment" || e === "WorkingPlanning")
return "company";
if (e === "Form" || e === "FormResult")
return "form";
if (e === "Queue")
return "queue";
if (e === "Planning")
return "planning";
if (e === "Conference" || e === "ConferenceForms" || e === "ConferenceRegistrations" || e === "ConferenceSessions" || e === "ConferenceSpeakers" || e === "ConferenceTags")
return "conference";
if (e === "WhiteLabelName" || e === "WhiteLabelLogo" || e === "WhiteLabelFavicon")
return "whiteLabel";
}
};
function z(t) {
return t.includes("readDashboard") || t.includes("readAppointment") || t.includes("readStaffAppointment") ? !1 : G.filter(
(e) => t.includes(e)
).length > 1;
}
const Z = () => {
r.abilities && ((r.abilities.includes("readService") || r.abilities.includes("readStaff")) && !r.abilities.includes("readCompany") && r.abilities.push("readCompany"), z(r.abilities) && r.abilities.push("readDashboard"), (r.abilities.includes("readConferenceRegistrations") || r.abilities.includes("readConferenceSessions")) && !r.abilities.includes("readConference") && r.abilities.push("readConference")), r.companiesAbilities && r.companiesAbilities.size > 0 && [...r.companiesAbilities.keys()].forEach((e) => {
let i = r.companiesAbilities.get(e);
i !== void 0 && ((i.includes("readService") || i.includes("readStaff")) && !i.includes("readCompany") && i.push("readCompany"), (i.includes("readConferenceRegistrations") || i.includes("readConferenceSessions")) && !i.includes("readConference") && i.push("readConference"));
});
}, G = ["readForm", "readQueue", "readClient", "readReport", "readConference"];
export {
G as ABILITIES_FOR_OLD_DASHBOARD,
y as AbilityResolver,
K as createAcl,
V as useAcl
};