]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | import { Component, OnInit } from '@angular/core'; |
2 | ||
f67539c2 | 3 | import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap'; |
11fdf7f2 TL |
4 | import { forkJoin } from 'rxjs'; |
5 | ||
f67539c2 TL |
6 | import { RoleService } from '~/app/shared/api/role.service'; |
7 | import { ScopeService } from '~/app/shared/api/scope.service'; | |
8 | import { ListWithDetails } from '~/app/shared/classes/list-with-details.class'; | |
9 | import { CriticalConfirmationModalComponent } from '~/app/shared/components/critical-confirmation-modal/critical-confirmation-modal.component'; | |
10 | import { FormModalComponent } from '~/app/shared/components/form-modal/form-modal.component'; | |
11 | import { ActionLabelsI18n } from '~/app/shared/constants/app.constants'; | |
12 | import { CellTemplate } from '~/app/shared/enum/cell-template.enum'; | |
13 | import { Icons } from '~/app/shared/enum/icons.enum'; | |
14 | import { NotificationType } from '~/app/shared/enum/notification-type.enum'; | |
15 | import { CdTableAction } from '~/app/shared/models/cd-table-action'; | |
16 | import { CdTableColumn } from '~/app/shared/models/cd-table-column'; | |
17 | import { CdTableSelection } from '~/app/shared/models/cd-table-selection'; | |
18 | import { Permission } from '~/app/shared/models/permissions'; | |
19 | import { EmptyPipe } from '~/app/shared/pipes/empty.pipe'; | |
20 | import { AuthStorageService } from '~/app/shared/services/auth-storage.service'; | |
21 | import { ModalService } from '~/app/shared/services/modal.service'; | |
22 | import { NotificationService } from '~/app/shared/services/notification.service'; | |
23 | import { URLBuilderService } from '~/app/shared/services/url-builder.service'; | |
11fdf7f2 TL |
24 | |
25 | const BASE_URL = 'user-management/roles'; | |
26 | ||
27 | @Component({ | |
28 | selector: 'cd-role-list', | |
29 | templateUrl: './role-list.component.html', | |
30 | styleUrls: ['./role-list.component.scss'], | |
31 | providers: [{ provide: URLBuilderService, useValue: new URLBuilderService(BASE_URL) }] | |
32 | }) | |
e306af50 | 33 | export class RoleListComponent extends ListWithDetails implements OnInit { |
11fdf7f2 TL |
34 | permission: Permission; |
35 | tableActions: CdTableAction[]; | |
36 | columns: CdTableColumn[]; | |
37 | roles: Array<any>; | |
38 | scopes: Array<string>; | |
39 | selection = new CdTableSelection(); | |
40 | ||
f67539c2 | 41 | modalRef: NgbModalRef; |
11fdf7f2 TL |
42 | |
43 | constructor( | |
44 | private roleService: RoleService, | |
45 | private scopeService: ScopeService, | |
46 | private emptyPipe: EmptyPipe, | |
47 | private authStorageService: AuthStorageService, | |
f67539c2 | 48 | private modalService: ModalService, |
11fdf7f2 | 49 | private notificationService: NotificationService, |
11fdf7f2 TL |
50 | private urlBuilder: URLBuilderService, |
51 | public actionLabels: ActionLabelsI18n | |
52 | ) { | |
e306af50 | 53 | super(); |
11fdf7f2 TL |
54 | this.permission = this.authStorageService.getPermissions().user; |
55 | const addAction: CdTableAction = { | |
56 | permission: 'create', | |
9f95a23c | 57 | icon: Icons.add, |
11fdf7f2 TL |
58 | routerLink: () => this.urlBuilder.getCreate(), |
59 | name: this.actionLabels.CREATE | |
60 | }; | |
9f95a23c TL |
61 | const cloneAction: CdTableAction = { |
62 | permission: 'create', | |
63 | icon: Icons.clone, | |
64 | name: this.actionLabels.CLONE, | |
65 | disable: () => !this.selection.hasSingleSelection, | |
66 | click: () => this.cloneRole() | |
67 | }; | |
11fdf7f2 TL |
68 | const editAction: CdTableAction = { |
69 | permission: 'update', | |
9f95a23c | 70 | icon: Icons.edit, |
11fdf7f2 TL |
71 | disable: () => !this.selection.hasSingleSelection || this.selection.first().system, |
72 | routerLink: () => | |
73 | this.selection.first() && this.urlBuilder.getEdit(this.selection.first().name), | |
74 | name: this.actionLabels.EDIT | |
75 | }; | |
76 | const deleteAction: CdTableAction = { | |
77 | permission: 'delete', | |
9f95a23c | 78 | icon: Icons.destroy, |
11fdf7f2 TL |
79 | disable: () => !this.selection.hasSingleSelection || this.selection.first().system, |
80 | click: () => this.deleteRoleModal(), | |
81 | name: this.actionLabels.DELETE | |
82 | }; | |
9f95a23c | 83 | this.tableActions = [addAction, cloneAction, editAction, deleteAction]; |
11fdf7f2 TL |
84 | } |
85 | ||
86 | ngOnInit() { | |
87 | this.columns = [ | |
88 | { | |
f67539c2 | 89 | name: $localize`Name`, |
11fdf7f2 TL |
90 | prop: 'name', |
91 | flexGrow: 3 | |
92 | }, | |
93 | { | |
f67539c2 | 94 | name: $localize`Description`, |
11fdf7f2 TL |
95 | prop: 'description', |
96 | flexGrow: 5, | |
97 | pipe: this.emptyPipe | |
98 | }, | |
99 | { | |
f67539c2 | 100 | name: $localize`System Role`, |
11fdf7f2 TL |
101 | prop: 'system', |
102 | cellClass: 'text-center', | |
103 | flexGrow: 1, | |
104 | cellTransformation: CellTemplate.checkIcon | |
105 | } | |
106 | ]; | |
107 | } | |
108 | ||
109 | getRoles() { | |
110 | forkJoin([this.roleService.list(), this.scopeService.list()]).subscribe( | |
111 | (data: [Array<any>, Array<string>]) => { | |
112 | this.roles = data[0]; | |
113 | this.scopes = data[1]; | |
114 | } | |
115 | ); | |
116 | } | |
117 | ||
118 | updateSelection(selection: CdTableSelection) { | |
119 | this.selection = selection; | |
120 | } | |
121 | ||
122 | deleteRole(role: string) { | |
123 | this.roleService.delete(role).subscribe( | |
124 | () => { | |
125 | this.getRoles(); | |
f67539c2 TL |
126 | this.modalRef.close(); |
127 | this.notificationService.show(NotificationType.success, $localize`Deleted role '${role}'`); | |
11fdf7f2 TL |
128 | }, |
129 | () => { | |
f67539c2 | 130 | this.modalRef.componentInstance.stopLoadingSpinner(); |
11fdf7f2 TL |
131 | } |
132 | ); | |
133 | } | |
134 | ||
135 | deleteRoleModal() { | |
136 | const name = this.selection.first().name; | |
137 | this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, { | |
f67539c2 TL |
138 | itemDescription: 'Role', |
139 | itemNames: [name], | |
140 | submitAction: () => this.deleteRole(name) | |
11fdf7f2 TL |
141 | }); |
142 | } | |
9f95a23c TL |
143 | |
144 | cloneRole() { | |
145 | const name = this.selection.first().name; | |
146 | this.modalRef = this.modalService.show(FormModalComponent, { | |
f67539c2 TL |
147 | fields: [ |
148 | { | |
149 | type: 'text', | |
150 | name: 'newName', | |
151 | value: `${name}_clone`, | |
152 | label: $localize`New name`, | |
153 | required: true | |
9f95a23c | 154 | } |
f67539c2 TL |
155 | ], |
156 | titleText: $localize`Clone Role`, | |
157 | submitButtonText: $localize`Clone Role`, | |
158 | onSubmit: (values: object) => { | |
159 | this.roleService.clone(name, values['newName']).subscribe(() => { | |
160 | this.getRoles(); | |
161 | this.notificationService.show( | |
162 | NotificationType.success, | |
163 | $localize`Cloned role '${values['newName']}' from '${name}'` | |
164 | ); | |
165 | }); | |
9f95a23c TL |
166 | } |
167 | }); | |
168 | } | |
11fdf7f2 | 169 | } |