import { Injectable } from '@angular/core'; import { Router, ActivatedRouteSnapshot, RouterStateSnapshot, CanActivateChild, } from '@angular/router'; import { MwUserAccessService } from 'projects/core/src//auth'; import { Observable, of } from 'rxjs'; import { map } from 'rxjs/operators'; @Injectable() export class UserActionGuard implements CanActivateChild { constructor( private readonly router: Router, private readonly userAccessService: MwUserAccessService ) {} canActivateChild( route: ActivatedRouteSnapshot, state: RouterStateSnapshot ): Observable { const actions: string[] = route.data['action'] || []; if (actions.length > 0) { this.userAccessService.hasAccess(actions).pipe( map((hasAccess) => { if (!hasAccess) { this.router.navigate(['/']); return false; } return true; }) ); } return of(true); } }