@availity/authorize
Version:
Check user permissions to see if the current user is authorized to see your content.
195 lines (188 loc) • 7.5 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __getProtoOf = Object.getPrototypeOf;
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 __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
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/index.ts
var index_exports = {};
__export(index_exports, {
default: () => Authorize_default,
useAuthorize: () => useAuthorize_default
});
module.exports = __toCommonJS(index_exports);
// src/Authorize.tsx
var import_block_ui = __toESM(require("@availity/block-ui"));
// src/useAuthorize.ts
var import_react_query = require("@tanstack/react-query");
// src/api.ts
var import_api_axios = require("@availity/api-axios");
var getRegion = (region) => __async(null, null, function* () {
var _a, _b, _c;
if (region === true) {
const resp = yield import_api_axios.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 import_api_axios.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 = (0, import_react_query.useQueryClient)();
const { organizationId, customerId, region = true, resources } = parameters;
const { data: authorized = false, isLoading } = (0, import_react_query.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
var import_jsx_runtime = require("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__ */ (0, import_jsx_runtime.jsx)(import_block_ui.default, { blocking: true }) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: loader });
return null;
}
if ((authorized || negate) && !(authorized && negate)) {
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children });
}
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: unauthorized });
};
var Authorize_default = Authorize;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
useAuthorize
});