From 080feea1e039fceac9941de4ddf42659702e4e4a Mon Sep 17 00:00:00 2001 From: alep85 Date: Tue, 27 May 2025 12:53:44 +0200 Subject: [PATCH] AAE-34959 Run security options loader when alfresco-api-v2-loader.serfvice is initialized because on content app the app-config.loader is run after AlfrescoApiLoaderService.init and the callback function that should setDefaultSecurityOption is not executed --- .../alfresco-api-v2-loader.service.ts | 4 +- .../src/lib/content.module.ts | 4 +- .../security-options-loader.service.spec.ts | 73 +++++++++++++++++++ .../security-options-loader.service.ts | 32 ++++++++ 4 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 lib/content-services/src/lib/security-options-loader/security-options-loader.service.spec.ts create mode 100644 lib/content-services/src/lib/security-options-loader/security-options-loader.service.ts diff --git a/lib/content-services/src/lib/api-factories/alfresco-api-v2-loader.service.ts b/lib/content-services/src/lib/api-factories/alfresco-api-v2-loader.service.ts index 101a6109e3..19840ae99f 100644 --- a/lib/content-services/src/lib/api-factories/alfresco-api-v2-loader.service.ts +++ b/lib/content-services/src/lib/api-factories/alfresco-api-v2-loader.service.ts @@ -19,6 +19,7 @@ import { AlfrescoApiConfig } from '@alfresco/js-api'; import { Injectable } from '@angular/core'; import { AppConfigService, AppConfigValues, StorageService } from '@alfresco/adf-core'; import { AlfrescoApiService } from '../services/alfresco-api.service'; +import { SecurityOptionsLoaderService } from '../security-options-loader/security-options-loader.service'; /** * Create a factory to resolve an api service instance @@ -37,11 +38,12 @@ export class AlfrescoApiLoaderService { constructor( private readonly appConfig: AppConfigService, private readonly apiService: AlfrescoApiService, + private readonly securityOptionsLoaderService: SecurityOptionsLoaderService, private storageService: StorageService ) {} async init(): Promise { - await this.appConfig.load(); + await this.appConfig.load(this.securityOptionsLoaderService.load); return this.initAngularAlfrescoApi(); } diff --git a/lib/content-services/src/lib/content.module.ts b/lib/content-services/src/lib/content.module.ts index 84df9a9518..3bf63c960d 100644 --- a/lib/content-services/src/lib/content.module.ts +++ b/lib/content-services/src/lib/content.module.ts @@ -49,6 +49,7 @@ import { AlfrescoIconComponent } from './alfresco-icon/alfresco-icon.component'; import { AlfrescoApiService } from './services/alfresco-api.service'; import { AlfrescoApiNoAuthService } from './api-factories/alfresco-api-no-auth.service'; import { AlfrescoApiLoaderService, createAlfrescoApiInstance } from './api-factories/alfresco-api-v2-loader.service'; +import { SecurityOptionsLoaderService } from './security-options-loader/security-options-loader.service'; @NgModule({ imports: [ @@ -132,7 +133,8 @@ export class ContentModule { useFactory: createAlfrescoApiInstance, deps: [AlfrescoApiLoaderService], multi: true - } + }, + SecurityOptionsLoaderService ] }; } diff --git a/lib/content-services/src/lib/security-options-loader/security-options-loader.service.spec.ts b/lib/content-services/src/lib/security-options-loader/security-options-loader.service.spec.ts new file mode 100644 index 0000000000..48db0ac47c --- /dev/null +++ b/lib/content-services/src/lib/security-options-loader/security-options-loader.service.spec.ts @@ -0,0 +1,73 @@ +/*! + * @license + * Copyright © 2005-2025 Hyland Software, Inc. and its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { SecurityOptionsLoaderService } from './security-options-loader.service'; +import { AppConfigService, AppConfigValues } from '@alfresco/adf-core'; +import { AdfHttpClient } from '@alfresco/adf-core/api'; + +describe('SecurityOptionsLoaderService', () => { + let service: SecurityOptionsLoaderService; + let appConfigServiceSpy: jasmine.SpyObj; + let adfHttpClientSpy: jasmine.SpyObj; + + beforeEach(() => { + appConfigServiceSpy = jasmine.createSpyObj('AppConfigService', ['get']); + adfHttpClientSpy = jasmine.createSpyObj('AdfHttpClient', ['setDefaultSecurityOption']); + + service = new SecurityOptionsLoaderService(appConfigServiceSpy, adfHttpClientSpy); + }); + + it('should set withCredentials when value is true', () => { + appConfigServiceSpy.get.and.callFake((key: string): any => { + if (key === AppConfigValues.AUTH_WITH_CREDENTIALS) { + return true; + } + }); + + service.load(); + + expect(adfHttpClientSpy.setDefaultSecurityOption).toHaveBeenCalledWith({ withCredentials: true }); + }); + + it('should set withCredentials when value is false', () => { + appConfigServiceSpy.get.and.callFake((key: string): any => { + if (key === AppConfigValues.AUTH_WITH_CREDENTIALS) { + return false; + } + }); + + service.load(); + + expect(adfHttpClientSpy.setDefaultSecurityOption).toHaveBeenCalledWith({ withCredentials: false }); + }); + + it('should not call setDefaultSecurityOption when value is undefined', () => { + appConfigServiceSpy.get.and.returnValue(undefined); + + service.load(); + + expect(adfHttpClientSpy.setDefaultSecurityOption).not.toHaveBeenCalled(); + }); + + it('should not call setDefaultSecurityOption when value is null', () => { + appConfigServiceSpy.get.and.returnValue(null); + + service.load(); + + expect(adfHttpClientSpy.setDefaultSecurityOption).not.toHaveBeenCalled(); + }); +}); diff --git a/lib/content-services/src/lib/security-options-loader/security-options-loader.service.ts b/lib/content-services/src/lib/security-options-loader/security-options-loader.service.ts new file mode 100644 index 0000000000..afb552d5fb --- /dev/null +++ b/lib/content-services/src/lib/security-options-loader/security-options-loader.service.ts @@ -0,0 +1,32 @@ +/*! + * @license + * Copyright © 2005-2025 Hyland Software, Inc. and its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AppConfigService, AppConfigValues } from '@alfresco/adf-core'; +import { AdfHttpClient } from '@alfresco/adf-core/api'; +import { Injectable } from '@angular/core'; + +@Injectable() +export class SecurityOptionsLoaderService { + constructor(private appConfigService: AppConfigService, private adfHttpClient: AdfHttpClient) {} + + load = () => { + const withCredentials = this.appConfigService.get(AppConfigValues.AUTH_WITH_CREDENTIALS); + if (withCredentials !== undefined && withCredentials !== null) { + this.adfHttpClient.setDefaultSecurityOption({ withCredentials }); + } + }; +}