Aw4751 Update interface
All checks were successful
FarmMaps.Develop/FarmMapsLib/pipeline/head This commit looks good
All checks were successful
FarmMaps.Develop/FarmMapsLib/pipeline/head This commit looks good
This commit is contained in:
parent
6555e68145
commit
7c8c16362c
@ -2,16 +2,18 @@ import { Injectable } from '@angular/core';
|
|||||||
import {
|
import {
|
||||||
CanActivate, Router, CanLoad, Route, CanActivateChild ,
|
CanActivate, Router, CanLoad, Route, CanActivateChild ,
|
||||||
ActivatedRouteSnapshot,
|
ActivatedRouteSnapshot,
|
||||||
RouterStateSnapshot
|
RouterStateSnapshot,
|
||||||
|
UrlSegment,
|
||||||
|
UrlTree
|
||||||
} from '@angular/router';
|
} from '@angular/router';
|
||||||
|
|
||||||
|
|
||||||
import { Store } from '@ngrx/store';
|
import { Store } from '@ngrx/store';
|
||||||
import { OAuthService } from 'angular-oauth2-oidc';
|
import { OAuthService } from 'angular-oauth2-oidc';
|
||||||
|
import { Observable } from 'rxjs';
|
||||||
|
|
||||||
|
|
||||||
import * as appCommonReducer from '../reducers/app-common.reducer'
|
import * as appCommonReducer from '../reducers/app-common.reducer';
|
||||||
import * as appCommonActions from '../actions/app-common.actions';
|
|
||||||
|
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
@ -21,48 +23,46 @@ export class AuthGuard implements CanActivate, CanLoad, CanActivateChild {
|
|||||||
|
|
||||||
constructor(private oauthService: OAuthService, private router: Router, private store: Store<appCommonReducer.State>) { }
|
constructor(private oauthService: OAuthService, private router: Router, private store: Store<appCommonReducer.State>) { }
|
||||||
|
|
||||||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean | UrlTree | Observable<boolean | UrlTree> | Promise<boolean | UrlTree> {
|
||||||
console.debug("AuthGuard->canActivate", route, state);
|
console.debug("AuthGuard->canActivate", route, state);
|
||||||
const url: string = state.url;
|
const url: string = state.url;
|
||||||
|
|
||||||
return this.checkLogin(url, route);
|
return this.checkLogin(url, route);
|
||||||
}
|
}
|
||||||
|
|
||||||
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
|
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean | UrlTree | Observable<boolean | UrlTree> | Promise<boolean | UrlTree> {
|
||||||
console.debug("AuthGuard->canActivateChild", childRoute, state);
|
console.debug("AuthGuard->canActivateChild", childRoute, state);
|
||||||
const url: string = state.url;
|
const url: string = state.url;
|
||||||
|
|
||||||
return this.checkLogin(url, childRoute);
|
return this.checkLogin(url, childRoute);
|
||||||
}
|
}
|
||||||
|
|
||||||
canLoad(route: Route): Promise<boolean> {
|
canLoad(route: Route, segments: UrlSegment[]): boolean | UrlTree | Observable<boolean | UrlTree> | Promise<boolean | UrlTree> {
|
||||||
console.debug("AuthGuard->canLoad", route);
|
console.debug("AuthGuard->canLoad", route, segments);
|
||||||
return this.checkLogin(route.path, null);
|
return this.checkLogin(route.path, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
checkLogin(url: string, route: ActivatedRouteSnapshot): Promise<boolean> {
|
checkLogin(url: string, route: ActivatedRouteSnapshot): boolean {
|
||||||
console.debug("AuthGuard->checkLogin", url, route);
|
console.debug("AuthGuard->checkLogin", url, route);
|
||||||
return new Promise<boolean>((resolve) => {
|
|
||||||
if (!this.oauthService.hasValidAccessToken()) {
|
if (!this.oauthService.hasValidAccessToken()) {
|
||||||
console.debug("No valid token");
|
console.debug("No valid token");
|
||||||
this.oauthService.initCodeFlow(url);
|
this.oauthService.initCodeFlow(url);
|
||||||
resolve(false);
|
return false;
|
||||||
} else {
|
} else {
|
||||||
const requiredRoleClaim = route.data.role;
|
const requiredRoleClaim = route.data.role;
|
||||||
if (!requiredRoleClaim) { resolve(true); }
|
if (!requiredRoleClaim) { return true; }
|
||||||
const ownedClaims = this.oauthService.getIdentityClaims();
|
const ownedClaims = this.oauthService.getIdentityClaims();
|
||||||
if (!ownedClaims) { console.debug("No owned claims"); resolve(false); }
|
if (!ownedClaims) { console.debug("No owned claims"); return false; }
|
||||||
const ownedRoleClaims: string[] = ownedClaims['role'];
|
const ownedRoleClaims: string[] = ownedClaims['role'];
|
||||||
if (!ownedRoleClaims) { console.debug("No owned role claims"); resolve(false); }
|
if (!ownedRoleClaims) { console.debug("No owned role claims"); return false; }
|
||||||
if (Array.isArray(ownedRoleClaims)) {
|
if (Array.isArray(ownedRoleClaims)) {
|
||||||
if (ownedRoleClaims.findIndex(r => r === requiredRoleClaim) <= -1) { console.debug("No required role claim", ownedRoleClaims, requiredRoleClaim); resolve(false); }
|
if (ownedRoleClaims.findIndex(r => r === requiredRoleClaim) <= -1) { console.debug("No required role claim", ownedRoleClaims, requiredRoleClaim); return false; }
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (ownedRoleClaims !== requiredRoleClaim) { console.debug("No required role claim", ownedRoleClaims, requiredRoleClaim); resolve(false); }
|
if (ownedRoleClaims !== requiredRoleClaim) { console.debug("No required role claim", ownedRoleClaims, requiredRoleClaim); return false; }
|
||||||
}
|
}
|
||||||
console.debug("Has required role claim", requiredRoleClaim);
|
console.debug("Has required role claim", requiredRoleClaim);
|
||||||
resolve(true);
|
return true;
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user