[ACA-3315] - add predefined extension guard (#1478)

* [ACA-3315] - add predefined extension guard

* update extension loader

* update doc

* fix lint

* PR changes

* PR changes

* remove unnecesary code

Co-authored-by: Silviu Popa <p3701014@L3700101120.ness.com>
This commit is contained in:
Silviu Popa 2020-06-02 15:03:46 +03:00 committed by GitHub
parent cef088794f
commit 33327bb505
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 22 deletions

View File

@ -74,6 +74,7 @@ Below is the list of the authentication guards main application registers on sta
| Key | Type | Description |
| -------- | ------------ | ------------------------------------------------------------------------- |
| app.auth | AuthGuardEcm | ADF guard, validates ACS authentication and redirects to Login if needed. |
| app.extensions.dataLoaderGuard | ExtensionsDataLoaderGuard | ACA guard, validates EXTENSION_DATA_LOADERS provider and redirects to Login if needed. |
You can refer those guards from within your custom extensions,
or [register](/extending/registration) your custom implementations.

View File

@ -131,5 +131,23 @@ describe('ExtensionsDataLoaderGuard', () => {
expect(erroredSpy).not.toHaveBeenCalled();
expect(completedSpy).toHaveBeenCalled();
});
it('should call canActivate only once', () => {
const subject1 = new Subject<true>();
const extensionLoaders = {
fct1: function() {
return subject1.asObservable();
}
};
const extensionLoaderSpy = spyOn(extensionLoaders, 'fct1');
const guard = new ExtensionsDataLoaderGuard([extensionLoaders.fct1]);
guard.canActivate(route).subscribe(emittedSpy, erroredSpy, completedSpy);
expect(extensionLoaderSpy).toHaveBeenCalled();
extensionLoaderSpy.calls.reset();
guard.canActivate(route).subscribe(emittedSpy, erroredSpy, completedSpy);
expect(extensionLoaderSpy).not.toHaveBeenCalled();
});
});
});

View File

@ -45,34 +45,46 @@ export const EXTENSION_DATA_LOADERS = new InjectionToken<
@Injectable({ providedIn: 'root' })
export class ExtensionsDataLoaderGuard implements CanActivate {
private invoked = false;
constructor(
@Inject(EXTENSION_DATA_LOADERS)
private extensionDataLoaders: ExtensionLoaderCallback[]
) {}
canActivate(route: ActivatedRouteSnapshot): Observable<boolean> {
if (!this.extensionDataLoaders.length) {
if (!this.invoked) {
this.invoked = true;
if (!this.extensionDataLoaders.length) {
return of(true);
}
const dataLoaderCallbacks = this.extensionDataLoaders.map(callback =>
callback(route)
);
// Undocumented forkJoin behaviour/bug:
// https://github.com/ReactiveX/rxjs/issues/3246
// So all callbacks need to emit before completion, otherwise forkJoin will short circuit
return forkJoin(...dataLoaderCallbacks).pipe(
map(() => true),
catchError(e => {
// tslint:disable-next-line
console.error(
'Some of the extension data loader guards has been errored.'
);
// tslint:disable-next-line
console.error(e);
return of(true);
})
);
} else {
return of(true);
}
}
const dataLoaderCallbacks = this.extensionDataLoaders.map(callback =>
callback(route)
);
// Undocumented forkJoin behaviour/bug:
// https://github.com/ReactiveX/rxjs/issues/3246
// So all callbacks need to emit before completion, otherwise forkJoin will short circuit
return forkJoin(...dataLoaderCallbacks).pipe(
map(() => true),
catchError(e => {
// tslint:disable-next-line
console.error(
'Some of the extension data loader guards has been errored.'
);
// tslint:disable-next-line
console.error(e);
return of(true);
})
);
canActivateChild(): Observable<boolean> {
return of(true);
}
}

View File

@ -53,7 +53,10 @@ import { ViewNodeComponent } from '../components/toolbar/view-node/view-node.com
import { LanguagePickerComponent } from '../components/common/language-picker/language-picker.component';
import { LogoutComponent } from '../components/common/logout/logout.component';
import { CurrentUserComponent } from '../components/current-user/current-user.component';
import { AppExtensionService } from '@alfresco/aca-shared';
import {
AppExtensionService,
ExtensionsDataLoaderGuard
} from '@alfresco/aca-shared';
export function setupExtensions(service: AppExtensionService): Function {
return () => service.load();
@ -111,7 +114,8 @@ export class CoreExtensionsModule {
});
extensions.setAuthGuards({
'app.auth': AuthGuardEcm
'app.auth': AuthGuardEcm,
'app.extensions.dataLoaderGuard': ExtensionsDataLoaderGuard
});
extensions.setEvaluators({