mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[ADF-4994] Move realmRole functions inside JwtHelperService. (#5254)
* Moved Realm and client role function inside jwtHelperService. * Updated unit tests.
This commit is contained in:
committed by
Eugenio Romano
parent
81dcfa4341
commit
ac4679fc10
@@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user