@agendize/vue-acl
Version:
Vue agendize's ACL management
433 lines (432 loc) • 21.8 kB
JavaScript
import { reactive as O } from "vue";
class L {
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)), e.user && i.push(...this.getUserAbilities(e.user)), e.account && i.push(...this.getAccountAbilities(e.account)), e.menu && i.push(...this.getMenuAbilities(e.menu)), e.privacy && i.push(...this.getPrivacyAbilities(e.privacy)), e.developer && i.push(...this.getDeveloperRight(e.developer))), 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)), e.dailyView && i.push(...this.rightLettersToAbilities("dailyView", e.dailyView)), e.weeklyView && i.push(...this.rightLettersToAbilities("weeklyView", e.weeklyView)), e.monthlyView && i.push(...this.rightLettersToAbilities("monthlyView", e.monthlyView)), i;
}
static getSettingsAbilities(e) {
const i = [];
return e.general && i.push(...this.rightLettersToAbilities("schedulingGeneralSettings", e.general)), 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)), e.resource && i.push(...this.rightLettersToAbilities("resource", e.resource)), e.staffCustomPeriod && i.push(...this.rightLettersToAbilities("staffCustomPeriod", e.staffCustomPeriod)), e.process && i.push(...this.rightLettersToAbilities("schedulingProcessSettings", e.process)), e.widget && i.push(...this.rightLettersToAbilities("schedulingWidgetSettings", e.widget)), e.widgetForm && i.push(...this.rightLettersToAbilities("schedulingWidgetFormSettings", e.widgetForm)), e.notifications && i.push(...this.rightLettersToAbilities("schedulingNotificationsSettings", e.notifications)), e.payments && i.push(...this.rightLettersToAbilities("schedulingPaymentSettings", e.payments)), e.messages && i.push(...this.rightLettersToAbilities("schedulingMessagesSettings", e.messages)), e.calendars && i.push(...this.rightLettersToAbilities("schedulingCalendarsSettings", e.calendars)), e.crm && i.push(...this.rightLettersToAbilities("schedulingCRMSettings", e.crm)), e.crm && i.push(...this.rightLettersToAbilities("schedulingCRMSettings", e.crm)), e.dashboard && i.push(...this.rightLettersToAbilities("schedulingDashboardSettings", e.dashboard)), e.reminders && i.push(...this.rightLettersToAbilities("schedulingRemindersSettings", e.reminders)), e.manager && i.push(...this.rightLettersToAbilities("schedulingManagerSettings", e.manager)), e.feedback && i.push(...this.rightLettersToAbilities("schedulingFeedbackSettings", e.feedback)), e.items && i.push(...this.rightLettersToAbilities("schedulingItemsSettings", e.items)), e.itemsService && i.push(...this.rightLettersToAbilities("schedulingItemsServiceSettings", e.itemsService)), e.itemsServiceStaff && i.push(...this.rightLettersToAbilities("schedulingItemsServiceStaffSettings", e.itemsServiceStaff)), e.rules && i.push(...this.rightLettersToAbilities("schedulingRulesSettings", e.rules)), e.activities && i.push(...this.rightLettersToAbilities("schedulingActivitySettings", e.activities)), e.visio && i.push(...this.rightLettersToAbilities("schedulingVisioSettings", e.visio)), e.sms && i.push(...this.rightLettersToAbilities("schedulingSMSSettings", e.sms)), e.staffRole && i.push(...this.rightLettersToAbilities("schedulingStaffRoleSettings", e.staffRole)), e.loginList && i.push(...this.rightLettersToAbilities("schedulingLoginListSettings", e.loginList)), e.paymentProviders && i.push(...this.rightLettersToAbilities("schedulingPaymentProviderSettings", e.paymentProviders)), e.mobility && i.push(...this.rightLettersToAbilities("schedulingMobilitySettings", e.mobility)), e.eventStatusType && i.push(...this.rightLettersToAbilities("schedulingEventStatusTypeSettings", e.eventStatusType)), e.groupExtId && i.push(...this.rightLettersToAbilities("schedulingGroupExtIdSettings", e.groupExtId)), e.franceConnectAuth && i.push(...this.rightLettersToAbilities("schedulingFranceConnectAuthSettings", e.franceConnectAuth)), e.facebookAuth && i.push(...this.rightLettersToAbilities("schedulingFacebookAuthSettings", e.facebookAuth)), e.googleAuth && i.push(...this.rightLettersToAbilities("schedulingGoogleAuthSettings", e.googleAuth)), e.myAgendizeAuth && i.push(...this.rightLettersToAbilities("schedulingMyAgendizeAuthSettings", e.myAgendizeAuth)), e.mConnectAuth && i.push(...this.rightLettersToAbilities("schedulingMConnectAuthSettings", e.mConnectAuth)), e.deployment && i.push(...this.rightLettersToAbilities("deployment", e.deployment)), 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)), e.formRouting && i.push(...this.rightLettersToAbilities("formRouting", e.formRouting)), e.validationProviders && i.push(...this.rightLettersToAbilities("formValidationProvider", e.validationProviders)), i;
}
static getClientAbilities(e) {
const i = [];
return e.client && i.push(...this.rightLettersToAbilities("client", e.client)), e.marketingEmail && i.push(...this.rightLettersToAbilities("marketingEmail", e.marketingEmail)), e.marketingSMS && i.push(...this.rightLettersToAbilities("marketingSMS", e.marketingSMS)), 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 getUserAbilities(e) {
const i = [];
return e.user && i.push(...this.rightLettersToAbilities("user", e.user)), e.userPermissions && i.push(...this.rightLettersToAbilities("userPermission", e.userPermissions)), e.connectors && i.push(...this.rightLettersToAbilities("connector", e.connectors)), i;
}
static getAccountAbilities(e) {
const i = [];
return e.account && i.push(...this.rightLettersToAbilities("account", e.account)), e.accessPermissions && i.push(...this.rightLettersToAbilities("accessPermission", e.accessPermissions)), e.paymentBudget && i.push(...this.rightLettersToAbilities("paymentBudget", e.paymentBudget)), i;
}
static getMenuAbilities(e) {
const i = [];
return e.support && i.push(...this.rightLettersToAbilities("support", e.support)), e.logout && i.push(...this.rightLettersToAbilities("logout", e.logout)), e.privacyPolicy && i.push(...this.rightLettersToAbilities("privacyPolicy", e.privacyPolicy)), e.termsOfService && i.push(...this.rightLettersToAbilities("termsOfService", e.termsOfService)), e.about && i.push(...this.rightLettersToAbilities("about", e.about)), e.developers && i.push(...this.rightLettersToAbilities("developers", e.developers)), e.contactUs && i.push(...this.rightLettersToAbilities("contactUs", e.contactUs)), i;
}
static getPrivacyAbilities(e) {
const i = [];
return e.privacy && i.push(...this.rightLettersToAbilities("privacy", e.privacy)), i;
}
static getDeveloperRight(e) {
const i = [];
return e.applications && i.push(...this.rightLettersToAbilities("developerApplication", e.applications)), e.watchers && i.push(...this.rightLettersToAbilities("developerWatcher", e.watchers)), e.watcherLogs && i.push(...this.rightLettersToAbilities("developerWatcherLog", e.watcherLogs)), e.cryptoKeys && i.push(...this.rightLettersToAbilities("developerCryptoKey", e.cryptoKeys)), e.apiLogs && i.push(...this.rightLettersToAbilities("developerAPILog", e.apiLogs)), e.certificates && i.push(...this.rightLettersToAbilities("developerCertificate", e.certificates)), i;
}
static rightLettersToAbilities(e, i) {
const s = [], r = e.charAt(0).toUpperCase() + e.slice(1);
return i.indexOf("r") > -1 && s.push("read" + r), i.indexOf("w") > -1 && s.push("create" + r), i.indexOf("u") > -1 && s.push("update" + r), i.indexOf("d") > -1 && s.push("delete" + r), i.indexOf("o") > -1 && (s.push("readUser" + r), s.push("createUser" + r), s.push("updateUser" + r), s.push("deleteUser" + r)), e === "staff" && i.indexOf("n") > -1 && s.push("createStaffWithAccount"), (e === "appointment" || e === "staffAppointment") && (i.indexOf("a") > -1 && s.push("accept" + r), i.indexOf("c") > -1 && s.push("statusChange" + r), i.indexOf("f") > -1 && s.push("freemode" + r), i.indexOf("e") > -1 && s.push("event" + r), i.indexOf("t") > -1 && s.push("addAttendee" + r), i.indexOf("x") > -1 && s.push("calendarExport" + r)), s;
}
}
const o = O({
options: {}
}), E = (t, e) => {
o.options = e;
function i(p, l, d, A) {
switch (p) {
case "can":
r(l, d);
break;
case "role":
f(l, d);
break;
}
}
function s(p, l, d, A, n, c) {
p ? d ? l.style.display = "none" : l.style.display = "" : d ? l.style.display = "" : A ? l.disabled = !0 : n ? l.readOnly = !0 : l.style.display = "none";
}
const r = (p, l, d) => {
const A = l.arg, n = l.value, c = l.modifiers, a = !!c.any, u = !!c.not, b = !!c.readonly, y = !!(c.disable || c.disabled), T = !!(c.hide || c.hidden), x = D({ ability: A, args: n, any: a });
s(x, p, u, y, b, T);
}, f = (p, l, d) => {
const A = l.arg, n = l.modifiers, c = !!n.any, a = !!n.not, u = !!n.readonly, b = !!(n.disable || n.disabled), y = !!(n.hide || n.hidden), T = W({ roles: [A], any: c });
s(T, p, a, b, u, y);
}, h = (p, l) => {
p.directive(`${l}`, {
mounted(d, A, n) {
i(l, d, A);
},
updated(d, A, n) {
i(l, d, A);
}
});
};
if (t && (h(t, "can"), h(t, "role")), e.router) {
let p = function(n, c) {
let a;
if (n.meta.can) {
const u = n.meta.can;
typeof u == "function" ? a = u(n, c, v) : a = v(u);
} else if (n.meta.canNot) {
const u = n.meta.canNot;
typeof u == "function" ? (_(u), a = u(n, c, C)) : a = C(u);
} else if (n.meta.canAny) {
const u = n.meta.canAny;
typeof u == "function" ? a = u(n, c, m) : a = m(u);
} else if (n.meta.canNone) {
const u = n.meta.canNone;
typeof u == "function" ? a = !u(n, c, m) : a = !m(u);
}
return a;
}, l = function(n) {
let c;
if (n.meta.role) {
const a = n.meta.role;
c = F(a);
} else if (n.meta.roleNot) {
const a = n.meta.roleNot;
c = I(a);
} else if (n.meta.roleAny) {
const a = n.meta.roleAny;
c = H(a);
}
return c;
};
const d = (n, c, a, u) => {
if (u)
a();
else {
let b = e.onDeniedRoute;
n.meta && n.meta.onDeniedRoute && (b = n.meta.onDeniedRoute), a(typeof b == "object" ? b : b === "$from" ? c : { path: `${b}`, replace: !0 });
}
}, A = (n, c, a) => {
if (!n.meta) {
a();
return;
}
let u = p(n, c);
if (u === !1) {
d(n, c, a, u);
return;
}
u = l(n), u === void 0 ? a() : d(n, c, a, u);
};
o.options.router.beforeEach((n, c, a) => {
U().then(() => {
A(n, c, a);
});
});
}
};
function U() {
return new Promise(t);
function t(e, i) {
S ? e() : setTimeout(function() {
t(e);
}, 30);
}
}
const X = (t) => ({
install: (e, i = {}) => {
E(e, t);
}
});
let S = !1;
function N(t) {
var i;
return (((i = t.rightIds) == null ? void 0 : i.map((s) => V(s))) ?? []).filter((s) => !!s);
}
function V(t) {
let e = t;
switch (t.includes("-") && (e = t.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 Y = () => {
const t = {};
t.can = v, t.can.not = C, t.can.any = m, t.role = F, t.role.not = I, t.role.any = H;
async function e(i) {
if (!(i && i.rightIds && i.rightIds.length > 0))
return;
(await o.options.api.getRights(i.organisation, i.rightIds)).forEach((r) => {
var h, p, l;
if (!r.object || !r.object.type) {
(h = o.abilities) == null || h.push(...L.rightToAbilities(r));
return;
}
if (r.object.type !== "company")
return;
let f = (p = o.companiesAbilities) == null ? void 0 : p.get(r.object.id);
f ? f.push(...L.rightToAbilities(r)) : f = L.rightToAbilities(r), (l = o.companiesAbilities) == null || l.set(r.object.id, f);
});
}
return t.init = async (i, s) => {
var r, f;
return o.organisationEmail = i.organisation, o.userId = s, o.rolesType = N(i), (r = o.options.logger) == null || r.info("vue-acl", "User roles", o.rolesType), o.abilities = [], o.companiesAbilities = /* @__PURE__ */ new Map(), await e(i), K(), (f = o.options.logger) == null || f.info("vue-acl", "User abilities", o.abilities, o.companiesAbilities), S = !0, Promise.resolve();
}, t.initStaffIds = (i) => {
o.staffIds = i;
}, t.ready = () => S, O(t);
}, v = (t, e, i) => w({ ability: t, args: e, args2: i, any: !1 }), C = (t, e, i) => !w({ ability: t, args: e, args2: i, any: !1 }), m = (t, e, i) => w({ ability: t, args: e, args2: i, any: !0 }), F = (t) => k({ roles: t, any: !1 }), I = (t) => k({ roles: t, any: !1 }), H = (t) => k({ roles: t, any: !0 }), w = ({
ability: t,
args: e,
args2: i,
any: s = !1
}) => {
var r;
return t ? D({
ability: t,
args: e,
args2: i,
any: s
}) : ((r = o.options.logger) == null || r.warn("vue-acl", "Invalid ACL arguments specified: ", t, e, i, s), !1);
}, k = ({ roles: t, any: e }) => {
var i;
return t ? W({
roles: t,
any: e
}) : ((i = o.options.logger) == null || i.warn("vue-acl", "Invalid ACL arguments specified: ", t), !1);
}, D = ({ ability: t, args: e, args2: i, any: s = !1 }) => {
const r = t, f = e;
return P(f ? { ability: r, args: f, args2: i, any: s } : { ability: r, any: s });
}, W = ({ roles: t, any: e }) => j({ roles: t, any: e }), P = ({ ability: t, args: e, args2: i, any: s = !1 }) => {
if (t) {
if (s && Array.isArray(t)) {
for (const r in t)
if (g(t[r], e, i))
return !0;
return !1;
} else if (typeof t == "string")
return g(t, e, i);
}
return Array.isArray(e) && s ? B(Object.values(e)) : !1;
}, j = ({ roles: t, any: e }) => t ? z(t, e) : !1, B = (t) => {
let e = !1;
for (const i of t) {
const [s, r] = i;
if (r) {
const f = g(s, r);
if (f) {
e = f;
break;
}
} else if (s === !0) {
e = !0;
break;
}
}
return e;
}, _ = (t) => {
let e = null;
const i = /\(([a-z0-9 ,$_+*\-/]+)\)/im, r = t.toString().match(i);
if (r && Array.isArray(r) && r[0]) {
let f = r[0];
f = f.replace(/[() ]/g, "");
const h = f.split(",");
h.length > 0 && (e = h);
}
return e;
}, g = (t, e, i) => {
var r, f, h;
const s = (r = o.abilities) == null ? void 0 : r.includes(t);
if (s)
return s;
if (!e) {
let p = !1;
return (f = o.companiesAbilities) == null || f.forEach((l, d) => {
l.includes(t) && (p = !0);
}), p;
}
if (typeof e == "object") {
if (e.type === "company" || e.type === "conference") {
const p = (h = o.companiesAbilities) == null ? void 0 : h.get(e.id);
if (p)
return p.includes(t);
}
return !1;
}
return q(t, e, i);
};
function q(t, e, i) {
var f, h;
const s = $(t);
if (s !== "company" && s !== "conference")
return !1;
const r = (f = o.companiesAbilities) == null ? void 0 : f.get(e);
return r ? (t === "updateStaffAppointment" || t === "acceptStaffAppointment" || t === "readStaffAppointment" || t === "deleteStaffAppointment" || t === "statusChangeStaffAppointment") && r.includes(t) && i && typeof i == "string" ? ((h = o.staffIds) == null ? void 0 : h.includes(i)) ?? !1 : r.includes(t) : !1;
}
const z = (t, e = !1) => e ? t.find(
(i) => {
var s;
return (s = o.rolesType) == null ? void 0 : s.includes(i);
}
) !== void 0 : t.filter(
(i) => {
var s;
return (s = o.rolesType) == null ? void 0 : s.includes(i);
}
).length === t.length, Q = (t) => {
const i = /[A-Z]/g.exec(t);
if (i)
return t.substring(i.index);
}, $ = (t) => {
const e = Q(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 G(t) {
return t.includes("readDashboard") || t.includes("readAppointment") || t.includes("readStaffAppointment") ? !1 : Z.filter(
(e) => t.includes(e)
).length > 1;
}
const M = [
{
test: (t) => (t.includes("readService") || t.includes("readStaff")) && !t.includes("readCompany"),
gain: "readCompany"
},
{
test: G,
gain: "readDashboard"
},
{
test: (t) => (t.includes("readConferenceRegistrations") || t.includes("readConferenceSessions")) && !t.includes("readConference"),
gain: "readConference"
}
], K = () => {
if (o.abilities)
for (const e of M)
e.test(o.abilities) && o.abilities.push(e.gain);
if (!o.companiesAbilities || o.companiesAbilities.size === 0)
return;
[...o.companiesAbilities.keys()].forEach((e) => {
let i = o.companiesAbilities.get(e);
if (i !== void 0)
for (const s of M)
s.test(i) && i.push(s.gain);
});
}, Z = ["readForm", "readQueue", "readClient", "readReport", "readConference"];
export {
Z as ABILITIES_FOR_OLD_DASHBOARD,
L as AbilityResolver,
X as createAcl,
Y as useAcl
};