[AAE-8061] Add groups restriction to people widget (#7586)

* [AAE-8061] add groups restriction to people widget

* Trigger travis

* [AAE-8061] fix lint

* [AAE-8061] fix unit tests
This commit is contained in:
Tomasz Gnyp
2022-04-20 15:51:32 +02:00
committed by GitHub
parent 6963bef092
commit 694d71a103
6 changed files with 159 additions and 20 deletions

View File

@@ -33,6 +33,7 @@ import { Observable, of, BehaviorSubject, Subject } from 'rxjs';
import { switchMap, debounceTime, distinctUntilChanged, mergeMap, tap, filter, map, takeUntil } from 'rxjs/operators';
import {
FullNamePipe,
IdentityGroupModel,
IdentityUserModel,
IdentityUserService,
LogService
@@ -103,6 +104,12 @@ export class PeopleCloudComponent implements OnInit, OnChanges, OnDestroy {
@Input()
excludedUsers: IdentityUserModel[] = [];
/** Array of groups to restrict user searches.
* Mandatory property is group id
*/
@Input()
groupsRestriction: string[] = [];
/** FormControl to list of users */
@Input()
userChipsCtrl: FormControl = new FormControl({ value: '', disabled: false });
@@ -216,13 +223,15 @@ export class PeopleCloudComponent implements OnInit, OnChanges, OnDestroy {
this.resetSearchUsers();
}),
switchMap((search) =>
this.identityUserService.findUsersByName(search.trim())),
this.findUsers(search)
),
mergeMap((users) => {
this.resetSearchUsers();
this.searchLoading = false;
return users;
}),
filter(user => !this.isUserAlreadySelected(user) && !this.isExcludedUser(user)),
mergeMap(user => this.filterUsersByGroupsRestriction(user)),
mergeMap(user => {
if (this.appName) {
return this.checkUserHasAccess(user.id).pipe(
@@ -275,6 +284,19 @@ export class PeopleCloudComponent implements OnInit, OnChanges, OnDestroy {
map((filteredUser: { hasRole: boolean; user: IdentityUserModel }) => filteredUser.user));
}
private filterUsersByGroupsRestriction(user: IdentityUserModel): Observable<IdentityUserModel> {
if (this.groupsRestriction?.length) {
return this.isUserPartOfAllRestrictedGroups(user).pipe(
mergeMap(isPartOfAllGroups => isPartOfAllGroups ? of(user) : of())
);
}
return of(user);
}
private findUsers(search: string): Observable<IdentityUserModel[]> {
return this.identityUserService.findUsersByName(search.trim());
}
private isUserAlreadySelected(searchUser: IdentityUserModel): boolean {
if (this.selectedUsers && this.selectedUsers.length > 0) {
const result = this.selectedUsers.find((selectedUser) => this.compare(selectedUser, searchUser));
@@ -325,7 +347,17 @@ export class PeopleCloudComponent implements OnInit, OnChanges, OnDestroy {
if (this.compare(user, validationResult)) {
validationResult.readonly = user.readonly;
validUsers.push(validationResult);
if (this.groupsRestriction?.length) {
const isUserPartOfAllRestrictedGroups = await this.isUserPartOfAllRestrictedGroups(validationResult).toPromise();
if (isUserPartOfAllRestrictedGroups) {
validUsers.push(user);
} else {
this.invalidUsers.push(user);
}
} else {
validUsers.push(validationResult);
}
} else {
this.invalidUsers.push(user);
}
@@ -529,6 +561,20 @@ export class PeopleCloudComponent implements OnInit, OnChanges, OnDestroy {
this.searchUsers$.next(this._searchUsers);
}
private isUserPartOfAllRestrictedGroups(user: IdentityUserModel): Observable<boolean> {
return this.getUserGroups(user.id).pipe(
map(userGroups => userGroups.filter(
restrictedGroup => userGroups.includes(restrictedGroup)
).length >= this.groupsRestriction.length)
);
}
private getUserGroups(userId: string): Observable<IdentityGroupModel[]> {
return this.identityUserService.getInvolvedGroups(userId).pipe(
map(groups => groups.map(({id, name}) => ({id, name})))
);
}
getSelectedUsers(): IdentityUserModel[] {
return this.selectedUsers;
}