Commit c7031ee8 authored by Tifenn Guillas's avatar Tifenn Guillas
Browse files

Merge branch '127-restrict-navigation-depending-on-instance-config' into 'develop'

Resolve "Restrict navigation depending on instance config"

Closes #127

See merge request !155
parents 20084a95 27c8f860
Pipeline #3287 passed with stages
in 8 minutes and 49 seconds
......@@ -17,6 +17,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- #138 => Sort attributes and put scrollbar if table too long in Documentation module
- #133 => Change typo if only one dataset
### Security
- #127 => Restrict navigation depending on instance configuration
## [3.4.0] - 2020-10
### Added
- #132 => Add reset cone search button in search multiple
......
import { NgModule } from '@angular/core';
import { Routes, RouterModule, PreloadAllModules } from '@angular/router';
import { NavigationGuard } from './core/navigation.guard';
import { NotFoundPageComponent } from './core/containers/not-found-page.component';
const routes: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{
path: 'search',
loadChildren: () => import('./search/search.module').then(m => m.SearchModule)
loadChildren: () => import('./search/search.module').then(m => m.SearchModule),
canActivate: [NavigationGuard]
},
{
path: 'search-multiple',
loadChildren: () => import('./search-multiple/search-multiple.module').then(m => m.SearchMultipleModule)
loadChildren: () => import('./search-multiple/search-multiple.module').then(m => m.SearchMultipleModule),
canActivate: [NavigationGuard]
},
{
path: 'detail/:dname/:objectSelected',
loadChildren: () => import('./detail/detail.module').then(m => m.DetailModule)
loadChildren: () => import('./detail/detail.module').then(m => m.DetailModule),
canActivate: [NavigationGuard]
},
{
path: 'documentation',
loadChildren: () => import('./documentation/documentation.module').then(m => m.DocumentationModule)
loadChildren: () => import('./documentation/documentation.module').then(m => m.DocumentationModule),
canActivate: [NavigationGuard]
},
{ path: '**', component: NotFoundPageComponent }
];
......
......@@ -58,7 +58,7 @@ describe('[Core] Component: NavComponent', () => {
const template = fixture.nativeElement;
expect(template.querySelector('#search_link')).toBeTruthy();
expect(template.querySelector('#search_multiple_link')).toBeTruthy();
expect(template.querySelector('#documentation_link')).toBeTruthy();
expect(template.querySelector('#documentation_link')).toBeNull();
});
it('should not display search, search multiple and documentation links if instance config don\'t allows it', () => {
......
import { TestBed } from '@angular/core/testing';
import { provideMockStore } from '@ngrx/store/testing';
import { NavigationGuard } from './navigation.guard';
import * as fromMetamodel from '../metamodel/reducers';
import { INSTANCE } from '../../settings/test-data/instance';
describe('[Guard] NavigationGuard', () => {
let guard: NavigationGuard;
const initialState = { metamodel: {
...fromMetamodel,
instance: { instance: INSTANCE }
}};
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
provideMockStore({ initialState })
]
});
guard = TestBed.inject(NavigationGuard);
});
it('should be created', () => {
expect(guard).toBeTruthy();
});
it('#isModuleAllowed() should return if module is allowed or not', () => {
guard.isModuleAllowed('search').subscribe(allowed => expect(allowed).toBeTruthy());
guard.isModuleAllowed('search-multiple').subscribe(allowed => expect(allowed).toBeTruthy());
guard.isModuleAllowed('documentation').subscribe(allowed => expect(allowed).toBeFalsy());
});
});
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot, UrlTree } from '@angular/router';
import { Store } from '@ngrx/store';
import { Observable } from 'rxjs';
import { filter, map } from 'rxjs/operators';
import * as fromMetamodel from '../metamodel/reducers';
@Injectable({
providedIn: 'root'
})
export class NavigationGuard implements CanActivate {
constructor(private store$: Store<{ metamodel: fromMetamodel.State }>) { }
// canLoad(
// route: Route,
// segments: UrlSegment[]): Observable<boolean> | Promise<boolean> | boolean {
// const module: string = route.path;
// return this.isModuleAllowed(module).pipe(take(1));
// }
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
const module: string = next.routeConfig.path;
return this.isModuleAllowed(module);
}
isModuleAllowed(module: string): Observable<boolean> {
return this.store$.select(state => state.metamodel.instance.instance)
.pipe(
filter( instance => instance !== null),
map(instance => {
if (module === 'search-multiple') {
return instance.config.search_multiple.allowed;
} else {
return instance.config[module];
}
})
);
}
}
......@@ -12,6 +12,6 @@ export const INSTANCE: Instance = {
allowed: true,
all_datasets_selected: true,
},
documentation: true
documentation: false
}
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment