added svcapi ui and camunda code
[it/otf.git] / otf-frontend / client / src / app / layout / manage-group / manage-group-roles / manage-group-roles.component.ts
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\r
3 #   Licensed under the Apache License, Version 2.0 (the "License");            #\r
4 #   you may not use this file except in compliance with the License.           #\r
5 #   You may obtain a copy of the License at                                    #\r
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\r
9 #   Unless required by applicable law or agreed to in writing, software        #\r
10 #   distributed under the License is distributed on an "AS IS" BASIS,          #\r
11 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
12 #   See the License for the specific language governing permissions and        #\r
13 #   limitations under the License.                                             #\r
14 ##############################################################################*/\r
15 \r
16 \r
17 import { Component, OnInit } from '@angular/core';\r
18 import { routerLeftTransition } from 'app/router.animations';\r
19 import { ActivatedRoute } from '@angular/router';\r
20 import { GroupService } from 'app/shared/services/group.service';\r
21 import { CookieService } from 'ngx-cookie-service';\r
22 import { MatTableDataSource } from '@angular/material/table';\r
23 import { MatDialog } from '@angular/material';\r
24 import {AlertModalComponent} from '../../../shared/modules/alert-modal/alert-modal.component';\r
25 import { NEXT } from '@angular/core/src/render3/interfaces/view';\r
26 import { convertPropertyBindingBuiltins } from '@angular/compiler/src/compiler_util/expression_converter';\r
27 import { extractDirectiveDef } from '@angular/core/src/render3/definition';\r
28 import { take } from 'rxjs/operators';\r
29 \r
30 \r
31 \r
32 \r
33 \r
34 @Component({\r
35   selector: 'app-manage-group-roles',\r
36   templateUrl: './manage-group-roles.component.pug',\r
37   styleUrls: ['./manage-group-roles.component.scss'],\r
38   animations: [routerLeftTransition()]\r
39 })\r
40 \r
41 export class ManageGroupRolesComponent implements OnInit {\r
42 \r
43 \r
44   public selectedGroup;\r
45   public groupRoles;\r
46   public roleName = null;\r
47   public loading = false;\r
48   public groupPermissions;\r
49   public element = {};\r
50   public groupName;\r
51 \r
52   public dataSource;\r
53   public displayedColumns;\r
54   public readPermission = false;\r
55   public writePermission = false;\r
56   public executePermission = false;\r
57   public deletePermission = false;\r
58   public managementPermission = false;\r
59 \r
60   \r
61 \r
62 \r
63 \r
64   constructor(\r
65     public _groups: GroupService,\r
66     private cookie: CookieService,\r
67     private modal: MatDialog,\r
68     private ResponseMatDialog: MatDialog\r
69     \r
70   ) { \r
71 \r
72     \r
73   }\r
74 \r
75   ngOnInit() {\r
76 \r
77     this.setComponentData(this._groups.getGroup());\r
78     this._groups.groupChange().subscribe(group => {\r
79       this.setComponentData(group);\r
80 \r
81     });\r
82 \r
83 \r
84   }\r
85 \r
86   setComponentData(group) {\r
87     if(!group){\r
88       return;\r
89     }\r
90     this.groupName = group.groupName;\r
91     this.loading = true;\r
92     this.dataSource = new MatTableDataSource();\r
93     this._groups.find({ \r
94       $limit: -1,\r
95       _id: group['_id'], \r
96       $select: ['_id', 'roles']\r
97     }).subscribe((res) => {\r
98 \r
99       this.selectedGroup = res[0];\r
100       this.groupRoles = res[0].roles;\r
101 \r
102       //If current group does not have any roles\r
103       if ( (this.groupRoles == null) || (this.groupRoles.length < 1))\r
104       {\r
105         this.groupRoles = [\r
106           {roleName: "admin", permissions: "read, write, execute, delete, management"},\r
107           {roleName: "user", permissions: "read"},\r
108           {roleName: "developer", permissions: "read, write, execute, delete"}\r
109         ];\r
110         \r
111       }\r
112 \r
113 \r
114       for (let i = 0; i < this.groupRoles.length; i++){\r
115         this.groupRoles[i].readPermission = false;\r
116         this.groupRoles[i].writePermission = false;\r
117         this.groupRoles[i].executePermission = false;\r
118         this.groupRoles[i].deletePermission = false;\r
119         this.groupRoles[i].managementPermission = false;\r
120         if (this.groupRoles[i].permissions.includes('read')){\r
121           this.groupRoles[i].readPermission = true;\r
122         }\r
123         if (this.groupRoles[i].permissions.includes('write')){\r
124           this.groupRoles[i].writePermission = true;\r
125         }\r
126         if (this.groupRoles[i].permissions.includes('execute')){\r
127           this.groupRoles[i].executePermission = true;\r
128         }\r
129         if (this.groupRoles[i].permissions.includes('delete')){\r
130           this.groupRoles[i].deletePermission = true;\r
131         }\r
132         if (this.groupRoles[i].permissions.includes('management')){\r
133           this.groupRoles[i].managementPermission = true;\r
134         }\r
135       }\r
136      \r
137       this.dataSource.data = this.groupRoles;\r
138       this.loading = false;\r
139       this.update();\r
140 \r
141       \r
142 \r
143 \r
144     })\r
145 \r
146     this.displayedColumns = ['roleName', 'read', 'write', 'execute', 'delete', 'management', 'actions']\r
147 \r
148   }\r
149 \r
150 \r
151   async create(){\r
152     \r
153     for (let i = 0; i < this.groupRoles.length; i++){\r
154       if (this.groupRoles[i].roleName == this.roleName){\r
155         this.sendFeedbackAlert('warning', 'Please do not add a duplicate role name.');\r
156         return;\r
157       }\r
158     }\r
159   \r
160       this.groupRoles.push({roleName: this.roleName, readPermission: true, writePermission: false, executePermission: false, deletePermission: false, managementPermission: false});\r
161       await this.update();\r
162       this.setComponentData(this._groups.getGroup());\r
163   \r
164 \r
165   }\r
166 \r
167   async update(){\r
168   \r
169     \r
170     for (let i = 0; i < this.groupRoles.length; i++) {\r
171       this.groupRoles[i].permissions = [];\r
172       if(this.groupRoles[i].readPermission){\r
173         this.groupRoles[i].permissions.push('read');\r
174       }\r
175       if(this.groupRoles[i].writePermission){\r
176         this.groupRoles[i].permissions.push('write');\r
177       }\r
178       if(this.groupRoles[i].executePermission){\r
179         this.groupRoles[i].permissions.push('execute');\r
180       }\r
181       if(this.groupRoles[i].deletePermission){\r
182         this.groupRoles[i].permissions.push('delete');\r
183       }\r
184       if(this.groupRoles[i].managementPermission){\r
185         this.groupRoles[i].permissions.push('management');\r
186       }\r
187 \r
188     }\r
189       \r
190     this.groupPermissions = this.groupRoles.map(({ roleName, permissions }) => ({roleName, permissions}));\r
191    \r
192     let groupPatch = {\r
193       '_id': this.selectedGroup._id,\r
194       'roles': this.groupPermissions\r
195     };\r
196     //console.log(groupPatch);\r
197     await this._groups.patch(groupPatch).pipe(take(1)).toPromise();\r
198 \r
199       \r
200 \r
201   }\r
202 \r
203   async deleteRole(element){\r
204 \r
205     for (let i = 0; i < this.groupRoles.length; i++){\r
206         if (this.groupRoles[i].roleName == element.roleName){\r
207           this.groupRoles.splice(i, 1);\r
208           break;\r
209         }\r
210     }\r
211     await this.update();\r
212     this.setComponentData(this._groups.getGroup());\r
213 \r
214 \r
215 \r
216 \r
217   }\r
218 \r
219   public sendFeedbackAlert(type: string, message: string) {\r
220     this.ResponseMatDialog.open(AlertModalComponent, {\r
221         width: '250px',\r
222         data: {\r
223             type: type,\r
224             message: message\r
225         }\r
226     });\r
227   }\r
228 \r
229 \r
230 \r
231 \r
232 \r
233 \r
234 \r
235 \r
236  \r
237 \r
238 \r
239 \r
240 \r
241 \r
242 \r
243 }\r