UNPKG

react-permissions

Version:
71 lines (53 loc) 3.71 kB
'use strict'; exports.__esModule = true; exports.Permissioned = Permissioned; var _react = require('react'); var _react2 = _interopRequireDefault(_react); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } function Permissioned(WrappedComponent, allowedPermissions) { var PermissionedComponent = function (_React$Component) { _inherits(PermissionedComponent, _React$Component); function PermissionedComponent(props, context) { _classCallCheck(this, PermissionedComponent); var _this = _possibleConstructorReturn(this, _React$Component.call(this, props, context)); _this.state = { allowedPermissions: allowedPermissions || props.allowedPermissions || [] }; _this.allPermissions = Permissioned.prototype.allPermissions || (Permissioned.mapPermissions ? Permissioned.mapPermissions(props) : []); _this.alternateView = props.alternateView || _react2.default.createClass({ displayName: 'alternateView', render: function render() { return _react2.default.createElement('div', null); } }); return _this; } PermissionedComponent.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) { nextProps.allowedPermissions && this.setState({ allowedPermissions: nextProps.allowedPermissions }); }; PermissionedComponent.prototype.componentWillUpdate = function componentWillUpdate(props, state) { this.allPermissions = Permissioned.prototype.allPermissions || (Permissioned.mapPermissions ? Permissioned.mapPermissions(props) : []); }; PermissionedComponent.prototype.hasPermission = function hasPermission(and) { var _this2 = this; var hasPermissionTo = true; var filteredList = this.allPermissions.filter(function (permission) { return _this2.state.allowedPermissions.filter(function (allowedPermission) { return allowedPermission == permission; }).length > 0; }); hasPermissionTo = and ? filteredList.length === this.state.allowedPermissions.length : filteredList.length > 0; return hasPermissionTo; }; PermissionedComponent.prototype.render = function render() { var AlternateView = this.alternateView; if (this.hasPermission(this.props.hasAll)) return _react2.default.createElement(WrappedComponent, this.props); return _react2.default.createElement(AlternateView, null); }; return PermissionedComponent; }(_react2.default.Component); return PermissionedComponent; }