|
|
|
@ -4,10 +4,11 @@ import { |
|
|
|
CanActivateFn, |
|
|
|
Router, |
|
|
|
RouterStateSnapshot, |
|
|
|
UrlTree, |
|
|
|
} from '@angular/router'; |
|
|
|
import { HttpErrorResponse } from '@angular/common/http'; |
|
|
|
import { Observable, of } from 'rxjs'; |
|
|
|
import { filter, take, tap } from 'rxjs/operators'; |
|
|
|
import { map, take } from 'rxjs/operators'; |
|
|
|
import { AuthService, IAbpGuard } from '../abstracts'; |
|
|
|
import { findRoute, getRoutePath } from '../utils/route-utils'; |
|
|
|
import { RoutesService, PermissionService, HttpErrorReporterService } from '../services'; |
|
|
|
@ -25,7 +26,7 @@ export class PermissionGuard implements IAbpGuard { |
|
|
|
protected readonly permissionService = inject(PermissionService); |
|
|
|
protected readonly httpErrorReporter = inject(HttpErrorReporterService); |
|
|
|
|
|
|
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> { |
|
|
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> { |
|
|
|
let { requiredPolicy } = route.data || {}; |
|
|
|
|
|
|
|
if (!requiredPolicy) { |
|
|
|
@ -38,12 +39,19 @@ export class PermissionGuard implements IAbpGuard { |
|
|
|
} |
|
|
|
|
|
|
|
return this.permissionService.getGrantedPolicy$(requiredPolicy).pipe( |
|
|
|
filter(Boolean), |
|
|
|
take(1), |
|
|
|
tap(access => { |
|
|
|
if (!access && this.authService.isAuthenticated) { |
|
|
|
map(access => { |
|
|
|
if (access) return true; |
|
|
|
|
|
|
|
if (route.data?.['redirectUrl']) { |
|
|
|
return this.router.parseUrl(route.data['redirectUrl']); |
|
|
|
} |
|
|
|
|
|
|
|
if (this.authService.isAuthenticated) { |
|
|
|
this.httpErrorReporter.reportError({ status: 403 } as HttpErrorResponse); |
|
|
|
} |
|
|
|
|
|
|
|
return false; |
|
|
|
}), |
|
|
|
); |
|
|
|
} |
|
|
|
@ -77,12 +85,19 @@ export const permissionGuard: CanActivateFn = ( |
|
|
|
} |
|
|
|
|
|
|
|
return permissionService.getGrantedPolicy$(requiredPolicy).pipe( |
|
|
|
filter(Boolean), |
|
|
|
take(1), |
|
|
|
tap(access => { |
|
|
|
if (!access && authService.isAuthenticated) { |
|
|
|
map(access => { |
|
|
|
if (access) return true; |
|
|
|
|
|
|
|
if (route.data?.['redirectUrl']) { |
|
|
|
return router.parseUrl(route.data['redirectUrl']); |
|
|
|
} |
|
|
|
|
|
|
|
if (authService.isAuthenticated) { |
|
|
|
httpErrorReporter.reportError({ status: 403 } as HttpErrorResponse); |
|
|
|
} |
|
|
|
|
|
|
|
return false; |
|
|
|
}), |
|
|
|
); |
|
|
|
}; |
|
|
|
|