[ADF-4003] Add roles filtering to PeopleCloudComponent (#4261)

* Provided a way to filter users bya  specifi role.
This commit is contained in:
siva kumar
2019-02-05 15:51:32 +05:30
committed by Maurizio Vitale
parent da379eefd7
commit f373c28965
4 changed files with 99 additions and 3 deletions

View File

@@ -247,4 +247,26 @@ describe('IdentityUserService', () => {
} }
); );
}); });
it('should return true if user has given role', (done) => {
spyOn(service, 'getUserRoles').and.returnValue(of(mockRoles));
service.checkUserHasRole('mock-user-id', ['MOCK-ROLE-1']).subscribe(
(res: boolean) => {
expect(res).toBeDefined();
expect(res).toBeTruthy();
done();
}
);
});
it('should return false if user does not have given role', (done) => {
spyOn(service, 'getUserRoles').and.returnValue(of(mockRoles));
service.checkUserHasRole('mock-user-id', ['MOCK-ROLE-10']).subscribe(
(res: boolean) => {
expect(res).toBeDefined();
expect(res).toBeFalsy();
done();
}
);
});
}); });

View File

@@ -231,12 +231,12 @@ export class IdentityUserService {
getUserRoles(userId: string): Observable<IdentityRoleModel[]> { getUserRoles(userId: string): Observable<IdentityRoleModel[]> {
const url = this.buildRolesUrl(userId); const url = this.buildRolesUrl(userId);
const httpMethod = 'GET', pathParams = {}, queryParams = {}, bodyParam = {}, headerParams = {}, const httpMethod = 'GET', pathParams = {}, queryParams = {}, bodyParam = {}, headerParams = {},
formParams = {}, authNames = [], contentTypes = ['application/json'], accepts = ['application/json']; formParams = {}, contentTypes = ['application/json'], accepts = ['application/json'];
return from(this.apiService.getInstance().oauth2Auth.callCustomApi( return from(this.apiService.getInstance().oauth2Auth.callCustomApi(
url, httpMethod, pathParams, queryParams, url, httpMethod, pathParams, queryParams,
headerParams, formParams, bodyParam, authNames, headerParams, formParams, bodyParam,
contentTypes, accepts, null, null) contentTypes, accepts, Object, null, null)
).pipe( ).pipe(
map((response: IdentityRoleModel[]) => { map((response: IdentityRoleModel[]) => {
return response; return response;
@@ -302,6 +302,24 @@ export class IdentityUserService {
return hasAnyRole; return hasAnyRole;
} }
checkUserHasRole(userId: string, roleNames: string[]): Observable<boolean> {
return this.getUserRoles(userId).pipe(map((userRoles: IdentityRoleModel[]) => {
let hasRole = false;
if (userRoles && userRoles.length > 0) {
roleNames.forEach((roleName: string) => {
const role = userRoles.find((userRole) => {
return roleName === userRole.name;
});
if (role) {
hasRole = true;
return;
}
});
}
return hasRole;
}));
}
private buildUserUrl(): any { private buildUserUrl(): any {
return `${this.appConfigService.get('identityHost')}/users`; return `${this.appConfigService.get('identityHost')}/users`;
} }

View File

@@ -248,4 +248,50 @@ describe('PeopleCloudComponent', () => {
}); });
})); }));
it('should return true if user has any specified role', async(() => {
const checkUserHasRoleSpy = spyOn(identityService, 'checkUserHasRole').and.returnValue(of(true));
component.roles = ['mock-role-1'];
fixture.detectChanges();
let inputHTMLElement: HTMLInputElement = <HTMLInputElement> element.querySelector('input');
inputHTMLElement.focus();
inputHTMLElement.value = 'M';
inputHTMLElement.dispatchEvent(new Event('input'));
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(checkUserHasRoleSpy).toHaveBeenCalled();
});
}));
it('should return false if user does not have any specified role', async(() => {
const checkUserHasRoleSpy = spyOn(identityService, 'checkUserHasRole').and.returnValue(of(false));
component.appName = '';
component.roles = ['mock-role-10'];
fixture.detectChanges();
let inputHTMLElement: HTMLInputElement = <HTMLInputElement> element.querySelector('input');
inputHTMLElement.focus();
inputHTMLElement.value = 'M';
inputHTMLElement.dispatchEvent(new Event('input'));
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(checkUserHasRoleSpy).toHaveBeenCalled();
});
}));
it('should not fire checkUserHasRole when roles are not specified', async(() => {
const checkUserHasRoleSpy = spyOn(identityService, 'checkUserHasRole').and.returnValue(of(false));
component.appName = '';
component.roles = [];
fixture.detectChanges();
let inputHTMLElement: HTMLInputElement = <HTMLInputElement> element.querySelector('input');
inputHTMLElement.focus();
inputHTMLElement.value = 'M';
inputHTMLElement.dispatchEvent(new Event('input'));
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(checkUserHasRoleSpy).not.toHaveBeenCalled();
});
}));
}); });

View File

@@ -148,6 +148,8 @@ export class PeopleCloudComponent implements OnInit {
return hasRole ? of(user) : of(); return hasRole ? of(user) : of();
}) })
); );
} else if (this.hasRoles()) {
return this.filterUsersByRoles(user);
} else { } else {
return of(user); return of(user);
} }
@@ -170,6 +172,14 @@ export class PeopleCloudComponent implements OnInit {
return this.roles && this.roles.length > 0; return this.roles && this.roles.length > 0;
} }
filterUsersByRoles(user: IdentityUserModel): Observable<IdentityUserModel> {
return this.identityUserService.checkUserHasRole(user.id, this.roles).pipe(
mergeMap((hasRole) => {
return hasRole ? of(user) : of();
})
);
}
private isUserAlreadySelected(user: IdentityUserModel): boolean { private isUserAlreadySelected(user: IdentityUserModel): boolean {
if (this.preSelectUsers && this.preSelectUsers.length > 0) { if (this.preSelectUsers && this.preSelectUsers.length > 0) {
const result = this.preSelectUsers.find((selectedUser) => { const result = this.preSelectUsers.find((selectedUser) => {