ra-core
Version:
Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React
69 lines • 3.17 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.CanAccess = void 0;
const React = __importStar(require("react"));
const useCanAccess_1 = require("./useCanAccess.cjs");
const routing_1 = require("../routing/index.cjs");
/**
* A component that only displays its children after checking whether users are authorized to access the provided resource and action.
* @param options
* @param options.action The action to check. One of 'list', 'create', 'edit', 'show', 'delete', or a custom action.
* @param options.resource The resource to check. e.g. 'posts', 'comments', 'users'
* @param options.children The component to render if users are authorized.
* @param options.loading An optional element to render while the authorization is being checked. Defaults to null.
* @param options.accessDenied An optional element to render if users are denied access. Defaults to null.
* @param options.error An optional element to render if an error occur while checking users access rights. Redirect users to `/authentication-error` by default.
*/
const CanAccess = ({ children, loading = null, accessDenied = null, error: errorElement = DEFAULT_ERROR, ...props }) => {
const { canAccess, error, isPending } = (0, useCanAccess_1.useCanAccess)(props);
if (isPending) {
return loading;
}
if (error) {
return errorElement;
}
if (canAccess === false) {
return accessDenied;
}
return children;
};
exports.CanAccess = CanAccess;
const CanAccessDefaultError = () => {
const basename = (0, routing_1.useBasename)();
return React.createElement(routing_1.Navigate, { to: `${basename}/authentication-error` });
};
const DEFAULT_ERROR = React.createElement(CanAccessDefaultError, null);
//# sourceMappingURL=CanAccess.js.map