Skip to content

Commit

Permalink
Remove change_resourcebase permission requirement as prerequisite f…
Browse files Browse the repository at this point in the history
…or Save As/Clone (#872)
  • Loading branch information
DavidQuartz authored Mar 15, 2022
1 parent 22d0678 commit 199851c
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ function ActionButtons({
onDownload
}) {

if (options?.length === 1 && options?.[0].action === 'download' && !resource.download_url) {
return null;
// do not render if the options only contain download or copy or both without meeting their requirements
if (options?.every(({action}) => action !== 'delete')) {
if (options?.every(({action}) => (action === 'download' && !resource.download_url) || (['copy'].includes(action) && !resource.is_copyable & !resource.download_url))) {
return null;
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('ALink test', () => {
});

it('render Action Button with defaults', () => {
ReactDOM.render(<ActionButtons />, document.getElementById('container'));
ReactDOM.render(<ActionButtons options={[{action: 'delete'}]} />, document.getElementById('container'));
const actionButton = document.querySelector(
'.gn-resource-action-buttons'
);
Expand Down
2 changes: 1 addition & 1 deletion geonode_mapstore_client/client/js/plugins/SaveAs.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ const ConnectedSaveAsButton = connect(
getResourceData,
getResourceDirtyState,
(loggedIn, userCanAddResource, resource, dirtyState) => ({
enabled: loggedIn && userCanAddResource,
enabled: loggedIn && userCanAddResource && resource?.is_copyable,
resource,
disabled: !!dirtyState
})
Expand Down
6 changes: 4 additions & 2 deletions geonode_mapstore_client/client/js/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import {
} from '@js/plugins/actionnavbar/buttons';
import { getMetadataUrl,
getMetadataDetailUrl,
resourceHasPermission } from '@js/utils/ResourceUtils';
resourceHasPermission,
canCopyResource } from '@js/utils/ResourceUtils';

const EXCLUDED_EPICS_NAMES = [
'loadGeostoryEpic',
Expand Down Expand Up @@ -414,7 +415,8 @@ const pluginsDefinition = {
requires: {
getMetadataUrl,
getMetadataDetailUrl,
resourceHasPermission
resourceHasPermission,
canCopyResource
},
epics: {},
reducers: {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function SaveModal({
if (copy && currentModal?.current) {
currentModal?.current?.addEventListener('keyup', (event) => {
if (event.keyCode === 13) {
event.preventDefault();
event.stopPropagation();
onSave(
update ? contentId : undefined,
{
Expand Down
10 changes: 10 additions & 0 deletions geonode_mapstore_client/client/js/utils/ResourceUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -577,3 +577,13 @@ export const parseMapConfig = (mapResponse, resource = {}) => {
type: 'map'
};
};

/**
* Util to check if resosurce can be cloned (Save As)
* Requirements for copying are 'add_resource' permission and is_copyable property on resource
*/
export const canCopyResource = (resource, user) => {
const canAdd = user?.perms?.includes('add_resource');
const canCopy = resource?.is_copyable;
return (canAdd && canCopy) ? true : false;
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
toGeoNodeMapConfig,
compareBackgroundLayers,
toMapStoreMapConfig,
parseStyleName
parseStyleName,
canCopyResource
} from '../ResourceUtils';

describe('Test Resource Utils', () => {
Expand Down Expand Up @@ -415,4 +416,11 @@ describe('Test Resource Utils', () => {

expect(pasrsedStyleName).toBe('test:testName');
});

it('should test canCopyResource', () => {
const resource = { is_copyable: true };
const user = { perms: ['add_resource'] };

expect(canCopyResource(resource, user)).toEqual(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -449,13 +449,7 @@
"action": "copy",
"labelId": "gnviewer.clone",
"icon": "copy",
"authenticated": true,
"perms": [
{
"type": "resource",
"value": "change_resourcebase"
}
]
"authenticated": true
},
{
"type": "button",
Expand Down Expand Up @@ -852,7 +846,7 @@
{
"labelId": "gnviewer.save",
"showPendingChangesIcon": true,
"disableIf": "{!context.resourceHasPermission(state('gnResourceData'), 'change_resourcebase')}",
"disableIf": "{!context.resourceHasPermission(state('gnResourceData'), 'change_resourcebase') && !context.canCopyResource(state('gnResourceData'), state('user'))}",
"type": "dropdown",
"items": [
{
Expand All @@ -861,8 +855,7 @@
},
{
"type": "plugin",
"name": "SaveAs",
"disableIf": "{state('gnResourceData') && state('gnResourceData').is_copyable ? false : true}"
"name": "SaveAs"
}
]
},
Expand Down Expand Up @@ -1571,7 +1564,7 @@
{
"labelId": "gnviewer.save",
"showPendingChangesIcon": true,
"disableIf": "{!state('isNewResource') && !context.resourceHasPermission(state('gnResourceData'), 'change_resourcebase')}",
"disableIf": "{!context.resourceHasPermission(state('gnResourceData'), 'change_resourcebase') && !context.canCopyResource(state('gnResourceData'), state('user'))}",
"type": "dropdown",
"items": [
{
Expand Down Expand Up @@ -2030,7 +2023,7 @@
{
"labelId": "gnviewer.save",
"showPendingChangesIcon": true,
"disableIf": "{!state('isNewResource') && !context.resourceHasPermission(state('gnResourceData'), 'change_resourcebase')}",
"disableIf": "{!context.resourceHasPermission(state('gnResourceData'), 'change_resourcebase') && !context.canCopyResource(state('gnResourceData'), state('user'))}",
"type": "dropdown",
"items": [
{
Expand Down Expand Up @@ -2215,16 +2208,15 @@
"labelId": "gnviewer.save",
"type": "dropdown",
"showPendingChangesIcon": true,
"disableIf": "{!context.resourceHasPermission(state('gnResourceData'), 'change_resourcebase')}",
"disableIf": "{!context.resourceHasPermission(state('gnResourceData'), 'change_resourcebase') && !context.canCopyResource(state('gnResourceData'), state('user'))}",
"items": [
{
"type": "plugin",
"name": "Save"
},
{
"type": "plugin",
"name": "SaveAs",
"disableIf": "{state('gnResourceData') && state('gnResourceData').is_copyable ? false : true}"
"name": "SaveAs"
}
]
},
Expand Down

0 comments on commit 199851c

Please sign in to comment.