Skip to content

Commit f47bf46

Browse files
authored
Merge pull request #3711 from tdonohue/port_3693_to_8x
[Port dspace-8_x] Removed unauthorized metadata-export-search request on search page
2 parents 30718cb + 0c81a79 commit f47bf46

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

src/app/shared/search/search-export-csv/search-export-csv.component.spec.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { AuthorizationDataService } from '../../../core/data/feature-authorizati
1313
import { ScriptDataService } from '../../../core/data/processes/script-data.service';
1414
import { getProcessDetailRoute } from '../../../process-page/process-page-routing.paths';
1515
import { Process } from '../../../process-page/processes/process.model';
16-
import { Script } from '../../../process-page/scripts/script.model';
1716
import { NotificationsService } from '../../notifications/notifications.service';
1817
import {
1918
createFailedRemoteDataObject$,
@@ -33,7 +32,6 @@ describe('SearchExportCsvComponent', () => {
3332
let notificationsService;
3433
let router;
3534

36-
const script = Object.assign(new Script(), { id: 'metadata-export-search', name: 'metadata-export-search' });
3735
const process = Object.assign(new Process(), { processId: 5, scriptName: 'metadata-export-search' });
3836

3937
const searchConfig = new PaginatedSearchOptions({
@@ -49,7 +47,7 @@ describe('SearchExportCsvComponent', () => {
4947

5048
function initBeforeEachAsync() {
5149
scriptDataService = jasmine.createSpyObj('scriptDataService', {
52-
findById: createSuccessfulRemoteDataObject$(script),
50+
scriptWithNameExistsAndCanExecute: observableOf(true),
5351
invoke: createSuccessfulRemoteDataObject$(process),
5452
});
5553
authorizationDataService = jasmine.createSpyObj('authorizationService', {
@@ -117,15 +115,22 @@ describe('SearchExportCsvComponent', () => {
117115
describe('when the metadata-export-search script is not present', () => {
118116
beforeEach(waitForAsync(() => {
119117
initBeforeEachAsync();
120-
(scriptDataService.findById as jasmine.Spy).and.returnValue(createFailedRemoteDataObject$('Not found', 404));
118+
(scriptDataService.scriptWithNameExistsAndCanExecute as jasmine.Spy).and.returnValue(observableOf(false));
121119
}));
122-
beforeEach(() => {
123-
initBeforeEach();
124-
});
120+
125121
it('should should not add the button', () => {
122+
initBeforeEach();
123+
126124
const debugElement = fixture.debugElement.query(By.css('button.export-button'));
127125
expect(debugElement).toBeNull();
128126
});
127+
128+
it('should not call scriptWithNameExistsAndCanExecute when unauthorized', () => {
129+
(authorizationDataService.isAuthorized as jasmine.Spy).and.returnValue(observableOf(false));
130+
initBeforeEach();
131+
132+
expect(scriptDataService.scriptWithNameExistsAndCanExecute).not.toHaveBeenCalled();
133+
});
129134
});
130135
});
131136
describe('export', () => {

src/app/shared/search/search-export-csv/search-export-csv.component.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ import {
1313
TranslateModule,
1414
TranslateService,
1515
} from '@ngx-translate/core';
16+
import { Observable } from 'rxjs';
1617
import {
17-
combineLatest as observableCombineLatest,
18-
Observable,
19-
} from 'rxjs';
20-
import { map } from 'rxjs/operators';
18+
filter,
19+
map,
20+
startWith,
21+
switchMap,
22+
} from 'rxjs/operators';
2123

2224
import { AuthorizationDataService } from '../../../core/data/feature-authorization/authorization-data.service';
2325
import { FeatureID } from '../../../core/data/feature-authorization/feature-id';
@@ -32,6 +34,7 @@ import {
3234
} from '../../empty.util';
3335
import { NotificationsService } from '../../notifications/notifications.service';
3436
import { PaginatedSearchOptions } from '../models/paginated-search-options.model';
37+
import { SearchFilter } from '../models/search-filter.model';
3538

3639
@Component({
3740
selector: 'ds-search-export-csv',
@@ -69,15 +72,11 @@ export class SearchExportCsvComponent implements OnInit {
6972
}
7073

7174
ngOnInit(): void {
72-
const scriptExists$ = this.scriptDataService.findById('metadata-export-search').pipe(
73-
getFirstCompletedRemoteData(),
74-
map((rd) => rd.isSuccess && hasValue(rd.payload)),
75-
);
76-
77-
const isAuthorized$ = this.authorizationDataService.isAuthorized(FeatureID.AdministratorOf);
78-
79-
this.shouldShowButton$ = observableCombineLatest([scriptExists$, isAuthorized$]).pipe(
80-
map(([scriptExists, isAuthorized]: [boolean, boolean]) => scriptExists && isAuthorized),
75+
this.shouldShowButton$ = this.authorizationDataService.isAuthorized(FeatureID.AdministratorOf).pipe(
76+
filter((isAuthorized: boolean) => isAuthorized),
77+
switchMap(() => this.scriptDataService.scriptWithNameExistsAndCanExecute('metadata-export-search')),
78+
map((canExecute: boolean) => canExecute),
79+
startWith(false),
8180
);
8281
}
8382

@@ -97,19 +96,19 @@ export class SearchExportCsvComponent implements OnInit {
9796
parameters.push({ name: '-c', value: this.searchConfig.configuration });
9897
}
9998
if (isNotEmpty(this.searchConfig.filters)) {
100-
this.searchConfig.filters.forEach((filter) => {
101-
if (hasValue(filter.values)) {
102-
filter.values.forEach((value) => {
99+
this.searchConfig.filters.forEach((searchFilter: SearchFilter) => {
100+
if (hasValue(searchFilter.values)) {
101+
searchFilter.values.forEach((value: string) => {
103102
let operator;
104103
let filterValue;
105-
if (hasValue(filter.operator)) {
106-
operator = filter.operator;
104+
if (hasValue(searchFilter.operator)) {
105+
operator = searchFilter.operator;
107106
filterValue = value;
108107
} else {
109108
operator = value.substring(value.lastIndexOf(',') + 1);
110109
filterValue = value.substring(0, value.lastIndexOf(','));
111110
}
112-
const valueToAdd = `${filter.key.substring(2)},${operator}=${filterValue}`;
111+
const valueToAdd = `${searchFilter.key.substring(2)},${operator}=${filterValue}`;
113112
parameters.push({ name: '-f', value: valueToAdd });
114113
});
115114
}

src/app/shared/search/search-filters/search-filter/search-facet-filter/search-facet-filter.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export class SearchFacetFilterComponent implements OnInit, OnDestroy {
160160
this.currentUrl = this.router.url;
161161
this.currentPage = this.getCurrentPage().pipe(distinctUntilChanged());
162162
this.searchOptions$ = this.searchConfigService.searchOptions.pipe(
163-
map((options: SearchOptions) => hasNoValue(this.scope) ? options : Object.assign({}, options, {
163+
map((options: SearchOptions) => hasNoValue(this.scope) ? options : Object.assign(new SearchOptions(options), {
164164
scope: this.scope,
165165
})),
166166
);

0 commit comments

Comments
 (0)