Merge pull request #4663 from abpframework/refactor/4662

Got the required policy in PermissionGuard from RoutesService
pull/4672/head
Mehmet Erim 5 years ago committed by GitHub
commit 5fff83e6c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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';
@ -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,
) {
@ -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 {

@ -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 }));

@ -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('/');
}

Loading…
Cancel
Save