From 8350833b61ab46d0e79c77dcdb06ea814187ed32 Mon Sep 17 00:00:00 2001 From: Kristof De Langhe Date: Thu, 18 Jun 2020 14:47:59 +0200 Subject: [PATCH] 71429: FeatureType enum and searchBy override --- .../eperson-form/eperson-form.component.ts | 4 +-- .../authorization-data.service.ts | 7 +++-- .../authorization-search-params.ts | 6 ++-- .../feature-authorization/feature-type.ts | 6 ++++ src/app/core/eperson/eperson-data.service.ts | 31 ++++++++++++++++++- 5 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 src/app/core/data/feature-authorization/feature-type.ts diff --git a/src/app/+admin/admin-access-control/epeople-registry/eperson-form/eperson-form.component.ts b/src/app/+admin/admin-access-control/epeople-registry/eperson-form/eperson-form.component.ts index 10c4dd5bfc..e9e446ed34 100644 --- a/src/app/+admin/admin-access-control/epeople-registry/eperson-form/eperson-form.component.ts +++ b/src/app/+admin/admin-access-control/epeople-registry/eperson-form/eperson-form.component.ts @@ -24,7 +24,7 @@ import { NotificationsService } from '../../../../shared/notifications/notificat import { PaginationComponentOptions } from '../../../../shared/pagination/pagination-component-options.model'; import { AuthService } from '../../../../core/auth/auth.service'; import { AuthorizationDataService } from '../../../../core/data/feature-authorization/authorization-data.service'; -import { BehaviorSubject } from 'rxjs/internal/BehaviorSubject'; +import { FeatureType } from '../../../../core/data/feature-authorization/feature-type'; @Component({ selector: 'ds-eperson-form', @@ -245,7 +245,7 @@ export class EPersonFormComponent implements OnInit, OnDestroy { }); })); this.canImpersonate$ = this.epersonService.getActiveEPerson().pipe( - switchMap((eperson) => this.authorizationService.isAuthenticated(eperson.self, undefined, 'loginOnBehalfOf')) + switchMap((eperson) => this.authorizationService.isAuthenticated(eperson.self, undefined, FeatureType.LoginOnBehalfOf)) ); }); } diff --git a/src/app/core/data/feature-authorization/authorization-data.service.ts b/src/app/core/data/feature-authorization/authorization-data.service.ts index 1a827c413e..686ff94b19 100644 --- a/src/app/core/data/feature-authorization/authorization-data.service.ts +++ b/src/app/core/data/feature-authorization/authorization-data.service.ts @@ -25,6 +25,7 @@ import { hasValue, isNotEmpty } from '../../../shared/empty.util'; import { RequestParam } from '../../cache/models/request-param.model'; import { AuthorizationSearchParams } from './authorization-search-params'; import { addAuthenticatedUserUuidIfEmpty, addSiteObjectUrlIfEmpty } from './authorization-utils'; +import { FeatureType } from './feature-type'; /** * A service to retrieve {@link Authorization}s from the REST API @@ -58,7 +59,7 @@ export class AuthorizationDataService extends DataService { * If not provided, the UUID of the currently authenticated {@link EPerson} will be used. * @param featureId ID of the {@link Feature} to check {@link Authorization} for */ - isAuthenticated(objectUrl?: string, ePersonUuid?: string, featureId?: string): Observable { + isAuthenticated(objectUrl?: string, ePersonUuid?: string, featureId?: FeatureType): Observable { return this.searchByObject(objectUrl, ePersonUuid, featureId).pipe( map((authorizationRD) => (authorizationRD.statusCode !== 401 && hasValue(authorizationRD.payload) && isNotEmpty(authorizationRD.payload.page))) ); @@ -75,7 +76,7 @@ export class AuthorizationDataService extends DataService { * @param options {@link FindListOptions} to provide pagination and/or additional arguments * @param linksToFollow List of {@link FollowLinkConfig} that indicate which {@link HALLink}s should be automatically resolved */ - searchByObject(objectUrl?: string, ePersonUuid?: string, featureId?: string, options: FindListOptions = {}, ...linksToFollow: Array>): Observable>> { + searchByObject(objectUrl?: string, ePersonUuid?: string, featureId?: FeatureType, options: FindListOptions = {}, ...linksToFollow: Array>): Observable>> { return observableOf(new AuthorizationSearchParams(objectUrl, ePersonUuid, featureId)).pipe( addSiteObjectUrlIfEmpty(this.siteService), addAuthenticatedUserUuidIfEmpty(this.authService), @@ -121,7 +122,7 @@ export class AuthorizationDataService extends DataService { * @param ePersonUuid Optional parameter value to add to {@link RequestParam} "eperson" * @param featureId Optional parameter value to add to {@link RequestParam} "feature" */ - private createSearchOptions(objectUrl: string, options: FindListOptions = {}, ePersonUuid?: string, featureId?: string): FindListOptions { + private createSearchOptions(objectUrl: string, options: FindListOptions = {}, ePersonUuid?: string, featureId?: FeatureType): FindListOptions { let params = []; if (isNotEmpty(options.searchParams)) { params = [...options.searchParams]; diff --git a/src/app/core/data/feature-authorization/authorization-search-params.ts b/src/app/core/data/feature-authorization/authorization-search-params.ts index 8d545039ba..dc045add84 100644 --- a/src/app/core/data/feature-authorization/authorization-search-params.ts +++ b/src/app/core/data/feature-authorization/authorization-search-params.ts @@ -1,9 +1,11 @@ +import { FeatureType } from './feature-type'; + export class AuthorizationSearchParams { objectUrl: string; ePersonUuid: string; - featureId: string; + featureId: FeatureType; - constructor(objectUrl?: string, ePersonUuid?: string, featureId?: string) { + constructor(objectUrl?: string, ePersonUuid?: string, featureId?: FeatureType) { this.objectUrl = objectUrl; this.ePersonUuid = ePersonUuid; this.featureId = featureId; diff --git a/src/app/core/data/feature-authorization/feature-type.ts b/src/app/core/data/feature-authorization/feature-type.ts new file mode 100644 index 0000000000..a4cbc23d15 --- /dev/null +++ b/src/app/core/data/feature-authorization/feature-type.ts @@ -0,0 +1,6 @@ +/** + * Enum object for all possible {@link Feature} types + */ +export enum FeatureType { + LoginOnBehalfOf = 'loginOnBehalfOf' +} diff --git a/src/app/core/eperson/eperson-data.service.ts b/src/app/core/eperson/eperson-data.service.ts index 86e53178a0..3dccbcb338 100644 --- a/src/app/core/eperson/eperson-data.service.ts +++ b/src/app/core/eperson/eperson-data.service.ts @@ -3,7 +3,7 @@ import { Injectable } from '@angular/core'; import { createSelector, select, Store } from '@ngrx/store'; import { Operation } from 'fast-json-patch/lib/core'; import { Observable } from 'rxjs'; -import { filter, map, take } from 'rxjs/operators'; +import { filter, find, map, skipWhile, switchMap, take, tap } from 'rxjs/operators'; import { EPeopleRegistryCancelEPersonAction, EPeopleRegistryEditEPersonAction @@ -249,4 +249,33 @@ export class EPersonDataService extends DataService { return '/admin/access-control/epeople'; } + /** + * Make a new FindListRequest with given search method + * + * @param searchMethod The search method for the object + * @param options The [[FindListOptions]] object + * @param linksToFollow The array of [[FollowLinkConfig]] + * @return {Observable>} + * Return an observable that emits response from the server + */ + searchBy(searchMethod: string, options: FindListOptions = {}, ...linksToFollow: Array>): Observable>> { + const hrefObs = this.getSearchByHref(searchMethod, options, ...linksToFollow); + + return hrefObs.pipe( + find((href: string) => hasValue(href)), + tap((href: string) => { + this.requestService.removeByHrefSubstring(href); + const request = new FindListRequest(this.requestService.generateRequestId(), href, options); + + this.requestService.configure(request); + } + ), + switchMap((href) => this.requestService.getByHref(href)), + skipWhile((requestEntry) => hasValue(requestEntry) && requestEntry.completed), + switchMap((href) => + this.rdbService.buildList(hrefObs, ...linksToFollow) as Observable>> + ) + ); + } + }