Fix relation link with admin users

Signed-off-by: soupette <cyril.lpz@gmail.com>
This commit is contained in:
soupette 2020-09-08 11:33:57 +02:00 committed by Pierre Noël
parent 6718a12242
commit ccebd4f4b1
5 changed files with 54 additions and 10 deletions

View File

@ -1,6 +1,7 @@
import React, { useMemo } from 'react'; import React, { useEffect, useMemo } from 'react';
import { useUserPermissions, LoadingIndicatorPage } from 'strapi-helper-plugin'; import { useUserPermissions, LoadingIndicatorPage } from 'strapi-helper-plugin';
import { Redirect } from 'react-router-dom'; import { Redirect, useLocation } from 'react-router-dom';
import { get } from 'lodash';
import adminPermissions from '../../../permissions'; import adminPermissions from '../../../permissions';
import EditPage from '../EditPage'; import EditPage from '../EditPage';
@ -16,13 +17,23 @@ const ProtectedEditPage = () => {
isLoading, isLoading,
allowedActions: { canRead, canUpdate }, allowedActions: { canRead, canUpdate },
} = useUserPermissions(permissions); } = useUserPermissions(permissions);
const { state } = useLocation();
const from = get(state, 'from', '/');
useEffect(() => {
if (!isLoading) {
if (!canRead && !canUpdate) {
strapi.notification.info('notification.permission.not-allowed-read');
}
}
}, [isLoading, canRead, canUpdate]);
if (isLoading) { if (isLoading) {
return <LoadingIndicatorPage />; return <LoadingIndicatorPage />;
} }
if (!canRead && !canUpdate) { if (!canRead && !canUpdate) {
return <Redirect to="/" />; return <Redirect to={from} />;
} }
return <EditPage canUpdate={canUpdate} />; return <EditPage canUpdate={canUpdate} />;

View File

@ -329,6 +329,7 @@
"notification.form.error.fields": "The form contains some errors", "notification.form.error.fields": "The form contains some errors",
"notification.form.success.fields": "Changes saved", "notification.form.success.fields": "Changes saved",
"notification.link-copied": "Link copied into the clipboard", "notification.link-copied": "Link copied into the clipboard",
"notification.permission.not-allowed-read": "You are not allowed to see this document",
"notification.success.delete": "The item has been deleted", "notification.success.delete": "The item has been deleted",
"notification.success.saved": "Saved", "notification.success.saved": "Saved",
"request.error.model.unknown": "This model doesn't exist" "request.error.model.unknown": "This model doesn't exist"

View File

@ -1,8 +1,10 @@
import React, { memo, useEffect } from 'react'; import React, { memo, useEffect, useMemo } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { useDrag, useDrop } from 'react-dnd'; import { useDrag, useDrop } from 'react-dnd';
import { getEmptyImage } from 'react-dnd-html5-backend'; import { getEmptyImage } from 'react-dnd-html5-backend';
import { has } from 'lodash'; import { has } from 'lodash';
import { useGlobalContext } from 'strapi-helper-plugin';
import pluralize from 'pluralize';
import pluginId from '../../pluginId'; import pluginId from '../../pluginId';
import ItemTypes from '../../utils/ItemTypes'; import ItemTypes from '../../utils/ItemTypes';
@ -18,7 +20,19 @@ function ListItem({
onRemove, onRemove,
targetModel, targetModel,
}) { }) {
const to = `/plugins/${pluginId}/collectionType/${targetModel}/${data.id}`; const { settingsBaseURL } = useGlobalContext();
const to = useMemo(() => {
const isSettingsModel = targetModel.includes('strapi::');
if (isSettingsModel) {
const model = pluralize(targetModel.replace('strapi::', ''));
return `${settingsBaseURL}/${model}/${data.id}`;
}
return `/plugins/${pluginId}/collectionType/${targetModel}/${data.id}`;
}, [targetModel, data.id, settingsBaseURL]);
const hasDraftAndPublish = has(data, 'published_at'); const hasDraftAndPublish = has(data, 'published_at');
const originalIndex = findRelation(data.id).index; const originalIndex = findRelation(data.id).index;

View File

@ -46,7 +46,9 @@ const Option = props => {
return ( return (
<Component {...props}> <Component {...props}>
<Text ellipsis>{props.label}</Text> <Text ellipsis fontWeight={fontWeight}>
{props.label}
</Text>
</Component> </Component>
); );
}; };

