Skip to content

Commit

Permalink
Merge pull request #29 from 10up/feature/restrict-editing-to-admin
Browse files Browse the repository at this point in the history
fix only users that have capability to `manage_options` can change auth settings
  • Loading branch information
Ryan Welcher committed Jun 19, 2020
2 parents 02b4dac + 3d2c909 commit 8581118
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
2 changes: 1 addition & 1 deletion includes/rest_routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,5 +249,5 @@ function update_apple_maps_wordpress_key_id( $request ) {
* Check wether user can Edit Posts
*/
function check_permissions() {
return current_user_can( 'edit_posts' );
return current_user_can( 'manage_options' );
}
15 changes: 9 additions & 6 deletions src/Settings/AuthenticationSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ import { PanelBody } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

import EditAuthForm from '../components/EditAuthForm';
import IsAdmin from '../helper';

export default function AuthenticationSettings() {
return (
<PanelBody
title={ __( 'Authentication', 'apple-maps-wordpress' ) }
initialOpen={ false }
>
<EditAuthForm />
</PanelBody>
<IsAdmin>
<PanelBody
title={ __( 'Authentication', 'apple-maps-wordpress' ) }
initialOpen={ false }
>
<EditAuthForm />
</PanelBody>
</IsAdmin>
);
}
13 changes: 10 additions & 3 deletions src/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useEffect, useRef, useState } from '@wordpress/element';
import { AppleMapEdit } from './components/AppleMap';
import EditAuthForm from './components/EditAuthForm';
import InspectorSettings from './inspector-settings';
import IsAdmin from './helper';

export default function AppleMapsWordPressEdit( props ) {
const {
Expand Down Expand Up @@ -132,7 +133,11 @@ export default function AppleMapsWordPressEdit( props ) {
) }
icon={ 'location-alt' }
instructions={
<>
<IsAdmin
fallback={ __(
'Sorry you are not allowed to do that. Please talk to your Administrator'
) }
>
{ __(
'In order to use an Apple Map on your website you need to get some credentials from Apple. Here you can find a detailed documentation on how to get these keys: ',
'apple-maps-wordpress'
Expand All @@ -147,11 +152,13 @@ export default function AppleMapsWordPressEdit( props ) {
'apple-maps-wordpress'
) }
</a>{ ' ' }
</>
</IsAdmin>
}
isColumnLayout={ true }
>
<EditAuthForm />
<IsAdmin>
<EditAuthForm />
</IsAdmin>
</Placeholder>
</>
);
Expand Down
17 changes: 17 additions & 0 deletions src/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useSelect } from '@wordpress/data';

export default function IsAdmin( { children, fallback } ) {
const canCreateUsers = useSelect( ( select ) =>
select( 'core' ).canUser( 'update', 'settings' )
);

if ( canCreateUsers ) {
return children;
}

if ( fallback ) {
return fallback;
}

return null;
}

0 comments on commit 8581118

Please sign in to comment.