Skip to content

Commit

Permalink
Have canUser() return undefined by default
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks committed Jan 23, 2019
1 parent 8797da3 commit 7b8d040
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 30 deletions.
14 changes: 8 additions & 6 deletions docs/designers-developers/developers/data/data-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ get back from the oEmbed preview API.

Is the preview for the URL an oEmbed link fallback.

### hasUploadPermissions
### hasUploadPermissions (deprecated)

Returns whether the current user can upload media.

Expand All @@ -145,6 +145,11 @@ Calling this may trigger an OPTIONS request to the REST API via the

https://developer.wordpress.org/rest-api/reference/

*Deprecated*

Deprecated since 4.9. Callers should use the more generic `canUser()` selector instead of
`hasUploadPermissions()`, e.g. `canUser( 'create', 'media' )`.

*Parameters*

* state: Data state.
Expand All @@ -170,14 +175,11 @@ https://developer.wordpress.org/rest-api/reference/
* action: Action to check. One of: 'create', 'read', 'update', 'delete'.
* resource: REST resource to check, e.g. 'media' or 'posts'.
* id: Optional ID of the rest resource to check.
* defaultIsAllowed: What to return when we don't know if the current user can
perform the given action on the given resource. Defaults to
`false`.

*Returns*

Whether or not the user can perform the action, or `defaultIsAllowed` if the
OPTIONS request is still being made.
Whether or not the user can perform the action,
or `undefined` if the OPTIONS request is still being made.

## Actions

Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/block/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export default compose( [
isFetching: isFetchingReusableBlock( ref ),
isSaving: isSavingReusableBlock( ref ),
block: reusableBlock ? getBlock( reusableBlock.clientId ) : null,
canUpdateBlock: !! reusableBlock && ! reusableBlock.isTemporary && canUser( 'update', 'blocks', ref ),
canUpdateBlock: !! reusableBlock && ! reusableBlock.isTemporary && !! canUser( 'update', 'blocks', ref ),
};
} ),
withDispatch( ( dispatch, ownProps ) => {
Expand Down
15 changes: 6 additions & 9 deletions packages/core-data/src/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import createSelector from 'rememo';
import { map, find, get, filter, compact } from 'lodash';
import { map, find, get, filter, compact, defaultTo } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -191,7 +191,7 @@ export function hasUploadPermissions( state ) {
deprecated( "select( 'core' ).hasUploadPermissions()", {
alternative: "select( 'core' ).canUser( 'create', 'media' )",
} );
return canUser( state, 'create', 'media', undefined, true );
return defaultTo( canUser( state, 'create', 'media' ), true );
}

/**
Expand All @@ -207,14 +207,11 @@ export function hasUploadPermissions( state ) {
* @param {string} action Action to check. One of: 'create', 'read', 'update', 'delete'.
* @param {string} resource REST resource to check, e.g. 'media' or 'posts'.
* @param {string=} id Optional ID of the rest resource to check.
* @param {boolean=} defaultIsAllowed What to return when we don't know if the current user can
* perform the given action on the given resource. Defaults to
* `false`.
*
* @return {boolean} Whether or not the user can perform the action, or `defaultIsAllowed` if the
* OPTIONS request is still being made.
* @return {boolean|undefined} Whether or not the user can perform the action,
* or `undefined` if the OPTIONS request is still being made.
*/
export function canUser( state, action, resource, id = undefined, defaultIsAllowed = false ) {
export function canUser( state, action, resource, id ) {
const key = compact( [ action, resource, id ] ).join( '/' );
return get( state, [ 'userPermissions', key ], defaultIsAllowed );
return get( state, [ 'userPermissions', key ] );
}
11 changes: 2 additions & 9 deletions packages/core-data/src/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,11 @@ describe( 'isPreviewEmbedFallback()', () => {
} );

describe( 'canUser', () => {
it( 'returns false by default', () => {
it( 'returns undefined by default', () => {
const state = deepFreeze( {
userPermissions: {},
} );
expect( canUser( state, 'create', 'media' ) ).toBe( false );
} );

it( 'returns true by default if defaultIsAllowed is specified', () => {
const state = deepFreeze( {
userPermissions: {},
} );
expect( canUser( state, 'create', 'media', undefined, true ) ).toBe( true );
expect( canUser( state, 'create', 'media' ) ).toBe( undefined );
} );

it( 'returns whether an action can be performed', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export default compose( [
) ) &&

// Hide 'Add to Reusable Blocks' when current doesn't have permission to do that
canUser( 'create', 'blocks' )
!! canUser( 'create', 'blocks' )
);

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default compose( [
null;

return {
isVisible: !! reusableBlock && canUser( 'delete', 'blocks', reusableBlock.id ),
isVisible: !! reusableBlock && !! canUser( 'delete', 'blocks', reusableBlock.id ),
isDisabled: reusableBlock && reusableBlock.isTemporary,
};
} ),
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/src/components/media-placeholder/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { every, get, noop, startsWith } from 'lodash';
import { every, get, noop, startsWith, defaultTo } from 'lodash';
import classnames from 'classnames';

/**
Expand Down Expand Up @@ -261,7 +261,7 @@ const applyWithSelect = withSelect( ( select ) => {
const { canUser } = select( 'core' );

return {
hasUploadPermissions: canUser( 'create', 'media', undefined, true ),
hasUploadPermissions: defaultTo( canUser( 'create', 'media' ), true ),
};
} );

Expand Down
7 changes: 6 additions & 1 deletion packages/editor/src/components/media-upload/check.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import { defaultTo } from 'lodash';

/**
* WordPress dependencies
*/
Expand All @@ -11,6 +16,6 @@ export default withSelect( ( select ) => {
const { canUser } = select( 'core' );

return {
hasUploadPermissions: canUser( 'create', 'media', undefined, true ),
hasUploadPermissions: defaultTo( canUser( 'create', 'media' ), true ),
};
} )( MediaUploadCheck );

0 comments on commit 7b8d040

Please sign in to comment.