mirror of
https://github.com/strapi/strapi.git
synced 2025-12-28 15:44:59 +00:00
Add permissions for update
Signed-off-by: soupette <cyril.lpz@gmail.com>
This commit is contained in:
parent
5dd399d803
commit
577471142d
@ -10,7 +10,8 @@
|
||||
},
|
||||
"attributes": {
|
||||
"name": {
|
||||
"type": "string"
|
||||
"type": "string",
|
||||
"required": true
|
||||
},
|
||||
"content": {
|
||||
"type": "component",
|
||||
|
||||
52
examples/getstarted/api/dummy/config/routes.json
Normal file
52
examples/getstarted/api/dummy/config/routes.json
Normal file
@ -0,0 +1,52 @@
|
||||
{
|
||||
"routes": [
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/dummies",
|
||||
"handler": "dummy.find",
|
||||
"config": {
|
||||
"policies": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/dummies/count",
|
||||
"handler": "dummy.count",
|
||||
"config": {
|
||||
"policies": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/dummies/:id",
|
||||
"handler": "dummy.findOne",
|
||||
"config": {
|
||||
"policies": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"method": "POST",
|
||||
"path": "/dummies",
|
||||
"handler": "dummy.create",
|
||||
"config": {
|
||||
"policies": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"method": "PUT",
|
||||
"path": "/dummies/:id",
|
||||
"handler": "dummy.update",
|
||||
"config": {
|
||||
"policies": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"method": "DELETE",
|
||||
"path": "/dummies/:id",
|
||||
"handler": "dummy.delete",
|
||||
"config": {
|
||||
"policies": []
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
8
examples/getstarted/api/dummy/controllers/dummy.js
Normal file
8
examples/getstarted/api/dummy/controllers/dummy.js
Normal file
@ -0,0 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Read the documentation (https://strapi.io/documentation/v3.x/concepts/controllers.html#core-controllers)
|
||||
* to customize this controller
|
||||
*/
|
||||
|
||||
module.exports = {};
|
||||
8
examples/getstarted/api/dummy/models/dummy.js
Normal file
8
examples/getstarted/api/dummy/models/dummy.js
Normal file
@ -0,0 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Read the documentation (https://strapi.io/documentation/v3.x/concepts/models.html#lifecycle-hooks)
|
||||
* to customize this model
|
||||
*/
|
||||
|
||||
module.exports = {};
|
||||
42
examples/getstarted/api/dummy/models/dummy.settings.json
Normal file
42
examples/getstarted/api/dummy/models/dummy.settings.json
Normal file
@ -0,0 +1,42 @@
|
||||
{
|
||||
"kind": "collectionType",
|
||||
"collectionName": "dummies",
|
||||
"info": {
|
||||
"name": "dummy"
|
||||
},
|
||||
"options": {
|
||||
"increments": true,
|
||||
"timestamps": true
|
||||
},
|
||||
"attributes": {
|
||||
"name": {
|
||||
"type": "string",
|
||||
"required": true
|
||||
},
|
||||
"notrequired": {
|
||||
"type": "string"
|
||||
},
|
||||
"required": {
|
||||
"type": "component",
|
||||
"repeatable": true,
|
||||
"component": "default.dish",
|
||||
"required": false,
|
||||
"min": 1
|
||||
},
|
||||
"dz": {
|
||||
"type": "dynamiczone",
|
||||
"components": [
|
||||
"default.dish"
|
||||
],
|
||||
"required": true,
|
||||
"min": 1
|
||||
},
|
||||
"dz2": {
|
||||
"type": "dynamiczone",
|
||||
"components": [
|
||||
"blog.subsub"
|
||||
],
|
||||
"min": 2
|
||||
}
|
||||
}
|
||||
}
|
||||
8
examples/getstarted/api/dummy/services/dummy.js
Normal file
8
examples/getstarted/api/dummy/services/dummy.js
Normal file
@ -0,0 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Read the documentation (https://strapi.io/documentation/v3.x/concepts/services.html#core-services)
|
||||
* to customize this service
|
||||
*/
|
||||
|
||||
module.exports = {};
|
||||
@ -8,7 +8,7 @@
|
||||
"attributes": {
|
||||
"name": {
|
||||
"type": "string",
|
||||
"required": true,
|
||||
"required": false,
|
||||
"default": "My super dish"
|
||||
},
|
||||
"description": {
|
||||
|
||||
@ -13,6 +13,7 @@ function useSelect(name) {
|
||||
moveComponentUp,
|
||||
moveComponentDown,
|
||||
removeComponentFromDynamicZone,
|
||||
updateActionAllowedFields,
|
||||
} = useDataManager();
|
||||
|
||||
const dynamicDisplayedComponents = useMemo(
|
||||
@ -21,10 +22,10 @@ function useSelect(name) {
|
||||
);
|
||||
|
||||
const isFieldAllowed = useMemo(() => {
|
||||
const allowedFields = isCreatingEntry ? createActionAllowedFields : [];
|
||||
const allowedFields = isCreatingEntry ? createActionAllowedFields : updateActionAllowedFields;
|
||||
|
||||
return allowedFields.includes(name);
|
||||
}, [name, isCreatingEntry, createActionAllowedFields]);
|
||||
}, [name, isCreatingEntry, createActionAllowedFields, updateActionAllowedFields]);
|
||||
|
||||
return {
|
||||
addComponentToDynamicZone,
|
||||
|
||||
@ -8,11 +8,12 @@ function useSelect({ isFromDynamicZone, name }) {
|
||||
isCreatingEntry,
|
||||
modifiedData,
|
||||
removeComponentFromField,
|
||||
updateActionAllowedFields,
|
||||
} = useDataManager();
|
||||
|
||||
const allowedFields = useMemo(() => {
|
||||
return isCreatingEntry ? createActionAllowedFields : [];
|
||||
}, [isCreatingEntry, createActionAllowedFields]);
|
||||
return isCreatingEntry ? createActionAllowedFields : updateActionAllowedFields;
|
||||
}, [isCreatingEntry, createActionAllowedFields, updateActionAllowedFields]);
|
||||
|
||||
const componentValue = get(modifiedData, name, null);
|
||||
|
||||
|
||||
@ -9,11 +9,12 @@ function useSelect(keys) {
|
||||
isCreatingEntry,
|
||||
modifiedData,
|
||||
onChange,
|
||||
updateActionAllowedFields,
|
||||
} = useDataManager();
|
||||
|
||||
const allowedFields = useMemo(() => {
|
||||
return isCreatingEntry ? createActionAllowedFields : [];
|
||||
}, [isCreatingEntry, createActionAllowedFields]);
|
||||
return isCreatingEntry ? createActionAllowedFields : updateActionAllowedFields;
|
||||
}, [isCreatingEntry, createActionAllowedFields, updateActionAllowedFields]);
|
||||
|
||||
const value = get(modifiedData, keys, null);
|
||||
|
||||
|
||||
@ -2,17 +2,27 @@ import { useMemo } from 'react';
|
||||
import useDataManager from '../../../hooks/useDataManager';
|
||||
|
||||
function useSelect({ isUserAllowedToEditField, name }) {
|
||||
const { isCreatingEntry, createActionAllowedFields } = useDataManager();
|
||||
const {
|
||||
isCreatingEntry,
|
||||
createActionAllowedFields,
|
||||
updateActionAllowedFields,
|
||||
} = useDataManager();
|
||||
|
||||
const isFieldAllowed = useMemo(() => {
|
||||
if (isUserAllowedToEditField === true) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const allowedFields = isCreatingEntry ? createActionAllowedFields : [];
|
||||
const allowedFields = isCreatingEntry ? createActionAllowedFields : updateActionAllowedFields;
|
||||
|
||||
return allowedFields.includes(name);
|
||||
}, [isCreatingEntry, createActionAllowedFields, name, isUserAllowedToEditField]);
|
||||
}, [
|
||||
isCreatingEntry,
|
||||
createActionAllowedFields,
|
||||
name,
|
||||
isUserAllowedToEditField,
|
||||
updateActionAllowedFields,
|
||||
]);
|
||||
|
||||
return {
|
||||
isFieldAllowed,
|
||||
|
||||
@ -52,7 +52,9 @@ const Header = () => {
|
||||
/* eslint-enable indent */
|
||||
|
||||
const headerTitle = useMemo(() => {
|
||||
return isSingleType ? currentContentTypeName : entryHeaderTitle;
|
||||
const title = isSingleType ? currentContentTypeName : entryHeaderTitle;
|
||||
|
||||
return title || currentContentTypeName;
|
||||
}, [currentContentTypeName, entryHeaderTitle, isSingleType]);
|
||||
|
||||
const headerActions = useMemo(() => {
|
||||
|
||||
@ -70,8 +70,6 @@ const EditViewDataManagerProvider = ({
|
||||
},
|
||||
]);
|
||||
|
||||
console.log({ dataManager: get(matchingPermissions, ['0', 'fields'], []) });
|
||||
|
||||
return get(matchingPermissions, ['0', 'fields'], []);
|
||||
}, [userPermissions, slug]);
|
||||
|
||||
@ -88,16 +86,19 @@ const EditViewDataManagerProvider = ({
|
||||
}, [isLoadingForPermissions, isCreatingEntry, canCreate]);
|
||||
|
||||
// TODO
|
||||
// const updateActionMatchingPermissions = useMemo(
|
||||
// () =>
|
||||
// findMatchingPermissions(userPermissions, [
|
||||
// {
|
||||
// action: 'plugins::content-manager.explorer.update',
|
||||
// subject: slug,
|
||||
// },
|
||||
// ]),
|
||||
// [slug, userPermissions]
|
||||
// );
|
||||
const updateActionAllowedFields = useMemo(() => {
|
||||
const matchingPermissions = findMatchingPermissions(userPermissions, [
|
||||
{
|
||||
action: 'plugins::content-manager.explorer.update',
|
||||
subject: slug,
|
||||
},
|
||||
]);
|
||||
|
||||
return get(matchingPermissions, ['0', 'fields'], []);
|
||||
}, [slug, userPermissions]);
|
||||
|
||||
console.log('dataManager', { createActionAllowedFields, updateActionAllowedFields });
|
||||
|
||||
// const readMatchingPermissions = useMemo(
|
||||
// () =>
|
||||
// findMatchingPermissions(userPermissions, [
|
||||
@ -116,6 +117,8 @@ const EditViewDataManagerProvider = ({
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [shouldCheckErrors]);
|
||||
|
||||
console.log({ modifiedData });
|
||||
|
||||
useEffect(() => {
|
||||
const fetchData = async () => {
|
||||
try {
|
||||
@ -537,6 +540,8 @@ const EditViewDataManagerProvider = ({
|
||||
return <Redirect to="/" />;
|
||||
}
|
||||
|
||||
console.log({ modifiedData });
|
||||
|
||||
return (
|
||||
<EditViewDataManagerContext.Provider
|
||||
value={{
|
||||
@ -570,6 +575,7 @@ const EditViewDataManagerProvider = ({
|
||||
shouldShowLoadingState,
|
||||
slug,
|
||||
triggerFormValidation,
|
||||
updateActionAllowedFields,
|
||||
}}
|
||||
>
|
||||
{showLoader ? (
|
||||
|
||||
@ -140,12 +140,47 @@ const createYupSchema = (model, { components }, isCreatingEntry = true) => {
|
||||
const { max, min } = attribute;
|
||||
|
||||
if (attribute.required) {
|
||||
dynamicZoneSchema = dynamicZoneSchema.required();
|
||||
// dynamicZoneSchema = dynamicZoneSchema.required();
|
||||
dynamicZoneSchema = dynamicZoneSchema.test('required', errorsTrads.required, value => {
|
||||
if (isCreatingEntry) {
|
||||
return value !== null || value !== undefined;
|
||||
}
|
||||
|
||||
if (value === undefined) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return value !== null;
|
||||
});
|
||||
|
||||
if (min) {
|
||||
// TODO remove code
|
||||
// dynamicZoneSchema = dynamicZoneSchema
|
||||
// .min(min, errorsTrads.min)
|
||||
// .required(errorsTrads.required);
|
||||
dynamicZoneSchema = dynamicZoneSchema
|
||||
.min(min, errorsTrads.min)
|
||||
.required(errorsTrads.required);
|
||||
.test('min', errorsTrads.min, value => {
|
||||
if (isCreatingEntry) {
|
||||
return value && value.length > 0;
|
||||
}
|
||||
|
||||
if (value === undefined) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return value !== null && value.length > 0;
|
||||
})
|
||||
.test('required', errorsTrads.required, value => {
|
||||
if (isCreatingEntry) {
|
||||
return value !== null || value !== undefined;
|
||||
}
|
||||
|
||||
if (value === undefined) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return value !== null;
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// eslint-disable-next-line no-lonely-if
|
||||
@ -237,7 +272,18 @@ const createYupSchemaAttribute = (type, validations, isCreatingEntry) => {
|
||||
}
|
||||
|
||||
if (type !== 'password') {
|
||||
schema = schema.required(errorsTrads.required);
|
||||
schema = schema.test('required', errorsTrads.required, value => {
|
||||
if (isCreatingEntry) {
|
||||
return !isEmpty(value);
|
||||
}
|
||||
|
||||
// Field is not touched and the user is editing the entry
|
||||
if (value === undefined) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return !isEmpty(value);
|
||||
});
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user