Summary Table

Categories Total Count
PII 0
URL 0
DNS 0
EKL 0
IP 0
PORT 0
VsID 0
CF 0
AI 0
VPD 0
PL 0
Other 0

File Content

import { Injectable } from '@angular/core';
import {
CanActivate,
ActivatedRouteSnapshot,
RouterStateSnapshot,
Router,
CanLoad,
Route
} from '@angular/router';
import { AuthenticationService } from '../../auth/auth.service';

@Injectable()
export class HasRolesGuard implements CanActivate, CanLoad {
constructor(
private router: Router,
private authenticationService: AuthenticationService
) {}
hasArsAdminRole = false;
hasEwvRole = false;
hasEwvAdminRole = false;
hasArsBaseRole = false;
hasArsSubmissionRole = false;
// hasBaseUserRole = false;
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): boolean {
this.getUserRoles();
if (state.url === '/search275') {
if (this.hasArsBaseRole) {
return true;
} else {
this.router.navigate(['/']);
return false;
}
}

if (state.url === '/rfaiSubmission' || state.url === '/search277') {
if (this.hasArsSubmissionRole) {
return true;
} else {
this.router.navigate(['/']);
return false;
}
}

if (
state.url === '/userAdmin' ||
state.url === '/arsReports' ||
state.url === '/rfaiAdmin'
) {
if (this.hasArsAdminRole) {
return true;
} else {
this.router.navigate(['/']);
return false;
}
}

if (state.url === '/ewvSearch' || state.url === '/ewv') {
if (this.hasEwvRole) {
return true;
} else {
this.router.navigate(['/']);
return false;
}
}
// change back after ewv admin role is implemented in the backend
// if (state.url === '/ewvAdmin' && this.hasEwvAdminRole) {
if (state.url === '/ewvAdmin' && this.hasEwvAdminRole) {
return true;
} else {
this.router.navigate(['/']);
return false;
}
}
canLoad(route: Route): boolean {
this.getUserRoles();
if (
route.path === 'arsReports' ||
route.path === 'rfaiAdmin' ||
(route.path === 'userAdmin' && this.hasArsAdminRole)
) {
return true;
} else {
this.router.navigate(['/']);
return false;
}
}
// utility method that sets respective variables true or false based on user roles
getUserRoles() {
const userInfo = this.authenticationService.getDecodedUserInfo();
if (userInfo) {
const userRoles = userInfo['userRoles'];
this.hasArsSubmissionRole = userRoles.some(
role => role === 'ARS_BASE_SUBMISSION_USER'
);
this.hasArsBaseRole = userRoles.some(
role => role === 'ARS_BASE_ATTACHMENT_USER'
);
this.hasArsAdminRole = userRoles.some(data => data === 'ARS_ADMIN');
this.hasEwvRole = userRoles.some(data => data === 'EWV_BASE_USER');
this.hasEwvAdminRole = userRoles.some(data => data === 'EWV_ADMIN');
}
}
}