From bba42b762a77587eb41c85be3a70804e169ba52e Mon Sep 17 00:00:00 2001 From: alexKhymenko Date: Mon, 26 Jul 2021 14:06:41 +0300 Subject: [PATCH] Add defaultRedirectTo to configurationService --- ...s-guard-can-activate-child.service.spec.ts | 42 ++++----- ...permissions-guard-can-load.service.spec.ts | 46 +++++----- .../router/permissions-guard.service.spec.ts | 89 ++++++++++++++----- .../lib/router/permissions-guard.service.ts | 9 +- .../src/lib/service/configuration.service.ts | 4 + .../src/lib/store/configuration.store.ts | 3 + 6 files changed, 126 insertions(+), 67 deletions(-) diff --git a/projects/ngx-permissions/src/lib/router/permissions-guard-can-activate-child.service.spec.ts b/projects/ngx-permissions/src/lib/router/permissions-guard-can-activate-child.service.spec.ts index 3fef0ef..5339762 100644 --- a/projects/ngx-permissions/src/lib/router/permissions-guard-can-activate-child.service.spec.ts +++ b/projects/ngx-permissions/src/lib/router/permissions-guard-can-activate-child.service.spec.ts @@ -1,7 +1,7 @@ import { fakeAsync, inject, TestBed } from '@angular/core/testing'; import { ActivatedRouteSnapshot, Router, RouterStateSnapshot } from '@angular/router'; import { RouterTestingModule } from '@angular/router/testing'; -import { NgxPermissionsModule } from '../index'; +import {NgxPermissionsConfigurationService, NgxPermissionsModule} from '../index'; import { NgxPermissionsService } from '../service/permissions.service'; import { NgxRolesService } from '../service/roles.service'; import { NgxPermissionsGuard } from './permissions-guard.service'; @@ -16,7 +16,7 @@ describe('Permissions guard only', () => { imports: [NgxPermissionsModule.forRoot()] }); }); - beforeEach(inject([NgxPermissionsService, NgxRolesService], (service: NgxPermissionsService, rolesService: NgxRolesService) => { + beforeEach(inject([NgxPermissionsService, NgxRolesService, NgxPermissionsConfigurationService], (service: NgxPermissionsService, rolesService: NgxRolesService, configurationService: NgxPermissionsConfigurationService) => { fakeRouter = { navigate: () => { } @@ -24,7 +24,7 @@ describe('Permissions guard only', () => { spyOn(fakeRouter, 'navigate'); service.addPermission('ADMIN'); - permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router); + permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router, configurationService); })); it('should create an instance', () => { @@ -124,7 +124,7 @@ describe('Permissions guard Except', () => { imports: [NgxPermissionsModule.forRoot()] }); }); - beforeEach(inject([NgxPermissionsService, NgxRolesService], (service: NgxPermissionsService, rolesService: NgxRolesService) => { + beforeEach(inject([NgxPermissionsService, NgxRolesService, NgxPermissionsConfigurationService], (service: NgxPermissionsService, rolesService: NgxRolesService, configurationService: NgxPermissionsConfigurationService) => { fakeRouter = { navigate: () => { } @@ -132,7 +132,7 @@ describe('Permissions guard Except', () => { spyOn(fakeRouter, 'navigate'); service.addPermission('MANAGER'); - permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router); + permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router, configurationService); })); it('should create an instance', () => { @@ -234,7 +234,7 @@ describe('Permissions guard Except and only together', () => { imports: [NgxPermissionsModule.forRoot()] }); }); - beforeEach(inject([NgxPermissionsService, NgxRolesService], (service: NgxPermissionsService, rolesService: NgxRolesService) => { + beforeEach(inject([NgxPermissionsService, NgxRolesService, NgxPermissionsConfigurationService], (service: NgxPermissionsService, rolesService: NgxRolesService, configurationService: NgxPermissionsConfigurationService) => { fakeRouter = { navigate: () => { } @@ -242,7 +242,7 @@ describe('Permissions guard Except and only together', () => { spyOn(fakeRouter, 'navigate'); service.addPermission('MANAGER'); - permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router); + permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router, configurationService); })); it('should create an instance', () => { @@ -351,8 +351,8 @@ describe('Permissions guard use only dynamically', () => { }); }); beforeEach(inject( - [NgxPermissionsService, NgxRolesService], - (service: NgxPermissionsService, rolesService: NgxRolesService, router: Router) => { + [NgxPermissionsService, NgxRolesService, NgxPermissionsConfigurationService], + (service: NgxPermissionsService, rolesService: NgxRolesService, configurationService: NgxPermissionsConfigurationService, router: Router) => { fakeRouter = { navigate: () => { } @@ -361,7 +361,7 @@ describe('Permissions guard use only dynamically', () => { service.addPermission('MANAGER'); // fakeRouter = router; spyOn(fakeRouter, 'navigate'); - permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router); + permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router, configurationService); })); it('should create an instance', () => { @@ -498,8 +498,8 @@ describe('Permissions guard test redirectTo path parameters dynamically', () => }); }); beforeEach(inject( - [NgxPermissionsService, NgxRolesService], - (service: NgxPermissionsService, rolesService: NgxRolesService, router: Router) => { + [NgxPermissionsService, NgxRolesService, NgxPermissionsConfigurationService], + (service: NgxPermissionsService, rolesService: NgxRolesService, configurationService: NgxPermissionsConfigurationService, router: Router) => { fakeRouter = { navigate: () => { } @@ -508,7 +508,7 @@ describe('Permissions guard test redirectTo path parameters dynamically', () => service.addPermission('MANAGER'); // fakeRouter = router; spyOn(fakeRouter, 'navigate'); - permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router); + permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router, configurationService); })); it('should create an instance', () => { @@ -603,7 +603,7 @@ describe('Permissions guard test redirectTo path multiple redirectionRule', () = imports: [NgxPermissionsModule.forRoot()] }); }); - beforeEach(inject([NgxPermissionsService, NgxRolesService], (service: NgxPermissionsService, rolesService: NgxRolesService) => { + beforeEach(inject([NgxPermissionsService, NgxRolesService, NgxPermissionsConfigurationService], (service: NgxPermissionsService, rolesService: NgxRolesService, configurationService: NgxPermissionsConfigurationService) => { fakeRouter = { navigate: () => { } @@ -613,7 +613,7 @@ describe('Permissions guard test redirectTo path multiple redirectionRule', () = fakeService = service; // fakeRouter = router; spyOn(fakeRouter, 'navigate'); - permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router); + permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router, configurationService); })); it('should create an instance', () => { @@ -904,7 +904,7 @@ describe('Permissions guard test redirectTo path multiple redirectionRule permis imports: [NgxPermissionsModule.forRoot()] }); }); - beforeEach(inject([NgxPermissionsService, NgxRolesService], (service: NgxPermissionsService, rolesService: NgxRolesService) => { + beforeEach(inject([NgxPermissionsService, NgxRolesService, NgxPermissionsConfigurationService], (service: NgxPermissionsService, rolesService: NgxRolesService, configurationService: NgxPermissionsConfigurationService) => { fakeRouter = { navigate: () => { } @@ -914,7 +914,7 @@ describe('Permissions guard test redirectTo path multiple redirectionRule permis fakeService = service; // fakeRouter = router; spyOn(fakeRouter, 'navigate'); - permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router); + permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router, configurationService); })); it('should create an instance', () => { @@ -1088,7 +1088,7 @@ describe('Permissions guard test redirectTo path dynamic redirectionRule permiss imports: [NgxPermissionsModule.forRoot()] }); }); - beforeEach(inject([NgxPermissionsService, NgxRolesService], (service: NgxPermissionsService, rolesService: NgxRolesService) => { + beforeEach(inject([NgxPermissionsService, NgxRolesService, NgxPermissionsConfigurationService], (service: NgxPermissionsService, rolesService: NgxRolesService, configurationService: NgxPermissionsConfigurationService) => { fakeRouter = { navigate: () => { } @@ -1098,7 +1098,7 @@ describe('Permissions guard test redirectTo path dynamic redirectionRule permiss fakeService = service; // fakeRouter = router; spyOn(fakeRouter, 'navigate'); - permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router); + permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router, configurationService); })); it('should create an instance', () => { @@ -1319,7 +1319,7 @@ describe('Permissions guard test redirectTo as function', () => { imports: [NgxPermissionsModule.forRoot()] }); }); - beforeEach(inject([NgxPermissionsService, NgxRolesService], (service: NgxPermissionsService, rolesService: NgxRolesService) => { + beforeEach(inject([NgxPermissionsService, NgxRolesService, NgxPermissionsConfigurationService], (service: NgxPermissionsService, rolesService: NgxRolesService, configurationService: NgxPermissionsConfigurationService) => { fakeRouter = { navigate: () => { } @@ -1329,7 +1329,7 @@ describe('Permissions guard test redirectTo as function', () => { fakeService = service; // fakeRouter = router; spyOn(fakeRouter, 'navigate'); - permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router); + permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router, configurationService); })); it('should create an instance', () => { diff --git a/projects/ngx-permissions/src/lib/router/permissions-guard-can-load.service.spec.ts b/projects/ngx-permissions/src/lib/router/permissions-guard-can-load.service.spec.ts index ed590c6..12b87bd 100644 --- a/projects/ngx-permissions/src/lib/router/permissions-guard-can-load.service.spec.ts +++ b/projects/ngx-permissions/src/lib/router/permissions-guard-can-load.service.spec.ts @@ -1,7 +1,7 @@ import { fakeAsync, inject, TestBed } from '@angular/core/testing'; import { ActivatedRouteSnapshot, Router, RouterStateSnapshot } from '@angular/router'; import { RouterTestingModule } from '@angular/router/testing'; -import { NgxPermissionsModule } from '../index'; +import {NgxPermissionsConfigurationService, NgxPermissionsModule} from '../index'; import { NgxPermissionsService } from '../service/permissions.service'; import { NgxRolesService } from '../service/roles.service'; import { NgxPermissionsGuard } from './permissions-guard.service'; @@ -16,7 +16,7 @@ describe('Permissions guard only', () => { imports: [NgxPermissionsModule.forRoot()] }); }); - beforeEach(inject([NgxPermissionsService, NgxRolesService], (service: NgxPermissionsService, rolesService: NgxRolesService) => { + beforeEach(inject([NgxPermissionsService, NgxRolesService, NgxPermissionsConfigurationService], (service: NgxPermissionsService, rolesService: NgxRolesService, configurationService: NgxPermissionsConfigurationService) => { fakeRouter = { navigate: () => { } @@ -24,7 +24,7 @@ describe('Permissions guard only', () => { spyOn(fakeRouter, 'navigate'); service.addPermission('ADMIN'); - permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router); + permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router, configurationService); })); it('should create an instance', () => { @@ -124,7 +124,7 @@ describe('Permissions guard Except', () => { imports: [NgxPermissionsModule.forRoot()] }); }); - beforeEach(inject([NgxPermissionsService, NgxRolesService], (service: NgxPermissionsService, rolesService: NgxRolesService) => { + beforeEach(inject([NgxPermissionsService, NgxRolesService, NgxPermissionsConfigurationService], (service: NgxPermissionsService, rolesService: NgxRolesService, configurationService: NgxPermissionsConfigurationService) => { fakeRouter = { navigate: () => { } @@ -132,7 +132,7 @@ describe('Permissions guard Except', () => { spyOn(fakeRouter, 'navigate'); service.addPermission('MANAGER'); - permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router); + permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router, configurationService); })); it('should create an instance', () => { @@ -234,7 +234,7 @@ describe('Permissions guard Except and only together', () => { imports: [NgxPermissionsModule.forRoot()] }); }); - beforeEach(inject([NgxPermissionsService, NgxRolesService], (service: NgxPermissionsService, rolesService: NgxRolesService) => { + beforeEach(inject([NgxPermissionsService, NgxRolesService, NgxPermissionsConfigurationService], (service: NgxPermissionsService, rolesService: NgxRolesService, configurationService: NgxPermissionsConfigurationService) => { fakeRouter = { navigate: () => { } @@ -242,7 +242,7 @@ describe('Permissions guard Except and only together', () => { spyOn(fakeRouter, 'navigate'); service.addPermission('MANAGER'); - permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router); + permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router, configurationService); })); it('should create an instance', () => { @@ -333,7 +333,7 @@ describe('Permissions guard Except and only together with isolation in root', () imports: [NgxPermissionsModule.forRoot({permissionsIsolate: true, rolesIsolate: true})] }); }); - beforeEach(inject([NgxPermissionsService, NgxRolesService], (service: NgxPermissionsService, rolesService: NgxRolesService) => { + beforeEach(inject([NgxPermissionsService, NgxRolesService, NgxPermissionsConfigurationService], (service: NgxPermissionsService, rolesService: NgxRolesService, configurationService: NgxPermissionsConfigurationService) => { fakeRouter = { navigate: () => { } @@ -341,7 +341,7 @@ describe('Permissions guard Except and only together with isolation in root', () spyOn(fakeRouter, 'navigate'); service.addPermission('MANAGER'); - permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router); + permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router, configurationService); })); it('should create an instance', () => { @@ -450,8 +450,8 @@ describe('Permissions guard use only dynamically', () => { }); }); beforeEach(inject( - [NgxPermissionsService, NgxRolesService], - (service: NgxPermissionsService, rolesService: NgxRolesService, router: Router) => { + [NgxPermissionsService, NgxRolesService, NgxPermissionsConfigurationService], + (service: NgxPermissionsService, rolesService: NgxRolesService, configurationService: NgxPermissionsConfigurationService, router: Router) => { fakeRouter = { navigate: () => { } @@ -460,7 +460,7 @@ describe('Permissions guard use only dynamically', () => { service.addPermission('MANAGER'); // fakeRouter = router; spyOn(fakeRouter, 'navigate'); - permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router); + permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router, configurationService); })); it('should create an instance', () => { @@ -597,8 +597,8 @@ describe('Permissions guard test redirectTo path parameters dynamically', () => }); }); beforeEach(inject( - [NgxPermissionsService, NgxRolesService], - (service: NgxPermissionsService, rolesService: NgxRolesService, router: Router) => { + [NgxPermissionsService, NgxRolesService, NgxPermissionsConfigurationService], + (service: NgxPermissionsService, rolesService: NgxRolesService, configurationService: NgxPermissionsConfigurationService, router: Router) => { fakeRouter = { navigate: () => { } @@ -607,7 +607,7 @@ describe('Permissions guard test redirectTo path parameters dynamically', () => service.addPermission('MANAGER'); // fakeRouter = router; spyOn(fakeRouter, 'navigate'); - permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router); + permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router, configurationService); })); it('should create an instance', () => { @@ -702,7 +702,7 @@ describe('Permissions guard test redirectTo path multiple redirectionRule', () = imports: [NgxPermissionsModule.forRoot()] }); }); - beforeEach(inject([NgxPermissionsService, NgxRolesService], (service: NgxPermissionsService, rolesService: NgxRolesService) => { + beforeEach(inject([NgxPermissionsService, NgxRolesService, NgxPermissionsConfigurationService], (service: NgxPermissionsService, rolesService: NgxRolesService, configurationService: NgxPermissionsConfigurationService) => { fakeRouter = { navigate: () => { } @@ -712,7 +712,7 @@ describe('Permissions guard test redirectTo path multiple redirectionRule', () = fakeService = service; // fakeRouter = router; spyOn(fakeRouter, 'navigate'); - permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router); + permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router, configurationService); })); it('should create an instance', () => { @@ -1004,7 +1004,7 @@ describe('Permissions guard test redirectTo path multiple redirectionRule permis imports: [NgxPermissionsModule.forRoot()] }); }); - beforeEach(inject([NgxPermissionsService, NgxRolesService], (service: NgxPermissionsService, rolesService: NgxRolesService) => { + beforeEach(inject([NgxPermissionsService, NgxRolesService, NgxPermissionsConfigurationService], (service: NgxPermissionsService, rolesService: NgxRolesService, configurationService: NgxPermissionsConfigurationService) => { fakeRouter = { navigate: () => { } @@ -1014,7 +1014,7 @@ describe('Permissions guard test redirectTo path multiple redirectionRule permis fakeService = service; // fakeRouter = router; spyOn(fakeRouter, 'navigate'); - permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router); + permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router, configurationService); })); it('should create an instance', () => { @@ -1188,7 +1188,7 @@ describe('Permissions guard test redirectTo path dynamic redirectionRule permiss imports: [NgxPermissionsModule.forRoot()] }); }); - beforeEach(inject([NgxPermissionsService, NgxRolesService], (service: NgxPermissionsService, rolesService: NgxRolesService) => { + beforeEach(inject([NgxPermissionsService, NgxRolesService, NgxPermissionsConfigurationService], (service: NgxPermissionsService, rolesService: NgxRolesService, configurationService: NgxPermissionsConfigurationService) => { fakeRouter = { navigate: () => { } @@ -1198,7 +1198,7 @@ describe('Permissions guard test redirectTo path dynamic redirectionRule permiss fakeService = service; // fakeRouter = router; spyOn(fakeRouter, 'navigate'); - permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router); + permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router, configurationService); })); it('should create an instance', () => { @@ -1419,7 +1419,7 @@ describe('Permissions guard test redirectTo as function', () => { imports: [NgxPermissionsModule.forRoot()] }); }); - beforeEach(inject([NgxPermissionsService, NgxRolesService], (service: NgxPermissionsService, rolesService: NgxRolesService) => { + beforeEach(inject([NgxPermissionsService, NgxRolesService, NgxPermissionsConfigurationService], (service: NgxPermissionsService, rolesService: NgxRolesService, configurationService: NgxPermissionsConfigurationService) => { fakeRouter = { navigate: () => { } @@ -1429,7 +1429,7 @@ describe('Permissions guard test redirectTo as function', () => { fakeService = service; // fakeRouter = router; spyOn(fakeRouter, 'navigate'); - permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router); + permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router, configurationService); })); it('should create an instance', () => { diff --git a/projects/ngx-permissions/src/lib/router/permissions-guard.service.spec.ts b/projects/ngx-permissions/src/lib/router/permissions-guard.service.spec.ts index 3faf44b..033e198 100644 --- a/projects/ngx-permissions/src/lib/router/permissions-guard.service.spec.ts +++ b/projects/ngx-permissions/src/lib/router/permissions-guard.service.spec.ts @@ -1,7 +1,7 @@ import { fakeAsync, inject, TestBed } from '@angular/core/testing'; import { ActivatedRouteSnapshot, Router, RouterStateSnapshot } from '@angular/router'; import { RouterTestingModule } from '@angular/router/testing'; -import { NgxPermissionsModule } from '../index'; +import {NgxPermissionsConfigurationService, NgxPermissionsModule} from '../index'; import { DEFAULT_REDIRECT_KEY } from '../model/permissions-router-data.model'; import { NgxPermissionsService } from '../service/permissions.service'; import { NgxRolesService } from '../service/roles.service'; @@ -18,12 +18,12 @@ describe('Permissions guard only', () => { }); }); beforeEach( - inject([NgxPermissionsService, NgxRolesService], (service: NgxPermissionsService, rolesService: NgxRolesService) => { + inject([NgxPermissionsService, NgxRolesService, NgxPermissionsConfigurationService], (service: NgxPermissionsService, rolesService: NgxRolesService, configurationService: NgxPermissionsConfigurationService ) => { fakeRouter = {navigate: () => {}} as any; spyOn(fakeRouter, 'navigate'); service.addPermission('ADMIN'); - permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router); + permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router, configurationService); })); it('should create an instance', () => { @@ -137,12 +137,12 @@ describe('Permissions guard Except', () => { imports: [NgxPermissionsModule.forRoot()] }); }); - beforeEach(inject([NgxPermissionsService, NgxRolesService], (service: NgxPermissionsService, rolesService: NgxRolesService) => { + beforeEach(inject([NgxPermissionsService, NgxRolesService, NgxPermissionsConfigurationService], (service: NgxPermissionsService, rolesService: NgxRolesService, configurationService: NgxPermissionsConfigurationService) => { fakeRouter = {navigate: () => {}} as any; spyOn(fakeRouter, 'navigate'); service.addPermission('MANAGER'); - permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router); + permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router, configurationService); })); it('should create an instance', () => { @@ -233,12 +233,12 @@ describe('Permissions guard Except and only together', () => { imports: [NgxPermissionsModule.forRoot()] }); }); - beforeEach(inject([NgxPermissionsService, NgxRolesService], (service: NgxPermissionsService, rolesService: NgxRolesService) => { + beforeEach(inject([NgxPermissionsService, NgxRolesService, NgxPermissionsConfigurationService], (service: NgxPermissionsService, rolesService: NgxRolesService, configurationService: NgxPermissionsConfigurationService) => { fakeRouter = {navigate: () => {}} as any; spyOn(fakeRouter, 'navigate'); service.addPermission('MANAGER'); - permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router); + permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router, configurationService); })); it('should create an instance', () => { @@ -337,13 +337,13 @@ describe('Permissions guard use only dynamically', () => { }); }); beforeEach( - inject([NgxPermissionsService, NgxRolesService], (service: NgxPermissionsService, rolesService: NgxRolesService, router: Router) => { + inject([NgxPermissionsService, NgxRolesService, NgxPermissionsConfigurationService], (service: NgxPermissionsService, rolesService: NgxRolesService, router: Router, configurationService: NgxPermissionsConfigurationService) => { fakeRouter = {navigate: () => {}} as any; service.addPermission('MANAGER'); // fakeRouter = router; spyOn(fakeRouter, 'navigate'); - permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router); + permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router, configurationService); })); it('should create an instance', () => { @@ -472,13 +472,13 @@ describe('Permissions guard test redirectTo path parameters dynamically', () => }); }); beforeEach( - inject([NgxPermissionsService, NgxRolesService], (service: NgxPermissionsService, rolesService: NgxRolesService, router: Router) => { + inject([NgxPermissionsService, NgxRolesService, NgxPermissionsConfigurationService], (service: NgxPermissionsService, rolesService: NgxRolesService, router: Router, configurationService: NgxPermissionsConfigurationService) => { fakeRouter = {navigate: () => {}} as any; service.addPermission('MANAGER'); // fakeRouter = router; spyOn(fakeRouter, 'navigate'); - permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router); + permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router, configurationService); })); it('should create an instance', () => { @@ -567,14 +567,14 @@ describe('Permissions guard test redirectTo path multiple redirectionRule', () = imports: [NgxPermissionsModule.forRoot()] }); }); - beforeEach(inject([NgxPermissionsService, NgxRolesService], (service: NgxPermissionsService, rolesService: NgxRolesService) => { + beforeEach(inject([NgxPermissionsService, NgxRolesService, NgxPermissionsConfigurationService], (service: NgxPermissionsService, rolesService: NgxRolesService, configurationService: NgxPermissionsConfigurationService) => { fakeRouter = {navigate: () => {}} as any; service.addPermission('canReadAgenda'); fakeService = service; // fakeRouter = router; spyOn(fakeRouter, 'navigate'); - permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router); + permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router, configurationService); })); it('should create an instance', () => { @@ -810,6 +810,8 @@ describe('Permissions guard test redirectTo path multiple redirectionRule', () = + + it ('should path when nothing fails in only block', fakeAsync(() => { fakeService.addPermission('canEditAgenda'); @@ -837,20 +839,22 @@ describe('Permissions guard test redirectTo path multiple redirectionRule permis let fakeRouter; let testRoute; let fakeService; + let fakeConfigurationService; beforeEach(() => { TestBed.configureTestingModule({ imports: [NgxPermissionsModule.forRoot()] }); }); - beforeEach(inject([NgxPermissionsService, NgxRolesService], (service: NgxPermissionsService, rolesService: NgxRolesService) => { + beforeEach(inject([NgxPermissionsService, NgxRolesService, NgxPermissionsConfigurationService], (service: NgxPermissionsService, rolesService: NgxRolesService, configurationService: NgxPermissionsConfigurationService) => { fakeRouter = {navigate: () => {}} as any; service.addPermission('canReadAgenda'); fakeService = service; // fakeRouter = router; + fakeConfigurationService = configurationService spyOn(fakeRouter, 'navigate'); - permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router); + permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router, configurationService); })); it('should create an instance', () => { @@ -935,6 +939,47 @@ describe('Permissions guard test redirectTo path multiple redirectionRule permis }); })); + it ('redirect to default route in configuration', fakeAsync(() => { + fakeConfigurationService.defaultRedirectTo = 'login' + testRoute = { data: { + permissions: { + except: 'canReadAgenda', + redirectTo: { + canRun: 'run', + } + }, + path: 'crisis-center/44' + }}; + (permissionGuard.canActivate(testRoute, {} as RouterStateSnapshot) as any).then((data) => { + expect(data).toEqual(false); + expect(fakeRouter.navigate).toHaveBeenCalledWith(['login']); + + }); + })); + + it ('redirect to default route in configuration using function', fakeAsync(() => { + + function loginRedirect(activateRouteSnapshot: ActivatedRouteSnapshot, + routerStateSnapshot: RouterStateSnapshot) { + return 'login'; + } + fakeConfigurationService.defaultRedirectTo = loginRedirect + + testRoute = { data: { + permissions: { + except: 'canReadAgenda', + redirectTo: { + canRun: 'run', + } + }, + path: 'crisis-center/44' + }}; + (permissionGuard.canActivate(testRoute, {} as RouterStateSnapshot) as any).then((data) => { + expect(data).toEqual(false); + expect(fakeRouter.navigate).toHaveBeenCalledWith(['login']); + + }); + })); it ('redirect to only failed route when except passes but only fails', fakeAsync(() => { testRoute = { data: { @@ -1012,14 +1057,14 @@ describe('Permissions guard test redirectTo path dynamic redirectionRule permiss imports: [NgxPermissionsModule.forRoot()] }); }); - beforeEach(inject([NgxPermissionsService, NgxRolesService], (service: NgxPermissionsService, rolesService: NgxRolesService) => { + beforeEach(inject([NgxPermissionsService, NgxRolesService, NgxPermissionsConfigurationService], (service: NgxPermissionsService, rolesService: NgxRolesService, configurationService: NgxPermissionsConfigurationService) => { fakeRouter = {navigate: () => {}} as any; service.addPermission('canReadAgenda'); fakeService = service; // fakeRouter = router; spyOn(fakeRouter, 'navigate'); - permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router); + permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router, configurationService); })); it('should create an instance', () => { @@ -1218,20 +1263,22 @@ describe('Permissions guard test redirectTo as function', () => { let fakeRouter; let testRoute; let fakeService; + let fakeConfigurationService; beforeEach(() => { TestBed.configureTestingModule({ imports: [NgxPermissionsModule.forRoot()] }); }); - beforeEach(inject([NgxPermissionsService, NgxRolesService], (service: NgxPermissionsService, rolesService: NgxRolesService) => { + beforeEach(inject([NgxPermissionsService, NgxRolesService, NgxPermissionsConfigurationService], (service: NgxPermissionsService, rolesService: NgxRolesService, configurationService: NgxPermissionsConfigurationService) => { fakeRouter = {navigate: () => {}} as any; service.addPermission('canReadAgenda'); fakeService = service; + fakeConfigurationService = configurationService; // fakeRouter = router; spyOn(fakeRouter, 'navigate'); - permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router); + permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router, configurationService); })); it('should create an instance', () => { @@ -1357,7 +1404,7 @@ describe('Role guard test redirectTo as function', () => { imports: [NgxPermissionsModule.forRoot()] }); }); - beforeEach(inject([NgxPermissionsService, NgxRolesService], (service: NgxPermissionsService, rolesService: NgxRolesService) => { + beforeEach(inject([NgxPermissionsService, NgxRolesService, NgxPermissionsConfigurationService], (service: NgxPermissionsService, rolesService: NgxRolesService, configurationService: NgxPermissionsConfigurationService) => { fakeRouter = {navigate: () => {}} as any; service.addPermission('canReadAgenda'); @@ -1367,7 +1414,7 @@ describe('Role guard test redirectTo as function', () => { fakeService = service; // fakeRouter = router; spyOn(fakeRouter, 'navigate'); - permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router); + permissionGuard = new NgxPermissionsGuard(service, rolesService, fakeRouter as Router, configurationService); })); it('should create an instance', () => { diff --git a/projects/ngx-permissions/src/lib/router/permissions-guard.service.ts b/projects/ngx-permissions/src/lib/router/permissions-guard.service.ts index 0b2c646..c93d749 100644 --- a/projects/ngx-permissions/src/lib/router/permissions-guard.service.ts +++ b/projects/ngx-permissions/src/lib/router/permissions-guard.service.ts @@ -27,6 +27,7 @@ import { import { NgxPermissionsService } from '../service/permissions.service'; import { NgxRolesService } from '../service/roles.service'; import { isFunction, isPlainObject, transformStringToArray } from '../utils/utils'; +import {NgxPermissionsConfigurationService} from "../service/configuration.service"; export interface NgxPermissionsData { only?: string | string[]; @@ -37,7 +38,10 @@ export interface NgxPermissionsData { @Injectable() export class NgxPermissionsGuard implements CanActivate, CanLoad, CanActivateChild { - constructor(private permissionsService: NgxPermissionsService, private rolesService: NgxRolesService, private router: Router) { + constructor(private permissionsService: NgxPermissionsService, + private rolesService: NgxRolesService, + private router: Router, + private configurationService: NgxPermissionsConfigurationService,) { } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise | boolean { @@ -276,7 +280,8 @@ export class NgxPermissionsGuard implements CanActivate, CanLoad, CanActivateChi if (isFunction(permissions.redirectTo)) { this.redirectToAnotherRoute(permissions.redirectTo, route, state, failedPermission); } else { - this.redirectToAnotherRoute(permissions.redirectTo[DEFAULT_REDIRECT_KEY], route, state, failedPermission); + const defaultRedirection = permissions.redirectTo[DEFAULT_REDIRECT_KEY] || this.configurationService.defaultRedirectTo + this.redirectToAnotherRoute(defaultRedirection, route, state, failedPermission); } } } diff --git a/projects/ngx-permissions/src/lib/service/configuration.service.ts b/projects/ngx-permissions/src/lib/service/configuration.service.ts index 63ed0ca..312b538 100644 --- a/projects/ngx-permissions/src/lib/service/configuration.service.ts +++ b/projects/ngx-permissions/src/lib/service/configuration.service.ts @@ -2,6 +2,7 @@ import { Inject, Injectable, InjectionToken, TemplateRef } from '@angular/core'; import { BehaviorSubject, Observable } from 'rxjs'; import { NgxPermissionsPredefinedStrategies } from '../enums/predefined-strategies.enum'; import { NgxPermissionsConfigurationStore } from '../store/configuration.store'; +import {RedirectTo, RedirectToFn} from "../model/permissions-router-data.model"; export type StrategyFunction = (templateRef?: TemplateRef) => void; @@ -19,6 +20,8 @@ export class NgxPermissionsConfigurationService { public onAuthorisedDefaultStrategy: string | undefined; public onUnAuthorisedDefaultStrategy: string | undefined; + public defaultRedirectTo: RedirectTo | RedirectToFn | undefined; + constructor( @Inject(USE_CONFIGURATION_STORE) private isolate: boolean = false, private configurationStore: NgxPermissionsConfigurationStore @@ -29,6 +32,7 @@ export class NgxPermissionsConfigurationService { this.onAuthorisedDefaultStrategy = this.isolate ? undefined : this.configurationStore.onAuthorisedDefaultStrategy; this.onUnAuthorisedDefaultStrategy = this.isolate ? undefined : this.configurationStore.onUnAuthorisedDefaultStrategy; + this.defaultRedirectTo = this.isolate ? undefined : this.configurationStore.defaultRedirectTo; } public setDefaultOnAuthorizedStrategy(name: string | 'remove' | 'show'): void { diff --git a/projects/ngx-permissions/src/lib/store/configuration.store.ts b/projects/ngx-permissions/src/lib/store/configuration.store.ts index bd0e3c9..0fcfa4c 100644 --- a/projects/ngx-permissions/src/lib/store/configuration.store.ts +++ b/projects/ngx-permissions/src/lib/store/configuration.store.ts @@ -1,6 +1,7 @@ import { Injectable } from '@angular/core'; import { BehaviorSubject, Observable } from 'rxjs'; import { Strategy } from '../service/configuration.service'; +import {RedirectTo, RedirectToFn} from "../model/permissions-router-data.model"; @Injectable() export class NgxPermissionsConfigurationStore { @@ -11,6 +12,8 @@ export class NgxPermissionsConfigurationStore { public onAuthorisedDefaultStrategy: string | undefined; public onUnAuthorisedDefaultStrategy: string | undefined; + public defaultRedirectTo: RedirectTo | RedirectToFn | undefined; + constructor() { }