@availity/authorize
Version:
Check user permissions to see if the current user is authorized to see your content.
161 lines (155 loc) • 5.73 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
// src/Authorize.tsx
import BlockUi from "@availity/block-ui";
// src/useAuthorize.ts
import { useQuery, useQueryClient } from "@tanstack/react-query";
// src/api.ts
import { avUserPermissionsApi, avRegionsApi } from "@availity/api-axios";
var getRegion = (region) => __async(null, null, function* () {
var _a, _b, _c;
if (region === true) {
const resp = yield avRegionsApi.getCurrentRegion();
return (_c = (_b = (_a = resp == null ? void 0 : resp.data) == null ? void 0 : _a.regions) == null ? void 0 : _b[0]) == null ? void 0 : _c.id;
}
return region || void 0;
});
var getPermissions = (permissions, region) => __async(null, null, function* () {
if (!permissions) return {};
const response = yield avUserPermissionsApi.getPermissions(permissions, region);
return response.reduce((prev, cur) => {
prev[cur.id] = cur;
return prev;
}, {});
});
var checkPermission = (permission, resources, organizationId, customerId) => {
if (!permission) return false;
let isAuthorizedForCustomerId = true;
let isAuthorizedForOrganizationId = true;
let isAuthorizedForResources = true;
if (organizationId) {
isAuthorizedForOrganizationId = permission.organizations.some(({ id: orgId }) => orgId === organizationId);
}
if (customerId) {
isAuthorizedForCustomerId = permission.organizations.some(
({ customerId: orgCustomerId }) => orgCustomerId === customerId
);
}
if (resources !== void 0) {
const resourceSets = Array.isArray(resources) ? resources : [resources];
isAuthorizedForResources = resourceSets.length === 0 || resourceSets.some((resourceSet) => {
if (Array.isArray(resourceSet)) {
return resourceSet.every(
(resource) => permission.organizations.some(
({ resources: orgResources = [] }) => orgResources.some(({ id }) => `${id}` === `${resource}`)
)
);
}
return permission.organizations.some(
({ resources: orgResources = [] }) => orgResources.some(({ id }) => `${id}` === `${resourceSet}`)
);
});
}
return isAuthorizedForCustomerId && isAuthorizedForOrganizationId && isAuthorizedForResources;
};
var checkPermissions = (permissions, region, resources, organizationId, customerId) => __async(null, null, function* () {
if (!permissions) return false;
permissions = Array.isArray(permissions) ? permissions : [permissions];
const response = yield getPermissions(permissions, region);
const authorized = permissions.some((permissionSet) => {
if (Array.isArray(permissionSet)) {
return permissionSet.every(
(permission) => checkPermission(response[permission], resources, organizationId, customerId)
);
}
return checkPermission(response[permissionSet], resources, organizationId, customerId);
});
return authorized;
});
// src/useAuthorize.ts
var useAuthorize = (permissions, parameters = {}, options) => {
const queryClient = useQueryClient();
const { organizationId, customerId, region = true, resources } = parameters;
const { data: authorized = false, isLoading } = useQuery(
["useAuthorize", permissions, region, resources, organizationId, customerId],
() => __async(null, null, function* () {
const currentRegion = yield queryClient.fetchQuery(["region"], () => getRegion(region));
return checkPermissions(permissions, currentRegion, resources, organizationId, customerId);
}),
__spreadValues({ enabled: permissions.length > 0 }, options)
);
return { authorized, isLoading };
};
var useAuthorize_default = useAuthorize;
// src/Authorize.tsx
import { Fragment, jsx } from "react/jsx-runtime";
var Authorize = ({
permissions,
resources,
customerId,
organizationId,
region = true,
loader,
negate,
children = null,
unauthorized = null,
queryOptions
}) => {
const { authorized, isLoading } = useAuthorize_default(
permissions,
{
customerId,
organizationId,
region,
resources
},
queryOptions
);
if (isLoading) {
if (loader) return loader === true ? /* @__PURE__ */ jsx(BlockUi, { blocking: true }) : /* @__PURE__ */ jsx(Fragment, { children: loader });
return null;
}
if ((authorized || negate) && !(authorized && negate)) {
return /* @__PURE__ */ jsx(Fragment, { children });
}
return /* @__PURE__ */ jsx(Fragment, { children: unauthorized });
};
var Authorize_default = Authorize;
export {
Authorize_default as default,
useAuthorize_default as useAuthorize
};