Skip to content

Commit

Permalink
refactor(core): get the required policy in PermissionGuard from Route…
Browse files Browse the repository at this point in the history
…sService

resolves #4662
  • Loading branch information
mehmet-erim committed Jul 7, 2020
1 parent 56a23d4 commit 854f3e5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
33 changes: 16 additions & 17 deletions npm/ng-packs/packages/core/src/lib/guards/permission.guard.ts
@@ -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
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?: string) {
const emptyGroup = { segments: [] } as UrlSegmentGroup;
const primaryGroup = router.parseUrl(router.url).root.children[PRIMARY_OUTLET];
const primaryGroup = router.parseUrl(url || router.url).root.children[PRIMARY_OUTLET];

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

0 comments on commit 854f3e5

Please sign in to comment.