import Telescope from 'meteor/nova:lib'; import React, { PropTypes } from 'react'; import { FormattedMessage } from 'react-intl'; import Users from 'meteor/nova:users'; import { withCurrentUser } from 'meteor/nova:users'; const CanDo = (props, context) => { // no user login, display the login form if (!props.currentUser && props.displayNoPermissionMessage) { return (

); } // default permission, is the user allowed to perform this action? let permission = Users.canDo(props.currentUser, props.action); // the permission is about viewing a document, check if the user is allowed if (props.document && props.action.indexOf('view') > -1) { // use the permission shortcut canView on the current user and requested document permission = Users.canView(props.currentUser, props.document); } // the permission is about editing a document, check if the user is allowed if (props.document && props.action.indexOf('edit') > -1) { // use the permission shortcut canEdit on the current user and requested document permission = Users.canEdit(props.currentUser, props.document); } // the user can perform the intented action in the component: display the component, // else: display a not allowed message if (permission) { return props.children; } else { return props.displayNoPermissionMessage ?

: null; } }; CanDo.propTypes = { action: React.PropTypes.string.isRequired, currentUser: React.PropTypes.object, document: React.PropTypes.object, noPermissionMessage: React.PropTypes.string, displayNoPermissionMessage: React.PropTypes.bool, }; CanDo.defaultProps = { noPermissionMessage: 'app.noPermission', displayNoPermissionMessage: false, }; CanDo.displayName = "CanDo"; Telescope.registerComponent('CanDo', CanDo, withCurrentUser);