[ADF-4994] Move realmRole functions inside JwtHelperService. (#5254)

* Moved Realm and client role function inside jwtHelperService.
* Updated unit tests.
This commit is contained in:
siva kumar
2019-11-15 20:04:22 +05:30
committed by Eugenio Romano
parent 81dcfa4341
commit ac4679fc10
4 changed files with 179 additions and 99 deletions

View File

@@ -32,13 +32,13 @@ export class AuthGuardSsoRoleService implements CanActivate {
if (route.data) {
if (route.data['roles']) {
const rolesToCheck = route.data['roles'];
hasRealmRole = this.hasRealmRoles(rolesToCheck);
hasRealmRole = this.jwtHelperService.hasRealmRoles(rolesToCheck);
}
if (route.data['clientRoles']) {
const clientRoleName = route.params[route.data['clientRoles']];
const rolesToCheck = route.data['roles'];
hasClientRole = this.hasRealmRolesForClientRole(clientRoleName, rolesToCheck);
hasClientRole = this.jwtHelperService.hasRealmRolesForClientRole(clientRoleName, rolesToCheck);
}
}
@@ -53,48 +53,4 @@ export class AuthGuardSsoRoleService implements CanActivate {
constructor(private jwtHelperService: JwtHelperService, private router: Router) {
}
getRealmRoles(): string[] {
const access = this.jwtHelperService.getValueFromLocalAccessToken<any>('realm_access');
return access ? access['roles'] : [];
}
getClientRoles(client: string): string[] {
const clientRole = this.jwtHelperService.getValueFromLocalAccessToken<any>('resource_access')[client];
return clientRole ? clientRole['roles'] : [];
}
hasRealmRole(role: string): boolean {
let hasRole = false;
if (this.jwtHelperService.getAccessToken()) {
const realmRoles = this.getRealmRoles();
hasRole = realmRoles.some((currentRole) => {
return currentRole === role;
});
}
return hasRole;
}
hasRealmRoles(rolesToCheck: string []): boolean {
return rolesToCheck.some((currentRole) => {
return this.hasRealmRole(currentRole);
});
}
hasRealmRolesForClientRole(clientRole: string, rolesToCheck: string []): boolean {
return rolesToCheck.some((currentRole) => {
return this.hasClientRole(clientRole, currentRole);
});
}
hasClientRole(clientRole, role: string): boolean {
let hasRole = false;
if (this.jwtHelperService.getAccessToken()) {
const clientRoles = this.getClientRoles(clientRole);
hasRole = clientRoles.some((currentRole) => {
return currentRole === role;
});
}
return hasRole;
}
}