Skip to content

Commit 70b855e

Browse files
121534: Removed unauthorized metadata-export-search request on search page for non-admins
1 parent 404ccd9 commit 70b855e

2 files changed

Lines changed: 26 additions & 24 deletions

File tree

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
@@ -6,7 +6,6 @@ import { AuthorizationDataService } from '../../../core/data/feature-authorizati
66
import { SearchExportCsvComponent } from './search-export-csv.component';
77
import { ScriptDataService } from '../../../core/data/processes/script-data.service';
88
import { createFailedRemoteDataObject$, createSuccessfulRemoteDataObject$ } from '../../remote-data.utils';
9-
import { Script } from '../../../process-page/scripts/script.model';
109
import { Process } from '../../../process-page/processes/process.model';
1110
import { NotificationsServiceStub } from '../../testing/notifications-service.stub';
1211
import { NotificationsService } from '../../notifications/notifications.service';
@@ -25,7 +24,6 @@ describe('SearchExportCsvComponent', () => {
2524
let notificationsService;
2625
let router;
2726

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

3129
const searchConfig = new PaginatedSearchOptions({
@@ -41,7 +39,7 @@ describe('SearchExportCsvComponent', () => {
4139

4240
function initBeforeEachAsync() {
4341
scriptDataService = jasmine.createSpyObj('scriptDataService', {
44-
findById: createSuccessfulRemoteDataObject$(script),
42+
scriptWithNameExistsAndCanExecute: observableOf(true),
4543
invoke: createSuccessfulRemoteDataObject$(process)
4644
});
4745
authorizationDataService = jasmine.createSpyObj('authorizationService', {
@@ -110,15 +108,22 @@ describe('SearchExportCsvComponent', () => {
110108
describe('when the metadata-export-search script is not present', () => {
111109
beforeEach(waitForAsync(() => {
112110
initBeforeEachAsync();
113-
(scriptDataService.findById as jasmine.Spy).and.returnValue(createFailedRemoteDataObject$('Not found', 404));
111+
(scriptDataService.scriptWithNameExistsAndCanExecute as jasmine.Spy).and.returnValue(observableOf(false));
114112
}));
115-
beforeEach(() => {
116-
initBeforeEach();
117-
});
113+
118114
it('should should not add the button', () => {
115+
initBeforeEach();
116+
119117
const debugElement = fixture.debugElement.query(By.css('button.export-button'));
120118
expect(debugElement).toBeNull();
121119
});
120+
121+
it('should not call scriptWithNameExistsAndCanExecute when unauthorized', () => {
122+
(authorizationDataService.isAuthorized as jasmine.Spy).and.returnValue(observableOf(false));
123+
initBeforeEach();
124+
125+
expect(scriptDataService.scriptWithNameExistsAndCanExecute).not.toHaveBeenCalled();
126+
});
122127
});
123128
});
124129
describe('export', () => {

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

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Component, Input, OnInit } from '@angular/core';
2-
import { combineLatest as observableCombineLatest, Observable } from 'rxjs';
2+
import { Observable } from 'rxjs';
33
import { ScriptDataService } from '../../../core/data/processes/script-data.service';
44
import { getFirstCompletedRemoteData } from '../../../core/shared/operators';
5-
import { map } from 'rxjs/operators';
5+
import { map, switchMap, filter, startWith } from 'rxjs/operators';
66
import { FeatureID } from '../../../core/data/feature-authorization/feature-id';
77
import { AuthorizationDataService } from '../../../core/data/feature-authorization/authorization-data.service';
88
import { hasValue, isNotEmpty } from '../../empty.util';
@@ -13,6 +13,7 @@ import { NotificationsService } from '../../notifications/notifications.service'
1313
import { TranslateService } from '@ngx-translate/core';
1414
import { Router } from '@angular/router';
1515
import { PaginatedSearchOptions } from '../models/paginated-search-options.model';
16+
import { SearchFilter } from '../models/search-filter.model';
1617

1718
@Component({
1819
selector: 'ds-search-export-csv',
@@ -48,15 +49,11 @@ export class SearchExportCsvComponent implements OnInit {
4849
}
4950

5051
ngOnInit(): void {
51-
const scriptExists$ = this.scriptDataService.findById('metadata-export-search').pipe(
52-
getFirstCompletedRemoteData(),
53-
map((rd) => rd.isSuccess && hasValue(rd.payload))
54-
);
55-
56-
const isAuthorized$ = this.authorizationDataService.isAuthorized(FeatureID.AdministratorOf);
57-
58-
this.shouldShowButton$ = observableCombineLatest([scriptExists$, isAuthorized$]).pipe(
59-
map(([scriptExists, isAuthorized]: [boolean, boolean]) => scriptExists && isAuthorized)
52+
this.shouldShowButton$ = this.authorizationDataService.isAuthorized(FeatureID.AdministratorOf).pipe(
53+
filter((isAuthorized: boolean) => isAuthorized),
54+
switchMap(() => this.scriptDataService.scriptWithNameExistsAndCanExecute('metadata-export-search')),
55+
map((canExecute: boolean) => canExecute),
56+
startWith(false),
6057
);
6158
}
6259

@@ -76,19 +73,19 @@ export class SearchExportCsvComponent implements OnInit {
7673
parameters.push({name: '-c', value: this.searchConfig.configuration});
7774
}
7875
if (isNotEmpty(this.searchConfig.filters)) {
79-
this.searchConfig.filters.forEach((filter) => {
80-
if (hasValue(filter.values)) {
81-
filter.values.forEach((value) => {
76+
this.searchConfig.filters.forEach((searchFilter: SearchFilter) => {
77+
if (hasValue(searchFilter.values)) {
78+
searchFilter.values.forEach((value: string) => {
8279
let operator;
8380
let filterValue;
84-
if (hasValue(filter.operator)) {
85-
operator = filter.operator;
81+
if (hasValue(searchFilter.operator)) {
82+
operator = searchFilter.operator;
8683
filterValue = value;
8784
} else {
8885
operator = value.substring(value.lastIndexOf(',') + 1);
8986
filterValue = value.substring(0, value.lastIndexOf(','));
9087
}
91-
const valueToAdd = `${filter.key.substring(2)},${operator}=${filterValue}`;
88+
const valueToAdd = `${searchFilter.key.substring(2)},${operator}=${filterValue}`;
9289
parameters.push({name: '-f', value: valueToAdd});
9390
});
9491
}

0 commit comments

Comments
 (0)