-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
65 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
export const ALLOWED_RESOURCE_ACTIONS = [ | ||
'create', | ||
'read', | ||
'update', | ||
'delete', | ||
]; | ||
|
||
export function getUserPermissionsFromResponse( response ) { | ||
const permissions = {}; | ||
|
||
// Optional chaining operator is used here because the API requests don't | ||
// return the expected result in the native version. Instead, API requests | ||
// only return the result, without including response properties like the headers. | ||
const allowHeader = response.headers?.get( 'allow' ); | ||
const allowedMethods = allowHeader?.allow || allowHeader || ''; | ||
|
||
const methods = { | ||
create: 'POST', | ||
read: 'GET', | ||
update: 'PUT', | ||
delete: 'DELETE', | ||
}; | ||
for ( const [ actionName, methodName ] of Object.entries( methods ) ) { | ||
permissions[ actionName ] = allowedMethods.includes( methodName ); | ||
} | ||
|
||
return permissions; | ||
} | ||
|
||
export function getUserPermissionCacheKey( action, resource, id ) { | ||
const key = ( | ||
typeof resource === 'object' | ||
? [ action, resource.kind, resource.name, resource.id ] | ||
: [ action, resource, id ] | ||
) | ||
.filter( Boolean ) | ||
.join( '/' ); | ||
|
||
return key; | ||
} |