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

Got the required policy in PermissionGuard from RoutesService #4663

Merged
merged 4 commits into from
Jul 7, 2020
Merged
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
@@ -1,9 +1,10 @@
import { Component, Injector, OnDestroy, Type, Optional, SkipSelf } from '@angular/core';
import { Component, Injector, OnDestroy, Optional, SkipSelf, Type } from '@angular/core';
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';
import { Store } from '@ngxs/store';
import { eLayoutType } from '../enums/common';
import { ABP } from '../models';
import { ReplaceableComponents } from '../models/replaceable-components';
import { LocalizationService } from '../services/localization.service';
import { RoutesService } from '../services/routes.service';
import { ReplaceableComponentsState } from '../states/replaceable-components.state';
import { findRoute, getRoutePath } from '../utils/route-utils';
Expand All @@ -16,15 +17,18 @@ import { TreeNode } from '../utils/tree-utils';
<ng-container *ngTemplateOutlet="layout ? componentOutlet : routerOutlet"></ng-container>
<ng-template #routerOutlet><router-outlet></router-outlet></ng-template>
<ng-template #componentOutlet
><ng-container *ngComponentOutlet="layout"></ng-container
><ng-container *ngIf="isLayoutVisible" [ngComponentOutlet]="layout"></ng-container
></ng-template>
`,
})
export class DynamicLayoutComponent implements OnDestroy {
layout: Type<any>;

isLayoutVisible = true;

constructor(
injector: Injector,
private localizationService: LocalizationService,
private store: Store,
@Optional() @SkipSelf() dynamicLayoutComponent: DynamicLayoutComponent,
) {
Expand Down Expand Up @@ -61,6 +65,15 @@ export class DynamicLayoutComponent implements OnDestroy {
this.layout = layouts[expectedLayout].component;
}
});

this.listenToLanguageChange();
}

private listenToLanguageChange() {
this.localizationService.languageChange.pipe(takeUntilDestroy(this)).subscribe(() => {
this.isLayoutVisible = false;
setTimeout(() => (this.isLayoutVisible = true), 0);
});
}

private getComponent(key: string): ReplaceableComponents.ReplaceableComponent {
Expand Down
33 changes: 16 additions & 17 deletions npm/ng-packs/packages/core/src/lib/guards/permission.guard.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot } from '@angular/router';
import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot, Router } 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';
import { ConfigState } from '../states';
import { RestOccurError } from '../actions/rest.actions';
import { ConfigState } from '../states/config.state';
import { RoutesService } from '../services/routes.service';
import { findRoute, getRoutePath } from '../utils/route-utils';

@Injectable({
providedIn: 'root',
})
export class PermissionGuard implements CanActivate {
constructor(private store: Store) {}
constructor(private router: Router, private routes: RoutesService, private store: Store) {}

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
let resource =
snq(() => route.data.routes.requiredPolicy) || snq(() => route.data.requiredPolicy as string);
if (!resource) {
resource = snq(
() =>
route.routeConfig.children.find(child => state.url.indexOf(child.path) > -1).data
.requiredPolicy,
);
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot,
): Observable<boolean> | boolean {
let { requiredPolicy } = route.data || {};

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

if (!requiredPolicy) return true;
}

return this.store.select(ConfigState.getGrantedPolicy(resource)).pipe(
return this.store.select(ConfigState.getGrantedPolicy(requiredPolicy)).pipe(
tap(access => {
if (!access) {
this.store.dispatch(new RestOccurError({ status: 403 }));
Expand Down
4 changes: 2 additions & 2 deletions npm/ng-packs/packages/core/src/lib/utils/route-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ export function findRoute(routes: RoutesService, path: string): TreeNode<ABP.Rou
);
}

export function getRoutePath(router: Router) {
export function getRoutePath(router: Router, url = router.url) {
const emptyGroup = { segments: [] } as UrlSegmentGroup;
const primaryGroup = router.parseUrl(router.url).root.children[PRIMARY_OUTLET];
const primaryGroup = router.parseUrl(url).root.children[PRIMARY_OUTLET];

return '/' + (primaryGroup || emptyGroup).segments.map(({ path }) => path).join('/');
}