mirror of
https://github.com/strapi/strapi.git
synced 2025-11-02 02:44:55 +00:00
Add permissions to doc plugin
Signed-off-by: soupette <cyril.lpz@gmail.com>
This commit is contained in:
parent
6e6c8c05cb
commit
4ce714c32f
@ -372,18 +372,18 @@ const data = {
|
||||
fields: null,
|
||||
conditions: [],
|
||||
},
|
||||
// {
|
||||
// action: 'plugins::documentation.settings.update',
|
||||
// subject: null,
|
||||
// fields: null,
|
||||
// conditions:[],
|
||||
// },
|
||||
// {
|
||||
// action: 'plugins::documentation.settings.regenerate',
|
||||
// subject: null,
|
||||
// fields: null,
|
||||
// conditions:[],
|
||||
// },
|
||||
{
|
||||
action: 'plugins::documentation.settings.update',
|
||||
subject: null,
|
||||
fields: null,
|
||||
conditions: [],
|
||||
},
|
||||
{
|
||||
action: 'plugins::documentation.settings.regenerate',
|
||||
subject: null,
|
||||
fields: null,
|
||||
conditions: [],
|
||||
},
|
||||
|
||||
// Upload plugin
|
||||
{
|
||||
|
||||
@ -1,16 +1,12 @@
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import { FormattedMessage } from 'react-intl';
|
||||
import { WithPermissions } from 'strapi-helper-plugin';
|
||||
import pluginPermissions from '../../permissions';
|
||||
import openWithNewTab from '../../utils/openWithNewTab';
|
||||
import { StyledButton } from './components';
|
||||
|
||||
const ButtonContainer = ({
|
||||
currentDocVersion,
|
||||
isHeader,
|
||||
onClick,
|
||||
onClickDelete,
|
||||
version,
|
||||
}) => {
|
||||
const ButtonContainer = ({ currentDocVersion, isHeader, onClick, onClickDelete, version }) => {
|
||||
if (isHeader) {
|
||||
return <div />;
|
||||
}
|
||||
@ -23,16 +19,17 @@ const ButtonContainer = ({
|
||||
>
|
||||
<FormattedMessage id="documentation.components.Row.open" />
|
||||
</StyledButton>
|
||||
<StyledButton
|
||||
type="generateDocumentation"
|
||||
onClick={() => onClick(version)}
|
||||
>
|
||||
<FormattedMessage id="documentation.components.Row.regenerate" />
|
||||
</StyledButton>
|
||||
<StyledButton
|
||||
type={version === currentDocVersion ? '' : 'trash'}
|
||||
onClick={() => onClickDelete(version)}
|
||||
/>
|
||||
<WithPermissions permissions={pluginPermissions.regenerate}>
|
||||
<StyledButton type="generateDocumentation" onClick={() => onClick(version)}>
|
||||
<FormattedMessage id="documentation.components.Row.regenerate" />
|
||||
</StyledButton>
|
||||
</WithPermissions>
|
||||
<WithPermissions permissions={pluginPermissions.update}>
|
||||
<StyledButton
|
||||
type={version === currentDocVersion ? '' : 'trash'}
|
||||
onClick={() => onClickDelete(version)}
|
||||
/>
|
||||
</WithPermissions>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
|
||||
import React from 'react';
|
||||
import { Switch, Route } from 'react-router-dom';
|
||||
import { NotFound, WithPagePermissions } from 'strapi-helper-plugin';
|
||||
import { NotFound, WithPagePermissions, useUser } from 'strapi-helper-plugin';
|
||||
// Utils
|
||||
import pluginPermissions from '../../permissions';
|
||||
import pluginId from '../../pluginId';
|
||||
@ -15,11 +15,17 @@ import pluginId from '../../pluginId';
|
||||
import HomePage from '../HomePage';
|
||||
|
||||
function App() {
|
||||
const userPermissions = useUser();
|
||||
|
||||
return (
|
||||
<WithPagePermissions permissions={pluginPermissions.main}>
|
||||
<div className={pluginId}>
|
||||
<Switch>
|
||||
<Route path={`/plugins/${pluginId}`} component={HomePage} exact />
|
||||
<Route
|
||||
path={`/plugins/${pluginId}`}
|
||||
render={props => <HomePage {...props} userPermissions={userPermissions} />}
|
||||
exact
|
||||
/>
|
||||
<Route component={NotFound} />
|
||||
</Switch>
|
||||
</div>
|
||||
|
||||
@ -17,9 +17,11 @@ import {
|
||||
LoadingIndicatorPage,
|
||||
InputsIndex as Input,
|
||||
GlobalContext,
|
||||
hasPermissions,
|
||||
} from 'strapi-helper-plugin';
|
||||
|
||||
import pluginId from '../../pluginId';
|
||||
import pluginPermissions from '../../permissions';
|
||||
import getTrad from '../../utils/getTrad';
|
||||
|
||||
import Block from '../../components/Block';
|
||||
@ -41,10 +43,37 @@ import selectHomePage from './selectors';
|
||||
import saga from './saga';
|
||||
|
||||
export class HomePage extends React.Component {
|
||||
state = { canOpen: false, canUpdate: false };
|
||||
|
||||
componentDidMount() {
|
||||
this.props.getDocInfos();
|
||||
this.getPermissions();
|
||||
}
|
||||
|
||||
getPermissions = async () => {
|
||||
const { userPermissions } = this.props;
|
||||
|
||||
const checkPermissions = async permissionName => {
|
||||
const hasPermission = await hasPermissions(
|
||||
userPermissions,
|
||||
pluginPermissions[permissionName]
|
||||
);
|
||||
|
||||
return hasPermission;
|
||||
};
|
||||
|
||||
const generateArrayOfPromises = array =>
|
||||
array.map(permissionName => checkPermissions(permissionName));
|
||||
|
||||
try {
|
||||
const [canOpen, canUpdate] = await Promise.all(generateArrayOfPromises(['open', 'update']));
|
||||
|
||||
this.setState({ canOpen, canUpdate });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
};
|
||||
|
||||
getRestrictedAccessValue = () => {
|
||||
const { form } = this.props;
|
||||
|
||||
@ -52,8 +81,11 @@ export class HomePage extends React.Component {
|
||||
};
|
||||
|
||||
getPluginHeaderActions = () => {
|
||||
return [
|
||||
{
|
||||
const { canOpen, canUpdate } = this.state;
|
||||
const actions = [];
|
||||
|
||||
if (canOpen) {
|
||||
actions.push({
|
||||
color: 'none',
|
||||
label: this.context.formatMessage({
|
||||
id: getTrad('containers.HomePage.Button.open'),
|
||||
@ -62,8 +94,11 @@ export class HomePage extends React.Component {
|
||||
onClick: this.openCurrentDocumentation,
|
||||
type: 'button',
|
||||
key: 'button-open',
|
||||
},
|
||||
{
|
||||
});
|
||||
}
|
||||
|
||||
if (canUpdate) {
|
||||
actions.push({
|
||||
label: this.context.formatMessage({
|
||||
id: getTrad('containers.HomePage.Button.update'),
|
||||
}),
|
||||
@ -71,8 +106,10 @@ export class HomePage extends React.Component {
|
||||
onClick: () => {},
|
||||
type: 'submit',
|
||||
key: 'button-submit',
|
||||
},
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
return actions;
|
||||
};
|
||||
|
||||
handleCopy = () => {
|
||||
@ -142,6 +179,7 @@ export class HomePage extends React.Component {
|
||||
onSubmit,
|
||||
versionToDelete,
|
||||
} = this.props;
|
||||
|
||||
const { formatMessage } = this.context;
|
||||
|
||||
if (isLoading) {
|
||||
@ -220,6 +258,7 @@ HomePage.defaultProps = {
|
||||
onSubmit: () => {},
|
||||
onUpdateDoc: () => {},
|
||||
prefix: '/documentation',
|
||||
userPermissions: [],
|
||||
versionToDelete: '',
|
||||
};
|
||||
|
||||
@ -237,6 +276,7 @@ HomePage.propTypes = {
|
||||
onSubmit: PropTypes.func,
|
||||
onUpdateDoc: PropTypes.func,
|
||||
prefix: PropTypes.string,
|
||||
userPermissions: PropTypes.array,
|
||||
versionToDelete: PropTypes.string,
|
||||
};
|
||||
|
||||
|
||||
@ -5,9 +5,15 @@ const pluginPermissions = {
|
||||
// plugin directly in the browser
|
||||
main: [
|
||||
{ action: 'plugins::documentation.read', subject: null },
|
||||
{ action: 'plugins::documentation.regenerate', subject: null },
|
||||
{ action: 'plugins::documentation.update', subject: null },
|
||||
{ action: 'plugins::documentation.settings.regenerate', subject: null },
|
||||
{ action: 'plugins::documentation.settings.update', subject: null },
|
||||
],
|
||||
open: [
|
||||
{ action: 'plugins::documentation.read', subject: null },
|
||||
{ action: 'plugins::documentation.settings.regenerate', subject: null },
|
||||
],
|
||||
regenerate: [{ action: 'plugins::documentation.settings.regenerate', subject: null }],
|
||||
update: [{ action: 'plugins::documentation.settings.update', subject: null }],
|
||||
};
|
||||
|
||||
export default pluginPermissions;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user