ra-core
Version:
Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React
106 lines • 4.41 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;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const React = __importStar(require("react"));
const react_1 = require("react");
const warning_1 = __importDefault(require("../util/warning.cjs"));
const useAuthenticated_1 = require("./useAuthenticated.cjs");
const usePermissions_1 = __importDefault(require("./usePermissions.cjs"));
const isEmptyChildren = children => react_1.Children.count(children) === 0;
/**
* After checking that the user is authenticated,
* retrieves the user's permissions for a specific context.
*
* Useful for Route components ; used internally by Resource.
* Use it to decorate your custom page components to require
* a custom role. It will pass the permissions as a prop to your
* component.
*
* You can set additional `authParams` at will if your authProvider
* requires it.
*
* @example
* import { Admin, CustomRoutes, WithPermissions } from 'react-admin';
*
* const Foo = ({ permissions }) => (
* {permissions === 'admin' ? <p>Sensitive data</p> : null}
* <p>Not sensitive data</p>
* );
*
* const customRoutes = [
* <Route path="/foo" element={
* <WithPermissions
* authParams={{ foo: 'bar' }}
* component={({ permissions, ...props }) => <Foo permissions={permissions} {...props} />}
* />
* } />
* ];
* const App = () => (
* <Admin>
* <CustomRoutes>{customRoutes}</CustomRoutes>
* </Admin>
* );
*/
const WithPermissions = (props) => {
const { authParams, children, render, component, loading: Loading = null, staticContext, ...rest } = props;
(0, warning_1.default)((render && children && !isEmptyChildren(children)) ||
(render && component) ||
(component && children && !isEmptyChildren(children)), 'You should only use one of the `component`, `render` and `children` props in <WithPermissions>');
const { isPending: isAuthenticationPending } = (0, useAuthenticated_1.useAuthenticated)(authParams);
const { permissions, isPending: isPendingPermissions } = (0, usePermissions_1.default)(authParams, {
enabled: !isAuthenticationPending,
});
// We must check both pending states here as if the authProvider does not implement getPermissions, isPendingPermissions will always be false
if (isAuthenticationPending || isPendingPermissions) {
return Loading ? React.createElement(Loading, null) : null;
}
if (component) {
return (0, react_1.createElement)(component, { permissions, ...rest });
}
// @deprecated
if (render) {
return render({ permissions, ...rest });
}
// @deprecated
if (children) {
return children({ permissions, ...rest });
}
};
exports.default = WithPermissions;
module.exports = exports.default;
//# sourceMappingURL=WithPermissions.js.map