Skip to content

Commit

Permalink
Add: Permission checks to avoid 403 errors on non admin roles.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Jul 11, 2024
1 parent 63c8ad1 commit b06762d
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 24 deletions.
6 changes: 4 additions & 2 deletions packages/edit-post/src/store/private-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ export const getEditedPostTemplateId = createRegistrySelector(
type: postType,
slug,
} = select( editorStore ).getCurrentPost();
const { getSite, getEntityRecords } = select( coreStore );
const siteSettings = getSite();
const { getSite, getEntityRecords, canUser } = select( coreStore );
const siteSettings = canUser( 'read', 'settings' )
? getSite()
: undefined;
// First check if the current page is set as the posts page.
const isPostsPage = +postId === siteSettings?.page_for_posts;
if ( isPostsPage ) {
Expand Down
6 changes: 4 additions & 2 deletions packages/editor/src/components/blog-title/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ export default function BlogTitle() {
const { editEntityRecord } = useDispatch( coreStore );
const { postsPageTitle, postsPageId, isTemplate, postSlug } = useSelect(
( select ) => {
const { getEntityRecord, getEditedEntityRecord } =
const { getEntityRecord, getEditedEntityRecord, canUser } =
select( coreStore );
const siteSettings = getEntityRecord( 'root', 'site' );
const siteSettings = canUser( 'read', 'settings' )
? getEntityRecord( 'root', 'site' )
: undefined;
const _postsPageRecord = siteSettings?.page_for_posts
? getEditedEntityRecord(
'postType',
Expand Down
42 changes: 30 additions & 12 deletions packages/editor/src/components/global-styles-provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,25 @@ export function mergeBaseAndUserConfigs( base, user ) {
function useGlobalStylesUserConfig() {
const { globalStylesId, isReady, settings, styles, _links } = useSelect(
( select ) => {
const { getEditedEntityRecord, hasFinishedResolution } =
select( coreStore );
const {
getEditedEntityRecord,
hasFinishedResolution,
getUser,
getCurrentUser,
} = select( coreStore );
const _globalStylesId =
select( coreStore ).__experimentalGetCurrentGlobalStylesId();
const record = _globalStylesId
? getEditedEntityRecord(
'root',
'globalStyles',
_globalStylesId
)
: undefined;
const userId = getCurrentUser()?.id;
const canEditThemeOptions =
userId && getUser( userId )?.capabilities?.edit_theme_options;
const record =
_globalStylesId && canEditThemeOptions
? getEditedEntityRecord(
'root',
'globalStyles',
_globalStylesId
)
: undefined;

let hasResolved = false;
if (
Expand Down Expand Up @@ -126,9 +134,19 @@ function useGlobalStylesUserConfig() {

function useGlobalStylesBaseConfig() {
const baseConfig = useSelect( ( select ) => {
return select(
coreStore
).__experimentalGetCurrentThemeBaseGlobalStyles();
const {
getCurrentUser,
getUser,
__experimentalGetCurrentThemeBaseGlobalStyles,
} = select( coreStore );
const userId = getCurrentUser()?.id;
const canEditThemeOptions =
userId && getUser( userId )?.capabilities?.edit_theme_options;

return (
canEditThemeOptions &&
__experimentalGetCurrentThemeBaseGlobalStyles()
);
}, [] );

return [ !! baseConfig, baseConfig ];
Expand Down
5 changes: 4 additions & 1 deletion packages/editor/src/components/post-card-panel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ export default function PostCardPanel( { actions } ) {
getCurrentPostId,
__experimentalGetTemplateInfo,
} = select( editorStore );
const { canUser } = select( coreStore );
const { getEditedEntityRecord } = select( coreStore );
const siteSettings = getEditedEntityRecord( 'root', 'site' );
const siteSettings = canUser( 'read', 'settings' )
? getEditedEntityRecord( 'root', 'site' )
: undefined;
const _type = getCurrentPostType();
const _id = getCurrentPostId();
const _record = getEditedEntityRecord( 'postType', _type, _id );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ export default function PostContentInformation() {
const { postContent } = useSelect( ( select ) => {
const { getEditedPostAttribute, getCurrentPostType, getCurrentPostId } =
select( editorStore );
const { canUser } = select( coreStore );
const { getEntityRecord } = select( coreStore );
const siteSettings = getEntityRecord( 'root', 'site' );
const siteSettings = canUser( 'read', 'settings' )
? getEntityRecord( 'root', 'site' )
: undefined;
const postType = getCurrentPostType();
const _id = getCurrentPostId();
const isPostsPage = +_id === siteSettings?.page_for_posts;
Expand Down
6 changes: 4 additions & 2 deletions packages/editor/src/components/post-url/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ export default function PostURLPanel() {
function PostURLToggle( { isOpen, onClick } ) {
const { slug, isFrontPage, postLink } = useSelect( ( select ) => {
const { getCurrentPostId, getCurrentPost } = select( editorStore );
const { getEditedEntityRecord } = select( coreStore );
const siteSettings = getEditedEntityRecord( 'root', 'site' );
const { getEditedEntityRecord, canUser } = select( coreStore );
const siteSettings = canUser( 'read', 'settings' )
? getEditedEntityRecord( 'root', 'site' )
: undefined;
const _id = getCurrentPostId();
return {
slug: select( editorStore ).getEditedPostSlug(),
Expand Down
6 changes: 4 additions & 2 deletions packages/editor/src/components/posts-per-page/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ export default function PostsPerPage() {
const { postsPerPage, isTemplate, postSlug } = useSelect( ( select ) => {
const { getEditedPostAttribute, getCurrentPostType } =
select( editorStore );
const { getEditedEntityRecord } = select( coreStore );
const siteSettings = getEditedEntityRecord( 'root', 'site' );
const { getEditedEntityRecord, canUser } = select( coreStore );
const siteSettings = canUser( 'read', 'settings' )
? getEditedEntityRecord( 'root', 'site' )
: undefined;
return {
isTemplate: getCurrentPostType() === TEMPLATE_POST_TYPE,
postSlug: getEditedPostAttribute( 'slug' ),
Expand Down
6 changes: 4 additions & 2 deletions packages/editor/src/components/site-discussion/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ export default function SiteDiscussion() {
( select ) => {
const { getEditedPostAttribute, getCurrentPostType } =
select( editorStore );
const { getEditedEntityRecord } = select( coreStore );
const siteSettings = getEditedEntityRecord( 'root', 'site' );
const { getEditedEntityRecord, canUser } = select( coreStore );
const siteSettings = canUser( 'read', 'settings' )
? getEditedEntityRecord( 'root', 'site' )
: undefined;
return {
isTemplate: getCurrentPostType() === TEMPLATE_POST_TYPE,
postSlug: getEditedPostAttribute( 'slug' ),
Expand Down

0 comments on commit b06762d

Please sign in to comment.