View File

@ -1,10 +1,10 @@
/* eslint-disable react-hooks/exhaustive-deps */
import React, { useState, useEffect, useMemo, useRef, memo } from 'react'; import React, { useState, useEffect, useMemo, useRef, memo } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import pluralize from 'pluralize';
import { FormattedMessage } from 'react-intl'; import { FormattedMessage } from 'react-intl';
import { Link, useLocation } from 'react-router-dom'; import { Link, useLocation } from 'react-router-dom';
import { cloneDeep, findIndex, get, isArray, isEmpty, set } from 'lodash'; import { cloneDeep, findIndex, get, isArray, isEmpty, set } from 'lodash';
import { request } from 'strapi-helper-plugin'; import { request, useGlobalContext } from 'strapi-helper-plugin';
import { Flex, Text, Padded } from '@buffetjs/core'; import { Flex, Text, Padded } from '@buffetjs/core';
import pluginId from '../../pluginId'; import pluginId from '../../pluginId';
import useDataManager from '../../hooks/useDataManager'; import useDataManager from '../../hooks/useDataManager';
@ -35,9 +35,12 @@ function SelectWrapper({
targetModel, targetModel,
placeholder, placeholder,
}) { }) {
const { settingsBaseURL } = useGlobalContext();
// Disable the input in case of a polymorphic relation // Disable the input in case of a polymorphic relation
const isMorph = relationType.toLowerCase().includes('morph'); const isMorph = relationType.toLowerCase().includes('morph');
const { addRelation, modifiedData, moveRelation, onChange, onRemoveRelation } = useDataManager(); const { addRelation, modifiedData, moveRelation, onChange, onRemoveRelation } = useDataManager();
const { isDraggingComponent } = useEditView(); const { isDraggingComponent } = useEditView();
// This is needed for making requests when used in a component // This is needed for making requests when used in a component
@ -149,6 +152,7 @@ function SelectWrapper({
return () => { return () => {
abortController.abort(); abortController.abort();
}; };
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [state._contains, isFieldAllowed]); }, [state._contains, isFieldAllowed]);
useEffect(() => { useEffect(() => {
@ -159,6 +163,7 @@ function SelectWrapper({
return () => { return () => {
abortController.abort(); abortController.abort();
}; };
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [state._start]); }, [state._start]);
const onInputChange = (inputValue, { action }) => { const onInputChange = (inputValue, { action }) => {
@ -183,7 +188,18 @@ function SelectWrapper({
relationType relationType
); );
const to = `/plugins/${pluginId}/collectionType/${targetModel}/${value ? value.id : null}`; const to = useMemo(() => {
const isSettingsModel = targetModel.includes('strapi::');
if (isSettingsModel) {
const model = pluralize(targetModel.replace('strapi::', ''));
return `${settingsBaseURL}/${model}/${value ? value.id : null}`;
}
return `/plugins/${pluginId}/collectionType/${targetModel}/${value ? value.id : null}`;
}, [targetModel, value, settingsBaseURL]);
const link = const link =
value === null || value === null ||
value === undefined || value === undefined ||
@ -209,7 +225,7 @@ function SelectWrapper({
} }
return !editable; return !editable;
}, [isMorph, isCreatingEntry, editable]); }, [isMorph, isCreatingEntry, editable, isFieldAllowed, isFieldReadable]);
if (!isFieldAllowed && isCreatingEntry) { if (!isFieldAllowed && isCreatingEntry) {
return <NotAllowedInput label={label} />; return <NotAllowedInput label={label} />;