[PORTAL-7] Rebase
[portal.git] / ecomp-portal-FE-common / client / app / views / admins / add-admin-dialogs / new-admin.controller.js
1 /*-\r
2  * ================================================================================\r
3  * ECOMP Portal\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ================================================================================\r
19  */\r
20 \r
21 'use strict';\r
22 (function () {\r
23     class NewAdminModalCtrl {\r
24         constructor($log, adminsService, $scope, confirmBoxService, utilsService, $location) {\r
25 \r
26             let init = () => {\r
27                 this.isSaving = false;\r
28                 this.originalApps = [];\r
29                 /* istanbul ignore if */\r
30                 if ($scope.ngDialogData && $scope.ngDialogData.selectedUser && $scope.ngDialogData.dialogState) {\r
31                     this.selectedUser = $scope.ngDialogData.selectedUser;\r
32                     this.dialogState = $scope.ngDialogData.dialogState;\r
33                     this.isShowBack = false;\r
34                     if (this.dialogState === 2) {\r
35                         this.getAdminAppsRoles();\r
36                     }\r
37                 } else {\r
38                     this.isShowBack = true;\r
39                     this.selectedUser = null;\r
40                     this.dialogState = 1;\r
41                 }\r
42 \r
43                 //this.searchUsersInProgress = false;\r
44                 //this.showNewAdminAppDropdown = false;\r
45                 $log.info('NewAdminModalCtrl::initiated');\r
46                 this.appsOrder = [];\r
47             };\r
48 \r
49             let orderList = (apps) => {\r
50                 this.appsOrder = [];\r
51                 for (var i = 0; i < apps.length; i++) {\r
52                     if (apps[i].isAdmin) {\r
53                         this.appsOrder.push(apps[i].id);\r
54                     }\r
55                 }\r
56             };\r
57 \r
58             this.orderFilter = app => {\r
59                 if (!app || !app.id || !this.appsOrder.length) {\r
60                     return;\r
61                 }\r
62                 return this.appsOrder.indexOf(app.id);\r
63             };\r
64 \r
65             /**\r
66              * this function get the selected admin apps roles\r
67              */\r
68             this.getAdminAppsRoles = () => {\r
69                 if (!this.selectedUser || !this.selectedUser.orgUserId) {\r
70                     $log.error('No user is selected / searchUsers is InProgress');\r
71                     this.dialogState = 1;\r
72                     return;\r
73                 }\r
74                 adminsService.getAdminAppsRoles(this.selectedUser.orgUserId).then(roles => {\r
75                     $log.debug('apps roles res: ', JSON.stringify(roles));\r
76                     if (!roles.appsRoles) {\r
77                         return;\r
78                     }\r
79 \r
80                     this.adminAppsRoles = [];\r
81                     for (var i = 0; i < roles.appsRoles.length; i++) {\r
82                         if (!roles.appsRoles[i].restrictedApp) {\r
83                             $log.debug('pushing: {id: ', roles.appsRoles[i].id,\r
84                                 'name: ', roles.appsRoles[i].appName,\r
85                                 'restrictedApp: ', roles.appsRoles[i].restrictedApp,\r
86                                 'isAdmin: ', roles.appsRoles[i].isAdmin, '}');\r
87                             this.adminAppsRoles.push({\r
88                                 id: roles.appsRoles[i].id,\r
89                                 appName: roles.appsRoles[i].appName,\r
90                                 isAdmin: roles.appsRoles[i].isAdmin,\r
91                                 restrictedApp: roles.appsRoles[i].restrictedApp\r
92                             });\r
93                         }\r
94                     }\r
95                     this.dialogState = 2;\r
96                     this.adminAppsRoles = this.adminAppsRoles.sort(getSortOrder("appName"));\r
97 \r
98                     orderList(roles.appsRoles);\r
99                     if (this.appsOrder != null) {\r
100                         for (var j = 0; j < this.appsOrder.length; j++) {\r
101                             this.originalApps.push(this.appsOrder[j]);\r
102                         }\r
103                     }\r
104                 }).catch(err => {\r
105                     $log.error(err);\r
106                 });\r
107             };\r
108 \r
109             // Refactor this into a directive\r
110             let getSortOrder = (prop) => {\r
111                 return function (a, b) {\r
112                     if (a[prop].toLowerCase() > b[prop].toLowerCase()) {\r
113                         return 1;\r
114                     } else if (a[prop].toLowerCase() < b[prop].toLowerCase()) {\r
115                         return -1;\r
116                     }\r
117                     return 0;\r
118                 }\r
119             }\r
120 \r
121             /**\r
122              * this function set the selected user\r
123              * @param user: selected user object\r
124              */\r
125             this.setSelectedUser = (user) => {\r
126                 $log.debug('selected user: ', user);\r
127                 this.selectedUser = user;\r
128             };\r
129 \r
130             /**\r
131              * Mark the user as not admin of the selected app\r
132              * @param app: selected app object\r
133              */\r
134             this.unadminApp = (app) => {\r
135                 confirmBoxService.deleteItem(app.appName).then(confirmed => {\r
136                     if (confirmed === true) {\r
137                         app.isAdmin = false;\r
138                         this.appsOrder.splice(this.appsOrder.indexOf(app.id), 1);\r
139                     }\r
140                 }).catch(err => {\r
141                     $log(err);\r
142                 });\r
143             };\r
144 \r
145             /**\r
146              * update the selected admin app with the new roles\r
147              */\r
148             this.updateAdminAppsRoles = () => {\r
149                 if (!this.selectedUser || !this.selectedUser.orgUserId || !this.adminAppsRoles) {\r
150                     return;\r
151                 }\r
152                 this.isSaving = true;\r
153                 $log.debug('going to update user: ' + this.selectedUser.orgUserId + ' with app roles: ' + JSON.stringify(this.adminAppsRoles));\r
154                 confirmBoxService.makeAdminChanges('Are you sure you want to make these admin changes?')\r
155                     .then(confirmed => {\r
156                         if(confirmed === true){\r
157                         adminsService.updateAdminAppsRoles({\r
158                                 orgUserId: this.selectedUser.orgUserId,\r
159                                 appsRoles: this.adminAppsRoles\r
160                             })\r
161                             .then(res => {\r
162                                 $log.debug('Admin apps roles updated successfully!', res);\r
163                                 //close and resolve dialog promise with true (to update the table)\r
164                                 this.remindToAddUserIfNecessary();\r
165                                 $scope.closeThisDialog(true);\r
166                             }).catch(err => {\r
167                             $log.error('NewAdminModalCtrl.updateAdminAppsRoles:: Failed - ' + err);\r
168                         }).finally(()=> {\r
169                             this.isSaving = false;\r
170                         })\r
171                         }else{\r
172                                 this.isSaving = false;\r
173                         }\r
174                     });\r
175             };\r
176 \r
177             /**\r
178              * Navigate between dialog screens using step number: 1,2,...\r
179              */\r
180             this.navigateBack = () => {\r
181                 if (this.dialogState === 1) {\r
182                     //back from 1st screen?\r
183                 }\r
184                 if (this.dialogState === 2) {\r
185                     this.dialogState = 1;\r
186                 }\r
187             };\r
188 \r
189             init();\r
190 \r
191             /**\r
192              * each time new app is selected in the drop down,\r
193              * add it to the user administrated apps list\r
194              */\r
195             $scope.$watch('newAdmin.selectedNewApp', (newVal) => {\r
196                 if (!newVal || newVal.isAdmin === undefined) {\r
197                     return;\r
198                 }\r
199                 //newVal.isAdmin = true; - track by ruined this, here is the workaround:\r
200                 let app = _.find(this.adminAppsRoles, {id: newVal.id});\r
201                 if (app) {\r
202                     app.isAdmin = true;\r
203                     this.appsOrder.push(app.id);\r
204                 }\r
205                 this.selectedNewApp = null;\r
206                 //this.showNewAdminAppDropdown = false;\r
207             });\r
208 \r
209             $scope.$on('$stateChangeStart', e => {\r
210                 //Disable navigation when modal is opened\r
211                 //**Nabil - note: this will cause the history back state to be replaced with current state\r
212                 e.preventDefault();\r
213             });\r
214 \r
215             /**\r
216              * If an Admin was added for an application remind the portal admin to add the admin as a user\r
217              */\r
218             this.remindToAddUserIfNecessary = () => {\r
219 \r
220                 var adminAddedToNewApp = false;\r
221                 if ((this.originalApps != null) && (this.originalApps.length > 0)) {\r
222                     for (var i = 0; i < this.appsOrder.length; i++) {\r
223                         var foundApp = false;\r
224                         for (var j = 0; j < this.originalApps.length; j++) {\r
225                             if (this.originalApps[j] === this.appsOrder[i]) {\r
226                                 foundApp = true;\r
227                             }\r
228                         }\r
229                         if (foundApp === false) {\r
230                             adminAddedToNewApp = true;\r
231                             break;\r
232                         }\r
233                     }\r
234                 } else {\r
235                     adminAddedToNewApp = true;\r
236                 }\r
237 \r
238                 if (adminAddedToNewApp === true) {\r
239                     confirmBoxService.confirm('Add this person as an application user? This allows them to access the application from ECOMP Portal. Press OK to go to the Add Users page.')\r
240                         .then(confirmed => {\r
241                             if (confirmed === true) {\r
242                                 $location.path('/users');\r
243                             }\r
244                         });\r
245                 }\r
246             }\r
247 \r
248         }\r
249     }\r
250     NewAdminModalCtrl.$inject = ['$log', 'adminsService', '$scope', 'confirmBoxService', 'utilsService', '$location'];\r
251     angular.module('ecompApp').controller('NewAdminModalCtrl', NewAdminModalCtrl);\r
252 })();\r