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