Added basic RBAC component BOD-232
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import PropTypes from "prop-types";
|
||||
import React from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { connect } from "react-redux";
|
||||
import { createStructuredSelector } from "reselect";
|
||||
import {
|
||||
selectAuthLevel,
|
||||
selectBodyshop,
|
||||
selectCurrentUser,
|
||||
} from "../../redux/user/user.selectors";
|
||||
import AlertComponent from "../alert/alert.component";
|
||||
import rbacDefaults from "./rbac-defaults";
|
||||
|
||||
const mapStateToProps = createStructuredSelector({
|
||||
currentUser: selectCurrentUser,
|
||||
authLevel: selectAuthLevel,
|
||||
bodyshop: selectBodyshop,
|
||||
});
|
||||
|
||||
function RbacWrapper({
|
||||
currentUser,
|
||||
authLevel,
|
||||
bodyshop,
|
||||
requiredAuthLevel,
|
||||
noauth,
|
||||
children,
|
||||
action,
|
||||
dispatch,
|
||||
...restProps
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
if (
|
||||
(requiredAuthLevel && requiredAuthLevel <= authLevel) ||
|
||||
(bodyshop.md_rbac && bodyshop.md_rbac[action] <= authLevel) ||
|
||||
(!!!bodyshop.md_rbac && rbacDefaults[action] <= authLevel)
|
||||
)
|
||||
return <div>{React.cloneElement(children, restProps)}</div>;
|
||||
|
||||
return (
|
||||
noauth || (
|
||||
<AlertComponent
|
||||
message={t("general.messages.rbacunauth")}
|
||||
type="warning"
|
||||
/>
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
RbacWrapper.propTypes = {
|
||||
currentUser: PropTypes.object.isRequired,
|
||||
authLevel: PropTypes.number.isRequired,
|
||||
noauth: PropTypes.oneOfType(PropTypes.string, PropTypes.func),
|
||||
requiredAuthLevel: PropTypes.number,
|
||||
action: PropTypes.string,
|
||||
};
|
||||
|
||||
export default connect(mapStateToProps, null)(RbacWrapper);
|
||||
Reference in New Issue
Block a user