Skip to content

Commit

Permalink
Merge pull request #224 from nmaas-platform/212-domain-name-not-displ…
Browse files Browse the repository at this point in the history
…ayed-in-privileges-section-of-user-profile-view

removed domaincache, add domainName to user role
  • Loading branch information
llopat authored Apr 10, 2024
2 parents 57b4428 + 75698bf commit 1f034be
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 29 deletions.
3 changes: 2 additions & 1 deletion src/app/model/userrole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ export function RoleAware(constructor: Function) {

export class UserRole {
public domainId: number = undefined;
public domainName: string = undefined;

public role: Role = undefined;

constructor(domainId?: number, role?: Role) {
constructor(domainId?: number, domainName?: string, role?: Role) {
this.domainId = domainId;
this.role = role;
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/service/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class UserService extends GenericDataService {
const url: string = (domainId === undefined ? this.getUsersUrl() : this.getDomainUsersUrl(domainId)) + userId + '/roles';
const targetDomainId: number = (domainId === undefined ? this.appConfig.getNmaasGlobalDomainId() : domainId);

return this.post<UserRole, UserRole>(url, new UserRole(targetDomainId, role));
return this.post<UserRole, UserRole>(url, new UserRole(targetDomainId, undefined, role));
}

public removeRole(userId: number, role: Role, domainId?: number): Observable<void> {
Expand Down
4 changes: 2 additions & 2 deletions src/app/shared/users/privileges/userprivileges.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<tbody *ngIf="(domainId == domainService.getGlobalDomainId() && authService.hasRole('ROLE_SYSTEM_ADMIN')) || isInMode(ComponentMode.PROFILVIEW)">
<ng-template ngFor let-userrole [ngForOf]="user?.roles">
<tr *ngIf="userrole.domainId != domainService.getGlobalDomainId() || userrole.role != 'ROLE_GUEST'">
<td>{{getDomainName(userrole.domainId)|async}}</td>
<td>{{userrole.domainName}}</td>
<td>{{"ENUM.USER_ROLES." + userrole.role.toUpperCase() | translate}}</td>
<td *ngIf="!isInMode(ComponentMode.PROFILVIEW) && authService.hasRole('ROLE_SYSTEM_ADMIN') && user?.username !== authService.getUsername()" class="text-right">
<button type="button" class="btn btn-danger"
Expand All @@ -61,7 +61,7 @@
<tbody *ngIf="domainId != domainService.getGlobalDomainId() && ((authService.hasDomainRole(domainId, 'ROLE_DOMAIN_ADMIN') || authService.hasRole('ROLE_SYSTEM_ADMIN'))) && !isInMode(ComponentMode.PROFILVIEW)">
<ng-template ngFor let-userrole [ngForOf]="user?.roles">
<tr *ngIf="domainId == userrole.domainId">
<td>{{getDomainName(userrole.domainId)|async}}</td>
<td>{{userrole.domainName}}</td>
<td>{{"ENUM.USER_ROLES." + userrole.role.toUpperCase() | translate}}</td>
<td class="text-right">
<button *ngIf="authService.hasRole('ROLE_SYSTEM_ADMIN') && domainId===domainService.getGlobalDomainId()" type="button" class="btn btn-danger"
Expand Down
26 changes: 1 addition & 25 deletions src/app/shared/users/privileges/userprivileges.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import {UserService} from '../../../service/user.service';
import {BaseComponent} from '../../common/basecomponent/base.component';
import {Component, Input, OnInit} from '@angular/core';
import {UntypedFormBuilder, UntypedFormGroup, Validators} from '@angular/forms';
import {Observable, of} from 'rxjs';
import {CacheService} from '../../../service/cache.service';
import {UserDataService} from '../../../service/userdata.service';

@Component({
Expand All @@ -28,9 +26,6 @@ export class UserPrivilegesComponent extends BaseComponent implements OnInit {

public domains: Domain[] = [];
public roles: Role[] = [];

public domainCache: CacheService<number, Domain> = new CacheService<number, Domain>();

public newPrivilegeForm: UntypedFormGroup;

constructor(protected fb: UntypedFormBuilder,
Expand Down Expand Up @@ -97,33 +92,20 @@ export class UserPrivilegesComponent extends BaseComponent implements OnInit {
if (this.authService.hasRole(Role[Role.ROLE_SYSTEM_ADMIN])) {
this.domainService.getAll().subscribe((domains) => {
this.domains = domains
this.saveMultipleDomainsInCache(domains);
});
} else if (this.authService.hasRole(Role[Role.ROLE_DOMAIN_ADMIN])) {
const domainIds: number[] = this.authService.getDomainsWithRole(Role[Role.ROLE_DOMAIN_ADMIN]);
domainIds.forEach((domainId) => {
this.domainService.getOne(domainId).subscribe((domain) => {
this.domains.push(domain)
this.saveDomainInCache(domain)
});
});
} else {
this.getMyDomains();
}
}

private saveMultipleDomainsInCache(domains: Domain[]) {
domains.forEach(domain => this.saveDomainInCache(domain))
}

private saveDomainInCache(domain: Domain) {
if (!this.domainCache.hasData(domain.id)) {
this.domainCache.setData(domain.id, domain)
}
}

public getMyDomains() {
this.domainService.getMyDomains().subscribe(domains => this.saveMultipleDomainsInCache(domains))
this.domainService.getMyDomains().subscribe(domains => this.domains.push(...domains))
}

public add(): void {
Expand All @@ -142,12 +124,6 @@ export class UserPrivilegesComponent extends BaseComponent implements OnInit {
() => this.userService.getOne(this.user.id).subscribe((user) => this.user = user))
}

public getDomainName(domainId: number): Observable<string> {
if (this.domainCache.hasData(domainId)) {
return of(this.domainCache.getData(domainId).name);
}
}

/**
* clear selected role after new domain is selected
*/
Expand Down

0 comments on commit 1f034be

Please sign in to comment.