]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | import { Component, OnInit } from '@angular/core'; |
2 | ||
3 | import { I18n } from '@ngx-translate/i18n-polyfill'; | |
4 | import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal'; | |
5 | import { forkJoin } from 'rxjs'; | |
6 | ||
7 | import { RoleService } from '../../../shared/api/role.service'; | |
8 | import { ScopeService } from '../../../shared/api/scope.service'; | |
9 | import { CriticalConfirmationModalComponent } from '../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component'; | |
10 | import { ActionLabelsI18n } from '../../../shared/constants/app.constants'; | |
11 | import { CellTemplate } from '../../../shared/enum/cell-template.enum'; | |
12 | import { NotificationType } from '../../../shared/enum/notification-type.enum'; | |
13 | import { CdTableAction } from '../../../shared/models/cd-table-action'; | |
14 | import { CdTableColumn } from '../../../shared/models/cd-table-column'; | |
15 | import { CdTableSelection } from '../../../shared/models/cd-table-selection'; | |
16 | import { Permission } from '../../../shared/models/permissions'; | |
17 | import { EmptyPipe } from '../../../shared/pipes/empty.pipe'; | |
18 | import { AuthStorageService } from '../../../shared/services/auth-storage.service'; | |
19 | import { NotificationService } from '../../../shared/services/notification.service'; | |
20 | import { URLBuilderService } from '../../../shared/services/url-builder.service'; | |
21 | ||
22 | const BASE_URL = 'user-management/roles'; | |
23 | ||
24 | @Component({ | |
25 | selector: 'cd-role-list', | |
26 | templateUrl: './role-list.component.html', | |
27 | styleUrls: ['./role-list.component.scss'], | |
28 | providers: [{ provide: URLBuilderService, useValue: new URLBuilderService(BASE_URL) }] | |
29 | }) | |
30 | export class RoleListComponent implements OnInit { | |
31 | permission: Permission; | |
32 | tableActions: CdTableAction[]; | |
33 | columns: CdTableColumn[]; | |
34 | roles: Array<any>; | |
35 | scopes: Array<string>; | |
36 | selection = new CdTableSelection(); | |
37 | ||
38 | modalRef: BsModalRef; | |
39 | ||
40 | constructor( | |
41 | private roleService: RoleService, | |
42 | private scopeService: ScopeService, | |
43 | private emptyPipe: EmptyPipe, | |
44 | private authStorageService: AuthStorageService, | |
45 | private modalService: BsModalService, | |
46 | private notificationService: NotificationService, | |
47 | private i18n: I18n, | |
48 | private urlBuilder: URLBuilderService, | |
49 | public actionLabels: ActionLabelsI18n | |
50 | ) { | |
51 | this.permission = this.authStorageService.getPermissions().user; | |
52 | const addAction: CdTableAction = { | |
53 | permission: 'create', | |
54 | icon: 'fa-plus', | |
55 | routerLink: () => this.urlBuilder.getCreate(), | |
56 | name: this.actionLabels.CREATE | |
57 | }; | |
58 | const editAction: CdTableAction = { | |
59 | permission: 'update', | |
60 | icon: 'fa-pencil', | |
61 | disable: () => !this.selection.hasSingleSelection || this.selection.first().system, | |
62 | routerLink: () => | |
63 | this.selection.first() && this.urlBuilder.getEdit(this.selection.first().name), | |
64 | name: this.actionLabels.EDIT | |
65 | }; | |
66 | const deleteAction: CdTableAction = { | |
67 | permission: 'delete', | |
68 | icon: 'fa-times', | |
69 | disable: () => !this.selection.hasSingleSelection || this.selection.first().system, | |
70 | click: () => this.deleteRoleModal(), | |
71 | name: this.actionLabels.DELETE | |
72 | }; | |
73 | this.tableActions = [addAction, editAction, deleteAction]; | |
74 | } | |
75 | ||
76 | ngOnInit() { | |
77 | this.columns = [ | |
78 | { | |
79 | name: this.i18n('Name'), | |
80 | prop: 'name', | |
81 | flexGrow: 3 | |
82 | }, | |
83 | { | |
84 | name: this.i18n('Description'), | |
85 | prop: 'description', | |
86 | flexGrow: 5, | |
87 | pipe: this.emptyPipe | |
88 | }, | |
89 | { | |
90 | name: this.i18n('System Role'), | |
91 | prop: 'system', | |
92 | cellClass: 'text-center', | |
93 | flexGrow: 1, | |
94 | cellTransformation: CellTemplate.checkIcon | |
95 | } | |
96 | ]; | |
97 | } | |
98 | ||
99 | getRoles() { | |
100 | forkJoin([this.roleService.list(), this.scopeService.list()]).subscribe( | |
101 | (data: [Array<any>, Array<string>]) => { | |
102 | this.roles = data[0]; | |
103 | this.scopes = data[1]; | |
104 | } | |
105 | ); | |
106 | } | |
107 | ||
108 | updateSelection(selection: CdTableSelection) { | |
109 | this.selection = selection; | |
110 | } | |
111 | ||
112 | deleteRole(role: string) { | |
113 | this.roleService.delete(role).subscribe( | |
114 | () => { | |
115 | this.getRoles(); | |
116 | this.modalRef.hide(); | |
117 | this.notificationService.show( | |
118 | NotificationType.success, | |
119 | this.i18n(`Deleted role '{{role_name}}'`, { role_name: role }) | |
120 | ); | |
121 | }, | |
122 | () => { | |
123 | this.modalRef.content.stopLoadingSpinner(); | |
124 | } | |
125 | ); | |
126 | } | |
127 | ||
128 | deleteRoleModal() { | |
129 | const name = this.selection.first().name; | |
130 | this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, { | |
131 | initialState: { | |
132 | itemDescription: 'Role', | |
eafe8130 | 133 | itemNames: [name], |
11fdf7f2 TL |
134 | submitAction: () => this.deleteRole(name) |
135 | } | |
136 | }); | |
137 | } | |
138 | } |