[AAE-9365] - Auth guards should return true when no roles to check are passed (#7695)

This commit is contained in:
Ardit Domi 2022-06-30 09:38:48 +01:00 committed by GitHub
parent e27833d770
commit ad9a468b11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 3 deletions

View File

@ -51,6 +51,22 @@ describe('UserAccessService', () => {
});
}
it('should return true when no roles to check are passed in global access', async () => {
spyUserAccess(['MOCK_USER_ROLE'], {});
await userAccessService.fetchUserAccess();
const hasGlobalAccess = userAccessService.hasGlobalAccess([]);
expect(hasGlobalAccess).toBe(true);
});
it('should return true when no roles to check are passed in application access', async () => {
spyUserAccess([], { mockApp: { roles: ['MOCK_APP_ROLE'] } });
await userAccessService.fetchUserAccess();
const hasApplicationAccess = userAccessService.hasApplicationAccess('mockApp', []);
expect(hasApplicationAccess).toBe(true);
});
describe('Access from JWT token', () => {
it('should return true when the user has one of the global roles', async () => {

View File

@ -84,7 +84,10 @@ export class UserAccessService {
* @returns True if it contains at least one of the given roles, false otherwise
*/
hasGlobalAccess(rolesToCheck: string[]): boolean {
return this.globalAccess ? this.globalAccess.some((role: string) => rolesToCheck.includes(role)) : false;
if (rolesToCheck?.length > 0) {
return this.globalAccess ? this.globalAccess.some((role: string) => rolesToCheck.includes(role)) : false;
}
return true;
}
/**
@ -95,8 +98,11 @@ export class UserAccessService {
* @returns True if it contains at least one of the given roles, false otherwise
*/
hasApplicationAccess(appName: string, rolesToCheck: string[]): boolean {
const appAccess = this.hasRolesInJwt() ? this.applicationAccess[appName] : this.applicationAccess.find((app: ApplicationAccessModel) => app.name === appName);
return appAccess ? appAccess.roles.some(appRole => rolesToCheck.includes(appRole)) : false;
if (rolesToCheck?.length > 0) {
const appAccess = this.hasRolesInJwt() ? this.applicationAccess[appName] : this.applicationAccess.find((app: ApplicationAccessModel) => app.name === appName);
return appAccess ? appAccess.roles.some(appRole => rolesToCheck.includes(appRole)) : false;
}
return true;
}
/**