Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check Replaceable Layout Component on Every Route Change #4738

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ import { TreeNode } from '../utils/tree-utils';
export class DynamicLayoutComponent implements OnDestroy {
layout: Type<any>;

// TODO: Consider a shared enum (eThemeSharedComponents) for known layouts
readonly layouts = new Map([
['application', 'Theme.ApplicationLayoutComponent'],
['account', 'Theme.AccountLayoutComponent'],
['empty', 'Theme.EmptyLayoutComponent'],
]);

isLayoutVisible = true;

constructor(
Expand All @@ -36,11 +43,6 @@ export class DynamicLayoutComponent implements OnDestroy {
const route = injector.get(ActivatedRoute);
const router = injector.get(Router);
const routes = injector.get(RoutesService);
const layouts = {
application: this.getComponent('Theme.ApplicationLayoutComponent'),
account: this.getComponent('Theme.AccountLayoutComponent'),
empty: this.getComponent('Theme.EmptyLayoutComponent'),
};

router.events.pipe(takeUntilDestroy(this)).subscribe(event => {
if (event instanceof NavigationEnd) {
Expand All @@ -62,7 +64,8 @@ export class DynamicLayoutComponent implements OnDestroy {

if (!expectedLayout) expectedLayout = eLayoutType.empty;

this.layout = layouts[expectedLayout].component;
const key = this.layouts.get(expectedLayout);
this.layout = this.getComponent(key).component;
}
});

Expand Down
17 changes: 7 additions & 10 deletions npm/ng-packs/packages/core/src/lib/guards/permission.guard.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot, Router } from '@angular/router';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';
import { Store } from '@ngxs/store';
import { Observable, of } from 'rxjs';
import { tap } from 'rxjs/operators';
import snq from 'snq';
import { RestOccurError } from '../actions/rest.actions';
import { ConfigState } from '../states/config.state';
import { RoutesService } from '../services/routes.service';
import { ConfigState } from '../states/config.state';
import { findRoute, getRoutePath } from '../utils/route-utils';

@Injectable({
Expand All @@ -15,18 +14,16 @@ import { findRoute, getRoutePath } from '../utils/route-utils';
export class PermissionGuard implements CanActivate {
constructor(private router: Router, private routes: RoutesService, private store: Store) {}

canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot,
): Observable<boolean> | boolean {
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
let { requiredPolicy } = route.data || {};

if (!requiredPolicy) {
requiredPolicy = findRoute(this.routes, getRoutePath(this.router, state.url))?.requiredPolicy;

if (!requiredPolicy) return true;
const route = findRoute(this.routes, getRoutePath(this.router, state.url));
requiredPolicy = route?.requiredPolicy;
}

if (!requiredPolicy) return of(true);

return this.store.select(ConfigState.getGrantedPolicy(requiredPolicy)).pipe(
tap(access => {
if (!access) {
Expand Down
74 changes: 62 additions & 12 deletions npm/ng-packs/packages/core/src/lib/tests/permission.guard.spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,57 @@
import { APP_BASE_HREF } from '@angular/common';
import { Component } from '@angular/core';
import { RouterModule } from '@angular/router';
import { createServiceFactory, SpectatorService, SpyObject } from '@ngneat/spectator/jest';
import { Store } from '@ngxs/store';
import { Actions, Store } from '@ngxs/store';
import { of } from 'rxjs';
import { PermissionGuard } from '../guards/permission.guard';
import { RestOccurError } from '../actions';
import { PermissionGuard } from '../guards/permission.guard';
import { RoutesService } from '../services/routes.service';

describe('PermissionGuard', () => {
let spectator: SpectatorService<PermissionGuard>;
let guard: PermissionGuard;
let routes: SpyObject<RoutesService>;
let store: SpyObject<Store>;

@Component({ template: '' })
class DummyComponent {}

const createService = createServiceFactory({
service: PermissionGuard,
mocks: [Store],
declarations: [DummyComponent],
imports: [
RouterModule.forRoot([
{
path: 'test',
component: DummyComponent,
data: {
requiredPolicy: 'TestPolicy',
},
},
]),
],
providers: [
{
provide: APP_BASE_HREF,
useValue: '/',
},
{
provide: Actions,
useValue: {
pipe() {
return of(null);
},
},
},
],
});

beforeEach(() => {
spectator = createService();
guard = spectator.service;
routes = spectator.inject(RoutesService);
store = spectator.get(Store);
});

Expand All @@ -41,17 +76,32 @@ describe('PermissionGuard', () => {
});
});

it('should find the requiredPolicy from child route', done => {
it('should check the requiredPolicy from RoutesService', done => {
routes.add([
{
path: '/test',
name: 'Test',
requiredPolicy: 'TestPolicy',
},
]);
store.select.andReturn(of(false));
const spy = jest.spyOn(store, 'select');
guard
.canActivate(
{ data: {}, routeConfig: { children: [{ path: 'test', data: { requiredPolicy: 'TestPolicy' } }] } } as any,
{ url: 'test' } as any,
)
.subscribe(() => {
expect(spy.mock.calls[0][0]({ auth: { grantedPolicies: { TestPolicy: true } } })).toBe(true);
done();
});
guard.canActivate({ data: {} } as any, { url: 'test' } as any).subscribe(() => {
expect(spy.mock.calls[0][0]({ auth: { grantedPolicies: { TestPolicy: true } } })).toBe(true);
done();
});
});

it('should return Observable<true> if RoutesService does not have requiredPolicy for given URL', done => {
routes.add([
{
path: '/test',
name: 'Test',
},
]);
guard.canActivate({ data: {} } as any, { url: 'test' } as any).subscribe(result => {
expect(result).toBe(true);
done();
});
});
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ConfigState } from '@abp/ng.core';
import { createHttpFactory, HttpMethod, SpectatorHttp, SpyObject } from '@ngneat/spectator/jest';
import { NgxsModule, Store } from '@ngxs/store';
import { of, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { Rest } from '../models';
import { RestService } from '../services/rest.service';
import { ConfigState } from '../states/config.state';
import { CORE_OPTIONS } from '../tokens';

describe('HttpClient testing', () => {
Expand Down