Added basic RBAC component BOD-232

This commit is contained in:
Patrick Fic
2020-08-10 16:07:30 -07:00
parent 0df61a2701
commit 83c83ac06e
52 changed files with 670 additions and 288 deletions

View File

@@ -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);