mirror of
https://github.com/Alfresco/alfresco-content-app.git
synced 2025-07-24 17:31:52 +00:00
[ACS-8726] Use functional route guards (#4096)
* [ACS-8726] Use functional route guards * [ACS-8726] reduce duplication
This commit is contained in:
committed by
GitHub
parent
49cd06d2f9
commit
3a2d870db1
@@ -22,22 +22,16 @@
|
||||
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { Injectable } from '@angular/core';
|
||||
import { CanActivate } from '@angular/router';
|
||||
import { Observable } from 'rxjs';
|
||||
import { inject } from '@angular/core';
|
||||
import { CanActivateFn } from '@angular/router';
|
||||
import { AuthenticationService } from '@alfresco/adf-core';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class ViewProfileRuleGuard implements CanActivate {
|
||||
constructor(private authService: AuthenticationService) {}
|
||||
export const ViewProfileRuleGuard: CanActivateFn = () => {
|
||||
const authService = inject(AuthenticationService);
|
||||
|
||||
canActivate(): Observable<boolean> | Promise<boolean> | boolean {
|
||||
return this.isEcmLoggedIn() || this.authService.isOauth();
|
||||
}
|
||||
const isEcmLoggedIn = (): boolean => {
|
||||
return authService.isEcmLoggedIn() || (authService.isECMProvider() && authService.isKerberosEnabled());
|
||||
};
|
||||
|
||||
private isEcmLoggedIn(): boolean {
|
||||
return this.authService.isEcmLoggedIn() || (this.authService.isECMProvider() && this.authService.isKerberosEnabled());
|
||||
}
|
||||
}
|
||||
return isEcmLoggedIn() || authService.isOauth();
|
||||
};
|
||||
|
@@ -22,9 +22,10 @@
|
||||
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { ExtensionsDataLoaderGuard } from './extensions-data-loader.guard';
|
||||
import { ActivatedRouteSnapshot } from '@angular/router';
|
||||
import { Subject, throwError } from 'rxjs';
|
||||
import { EXTENSION_DATA_LOADERS, ExtensionsDataLoaderGuard, resetInvoked } from './extensions-data-loader.guard';
|
||||
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { Observable, Subject, throwError } from 'rxjs';
|
||||
|
||||
describe('ExtensionsDataLoaderGuard', () => {
|
||||
let route: ActivatedRouteSnapshot;
|
||||
@@ -32,18 +33,22 @@ describe('ExtensionsDataLoaderGuard', () => {
|
||||
let completedSpy;
|
||||
let erroredSpy;
|
||||
|
||||
describe('canActivate', () => {
|
||||
const setupTest = (extensionDataLoaders: any[]) => {
|
||||
TestBed.overrideProvider(EXTENSION_DATA_LOADERS, { useValue: extensionDataLoaders });
|
||||
return TestBed.runInInjectionContext(() => ExtensionsDataLoaderGuard(route, {} as RouterStateSnapshot)) as Observable<boolean>;
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
route = {} as ActivatedRouteSnapshot;
|
||||
emittedSpy = jasmine.createSpy('emitted');
|
||||
completedSpy = jasmine.createSpy('completed');
|
||||
erroredSpy = jasmine.createSpy('errored');
|
||||
resetInvoked();
|
||||
});
|
||||
|
||||
it('should emit true and complete if no callback are present', () => {
|
||||
const guard = new ExtensionsDataLoaderGuard([]);
|
||||
|
||||
guard.canActivate(route).subscribe(emittedSpy, erroredSpy, completedSpy);
|
||||
const guard = setupTest([]);
|
||||
guard.subscribe(emittedSpy, erroredSpy, completedSpy);
|
||||
expect(emittedSpy).toHaveBeenCalledWith(true);
|
||||
expect(erroredSpy).not.toHaveBeenCalled();
|
||||
expect(completedSpy).toHaveBeenCalled();
|
||||
@@ -51,10 +56,8 @@ describe('ExtensionsDataLoaderGuard', () => {
|
||||
|
||||
it('should emit true and complete in case of only one callback is present, completed', () => {
|
||||
const subject = new Subject<true>();
|
||||
const guard = new ExtensionsDataLoaderGuard([() => subject.asObservable()]);
|
||||
|
||||
guard.canActivate(route).subscribe(emittedSpy, erroredSpy, completedSpy);
|
||||
|
||||
const guard = setupTest([() => subject.asObservable()]);
|
||||
guard.subscribe(emittedSpy, erroredSpy, completedSpy);
|
||||
subject.next(true);
|
||||
expect(emittedSpy).not.toHaveBeenCalled();
|
||||
expect(erroredSpy).not.toHaveBeenCalled();
|
||||
@@ -67,9 +70,9 @@ describe('ExtensionsDataLoaderGuard', () => {
|
||||
});
|
||||
|
||||
it('should emit true and complete in case of only one callback is present, errored', () => {
|
||||
const guard = new ExtensionsDataLoaderGuard([() => throwError(new Error())]);
|
||||
const guard = setupTest([() => throwError(new Error())]);
|
||||
guard.subscribe(emittedSpy, erroredSpy, completedSpy);
|
||||
|
||||
guard.canActivate(route).subscribe(emittedSpy, erroredSpy, completedSpy);
|
||||
expect(emittedSpy).toHaveBeenCalledWith(true);
|
||||
expect(erroredSpy).not.toHaveBeenCalled();
|
||||
expect(completedSpy).toHaveBeenCalled();
|
||||
@@ -78,13 +81,13 @@ describe('ExtensionsDataLoaderGuard', () => {
|
||||
it('should NOT complete in case of multiple callbacks are present and not all of them have been completed', () => {
|
||||
const subject1 = new Subject<true>();
|
||||
const subject2 = new Subject<true>();
|
||||
const guard = new ExtensionsDataLoaderGuard([() => subject1.asObservable(), () => subject2.asObservable()]);
|
||||
|
||||
guard.canActivate(route).subscribe(emittedSpy, erroredSpy, completedSpy);
|
||||
const guard = setupTest([() => subject1.asObservable(), () => subject2.asObservable()]);
|
||||
|
||||
guard.subscribe(emittedSpy, erroredSpy, completedSpy);
|
||||
subject1.next();
|
||||
subject2.next();
|
||||
subject1.complete();
|
||||
|
||||
expect(emittedSpy).not.toHaveBeenCalled();
|
||||
expect(erroredSpy).not.toHaveBeenCalled();
|
||||
expect(completedSpy).not.toHaveBeenCalled();
|
||||
@@ -93,14 +96,14 @@ describe('ExtensionsDataLoaderGuard', () => {
|
||||
it('should emit true and complete in case of multiple callbacks are present and all of them have been completed', () => {
|
||||
const subject1 = new Subject<true>();
|
||||
const subject2 = new Subject<true>();
|
||||
const guard = new ExtensionsDataLoaderGuard([() => subject1.asObservable(), () => subject2.asObservable()]);
|
||||
|
||||
guard.canActivate(route).subscribe(emittedSpy, erroredSpy, completedSpy);
|
||||
const guard = setupTest([() => subject1.asObservable(), () => subject2.asObservable()]);
|
||||
|
||||
guard.subscribe(emittedSpy, erroredSpy, completedSpy);
|
||||
subject1.next();
|
||||
subject2.next();
|
||||
subject1.complete();
|
||||
subject2.complete();
|
||||
|
||||
expect(emittedSpy).toHaveBeenCalledWith(true);
|
||||
expect(erroredSpy).not.toHaveBeenCalled();
|
||||
expect(completedSpy).toHaveBeenCalled();
|
||||
@@ -108,11 +111,11 @@ describe('ExtensionsDataLoaderGuard', () => {
|
||||
|
||||
it('should emit true and complete even if one of the observables are errored, to not block the application loading', () => {
|
||||
const subject1 = new Subject<true>();
|
||||
const guard = new ExtensionsDataLoaderGuard([() => subject1.asObservable(), () => throwError(new Error())]);
|
||||
|
||||
guard.canActivate(route).subscribe(emittedSpy, erroredSpy, completedSpy);
|
||||
const guard = setupTest([() => subject1.asObservable(), () => throwError(new Error())]);
|
||||
|
||||
guard.subscribe(emittedSpy, erroredSpy, completedSpy);
|
||||
subject1.next();
|
||||
|
||||
expect(emittedSpy).toHaveBeenCalledWith(true);
|
||||
expect(erroredSpy).not.toHaveBeenCalled();
|
||||
expect(completedSpy).toHaveBeenCalled();
|
||||
@@ -124,16 +127,16 @@ describe('ExtensionsDataLoaderGuard', () => {
|
||||
fct1: () => subject1.asObservable()
|
||||
};
|
||||
const extensionLoaderSpy = spyOn(extensionLoaders, 'fct1').and.callThrough();
|
||||
const guard = new ExtensionsDataLoaderGuard([extensionLoaders.fct1]);
|
||||
const guard = setupTest([extensionLoaders.fct1]);
|
||||
|
||||
guard.canActivate(route).subscribe(emittedSpy, erroredSpy, completedSpy);
|
||||
guard.subscribe(emittedSpy, erroredSpy, completedSpy);
|
||||
expect(extensionLoaderSpy).toHaveBeenCalled();
|
||||
|
||||
extensionLoaderSpy.calls.reset();
|
||||
subject1.next(true);
|
||||
subject1.complete();
|
||||
guard.canActivate(route).subscribe(emittedSpy, erroredSpy, completedSpy);
|
||||
|
||||
guard.subscribe(emittedSpy, erroredSpy, completedSpy);
|
||||
expect(extensionLoaderSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@@ -22,8 +22,8 @@
|
||||
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { Injectable, InjectionToken, Inject } from '@angular/core';
|
||||
import { CanActivate, ActivatedRouteSnapshot } from '@angular/router';
|
||||
import { InjectionToken, inject } from '@angular/core';
|
||||
import { ActivatedRouteSnapshot, CanActivateFn } from '@angular/router';
|
||||
import { Observable, forkJoin, of } from 'rxjs';
|
||||
import { tap, map, catchError } from 'rxjs/operators';
|
||||
|
||||
@@ -36,30 +36,24 @@ export const EXTENSION_DATA_LOADERS = new InjectionToken<ExtensionLoaderCallback
|
||||
factory: DefaultExtensionLoaderFactory
|
||||
});
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class ExtensionsDataLoaderGuard implements CanActivate {
|
||||
private invoked = false;
|
||||
let invoked = false;
|
||||
|
||||
constructor(
|
||||
@Inject(EXTENSION_DATA_LOADERS)
|
||||
private extensionDataLoaders: ExtensionLoaderCallback[]
|
||||
) {}
|
||||
|
||||
canActivate(route: ActivatedRouteSnapshot): Observable<boolean> {
|
||||
if (!this.invoked) {
|
||||
if (!this.extensionDataLoaders.length) {
|
||||
this.invoked = true;
|
||||
export const ExtensionsDataLoaderGuard: CanActivateFn = (route: ActivatedRouteSnapshot): Observable<boolean> => {
|
||||
const extensionDataLoaders = inject(EXTENSION_DATA_LOADERS);
|
||||
if (!invoked) {
|
||||
if (!extensionDataLoaders.length) {
|
||||
invoked = true;
|
||||
return of(true);
|
||||
}
|
||||
|
||||
const dataLoaderCallbacks = this.extensionDataLoaders.map((callback) => callback(route));
|
||||
const dataLoaderCallbacks = 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),
|
||||
tap(() => (this.invoked = true)),
|
||||
tap(() => (invoked = true)),
|
||||
catchError((e) => {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error('Some of the extension data loader guards has been errored.');
|
||||
@@ -71,9 +65,8 @@ export class ExtensionsDataLoaderGuard implements CanActivate {
|
||||
} else {
|
||||
return of(true);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
canActivateChild(): Observable<boolean> {
|
||||
return of(true);
|
||||
}
|
||||
}
|
||||
export const resetInvoked = () => {
|
||||
invoked = false;
|
||||
};
|
||||
|
@@ -25,11 +25,10 @@
|
||||
import { AppConfigService } from '@alfresco/adf-core';
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { PluginEnabledGuard } from './plugin-enabled.guard';
|
||||
import { ActivatedRouteSnapshot, Router } from '@angular/router';
|
||||
import { ActivatedRouteSnapshot, Router, RouterStateSnapshot } from '@angular/router';
|
||||
import { HttpClientTestingModule } from '@angular/common/http/testing';
|
||||
|
||||
describe('PluginEnabledGuard', () => {
|
||||
let service: PluginEnabledGuard;
|
||||
let getSpy: jasmine.Spy<(key: string, defaultValue?: boolean) => boolean>;
|
||||
let route: ActivatedRouteSnapshot;
|
||||
|
||||
@@ -37,7 +36,6 @@ describe('PluginEnabledGuard', () => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [HttpClientTestingModule]
|
||||
});
|
||||
service = TestBed.inject(PluginEnabledGuard);
|
||||
getSpy = spyOn(TestBed.inject(AppConfigService), 'get');
|
||||
route = new ActivatedRouteSnapshot();
|
||||
route.data = {
|
||||
@@ -45,41 +43,31 @@ describe('PluginEnabledGuard', () => {
|
||||
};
|
||||
});
|
||||
|
||||
describe('canActivate', () => {
|
||||
it('should call appConfigService.get with correct parameters', () => {
|
||||
service.canActivate(route);
|
||||
TestBed.runInInjectionContext(() => PluginEnabledGuard(route, {} as RouterStateSnapshot));
|
||||
expect(getSpy).toHaveBeenCalledWith(route.data.plugin, true);
|
||||
});
|
||||
|
||||
it('should return true if appConfigService.get returns true', () => {
|
||||
getSpy.and.returnValue(true);
|
||||
const result = TestBed.runInInjectionContext(() => PluginEnabledGuard(route, {} as RouterStateSnapshot));
|
||||
|
||||
expect(service.canActivate(route)).toBeTrue();
|
||||
expect(result).toBeTrue();
|
||||
});
|
||||
|
||||
it('should return false if appConfigService.get returns false', () => {
|
||||
getSpy.and.returnValue(true);
|
||||
getSpy.and.returnValue(false);
|
||||
const result = TestBed.runInInjectionContext(() => PluginEnabledGuard(route, {} as RouterStateSnapshot));
|
||||
|
||||
expect(service.canActivate(route)).toBeTrue();
|
||||
expect(result).toBeFalse();
|
||||
});
|
||||
|
||||
it('should navigate to root if plugin is not enabled', () => {
|
||||
getSpy.and.returnValue(false);
|
||||
const routerSpy = spyOn(TestBed.inject(Router), 'navigate');
|
||||
|
||||
service.canActivate(route);
|
||||
TestBed.runInInjectionContext(() => PluginEnabledGuard(route, {} as RouterStateSnapshot));
|
||||
|
||||
expect(routerSpy).toHaveBeenCalledWith(['/']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('canActivateChild', () => {
|
||||
it('should call canActivate with the same route and return its result', () => {
|
||||
spyOn(service, 'canActivate').and.callThrough();
|
||||
const result = service.canActivateChild(route);
|
||||
|
||||
expect(service.canActivate).toHaveBeenCalledWith(route);
|
||||
expect(result).toBe(service.canActivate(route));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@@ -22,27 +22,19 @@
|
||||
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { ActivatedRouteSnapshot, CanActivate, CanActivateChild, Router } from '@angular/router';
|
||||
import { ActivatedRouteSnapshot, Router, CanActivateFn } from '@angular/router';
|
||||
import { inject } from '@angular/core';
|
||||
import { AppConfigService } from '@alfresco/adf-core';
|
||||
import { Injectable } from '@angular/core';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class PluginEnabledGuard implements CanActivate, CanActivateChild {
|
||||
constructor(private appConfigService: AppConfigService, private router: Router) {}
|
||||
export const PluginEnabledGuard: CanActivateFn = (route: ActivatedRouteSnapshot) => {
|
||||
const appConfigService = inject(AppConfigService);
|
||||
const router = inject(Router);
|
||||
|
||||
canActivate(route: ActivatedRouteSnapshot): boolean {
|
||||
const isPluginEnabled = this.appConfigService.get(route.data.plugin, true);
|
||||
const isPluginEnabled = appConfigService.get(route.data.plugin, true);
|
||||
|
||||
if (!isPluginEnabled) {
|
||||
this.router.navigate(['/']);
|
||||
router.navigate(['/']);
|
||||
}
|
||||
|
||||
return isPluginEnabled;
|
||||
}
|
||||
|
||||
canActivateChild(route: ActivatedRouteSnapshot): boolean {
|
||||
return this.canActivate(route);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@@ -22,29 +22,44 @@
|
||||
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { of } from 'rxjs';
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
|
||||
import { Store } from '@ngrx/store';
|
||||
import { Observable, of } from 'rxjs';
|
||||
import { isQuickShareEnabled } from '@alfresco/aca-shared/store';
|
||||
import { AppSharedRuleGuard } from './shared.guard';
|
||||
|
||||
describe('AppSharedRuleGuard', () => {
|
||||
it('should allow activation if quick share is enabled', () => {
|
||||
const store: any = {
|
||||
select: () => of(true)
|
||||
};
|
||||
const guard = new AppSharedRuleGuard(store);
|
||||
const emittedSpy = jasmine.createSpy('emitted');
|
||||
|
||||
guard.canActivate().subscribe(emittedSpy);
|
||||
expect(emittedSpy).toHaveBeenCalledWith(true);
|
||||
let state: RouterStateSnapshot;
|
||||
const route: ActivatedRouteSnapshot = new ActivatedRouteSnapshot();
|
||||
const storeSpy = jasmine.createSpyObj('Store', ['select']);
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [{ provide: Store, useValue: storeSpy }]
|
||||
});
|
||||
|
||||
it('should allow child activation if quick share is enabled', () => {
|
||||
const store: any = {
|
||||
select: () => of(true)
|
||||
};
|
||||
const guard = new AppSharedRuleGuard(store);
|
||||
const emittedSpy = jasmine.createSpy('emitted');
|
||||
state = { url: 'some-url' } as RouterStateSnapshot;
|
||||
});
|
||||
|
||||
guard.canActivateChild().subscribe(emittedSpy);
|
||||
expect(emittedSpy).toHaveBeenCalledWith(true);
|
||||
it('should allow activation if quick share is enabled', (done) => {
|
||||
storeSpy.select.and.returnValue(of(true));
|
||||
const guard = TestBed.runInInjectionContext(() => AppSharedRuleGuard(route, state)) as Observable<boolean>;
|
||||
|
||||
guard.subscribe((response) => {
|
||||
expect(storeSpy.select).toHaveBeenCalledWith(isQuickShareEnabled);
|
||||
expect(response).toBeTrue();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should not allow activation if quick share is disabled', (done) => {
|
||||
storeSpy.select.and.returnValue(of(false));
|
||||
const guard = TestBed.runInInjectionContext(() => AppSharedRuleGuard(route, state)) as Observable<boolean>;
|
||||
|
||||
guard.subscribe((response) => {
|
||||
expect(storeSpy.select).toHaveBeenCalledWith(isQuickShareEnabled);
|
||||
expect(response).toBeFalse();
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@@ -22,27 +22,13 @@
|
||||
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { Injectable } from '@angular/core';
|
||||
import { CanActivate } from '@angular/router';
|
||||
import { CanActivateFn } from '@angular/router';
|
||||
import { inject } from '@angular/core';
|
||||
import { Observable } from 'rxjs';
|
||||
import { Store } from '@ngrx/store';
|
||||
import { AppStore, isQuickShareEnabled } from '@alfresco/aca-shared/store';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AppSharedRuleGuard implements CanActivate {
|
||||
isQuickShareEnabled$: Observable<boolean>;
|
||||
|
||||
constructor(store: Store<AppStore>) {
|
||||
this.isQuickShareEnabled$ = store.select(isQuickShareEnabled);
|
||||
}
|
||||
|
||||
canActivate(): Observable<boolean> {
|
||||
return this.isQuickShareEnabled$;
|
||||
}
|
||||
|
||||
canActivateChild(): Observable<boolean> {
|
||||
return this.canActivate();
|
||||
}
|
||||
}
|
||||
export const AppSharedRuleGuard: CanActivateFn = (): Observable<boolean> => {
|
||||
const store = inject(Store<AppStore>);
|
||||
return store.select(isQuickShareEnabled);
|
||||
};
|
||||
|
Reference in New Issue
Block a user