diff --git a/src/app/shared/users/list/userslist.component.ts b/src/app/shared/users/list/userslist.component.ts index 6115bb3588203fe47b6ff244521cfd992aa9e4a4..7394575d57a4f6d527893ff57c82ad1d4d71b007 100644 --- a/src/app/shared/users/list/userslist.component.ts +++ b/src/app/shared/users/list/userslist.component.ts @@ -8,321 +8,324 @@ import {Observable, of} from 'rxjs'; import {Role, UserRole} from '../../../model/userrole'; import {UserDataService} from '../../../service/userdata.service'; import {AuthService} from '../../../auth/auth.service'; -import {map, shareReplay, take} from 'rxjs/operators'; import {UntypedFormControl} from '@angular/forms'; import {ComponentMode} from '../../common/componentmode'; import {Router} from '@angular/router'; function userMatches(u: User, term: string): boolean { - const t = term || '' - return (u.username || '').toLowerCase().includes(t.toLowerCase()) || - (u.email || '').toLowerCase().includes(t.toLowerCase()) || - (u.firstname || '').toLowerCase().includes(t.toLowerCase()) || - (u.lastname || '').toLowerCase().includes(t.toLowerCase()) + const t = term || '' + return (u.username || '').toLowerCase().includes(t.toLowerCase()) || + (u.email || '').toLowerCase().includes(t.toLowerCase()) || + (u.firstname || '').toLowerCase().includes(t.toLowerCase()) || + (u.lastname || '').toLowerCase().includes(t.toLowerCase()) } @Component({ - selector: 'nmaas-userslist', - templateUrl: './userslist.component.html', - styleUrls: ['./userslist.component.css'] + selector: 'nmaas-userslist', + templateUrl: './userslist.component.html', + styleUrls: ['./userslist.component.css'] }) export class UsersListComponent extends BaseComponent implements OnInit, OnChanges { - public users_item_number_key = 'NUMBER_OF_USERS_ITEM_KEY'; + public users_item_number_key = 'NUMBER_OF_USERS_ITEM_KEY'; - @Input() - public users: User[] = []; // provided list of users + @Input() + public users: User[] = []; // provided list of users - @Input() - public domainMode = false; + @Input() + public domainMode = false; - public displayUsers: User[] = []; // list of users after transformations + public displayUsers: User[] = []; // list of users after transformations - public domainId: number; + public domainId: number; - @Output() - public onDelete: EventEmitter<User> = new EventEmitter<User>(); + @Output() + public onDelete: EventEmitter<User> = new EventEmitter<User>(); - @Output() - public onView: EventEmitter<number> = new EventEmitter<number>(); + @Output() + public onView: EventEmitter<number> = new EventEmitter<number>(); - @Output() - public onAddToDomain: EventEmitter<User> = new EventEmitter<User>(); + @Output() + public onAddToDomain: EventEmitter<User> = new EventEmitter<User>(); - @Output() - public onRemoveFromDomain: EventEmitter<User> = new EventEmitter<User>(); + @Output() + public onRemoveFromDomain: EventEmitter<User> = new EventEmitter<User>(); - @Output() - public onModeChange: EventEmitter<number> = new EventEmitter<number>(); + @Output() + public onModeChange: EventEmitter<number> = new EventEmitter<number>(); - @Output() - public onUserRoleChange: EventEmitter<any> = new EventEmitter<any>(); + @Output() + public onUserRoleChange: EventEmitter<any> = new EventEmitter<any>(); - public domainCache: CacheService<number, Domain> = new CacheService<number, Domain>(); + public domainCache: CacheService<number, Domain> = new CacheService<number, Domain>(); - private lastSearchCriteria: CustomerSearchCriteria = undefined; + private lastSearchCriteria: CustomerSearchCriteria = undefined; - public pageNumber = 1; - public paginatorName = 'paginator-identifier'; - public itemsPerPage: number[] = [15, 20, 25, 30, 50]; - public maxItemsOnPage = 15; + public pageNumber = 1; + public paginatorName = 'paginator-identifier'; + public itemsPerPage: number[] = [15, 20, 25, 30, 50]; + public maxItemsOnPage = 15; - public filteredUsers: User[] = []; + public filteredUsers: User[] = []; - public searchText = new UntypedFormControl(''); - public Role = Role; + public searchText = new UntypedFormControl(''); + public Role = Role; - constructor(private userService: UserService, - public domainService: DomainService, - private userDataService: UserDataService, - public authService: AuthService, - private router: Router) { - super(); - userDataService.selectedDomainId.subscribe(domain => this.domainId = domain); - } + constructor(private userService: UserService, + public domainService: DomainService, + private userDataService: UserDataService, + public authService: AuthService, + private router: Router) { + super(); + userDataService.selectedDomainId.subscribe(domain => this.domainId = domain); + } + + ngOnInit() { + // set stored value of maxElementsPerPage + const i = sessionStorage.getItem(this.users_item_number_key); + if (i) { + this.maxItemsOnPage = +i; + } + + this.searchText.valueChanges.subscribe( + term => this.onSearch(term) + ) + + this.userDataService.selectedDomainId.subscribe(domain => this.domainId = domain); + this.getAllDomain(); + + } - ngOnInit() { - // set stored value of maxElementsPerPage - const i = sessionStorage.getItem(this.users_item_number_key); - if (i) { - this.maxItemsOnPage = +i; + ngOnChanges(changes: SimpleChanges): void { + this.displayUsers = this.users; } - this.searchText.valueChanges.subscribe( - term => this.onSearch(term) - ) - - this.userDataService.selectedDomainId.subscribe(domain => this.domainId = domain); - } - - ngOnChanges(changes: SimpleChanges): void { - this.displayUsers = this.users; - } - - public getDomainName(domainId: number): Observable<string> { - if (this.domainCache.hasData(domainId)) { - return of(this.domainCache.getData(domainId).name); - } else { - return this.domainService.getOne(domainId).pipe( - map((domain) => { - this.domainCache.setData(domainId, domain); - return domain.name - }), - shareReplay(1), - take(1)); + public getAllDomain() { + this.domainService.getAll().subscribe(domains => { + domains.forEach(domain => { + this.domainCache.setData(domain.id, domain) + }) + }) } - } - - public filterDomainNames(user: User): UserRole[] { - return user.roles.filter(role => role.domainId !== this.domainService.getGlobalDomainId()); - } - - public getOnlyDomainRoles(user: User): UserRole { - return user.roles.find(role => role.domainId === this.domainId); - } - - public getGlobalRole(user: User): string { - const userRole: UserRole[] = user.roles.filter(role => role.domainId === this.domainService.getGlobalDomainId()); - return userRole[0].role.toString(); - } - - public getUserDomainIds(user: User): number[] { - if (user !== undefined) { - return user.getDomainIds(); - } else { - return []; + + public getDomainName(domainId: number): Observable<string> { + if (this.domainCache.hasData(domainId)) { + return of(this.domainCache.getData(domainId).name); + } else { + return new Observable<string>() + } } - } - public remove(user: User) { - this.onDelete.emit(user); - } + public filterDomainNames(user: User): UserRole[] { + return user.roles.filter(role => role.domainId !== this.domainService.getGlobalDomainId()); + } - public view(userId: number): void { - if (!this.domainMode) { - this.onView.emit(userId); + public getOnlyDomainRoles(user: User): UserRole { + return user.roles.find(role => role.domainId === this.domainId); } - } - - public changeUserStatus(user: User, enabled: boolean): void { - this.userService.changeUserStatus(user.id, enabled).subscribe(); - user.enabled = enabled; - // sort after changing params - if (this.lastSearchCriteria) { - this.handleSortEvent(this.lastSearchCriteria); + + public getGlobalRole(user: User): string { + const userRole: UserRole[] = user.roles.filter(role => role.domainId === this.domainService.getGlobalDomainId()); + return userRole[0].role.toString(); } - } - - onSorted($event) { - console.log('onSort', $event); - this.displayUsers = this.users; - this.handleSearchEvent(this.searchText.value) - this.handleSortEvent($event); - } - - onSearch(term) { - console.log('onSearch', term) - this.displayUsers = this.users; - this.handleSearchEvent(term) - this.handleSortEvent(this.lastSearchCriteria); - } - - handleSearchEvent(term: string) { - console.warn(this.displayUsers.filter( - u => userMatches(u, term) - )) - this.displayUsers = this.displayUsers.filter( - u => userMatches(u, term) - ) - } - - handleSortEvent(criteria: CustomerSearchCriteria) { - this.lastSearchCriteria = criteria; - const baseSortFunc = (a: any, b: any): number => { - if (a < b) { - return -1; - } - if (a > b) { - return 1; - } - return 0; - }; - - this.displayUsers.sort( - (a: User, b: User) => { - if (!criteria) { - return 0; - } - const direction = criteria.sortDirection === 'asc' ? 1 : -1; - let result: number; - - let p1: any, p2: any; - - // sorting rules for custom columns - if (criteria.sortColumn === 'domains') { - const ad = this.filterDomainNames(a); - const bd = this.filterDomainNames(b); - if (!ad) { - console.log(ad); + + public getUserDomainIds(user: User): number[] { + if (user !== undefined) { + return user.getDomainIds(); + } else { + return []; + } + } + + public remove(user: User) { + this.onDelete.emit(user); + } + + public view(userId: number): void { + if (!this.domainMode) { + this.onView.emit(userId); + } + } + + public changeUserStatus(user: User, enabled: boolean): void { + this.userService.changeUserStatus(user.id, enabled).subscribe(); + user.enabled = enabled; + // sort after changing params + if (this.lastSearchCriteria) { + this.handleSortEvent(this.lastSearchCriteria); + } + } + + onSorted($event) { + console.log('onSort', $event); + this.displayUsers = this.users; + this.handleSearchEvent(this.searchText.value) + this.handleSortEvent($event); + } + + onSearch(term) { + console.log('onSearch', term) + this.displayUsers = this.users; + this.handleSearchEvent(term) + this.handleSortEvent(this.lastSearchCriteria); + } + + handleSearchEvent(term: string) { + console.warn(this.displayUsers.filter( + u => userMatches(u, term) + )) + this.displayUsers = this.displayUsers.filter( + u => userMatches(u, term) + ) + } + + handleSortEvent(criteria: CustomerSearchCriteria) { + this.lastSearchCriteria = criteria; + const baseSortFunc = (a: any, b: any): number => { + if (a < b) { + return -1; } - if (!bd) { - console.log(bd); + if (a > b) { + return 1; } - const ar = ad.length > 0 ? ad[0].domainId : 0; - const br = bd.length > 0 ? bd[0].domainId : 0; - p1 = ar; - p2 = br; - } else if (criteria.sortColumn === 'globalRole') { - p1 = this.getGlobalRole(a); - p2 = this.getGlobalRole(b); - } else if (criteria.sortColumn === 'roles') { - const ad = this.getOnlyDomainRoles(a); - const bd = this.getOnlyDomainRoles(b); - const ar = ad !== undefined ? ad[0].role.toString() : ''; - const br = bd !== undefined ? bd[0].role.toString() : ''; - p1 = ar; - p2 = br; - } else { - p1 = a[criteria.sortColumn]; - p2 = b[criteria.sortColumn]; - } - - if (typeof p1 === 'string' && typeof p2 === 'string') { - p1 = p1.toLowerCase(); - p2 = p2.toLowerCase(); - } - - result = baseSortFunc(p1, p2); - - return result * direction; - } - ) - } - - public setItems(item) { - // store max items per page value in this session - sessionStorage.setItem(this.users_item_number_key, item); - this.maxItemsOnPage = item; - } - - public isGlobalGuestAndHasNoRoleInThisDomain(user: User): boolean { - const isGlobalGuest = user.roles.filter(r => - r.domainId === this.domainService.getGlobalDomainId() && - typeof r.role === 'string' ? Role[r.role] : r.role === Role.ROLE_GUEST).length > 0; - // console.log('is global guest:\t' + isGlobalGuest); - const hasNoRoleInThisDomain = user.roles.filter(r => r.domainId === this.domainId).length === 0; - return isGlobalGuest && hasNoRoleInThisDomain; - } - - public addToCurrentDomain(user: User) { - this.onAddToDomain.emit(user); - this.changeMode(); - } - - public changeMode() { - this.onModeChange.emit(0); - } - - public canUserBeDeleted(user: User): boolean { - if (user.enabled) { - return false; + return 0; + }; + + this.displayUsers.sort( + (a: User, b: User) => { + if (!criteria) { + return 0; + } + const direction = criteria.sortDirection === 'asc' ? 1 : -1; + let result: number; + + let p1: any, p2: any; + + // sorting rules for custom columns + if (criteria.sortColumn === 'domains') { + const ad = this.filterDomainNames(a); + const bd = this.filterDomainNames(b); + if (!ad) { + console.log(ad); + } + if (!bd) { + console.log(bd); + } + const ar = ad.length > 0 ? ad[0].domainId : 0; + const br = bd.length > 0 ? bd[0].domainId : 0; + p1 = ar; + p2 = br; + } else if (criteria.sortColumn === 'globalRole') { + p1 = this.getGlobalRole(a); + p2 = this.getGlobalRole(b); + } else if (criteria.sortColumn === 'roles') { + const ad = this.getOnlyDomainRoles(a); + const bd = this.getOnlyDomainRoles(b); + const ar = ad !== undefined ? ad[0].role.toString() : ''; + const br = bd !== undefined ? bd[0].role.toString() : ''; + p1 = ar; + p2 = br; + } else { + p1 = a[criteria.sortColumn]; + p2 = b[criteria.sortColumn]; + } + + if (typeof p1 === 'string' && typeof p2 === 'string') { + p1 = p1.toLowerCase(); + p2 = p2.toLowerCase(); + } + + result = baseSortFunc(p1, p2); + + return result * direction; + } + ) } - if (user.ssoUser) { - return false; + + public setItems(item) { + // store max items per page value in this session + sessionStorage.setItem(this.users_item_number_key, item); + this.maxItemsOnPage = item; } - const result = user.roles.find( - role => !(roleConvert(role.role) === Role.ROLE_GUEST && role.domainId === this.domainService.getGlobalDomainId()) - ); - return !result; - } - - public searchUsers(search: string) { - if (search === '') { - this.filteredUsers = []; - } else { - if (this.allowedModes.find(v => v === ComponentMode.EDIT) !== undefined) { - this.userService.getUserBySearch(search, this.domainId).subscribe(data => { - this.filteredUsers = data; - this.displayUsers = this.filteredUsers; - }) - } else { - // this.displayUsers = this.users; - } + + public isGlobalGuestAndHasNoRoleInThisDomain(user: User): boolean { + const isGlobalGuest = user.roles.filter(r => + r.domainId === this.domainService.getGlobalDomainId() && + typeof r.role === 'string' ? Role[r.role] : r.role === Role.ROLE_GUEST).length > 0; + // console.log('is global guest:\t' + isGlobalGuest); + const hasNoRoleInThisDomain = user.roles.filter(r => r.domainId === this.domainId).length === 0; + return isGlobalGuest && hasNoRoleInThisDomain; } - } - - - public getAllowedRoles(): Role[] { - let roles: Role[]; - if (this.authService.hasRole(Role[Role.ROLE_SYSTEM_ADMIN]) && - Number(this.domainId === this.domainService.getGlobalDomainId())) { - // admin (global) role set - roles = [Role.ROLE_OPERATOR, Role.ROLE_TOOL_MANAGER, Role.ROLE_SYSTEM_ADMIN]; - // roles = this.filterRoles(roles, this.domainId); - } else if (this.domainId != null) { - // default (domain) role set - roles = [Role.ROLE_USER, Role.ROLE_DOMAIN_ADMIN, Role.ROLE_GUEST]; - } else { - // no roles - roles = []; + + public addToCurrentDomain(user: User) { + this.onAddToDomain.emit(user); + this.changeMode(); } - return roles; - } + public changeMode() { + this.onModeChange.emit(0); + } - public changeUserRole(user: User, domainId: number, event: any) { - console.warn(event); - this.onUserRoleChange.emit({userId: user.id, domainId: domainId, role: event.value}) - } + public canUserBeDeleted(user: User): boolean { + if (user.enabled) { + return false; + } + if (user.ssoUser) { + return false; + } + const result = user.roles.find( + role => !(roleConvert(role.role) === Role.ROLE_GUEST && role.domainId === this.domainService.getGlobalDomainId()) + ); + return !result; + } - public checkUserIfIsCurrentUser(userName: string) { - return this.authService.getUsername() === userName - } + public searchUsers(search: string) { + if (search === '') { + this.filteredUsers = []; + } else { + if (this.allowedModes.find(v => v === ComponentMode.EDIT) !== undefined) { + this.userService.getUserBySearch(search, this.domainId).subscribe(data => { + this.filteredUsers = data; + this.displayUsers = this.filteredUsers; + }) + } else { + // this.displayUsers = this.users; + } + } + } + + + public getAllowedRoles(): Role[] { + let roles: Role[]; + if (this.authService.hasRole(Role[Role.ROLE_SYSTEM_ADMIN]) && + Number(this.domainId === this.domainService.getGlobalDomainId())) { + // admin (global) role set + roles = [Role.ROLE_OPERATOR, Role.ROLE_TOOL_MANAGER, Role.ROLE_SYSTEM_ADMIN]; + // roles = this.filterRoles(roles, this.domainId); + } else if (this.domainId != null) { + // default (domain) role set + roles = [Role.ROLE_USER, Role.ROLE_DOMAIN_ADMIN, Role.ROLE_GUEST]; + } else { + // no roles + roles = []; + } + return roles; + } + + + public changeUserRole(user: User, domainId: number, event: any) { + console.warn(event); + this.onUserRoleChange.emit({userId: user.id, domainId: domainId, role: event.value}) + } + + public checkUserIfIsCurrentUser(userName: string) { + return this.authService.getUsername() === userName + } } -function roleConvert(role: string | Role ): Role { - if (typeof role === 'string') { - return Role[role]; - } - return role; +function roleConvert(role: string | Role): Role { + if (typeof role === 'string') { + return Role[role]; + } + return role; } diff --git a/src/app/shared/users/privileges/userprivileges.component.ts b/src/app/shared/users/privileges/userprivileges.component.ts index b7b2423f50c79543347d9d25abf110e7f79f3297..1f65591f962ddcdc5b7ba0f63feaec5a576cc64b 100644 --- a/src/app/shared/users/privileges/userprivileges.component.ts +++ b/src/app/shared/users/privileges/userprivileges.component.ts @@ -10,7 +10,7 @@ import {UntypedFormBuilder, UntypedFormGroup, Validators} from '@angular/forms'; import {Observable, of} from 'rxjs'; import {CacheService} from '../../../service/cache.service'; import {UserDataService} from '../../../service/userdata.service'; -import {map, shareReplay, take} from 'rxjs/operators'; +import {debounceTime, map, shareReplay, take} from 'rxjs/operators'; @Component({ selector: 'nmaas-userprivileges', @@ -103,6 +103,15 @@ export class UserPrivilegesComponent extends BaseComponent implements OnInit { this.domainService.getOne(domainId).subscribe((domain) => this.domains.push(domain)); }); } + this.getAllDomain(); + } + + public getAllDomain() { + this.domainService.getAll().subscribe(domains => { + domains.forEach(domain => { + if (!this.domainCache.hasData(domain.id)) this.domainCache.setData(domain.id, domain) + }) + }) } public add(): void { @@ -124,14 +133,6 @@ export class UserPrivilegesComponent extends BaseComponent implements OnInit { public getDomainName(domainId: number): Observable<string> { if (this.domainCache.hasData(domainId)) { return of(this.domainCache.getData(domainId).name); - } else { - return this.domainService.getOne(domainId).pipe( - map((domain) => { - this.domainCache.setData(domainId, domain); - return domain.name - }), - shareReplay(1), - take(1)); } }