Merge pull request #17237 from noobCode-69/setting-permission

Fix settings menu permissions in CE
This commit is contained in:
Gustav Hansen 2023-07-10 13:51:49 +02:00 committed by GitHub
commit f1c4c09e50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 32 deletions

View File

@ -1,4 +1,4 @@
import { useState, useEffect } from 'react'; import { useState, useEffect, useCallback } from 'react';
import { hasPermissions, useRBACProvider, useStrapiApp, useAppInfo } from '@strapi/helper-plugin'; import { hasPermissions, useRBACProvider, useStrapiApp, useAppInfo } from '@strapi/helper-plugin';
import { useSelector } from 'react-redux'; import { useSelector } from 'react-redux';
@ -19,12 +19,26 @@ const useSettingsMenu = () => {
const { shouldUpdateStrapi } = useAppInfo(); const { shouldUpdateStrapi } = useAppInfo();
const { settings } = useStrapiApp(); const { settings } = useStrapiApp();
const permissions = useSelector(selectAdminPermissions); const permissions = useSelector(selectAdminPermissions);
const { global: globalLinks, admin: adminLinks } = useEnterprise( const { global: globalLinks, admin: adminLinks } = useEnterprise(
LINKS_CE, LINKS_CE,
async () => (await import('../../../../ee/admin/hooks/useSettingsMenu/constants')).LINKS_EE, async () => (await import('../../../../ee/admin/hooks/useSettingsMenu/constants')).LINKS_EE,
{ {
combine(ceLinks, eeLinks) { combine(ceLinks, eeLinks) {
function addPermissions(link) { return {
admin: [...eeLinks.admin, ...ceLinks.admin],
global: [...ceLinks.global, ...eeLinks.global],
};
},
defaultValue: {
admin: [],
global: [],
},
}
);
const addPermissions = useCallback(
(link) => {
if (!link.id) { if (!link.id) {
throw new Error('The settings menu item must have an id attribute.'); throw new Error('The settings menu item must have an id attribute.');
} }
@ -33,18 +47,8 @@ const useSettingsMenu = () => {
...link, ...link,
permissions: permissions.settings?.[link.id]?.main, permissions: permissions.settings?.[link.id]?.main,
}; };
}
return {
admin: [...eeLinks.admin, ...ceLinks.admin].map(addPermissions),
global: [...ceLinks.global, ...eeLinks.global].map(addPermissions),
};
}, },
defaultValue: { [permissions.settings]
admin: [],
global: [],
},
}
); );
useEffect(() => { useEffect(() => {
@ -86,26 +90,36 @@ const useSettingsMenu = () => {
}; };
const { global, ...otherSections } = settings; const { global, ...otherSections } = settings;
const sections = formatLinks([ const sections = formatLinks([
{ {
...settings.global, ...settings.global,
links: sortLinks([...settings.global.links, ...globalLinks]).map((link) => ({ links: sortLinks([...settings.global.links, ...globalLinks.map(addPermissions)]).map(
(link) => ({
...link, ...link,
hasNotification: link.id === '000-application-infos' && shouldUpdateStrapi, hasNotification: link.id === '000-application-infos' && shouldUpdateStrapi,
})), })
),
}, },
{ {
id: 'permissions', id: 'permissions',
intlLabel: { id: 'Settings.permissions', defaultMessage: 'Administration Panel' }, intlLabel: { id: 'Settings.permissions', defaultMessage: 'Administration Panel' },
links: adminLinks, links: adminLinks.map(addPermissions),
}, },
...Object.values(otherSections), ...Object.values(otherSections),
]); ]);
getData(); getData();
}, [adminLinks, globalLinks, userPermissions, settings, shouldUpdateStrapi]); }, [adminLinks, globalLinks, userPermissions, settings, shouldUpdateStrapi, addPermissions]);
return { isLoading, menu }; const filterMenu = (menuItem) => {
return {
...menuItem,
links: menuItem.links.filter((link) => link.isDisplayed),
};
};
return { isLoading, menu: menu.map(filterMenu) };
}; };
export default useSettingsMenu; export default useSettingsMenu;

View File

@ -50,7 +50,8 @@ const SettingsNav = ({ menu }) => {
<SubNavSections> <SubNavSections>
{sections.map((section) => ( {sections.map((section) => (
<SubNavSection key={section.id} label={formatMessage(section.intlLabel)}> <SubNavSection key={section.id} label={formatMessage(section.intlLabel)}>
{section.links.map((link) => ( {section.links.map((link) => {
return (
<SubNavLink <SubNavLink
as={NavLink} as={NavLink}
withBullet={link.hasNotification} withBullet={link.hasNotification}
@ -60,7 +61,8 @@ const SettingsNav = ({ menu }) => {
> >
{formatMessage(link.intlLabel)} {formatMessage(link.intlLabel)}
</SubNavLink> </SubNavLink>
))} );
})}
</SubNavSection> </SubNavSection>
))} ))}
</SubNavSections> </SubNavSections>