Skip to content

Commit

Permalink
Adapt get user permissions util to work with any allowed "header"
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Aug 15, 2024
1 parent bd66e07 commit 9fe7bd6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
13 changes: 10 additions & 3 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
forwardResolver,
getNormalizedCommaSeparable,
getUserPermissionCacheKey,
getUserPermissionsFromResponse,
getUserPermissionsFromAllowHeader,
ALLOWED_RESOURCE_ACTIONS,
} from './utils';
import { getSyncProvider } from './sync';
Expand Down Expand Up @@ -173,7 +173,9 @@ export const getEntityRecord =

const response = await apiFetch( { path, parse: false } );
const record = await response.json();
const permissions = getUserPermissionsFromResponse( response );
const permissions = getUserPermissionsFromAllowHeader(
response.headers?.get( 'allow' )
);

registry.batch( () => {
dispatch.receiveEntityRecords( kind, name, record, query );
Expand Down Expand Up @@ -440,7 +442,12 @@ export const canUser =
return;
}

const permissions = getUserPermissionsFromResponse( response );
// Optional chaining operator is used here because the API requests don't
// return the expected result in the React native version. Instead, API requests
// only return the result, without including response properties like the headers.
const permissions = getUserPermissionsFromAllowHeader(
response.headers?.get( 'allow' )
);
registry.batch( () => {
for ( const action of ALLOWED_RESOURCE_ACTIONS ) {
const key = getUserPermissionCacheKey( action, resource, id );
Expand Down
2 changes: 1 addition & 1 deletion packages/core-data/src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ export { default as getNestedValue } from './get-nested-value';
export { default as isNumericID } from './is-numeric-id';
export {
getUserPermissionCacheKey,
getUserPermissionsFromResponse,
getUserPermissionsFromAllowHeader,
ALLOWED_RESOURCE_ACTIONS,
} from './user-permissions';
10 changes: 4 additions & 6 deletions packages/core-data/src/utils/user-permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ export const ALLOWED_RESOURCE_ACTIONS = [
'delete',
];

export function getUserPermissionsFromResponse( response ) {
export function getUserPermissionsFromAllowHeader( allowedMethods ) {
const permissions = {};

// Optional chaining operator is used here because the API requests don't
// return the expected result in the React native version. Instead, API requests
// only return the result, without including response properties like the headers.
const allowedMethods = response.headers?.get( 'allow' ) || '';
if ( ! allowedMethods ) {
return permissions;
}

const methods = {
create: 'POST',
Expand Down

0 comments on commit 9fe7bd6

Please sign in to comment.