1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-23 09:50:31 +01:00
onion/js/components/acl_proxy.js

60 lines
1.8 KiB
JavaScript
Raw Normal View History

2015-07-14 11:11:28 +02:00
import React from 'react';
2016-06-17 12:44:26 +02:00
import { omitFromObject } from '../utils/general';
2015-07-14 11:14:15 +02:00
/**
* This component can easily be used to present another component conditionally
* - dependent on their acl.
*
* In order to do that, just wrap AclProxy around the component, add aclObject and
* the acl name you're looking for.
*/
2016-06-17 12:44:26 +02:00
const AclProxy = React.createClass({
2015-07-14 11:11:28 +02:00
propTypes: {
2016-06-17 12:44:26 +02:00
children: React.PropTypes.node.isRequired,
2015-07-14 22:34:34 +02:00
aclName: React.PropTypes.string,
2016-06-17 12:44:26 +02:00
aclObject: React.PropTypes.object,
2015-07-14 22:34:34 +02:00
show: React.PropTypes.bool
2015-07-14 11:11:28 +02:00
},
getChildren() {
2016-06-17 12:44:26 +02:00
const childProps = omitFromObject(this.props, ['aclName', 'aclObject', 'children', 'show']);
const children = React.Children.map(
this.props.children,
(child) => React.cloneElement(child, childProps)
);
if (children.length > 1) {
// This has the potential to ruin some styling, but React doesn't let us just return an
// array of components.
return (<span>{children}</span>);
} else {
return children[0];
}
},
render() {
2016-06-17 12:44:26 +02:00
const { aclName, aclObject, show } = this.props;
if (show) {
return this.getChildren();
2016-06-17 12:44:26 +02:00
} else if (aclObject) {
if (aclObject[aclName]) {
return this.getChildren();
} else if (false && process.env.NODE_ENV !== 'production') {
// Warning currently disabled because the main app's acls don't include whitelabel
// specific acls, causing a lot of warnings
// eslint-disable-next-line no-console
console.warn(`The aclName (${aclName}) you're filtering for was not present (or ` +
'undefined) in the aclObject.');
2015-07-14 11:11:28 +02:00
}
}
2016-06-17 12:44:26 +02:00
2015-07-14 22:50:48 +02:00
return null;
2015-07-14 11:11:28 +02:00
}
});
2015-07-14 22:50:48 +02:00
export default AclProxy;