1
|
import { Category } from 'src/app/shared/models/category.interface';
|
2
|
import { Role } from './../../../../shared/models/role.interface';
|
3
|
import { Component, EventEmitter, Input, OnInit, Output, SimpleChanges } from '@angular/core';
|
4
|
import { Right } from 'src/app/shared/models';
|
5
|
import { AuthService } from 'src/app/shared/services/auth.service';
|
6
|
import { RolesService } from 'src/app/shared/services/roles-users-management/role.service';
|
7
|
import { USER_RIGHTS } from 'src/app/shared/enums/USER_RIGHTS.enum';
|
8
|
import { environment } from 'src/environments/environment';
|
9
|
import { NotificationsHandlingService } from 'src/app/shared/services/notifications-handling/notifications-handling.service';
|
10
|
import { RightsCategory } from 'src/app/shared/models/rights-category.interface';
|
11
|
|
12
|
@Component({
|
13
|
selector: 'app-rights-per-role',
|
14
|
templateUrl: './rights-per-role.component.html',
|
15
|
styleUrls: ['./rights-per-role.component.scss']
|
16
|
})
|
17
|
|
18
|
export class RightsPerRoleComponent implements OnInit {
|
19
|
|
20
|
@Input() role: Role;
|
21
|
@Output() valueChange = new EventEmitter<Role>();
|
22
|
categories: RightsCategory[] = [];
|
23
|
selectedValues: number[] = [];
|
24
|
rights: Right[] = [];
|
25
|
temp:number[]=[];
|
26
|
isSelectedAll : boolean;
|
27
|
|
28
|
|
29
|
constructor(private auth: AuthService, private roleservice: RolesService, private notifications: NotificationsHandlingService) { }
|
30
|
|
31
|
ngOnInit(): void {
|
32
|
this.loadCategories();
|
33
|
if (this.auth.userRights.find(t => t.client.id == environment.globalRightsClientID)?.rights) {
|
34
|
this.rights = this.auth.userRights.find(t => t.client.id == environment.globalRightsClientID)?.rights;
|
35
|
}
|
36
|
}
|
37
|
|
38
|
ngOnChanges(changes: SimpleChanges): void {
|
39
|
this.selectedValues = this.role?.rightsList?.map((item: Right) => item.id);
|
40
|
|
41
|
}
|
42
|
|
43
|
loadCategories(): void {
|
44
|
this.roleservice.getCategory().subscribe(result => {
|
45
|
this.categories = result;
|
46
|
},
|
47
|
error => {
|
48
|
});
|
49
|
}
|
50
|
|
51
|
filterRights(rightslist: Right[]): any[] {
|
52
|
return rightslist.filter(i => i.isGlobal === this.role.isGlobal);
|
53
|
}
|
54
|
|
55
|
filterCategories(categories: RightsCategory[]): any[] {
|
56
|
return categories.filter(i => i.rights.some(i=>i.isGlobal === this.role.isGlobal));
|
57
|
}
|
58
|
|
59
|
checkAllCheckBoxes(){
|
60
|
if(!this.isSelectedAll){
|
61
|
this.temp = [];
|
62
|
this.categories.filter(i => i.rights.some(i=>i.isGlobal === this.role.isGlobal)).map(i=>i.rights.filter(i=>i.isGlobal === this.role.isGlobal).forEach(i=>this.temp.push(i.id)));
|
63
|
this.selectedValues = this.temp;
|
64
|
this.isSelectedAll = true;
|
65
|
}else{
|
66
|
this.selectedValues = [];
|
67
|
this.isSelectedAll = false;
|
68
|
}
|
69
|
}
|
70
|
|
71
|
rightsIsDisabled(): boolean {
|
72
|
if (USER_RIGHTS.G03.isGrantedToUser(this.rights)) {
|
73
|
return false;
|
74
|
} else {
|
75
|
return true;
|
76
|
}
|
77
|
}
|
78
|
|
79
|
updateRights(): void {
|
80
|
this.role.rightsList = Object.assign(this.selectedValues.map((item) => { return { id: item } }));
|
81
|
this.roleservice.update(this.role).subscribe(result => {
|
82
|
this.valueChange.emit(result);
|
83
|
this.auth.getUserDetails();
|
84
|
this.notifications.showUpdateRightsSuccess();
|
85
|
},
|
86
|
error => {
|
87
|
});
|
88
|
|
89
|
}
|
90
|
}
|