mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[ADF-4003] Add roles filtering to PeopleCloudComponent (#4261)
* Provided a way to filter users bya specifi role.
This commit is contained in:
committed by
Maurizio Vitale
parent
da379eefd7
commit
f373c28965
@@ -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();
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
@@ -231,12 +231,12 @@ export class IdentityUserService {
|
||||
getUserRoles(userId: string): Observable<IdentityRoleModel[]> {
|
||||
const url = this.buildRolesUrl(userId);
|
||||
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(
|
||||
url, httpMethod, pathParams, queryParams,
|
||||
headerParams, formParams, bodyParam, authNames,
|
||||
contentTypes, accepts, null, null)
|
||||
headerParams, formParams, bodyParam,
|
||||
contentTypes, accepts, Object, null, null)
|
||||
).pipe(
|
||||
map((response: IdentityRoleModel[]) => {
|
||||
return response;
|
||||
@@ -302,6 +302,24 @@ export class IdentityUserService {
|
||||
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 {
|
||||
return `${this.appConfigService.get('identityHost')}/users`;
|
||||
}
|
||||
|
Reference in New Issue
Block a user