Skip to content
Snippets Groups Projects
Unverified Commit 4029102e authored by Łukasz Łopatowski's avatar Łukasz Łopatowski Committed by GitHub
Browse files

Merge pull request #136 from nmaas-platform/135-fix-domain-call-in-user-list-and-user-view

135 fix domain call in user list and user view
parents 820242c4 f911db14
No related branches found
No related tags found
No related merge requests found
...@@ -8,321 +8,324 @@ import {Observable, of} from 'rxjs'; ...@@ -8,321 +8,324 @@ import {Observable, of} from 'rxjs';
import {Role, UserRole} from '../../../model/userrole'; import {Role, UserRole} from '../../../model/userrole';
import {UserDataService} from '../../../service/userdata.service'; import {UserDataService} from '../../../service/userdata.service';
import {AuthService} from '../../../auth/auth.service'; import {AuthService} from '../../../auth/auth.service';
import {map, shareReplay, take} from 'rxjs/operators';
import {UntypedFormControl} from '@angular/forms'; import {UntypedFormControl} from '@angular/forms';
import {ComponentMode} from '../../common/componentmode'; import {ComponentMode} from '../../common/componentmode';
import {Router} from '@angular/router'; import {Router} from '@angular/router';
function userMatches(u: User, term: string): boolean { function userMatches(u: User, term: string): boolean {
const t = term || '' const t = term || ''
return (u.username || '').toLowerCase().includes(t.toLowerCase()) || return (u.username || '').toLowerCase().includes(t.toLowerCase()) ||
(u.email || '').toLowerCase().includes(t.toLowerCase()) || (u.email || '').toLowerCase().includes(t.toLowerCase()) ||
(u.firstname || '').toLowerCase().includes(t.toLowerCase()) || (u.firstname || '').toLowerCase().includes(t.toLowerCase()) ||
(u.lastname || '').toLowerCase().includes(t.toLowerCase()) (u.lastname || '').toLowerCase().includes(t.toLowerCase())
} }
@Component({ @Component({
selector: 'nmaas-userslist', selector: 'nmaas-userslist',
templateUrl: './userslist.component.html', templateUrl: './userslist.component.html',
styleUrls: ['./userslist.component.css'] styleUrls: ['./userslist.component.css']
}) })
export class UsersListComponent extends BaseComponent implements OnInit, OnChanges { 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() @Input()
public users: User[] = []; // provided list of users public users: User[] = []; // provided list of users
@Input() @Input()
public domainMode = false; 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() @Output()
public onDelete: EventEmitter<User> = new EventEmitter<User>(); public onDelete: EventEmitter<User> = new EventEmitter<User>();
@Output() @Output()
public onView: EventEmitter<number> = new EventEmitter<number>(); public onView: EventEmitter<number> = new EventEmitter<number>();
@Output() @Output()
public onAddToDomain: EventEmitter<User> = new EventEmitter<User>(); public onAddToDomain: EventEmitter<User> = new EventEmitter<User>();
@Output() @Output()
public onRemoveFromDomain: EventEmitter<User> = new EventEmitter<User>(); public onRemoveFromDomain: EventEmitter<User> = new EventEmitter<User>();
@Output() @Output()
public onModeChange: EventEmitter<number> = new EventEmitter<number>(); public onModeChange: EventEmitter<number> = new EventEmitter<number>();
@Output() @Output()
public onUserRoleChange: EventEmitter<any> = new EventEmitter<any>(); 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 pageNumber = 1;
public paginatorName = 'paginator-identifier'; public paginatorName = 'paginator-identifier';
public itemsPerPage: number[] = [15, 20, 25, 30, 50]; public itemsPerPage: number[] = [15, 20, 25, 30, 50];
public maxItemsOnPage = 15; public maxItemsOnPage = 15;
public filteredUsers: User[] = []; public filteredUsers: User[] = [];
public searchText = new UntypedFormControl(''); public searchText = new UntypedFormControl('');
public Role = Role; public Role = Role;
constructor(private userService: UserService, constructor(private userService: UserService,
public domainService: DomainService, public domainService: DomainService,
private userDataService: UserDataService, private userDataService: UserDataService,
public authService: AuthService, public authService: AuthService,
private router: Router) { private router: Router) {
super(); super();
userDataService.selectedDomainId.subscribe(domain => this.domainId = domain); 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() { ngOnChanges(changes: SimpleChanges): void {
// set stored value of maxElementsPerPage this.displayUsers = this.users;
const i = sessionStorage.getItem(this.users_item_number_key);
if (i) {
this.maxItemsOnPage = +i;
} }
this.searchText.valueChanges.subscribe( public getAllDomain() {
term => this.onSearch(term) this.domainService.getAll().subscribe(domains => {
) domains.forEach(domain => {
this.domainCache.setData(domain.id, domain)
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 getDomainName(domainId: number): Observable<string> {
public filterDomainNames(user: User): UserRole[] { if (this.domainCache.hasData(domainId)) {
return user.roles.filter(role => role.domainId !== this.domainService.getGlobalDomainId()); return of(this.domainCache.getData(domainId).name);
} } else {
return new Observable<string>()
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 remove(user: User) { public filterDomainNames(user: User): UserRole[] {
this.onDelete.emit(user); return user.roles.filter(role => role.domainId !== this.domainService.getGlobalDomainId());
} }
public view(userId: number): void { public getOnlyDomainRoles(user: User): UserRole {
if (!this.domainMode) { return user.roles.find(role => role.domainId === this.domainId);
this.onView.emit(userId);
} }
}
public getGlobalRole(user: User): string {
public changeUserStatus(user: User, enabled: boolean): void { const userRole: UserRole[] = user.roles.filter(role => role.domainId === this.domainService.getGlobalDomainId());
this.userService.changeUserStatus(user.id, enabled).subscribe(); return userRole[0].role.toString();
user.enabled = enabled;
// sort after changing params
if (this.lastSearchCriteria) {
this.handleSortEvent(this.lastSearchCriteria);
} }
}
public getUserDomainIds(user: User): number[] {
onSorted($event) { if (user !== undefined) {
console.log('onSort', $event); return user.getDomainIds();
this.displayUsers = this.users; } else {
this.handleSearchEvent(this.searchText.value) return [];
this.handleSortEvent($event); }
} }
onSearch(term) { public remove(user: User) {
console.log('onSearch', term) this.onDelete.emit(user);
this.displayUsers = this.users; }
this.handleSearchEvent(term)
this.handleSortEvent(this.lastSearchCriteria); public view(userId: number): void {
} if (!this.domainMode) {
this.onView.emit(userId);
handleSearchEvent(term: string) { }
console.warn(this.displayUsers.filter( }
u => userMatches(u, term)
)) public changeUserStatus(user: User, enabled: boolean): void {
this.displayUsers = this.displayUsers.filter( this.userService.changeUserStatus(user.id, enabled).subscribe();
u => userMatches(u, term) user.enabled = enabled;
) // sort after changing params
} if (this.lastSearchCriteria) {
this.handleSortEvent(this.lastSearchCriteria);
handleSortEvent(criteria: CustomerSearchCriteria) { }
this.lastSearchCriteria = criteria; }
const baseSortFunc = (a: any, b: any): number => {
if (a < b) { onSorted($event) {
return -1; console.log('onSort', $event);
} this.displayUsers = this.users;
if (a > b) { this.handleSearchEvent(this.searchText.value)
return 1; this.handleSortEvent($event);
} }
return 0;
}; onSearch(term) {
console.log('onSearch', term)
this.displayUsers.sort( this.displayUsers = this.users;
(a: User, b: User) => { this.handleSearchEvent(term)
if (!criteria) { this.handleSortEvent(this.lastSearchCriteria);
return 0; }
}
const direction = criteria.sortDirection === 'asc' ? 1 : -1; handleSearchEvent(term: string) {
let result: number; console.warn(this.displayUsers.filter(
u => userMatches(u, term)
let p1: any, p2: any; ))
this.displayUsers = this.displayUsers.filter(
// sorting rules for custom columns u => userMatches(u, term)
if (criteria.sortColumn === 'domains') { )
const ad = this.filterDomainNames(a); }
const bd = this.filterDomainNames(b);
if (!ad) { handleSortEvent(criteria: CustomerSearchCriteria) {
console.log(ad); this.lastSearchCriteria = criteria;
const baseSortFunc = (a: any, b: any): number => {
if (a < b) {
return -1;
} }
if (!bd) { if (a > b) {
console.log(bd); return 1;
} }
const ar = ad.length > 0 ? ad[0].domainId : 0; return 0;
const br = bd.length > 0 ? bd[0].domainId : 0; };
p1 = ar;
p2 = br; this.displayUsers.sort(
} else if (criteria.sortColumn === 'globalRole') { (a: User, b: User) => {
p1 = this.getGlobalRole(a); if (!criteria) {
p2 = this.getGlobalRole(b); return 0;
} else if (criteria.sortColumn === 'roles') { }
const ad = this.getOnlyDomainRoles(a); const direction = criteria.sortDirection === 'asc' ? 1 : -1;
const bd = this.getOnlyDomainRoles(b); let result: number;
const ar = ad !== undefined ? ad[0].role.toString() : '';
const br = bd !== undefined ? bd[0].role.toString() : ''; let p1: any, p2: any;
p1 = ar;
p2 = br; // sorting rules for custom columns
} else { if (criteria.sortColumn === 'domains') {
p1 = a[criteria.sortColumn]; const ad = this.filterDomainNames(a);
p2 = b[criteria.sortColumn]; const bd = this.filterDomainNames(b);
} if (!ad) {
console.log(ad);
if (typeof p1 === 'string' && typeof p2 === 'string') { }
p1 = p1.toLowerCase(); if (!bd) {
p2 = p2.toLowerCase(); console.log(bd);
} }
const ar = ad.length > 0 ? ad[0].domainId : 0;
result = baseSortFunc(p1, p2); const br = bd.length > 0 ? bd[0].domainId : 0;
p1 = ar;
return result * direction; p2 = br;
} } else if (criteria.sortColumn === 'globalRole') {
) p1 = this.getGlobalRole(a);
} p2 = this.getGlobalRole(b);
} else if (criteria.sortColumn === 'roles') {
public setItems(item) { const ad = this.getOnlyDomainRoles(a);
// store max items per page value in this session const bd = this.getOnlyDomainRoles(b);
sessionStorage.setItem(this.users_item_number_key, item); const ar = ad !== undefined ? ad[0].role.toString() : '';
this.maxItemsOnPage = item; const br = bd !== undefined ? bd[0].role.toString() : '';
} p1 = ar;
p2 = br;
public isGlobalGuestAndHasNoRoleInThisDomain(user: User): boolean { } else {
const isGlobalGuest = user.roles.filter(r => p1 = a[criteria.sortColumn];
r.domainId === this.domainService.getGlobalDomainId() && p2 = b[criteria.sortColumn];
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; if (typeof p1 === 'string' && typeof p2 === 'string') {
return isGlobalGuest && hasNoRoleInThisDomain; p1 = p1.toLowerCase();
} p2 = p2.toLowerCase();
}
public addToCurrentDomain(user: User) {
this.onAddToDomain.emit(user); result = baseSortFunc(p1, p2);
this.changeMode();
} return result * direction;
}
public changeMode() { )
this.onModeChange.emit(0);
}
public canUserBeDeleted(user: User): boolean {
if (user.enabled) {
return false;
} }
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()) public isGlobalGuestAndHasNoRoleInThisDomain(user: User): boolean {
); const isGlobalGuest = user.roles.filter(r =>
return !result; 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);
public searchUsers(search: string) { const hasNoRoleInThisDomain = user.roles.filter(r => r.domainId === this.domainId).length === 0;
if (search === '') { return isGlobalGuest && hasNoRoleInThisDomain;
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 addToCurrentDomain(user: User) {
this.onAddToDomain.emit(user);
public getAllowedRoles(): Role[] { this.changeMode();
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 changeMode() {
this.onModeChange.emit(0);
}
public changeUserRole(user: User, domainId: number, event: any) { public canUserBeDeleted(user: User): boolean {
console.warn(event); if (user.enabled) {
this.onUserRoleChange.emit({userId: user.id, domainId: domainId, role: event.value}) 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) { public searchUsers(search: string) {
return this.authService.getUsername() === userName 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 { function roleConvert(role: string | Role): Role {
if (typeof role === 'string') { if (typeof role === 'string') {
return Role[role]; return Role[role];
} }
return role; return role;
} }
...@@ -10,7 +10,7 @@ import {UntypedFormBuilder, UntypedFormGroup, Validators} from '@angular/forms'; ...@@ -10,7 +10,7 @@ import {UntypedFormBuilder, UntypedFormGroup, Validators} from '@angular/forms';
import {Observable, of} from 'rxjs'; import {Observable, of} from 'rxjs';
import {CacheService} from '../../../service/cache.service'; import {CacheService} from '../../../service/cache.service';
import {UserDataService} from '../../../service/userdata.service'; import {UserDataService} from '../../../service/userdata.service';
import {map, shareReplay, take} from 'rxjs/operators'; import {debounceTime, map, shareReplay, take} from 'rxjs/operators';
@Component({ @Component({
selector: 'nmaas-userprivileges', selector: 'nmaas-userprivileges',
...@@ -103,6 +103,15 @@ export class UserPrivilegesComponent extends BaseComponent implements OnInit { ...@@ -103,6 +103,15 @@ export class UserPrivilegesComponent extends BaseComponent implements OnInit {
this.domainService.getOne(domainId).subscribe((domain) => this.domains.push(domain)); 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 { public add(): void {
...@@ -124,14 +133,6 @@ export class UserPrivilegesComponent extends BaseComponent implements OnInit { ...@@ -124,14 +133,6 @@ export class UserPrivilegesComponent extends BaseComponent implements OnInit {
public getDomainName(domainId: number): Observable<string> { public getDomainName(domainId: number): Observable<string> {
if (this.domainCache.hasData(domainId)) { if (this.domainCache.hasData(domainId)) {
return of(this.domainCache.getData(domainId).name); 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));
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment