mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-08 10:34:15 +00:00
61561: resolving second todo in auth srvice
This commit is contained in:
@@ -6,11 +6,18 @@ import { RequestService } from '../data/request.service';
|
|||||||
import { GLOBAL_CONFIG } from '../../../config';
|
import { GLOBAL_CONFIG } from '../../../config';
|
||||||
import { GlobalConfig } from '../../../config/global-config.interface';
|
import { GlobalConfig } from '../../../config/global-config.interface';
|
||||||
import { isNotEmpty } from '../../shared/empty.util';
|
import { isNotEmpty } from '../../shared/empty.util';
|
||||||
import { AuthGetRequest, AuthPostRequest, PostRequest, RestRequest } from '../data/request.models';
|
import {
|
||||||
|
AuthGetRequest,
|
||||||
|
AuthPostRequest,
|
||||||
|
GetRequest,
|
||||||
|
PostRequest,
|
||||||
|
RestRequest
|
||||||
|
} from '../data/request.models';
|
||||||
import { AuthStatusResponse, ErrorResponse } from '../cache/response.models';
|
import { AuthStatusResponse, ErrorResponse } from '../cache/response.models';
|
||||||
import { HttpOptions } from '../dspace-rest-v2/dspace-rest-v2.service';
|
import { HttpOptions } from '../dspace-rest-v2/dspace-rest-v2.service';
|
||||||
import { RequestEntry } from '../data/request.reducer';
|
import { RequestEntry } from '../data/request.reducer';
|
||||||
import { getResponseFromEntry } from '../shared/operators';
|
import { getResponseFromEntry } from '../shared/operators';
|
||||||
|
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AuthRequestService {
|
export class AuthRequestService {
|
||||||
@@ -56,8 +63,8 @@ export class AuthRequestService {
|
|||||||
map((endpointURL) => this.getEndpointByMethod(endpointURL, method)),
|
map((endpointURL) => this.getEndpointByMethod(endpointURL, method)),
|
||||||
distinctUntilChanged(),
|
distinctUntilChanged(),
|
||||||
map((endpointURL: string) => new AuthGetRequest(this.requestService.generateRequestId(), endpointURL, options)),
|
map((endpointURL: string) => new AuthGetRequest(this.requestService.generateRequestId(), endpointURL, options)),
|
||||||
tap((request: PostRequest) => this.requestService.configure(request, true)),
|
tap((request: GetRequest) => this.requestService.configure(request, true)),
|
||||||
mergeMap((request: PostRequest) => this.fetchRequest(request)),
|
mergeMap((request: GetRequest) => this.fetchRequest(request)),
|
||||||
distinctUntilChanged());
|
distinctUntilChanged());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -13,6 +13,8 @@ import { RestRequest } from '../data/request.models';
|
|||||||
import { AuthType } from './auth-type';
|
import { AuthType } from './auth-type';
|
||||||
import { AuthStatus } from './models/auth-status.model';
|
import { AuthStatus } from './models/auth-status.model';
|
||||||
import { NormalizedAuthStatus } from './models/normalized-auth-status.model';
|
import { NormalizedAuthStatus } from './models/normalized-auth-status.model';
|
||||||
|
import { NormalizedObject } from '../cache/models/normalized-object.model';
|
||||||
|
import { DSpaceObject } from '../shared/dspace-object.model';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AuthResponseParsingService extends BaseResponseParsingService implements ResponseParsingService {
|
export class AuthResponseParsingService extends BaseResponseParsingService implements ResponseParsingService {
|
||||||
@@ -27,11 +29,10 @@ export class AuthResponseParsingService extends BaseResponseParsingService imple
|
|||||||
|
|
||||||
parse(request: RestRequest, data: DSpaceRESTV2Response): RestResponse {
|
parse(request: RestRequest, data: DSpaceRESTV2Response): RestResponse {
|
||||||
if (isNotEmpty(data.payload) && isNotEmpty(data.payload._links) && (data.statusCode === 200)) {
|
if (isNotEmpty(data.payload) && isNotEmpty(data.payload._links) && (data.statusCode === 200)) {
|
||||||
const response = this.process<NormalizedAuthStatus, AuthType>(data.payload, request.uuid);
|
const response = this.process<NormalizedObject<AuthStatus>, AuthType>(data.payload, request.uuid);
|
||||||
return new AuthStatusResponse(response, data.statusCode, data.statusText);
|
return new AuthStatusResponse(response, data.statusCode, data.statusText);
|
||||||
} else {
|
} else {
|
||||||
return new AuthStatusResponse(data.payload as AuthStatus, data.statusCode, data.statusText);
|
return new AuthStatusResponse(data.payload as NormalizedAuthStatus, data.statusCode, data.statusText);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -146,14 +146,10 @@ export class AuthService {
|
|||||||
headers = headers.append('Authorization', `Bearer ${token.accessToken}`);
|
headers = headers.append('Authorization', `Bearer ${token.accessToken}`);
|
||||||
options.headers = headers;
|
options.headers = headers;
|
||||||
return this.authRequestService.getRequest('status', options).pipe(
|
return this.authRequestService.getRequest('status', options).pipe(
|
||||||
|
map((status) => this.rdbService.build(status)),
|
||||||
switchMap((status: AuthStatus) => {
|
switchMap((status: AuthStatus) => {
|
||||||
|
|
||||||
if (status.authenticated) {
|
if (status.authenticated) {
|
||||||
// TODO this should be cleaned up, AuthStatus could be parsed by the RemoteDataService as a whole...
|
return status.eperson.pipe(map((eperson) => eperson.payload));
|
||||||
// Review when https://jira.duraspace.org/browse/DS-4006 is fixed
|
|
||||||
// See https://github.com/DSpace/dspace-angular/issues/292
|
|
||||||
const person$ = this.rdbService.buildSingle<EPerson>(status.eperson.toString());
|
|
||||||
return person$.pipe(map((eperson) => eperson.payload));
|
|
||||||
} else {
|
} else {
|
||||||
throw(new Error('Not authenticated'));
|
throw(new Error('Not authenticated'));
|
||||||
}
|
}
|
||||||
|
@@ -3,8 +3,9 @@ import { AuthTokenInfo } from './auth-token-info.model';
|
|||||||
import { EPerson } from '../../eperson/models/eperson.model';
|
import { EPerson } from '../../eperson/models/eperson.model';
|
||||||
import { RemoteData } from '../../data/remote-data';
|
import { RemoteData } from '../../data/remote-data';
|
||||||
import { Observable } from 'rxjs';
|
import { Observable } from 'rxjs';
|
||||||
|
import { CacheableObject } from '../../cache/object-cache.reducer';
|
||||||
|
|
||||||
export class AuthStatus {
|
export class AuthStatus implements CacheableObject {
|
||||||
|
|
||||||
id: string;
|
id: string;
|
||||||
|
|
||||||
|
@@ -34,15 +34,10 @@ export class ServerAuthService extends AuthService {
|
|||||||
|
|
||||||
options.headers = headers;
|
options.headers = headers;
|
||||||
return this.authRequestService.getRequest('status', options).pipe(
|
return this.authRequestService.getRequest('status', options).pipe(
|
||||||
|
map((status) => this.rdbService.build(status)),
|
||||||
switchMap((status: AuthStatus) => {
|
switchMap((status: AuthStatus) => {
|
||||||
|
|
||||||
if (status.authenticated) {
|
if (status.authenticated) {
|
||||||
|
return status.eperson.pipe(map((eperson) => eperson.payload));
|
||||||
// TODO this should be cleaned up, AuthStatus could be parsed by the RemoteDataService as a whole...
|
|
||||||
const person$ = this.rdbService.buildSingle<EPerson>(status.eperson.toString());
|
|
||||||
return person$.pipe(
|
|
||||||
map((eperson) => eperson.payload)
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
throw(new Error('Not authenticated'));
|
throw(new Error('Not authenticated'));
|
||||||
}
|
}
|
||||||
|
@@ -18,6 +18,7 @@ import { CacheableObject } from '../object-cache.reducer';
|
|||||||
import { NormalizedSubmissionDefinitionsModel } from '../../config/models/normalized-config-submission-definitions.model';
|
import { NormalizedSubmissionDefinitionsModel } from '../../config/models/normalized-config-submission-definitions.model';
|
||||||
import { NormalizedSubmissionFormsModel } from '../../config/models/normalized-config-submission-forms.model';
|
import { NormalizedSubmissionFormsModel } from '../../config/models/normalized-config-submission-forms.model';
|
||||||
import { NormalizedSubmissionSectionModel } from '../../config/models/normalized-config-submission-section.model';
|
import { NormalizedSubmissionSectionModel } from '../../config/models/normalized-config-submission-section.model';
|
||||||
|
import { NormalizedAuthStatus } from '../../auth/models/normalized-auth-status.model';
|
||||||
|
|
||||||
export class NormalizedObjectFactory {
|
export class NormalizedObjectFactory {
|
||||||
public static getConstructor(type: ResourceType): GenericConstructor<NormalizedObject<CacheableObject>> {
|
public static getConstructor(type: ResourceType): GenericConstructor<NormalizedObject<CacheableObject>> {
|
||||||
@@ -52,6 +53,9 @@ export class NormalizedObjectFactory {
|
|||||||
case ResourceType.Group: {
|
case ResourceType.Group: {
|
||||||
return NormalizedGroup
|
return NormalizedGroup
|
||||||
}
|
}
|
||||||
|
case ResourceType.Status: {
|
||||||
|
return NormalizedAuthStatus
|
||||||
|
}
|
||||||
case ResourceType.MetadataSchema: {
|
case ResourceType.MetadataSchema: {
|
||||||
return NormalizedMetadataSchema
|
return NormalizedMetadataSchema
|
||||||
}
|
}
|
||||||
|
3
src/app/core/cache/response.models.ts
vendored
3
src/app/core/cache/response.models.ts
vendored
@@ -14,6 +14,7 @@ import { MetadataField } from '../metadata/metadatafield.model';
|
|||||||
import { PaginatedList } from '../data/paginated-list';
|
import { PaginatedList } from '../data/paginated-list';
|
||||||
import { SubmissionObject } from '../submission/models/submission-object.model';
|
import { SubmissionObject } from '../submission/models/submission-object.model';
|
||||||
import { DSpaceObject } from '../shared/dspace-object.model';
|
import { DSpaceObject } from '../shared/dspace-object.model';
|
||||||
|
import { NormalizedAuthStatus } from '../auth/models/normalized-auth-status.model';
|
||||||
|
|
||||||
/* tslint:disable:max-classes-per-file */
|
/* tslint:disable:max-classes-per-file */
|
||||||
export class RestResponse {
|
export class RestResponse {
|
||||||
@@ -202,7 +203,7 @@ export class AuthStatusResponse extends RestResponse {
|
|||||||
public toCache = false;
|
public toCache = false;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
public response: AuthStatus,
|
public response: NormalizedAuthStatus,
|
||||||
public statusCode: number,
|
public statusCode: number,
|
||||||
public statusText: string,
|
public statusText: string,
|
||||||
) {
|
) {
|
||||||
|
@@ -48,6 +48,7 @@ export class RequestEffects {
|
|||||||
const serializer = new DSpaceRESTv2Serializer(NormalizedObjectFactory.getConstructor(request.body.type));
|
const serializer = new DSpaceRESTv2Serializer(NormalizedObjectFactory.getConstructor(request.body.type));
|
||||||
body = serializer.serialize(request.body);
|
body = serializer.serialize(request.body);
|
||||||
}
|
}
|
||||||
|
console.log(JSON.stringify(request));
|
||||||
return this.restApi.request(request.method, request.href, body, request.options).pipe(
|
return this.restApi.request(request.method, request.href, body, request.options).pipe(
|
||||||
map((data: DSpaceRESTV2Response) => this.injector.get(request.getResponseParser()).parse(request, data)),
|
map((data: DSpaceRESTV2Response) => this.injector.get(request.getResponseParser()).parse(request, data)),
|
||||||
addToResponseCacheAndCompleteAction(request, this.EnvConfig),
|
addToResponseCacheAndCompleteAction(request, this.EnvConfig),
|
||||||
|
@@ -71,6 +71,7 @@ export class DSpaceRESTv2Service {
|
|||||||
if (options && options.responseType) {
|
if (options && options.responseType) {
|
||||||
requestOptions.responseType = options.responseType;
|
requestOptions.responseType = options.responseType;
|
||||||
}
|
}
|
||||||
|
console.log('url', url);
|
||||||
return this.http.request(method, url, requestOptions).pipe(
|
return this.http.request(method, url, requestOptions).pipe(
|
||||||
map((res) => ({ payload: res.body, headers: res.headers, statusCode: res.status, statusText: res.statusText })),
|
map((res) => ({ payload: res.body, headers: res.headers, statusCode: res.status, statusText: res.statusText })),
|
||||||
catchError((err) => {
|
catchError((err) => {
|
||||||
|
@@ -20,4 +20,5 @@ export enum ResourceType {
|
|||||||
SubmissionForms = 'submissionforms',
|
SubmissionForms = 'submissionforms',
|
||||||
SubmissionSections = 'submissionsections',
|
SubmissionSections = 'submissionsections',
|
||||||
SubmissionSection = 'submissionsection',
|
SubmissionSection = 'submissionsection',
|
||||||
|
Status = 'status',
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user