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