@agendize/vue-acl
Version:
Vue agendize's ACL management
413 lines (412 loc) • 15.2 kB
JavaScript
import { reactive as F } from "vue";
class C {
static rightToAbilities(e) {
const t = [];
return e && (e.scheduling && t.push(...this.getSchedulingAbilities(e.scheduling)), e.settings && t.push(...this.getSettingsAbilities(e.settings)), e.report && t.push(...this.getReportAbilities(e.report)), e.queue && t.push(...this.getQueueAbilities(e.queue)), e.form && t.push(...this.getFormAbilities(e.form)), e.client && t.push(...this.getClientAbilities(e.client)), e.workflow && t.push(...this.getWorkflowAbilities(e.workflow)), e.planning && t.push(...this.getPlanningAbilities(e.planning)), e.workingPlanning && t.push(...this.getWorkingPlanningAbilities(e.workingPlanning)), e.conference && t.push(...this.getConferenceAbilities(e.conference)), e.dashboard && t.push(...this.getDashboardAbilities(e.dashboard)), e.whiteLabel && t.push(...this.getWhiteLabelAbilities(e.whiteLabel))), t;
}
static getSchedulingAbilities(e) {
const t = [];
return e.appointment && t.push(...this.rightLettersToAbilities("appointment", e.appointment)), e.staffAppointment && t.push(...this.rightLettersToAbilities("staffAppointment", e.staffAppointment)), t;
}
static getSettingsAbilities(e) {
const t = [];
return e.company && t.push(...this.rightLettersToAbilities("company", e.company)), e.service && t.push(...this.rightLettersToAbilities("service", e.service)), e.staff && t.push(...this.rightLettersToAbilities("staff", e.staff)), t;
}
static getReportAbilities(e) {
const t = [];
return e.report && t.push(...this.rightLettersToAbilities("report", e.report)), t;
}
static getQueueAbilities(e) {
const t = [];
return e.queue && t.push(...this.rightLettersToAbilities("queue", e.queue)), t;
}
static getFormAbilities(e) {
const t = [];
return e.form && t.push(...this.rightLettersToAbilities("form", e.form)), t;
}
static getClientAbilities(e) {
const t = [];
return e.client && t.push(...this.rightLettersToAbilities("client", e.client)), t;
}
static getWorkflowAbilities(e) {
const t = [];
return e.workflow && t.push(...this.rightLettersToAbilities("workflow", e.workflow)), t;
}
static getPlanningAbilities(e) {
const t = [];
return e.planning && t.push(...this.rightLettersToAbilities("planning", e.planning)), t;
}
static getWorkingPlanningAbilities(e) {
const t = [];
return e.workingPlanning && t.push(...this.rightLettersToAbilities("workingPlanning", e.workingPlanning)), t;
}
static getConferenceAbilities(e) {
const t = [];
return e.conference && t.push(...this.rightLettersToAbilities("conference", e.conference)), e.forms && t.push(...this.rightLettersToAbilities("conferenceForms", e.forms)), e.registrations && t.push(...this.rightLettersToAbilities("conferenceRegistrations", e.registrations)), e.sessions && t.push(...this.rightLettersToAbilities("conferenceSessions", e.sessions)), e.speakers && t.push(...this.rightLettersToAbilities("conferenceSpeakers", e.speakers)), e.tags && t.push(...this.rightLettersToAbilities("conferenceTags", e.tags)), t;
}
static getDashboardAbilities(e) {
const t = [];
return e.dashboard && t.push(...this.rightLettersToAbilities("dashboard", e.dashboard)), t;
}
static getWhiteLabelAbilities(e) {
const t = [];
return e.name && t.push(...this.rightLettersToAbilities("whiteLabelName", e.name)), e.logo && t.push(...this.rightLettersToAbilities("whiteLabelLogo", e.logo)), e.favicon && t.push(...this.rightLettersToAbilities("whiteLabelFavicon", e.favicon)), t;
}
static rightLettersToAbilities(e, t) {
const n = [], s = e.charAt(0).toUpperCase() + e.slice(1);
return t.indexOf("r") > -1 && n.push("read" + s), t.indexOf("w") > -1 && n.push("create" + s), t.indexOf("u") > -1 && n.push("update" + s), t.indexOf("d") > -1 && n.push("delete" + s), t.indexOf("o") > -1 && (n.push("readUser" + s), n.push("createUser" + s), n.push("updateUser" + s), n.push("deleteUser" + s)), (e === "appointment" || e === "staffAppointment") && (t.indexOf("a") > -1 && n.push("accept" + s), t.indexOf("c") > -1 && n.push("statusChange" + s), t.indexOf("f") > -1 && n.push("freemode" + s), t.indexOf("e") > -1 && n.push("event" + s), t.indexOf("t") > -1 && n.push("addAttendee" + s)), n;
}
}
const o = F({
options: {}
}), U = (i, e) => {
o.options = e;
function t(p, l, d, b) {
switch (p) {
case "can":
s(l, d);
break;
case "role":
f(l, d);
break;
}
}
function n(p, l, d, b, r, c) {
p ? d ? l.style.display = "none" : l.style.display = "" : d ? l.style.display = "" : b ? l.disabled = !0 : r ? l.readOnly = !0 : l.style.display = "none";
}
const s = (p, l, d) => {
const b = l.arg, r = l.value, c = l.modifiers, a = !!c.any, u = !!c.not, m = !!c.readonly, g = !!(c.disable || c.disabled), y = !!(c.hide || c.hidden), W = I({ ability: b, args: r, any: a });
n(W, p, u, g, m, y);
}, f = (p, l, d) => {
const b = l.arg, r = l.modifiers, c = !!r.any, a = !!r.not, u = !!r.readonly, m = !!(r.disable || r.disabled), g = !!(r.hide || r.hidden), y = N({ roles: [b], any: c });
n(y, p, a, m, u, g);
}, A = (p, l) => {
p.directive(`${l}`, {
mounted(d, b, r) {
t(l, d, b);
},
updated(d, b, r) {
t(l, d, b);
}
});
};
if (i && (A(i, "can"), A(i, "role")), e.router) {
let p = function(r, c) {
let a;
if (r.meta.can) {
const u = r.meta.can;
typeof u == "function" ? a = u(r, c, T) : a = T(u);
} else if (r.meta.canNot) {
const u = r.meta.canNot;
typeof u == "function" ? (q(u), a = u(r, c, S)) : a = S(u);
} else if (r.meta.canAny) {
const u = r.meta.canAny;
typeof u == "function" ? a = u(r, c, h) : a = h(u);
} else if (r.meta.canNone) {
const u = r.meta.canNone;
typeof u == "function" ? a = !u(r, c, h) : a = !h(u);
}
return a;
}, l = function(r) {
let c;
if (r.meta.role) {
const a = r.meta.role;
c = P(a);
} else if (r.meta.roleNot) {
const a = r.meta.roleNot;
c = M(a);
} else if (r.meta.roleAny) {
const a = r.meta.roleAny;
c = D(a);
}
return c;
};
const d = (r, c, a, u) => {
if (u)
a();
else {
let m = e.onDeniedRoute;
r.meta && r.meta.onDeniedRoute && (m = r.meta.onDeniedRoute), a(typeof m == "object" ? m : m === "$from" ? c : { path: `${m}`, replace: !0 });
}
}, b = (r, c, a) => {
if (!r.meta) {
a();
return;
}
let u = p(r, c);
if (u === !1) {
d(r, c, a, u);
return;
}
u = l(r), u === void 0 ? a() : d(r, c, a, u);
};
o.options.router.beforeEach((r, c, a) => {
j().then(() => {
b(r, c, a);
});
});
}
};
function j() {
return new Promise(i);
function i(e, t) {
v ? e() : setTimeout(function() {
i(e);
}, 30);
}
}
const V = (i) => ({
install: (e, t = {}) => {
U(e, i);
}
});
let v = !1;
function R(i) {
var t;
return (((t = i.rightIds) == null ? void 0 : t.map((n) => x(n))) ?? []).filter((n) => !!n);
}
function x(i) {
let e = i;
switch (i.includes("-") && (e = i.split("-")[0]), e) {
case "0":
return "owner";
case "1":
return "accountAdministrator";
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 X = () => {
const i = {};
i.can = T, i.can.not = S, i.can.any = h, i.role = P, i.role.not = M, i.role.any = D;
async function e(t) {
if (!(t && t.rightIds && t.rightIds.length > 0))
return;
(await o.options.api.getRights(t.organisation, t.rightIds)).forEach((s) => {
var A, p, l;
if (!s.object || !s.object.type) {
(A = o.abilities) == null || A.push(...C.rightToAbilities(s));
return;
}
if (s.object.type !== "company")
return;
let f = (p = o.companiesAbilities) == null ? void 0 : p.get(s.object.id);
f ? f.push(...C.rightToAbilities(s)) : f = C.rightToAbilities(s), (l = o.companiesAbilities) == null || l.set(s.object.id, f);
});
}
return i.init = async (t, n) => {
var s, f;
return o.organisationEmail = t.organisation, o.userId = n, o.rolesType = R(t), (s = o.options.logger) == null || s.info("vue-acl", "User roles", o.rolesType), o.abilities = [], o.companiesAbilities = /* @__PURE__ */ new Map(), await e(t), G(), (f = o.options.logger) == null || f.info("vue-acl", "User abilities", o.abilities, o.companiesAbilities), v = !0, Promise.resolve();
}, i.initStaffIds = (t) => {
o.staffIds = t;
}, i.ready = () => v, F(i);
}, T = (i, e, t) => w({ ability: i, args: e, args2: t, any: !1 }), S = (i, e, t) => !w({ ability: i, args: e, args2: t, any: !1 }), h = (i, e, t) => w({ ability: i, args: e, args2: t, any: !0 }), P = (i) => L({ roles: i, any: !1 }), M = (i) => L({ roles: i, any: !1 }), D = (i) => L({ roles: i, any: !0 }), w = ({
ability: i,
args: e,
args2: t,
any: n = !1
}) => {
var s;
return i ? I({
ability: i,
args: e,
args2: t,
any: n
}) : ((s = o.options.logger) == null || s.warn("vue-acl", "Invalid ACL arguments specified: ", i, e, t, n), !1);
}, L = ({ roles: i, any: e }) => {
var t;
return i ? N({
roles: i,
any: e
}) : ((t = o.options.logger) == null || t.warn("vue-acl", "Invalid ACL arguments specified: ", i), !1);
}, I = ({ ability: i, args: e, args2: t, any: n = !1 }) => {
const s = i, f = e;
return O(f ? { ability: s, args: f, args2: t, any: n } : { ability: s, any: n });
}, N = ({ roles: i, any: e }) => E({ roles: i, any: e }), O = ({ ability: i, args: e, args2: t, any: n = !1 }) => {
if (i) {
if (n && Array.isArray(i)) {
for (const s in i)
if (k(i[s], e, t))
return !0;
return !1;
} else if (typeof i == "string")
return k(i, e, t);
}
return Array.isArray(e) && n ? _(Object.values(e)) : !1;
}, E = ({ roles: i, any: e }) => i ? $(i, e) : !1, _ = (i) => {
let e = !1;
for (const t of i) {
const [n, s] = t;
if (s) {
const f = k(n, s);
if (f) {
e = f;
break;
}
} else if (n === !0) {
e = !0;
break;
}
}
return e;
}, q = (i) => {
let e = null;
const t = /\(([a-z0-9 ,$_+*\-/]+)\)/im, s = i.toString().match(t);
if (s && Array.isArray(s) && s[0]) {
let f = s[0];
f = f.replace(/[() ]/g, "");
const A = f.split(",");
A.length > 0 && (e = A);
}
return e;
}, k = (i, e, t) => {
var s, f, A;
const n = (s = o.abilities) == null ? void 0 : s.includes(i);
if (n)
return n;
if (!e) {
let p = !1;
return (f = o.companiesAbilities) == null || f.forEach((l, d) => {
l.includes(i) && (p = !0);
}), p;
}
if (typeof e == "object") {
if (e.type === "company" || e.type === "conference") {
const p = (A = o.companiesAbilities) == null ? void 0 : A.get(e.id);
if (p)
return p.includes(i);
}
return !1;
}
return Q(i, e, t);
};
function Q(i, e, t) {
var f, A;
const n = z(i);
if (n !== "company" && n !== "conference")
return !1;
const s = (f = o.companiesAbilities) == null ? void 0 : f.get(e);
return s ? (i === "updateStaffAppointment" || i === "acceptStaffAppointment" || i === "readStaffAppointment" || i === "deleteStaffAppointment" || i === "statusChangeStaffAppointment") && s.includes(i) && t && typeof t == "string" ? ((A = o.staffIds) == null ? void 0 : A.includes(t)) ?? !1 : s.includes(i) : !1;
}
const $ = (i, e = !1) => e ? i.find(
(t) => {
var n;
return (n = o.rolesType) == null ? void 0 : n.includes(t);
}
) !== void 0 : i.filter(
(t) => {
var n;
return (n = o.rolesType) == null ? void 0 : n.includes(t);
}
).length === i.length, B = (i) => {
const t = /[A-Z]/g.exec(i);
if (t)
return i.substring(t.index);
}, z = (i) => {
const e = B(i);
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(i) {
return i.includes("readDashboard") || i.includes("readAppointment") || i.includes("readStaffAppointment") ? !1 : J.filter(
(e) => i.includes(e)
).length > 1;
}
const H = [
{
test: (i) => (i.includes("readService") || i.includes("readStaff")) && !i.includes("readCompany"),
gain: "readCompany"
},
{
test: Z,
gain: "readDashboard"
},
{
test: (i) => (i.includes("readConferenceRegistrations") || i.includes("readConferenceSessions")) && !i.includes("readConference"),
gain: "readConference"
}
], G = () => {
if (o.abilities)
for (const e of H)
e.test(o.abilities) && o.abilities.push(e.gain);
if (!o.companiesAbilities || o.companiesAbilities.size === 0)
return;
[...o.companiesAbilities.keys()].forEach((e) => {
let t = o.companiesAbilities.get(e);
if (t !== void 0)
for (const n of H)
n.test(t) && t.push(n.gain);
});
}, J = ["readForm", "readQueue", "readClient", "readReport", "readConference"];
export {
J as ABILITIES_FOR_OLD_DASHBOARD,
C as AbilityResolver,
V as createAcl,
X as useAcl
};