Deliver centralized role management feature
[portal.git] / ecomp-portal-FE-common / client / app / views / users / new-user-dialogs / new-user.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  * Created by nnaffar on 12/20/15.
22  */
23 'use strict';
24 (function () {
25     class NewUserModalCtrl {
26         constructor($scope, $log, usersService, applicationsService, confirmBoxService, items) {
27             var extRequestValue = false;
28             let init = () => {
29                 //$log.info('NewUserModalCtrl::init');
30                 this.isSaving = false;
31                 this.anyChanges = false;
32                 this.adminApps = [];
33                 this.isGettingAdminApps = false;
34                 if(items && items.selectedUser && items.dialogState){
35                     this.selectedUser = items.selectedUser;
36                     this.dialogState = items.dialogState;
37                     this.isShowBack = false;
38                     if(this.dialogState === 3){
39                         this.getUserAppsRoles();
40                     }
41                 }else{
42                     this.isShowBack = true;
43                     this.selectedUser = null;
44                     this.dialogState = 1;
45                 }
46             };
47
48             this.appChanged = (index) => {
49                 let myApp = this.adminApps[index];
50                 //$log.debug('NewUserModalCtrl::appChanged: index: ', index, '; app id: ', myApp.id, 'app name: ',myApp.name);
51                 myApp.isChanged = true;
52                 this.anyChanges = true;
53             }
54
55             this.deleteApp = (app) => {
56                 let appMessage = this.selectedUser.firstName + ' ' + this.selectedUser.lastName;
57                 confirmBoxService.deleteItem(appMessage).then(isConfirmed => {
58                     if(isConfirmed){
59                         this.anyChanges = true;
60                         app.isChanged = true;
61                         app.isDeleted = true; // use this to hide the app in the display
62                         app.appRoles.forEach(function(role){
63                             role.isApplied = false;
64                         });
65                     }
66                 }).catch(err => {
67                     $log.error('NewUserModalCtrl::deleteApp error: ',err);
68                     confirmBoxService.showInformation('There was a problem deleting the the applications. ' +
69                         'Please try again later. Error: ' + err.status).then(isConfirmed => {});
70                 });
71             };
72
73             this.getUserAppsRoles = () => {
74                 if (!this.selectedUser || !this.selectedUser.orgUserId) {
75                     $log.error('NewUserModalCtrl::getUserAppsRoles error: No user is selected');
76                     this.dialogState = 1;
77                     return;
78                 }
79                 //$log.debug('NewUserModalCtrl::getUserAppsRoles: about to call getAdminAppsSimpler');
80                 this.isGettingAdminApps = true;
81                 applicationsService.getAdminAppsSimpler().then((apps) => {
82                     //$log.debug('NewUserModalCtrl::getUserAppsRoles: beginning of then for getAdminAppsSimpler');
83                     this.isGettingAdminApps = false;
84                     if (!apps || !apps.length) {
85                         $log.error('NewUserModalCtrl::getUserAppsRoles error: no admin apps found');
86                         return null;
87                     }
88                     //$log.debug('NewUserModalCtrl::getUserAppsRoles: then for getAdminAppsSimpler: step 2');
89                     //$log.debug('NewUserModalCtrl::getUserAppsRoles: admin apps: ', apps);
90                     this.adminApps = apps;
91                     this.dialogState = 3;
92                     this.userAppRoles = {};
93                     this.numberAppsProcessed = 0;
94                     this.isLoading = true;
95                     apps.forEach(app => {
96                         //$log.debug('NewUserModalCtrl::getUserAppsRoles: app: id: ', app.id, 'name: ',app.name);
97                         // Keep track of which app has changed, so we know which apps to update using a BE API
98                         app.isChanged = false;
99                         // Each of these specifies a state, which corresponds to a different message and style that gets displayed
100                         app.isLoading = true;
101                         app.isError = false;
102                         app.isDeleted = false;
103                         app.printNoChanges = false;
104                         app.isUpdating = false;
105                         app.isErrorUpdating = false;
106                         app.isDoneUpdating = false;
107                         app.errorMessage = "";
108                         usersService.getUserAppRoles(app.id, this.selectedUser.orgUserId, extRequestValue).promise().then((userAppRolesResult) => {
109                             //$log.debug('NewUserModalCtrl::getUserAppsRoles: got a result for app: ',app.id,': ',app.name,': ',userAppRolesResult);
110                             app.appRoles = userAppRolesResult;
111                             app.isLoading = false;
112                             for(var i=0;i<app.appRoles.length;i++){
113                                 
114                                 if(app.appRoles[i].roleName.indexOf('global_')!=-1){
115                                         app.appRoles[i].roleName='*'+app.appRoles[i].roleName;
116                                                         
117                                                 }
118                                 }
119
120                         }).catch(err => {
121                             $log.error(err);
122                             app.isError = true;
123                             app.isLoading = false;
124                             app.errorMessage = err.headers('FEErrorString');
125                             //$log.debug('NewUserModalCtrl::getUserAppsRoles: in new-user.controller: response header: '+err.headers('FEErrorString'));
126                         }).finally(()=>{
127                             this.numberAppsProcessed++;
128                             if (this.numberAppsProcessed === this.adminApps.length) {
129                                 this.isLoading = false;
130                             }
131                         });
132                     })
133                     return;
134                 }).catch(err => {
135                     $log.error(err);
136                 })
137
138             }
139
140             /**
141              * Update the selected user apps with the new roles.
142              * If no roles remain, set the user to inactive.
143              */
144             this.updateUserAppsRoles = () => {
145                 // $log.debug('NewUserModalCtrl::updateUserAppsRoles: entering updateUserAppsRoles');
146                 if(!this.selectedUser || !this.selectedUser.orgUserId || !this.adminApps){
147                     $log.error('NewUserModalCtrl::updateUserAppsRoles: mmissing arguments');
148                     return;
149                 }
150                 this.isSaving = true;
151                 //$log.debug('NewUserModalCtrl::updateUserAppsRoles: going to update user: ' + this.selectedUser.orgUserId);
152                 this.numberAppsProcessed = 0;
153                 this.numberAppsSucceeded = 0;
154                 this.adminApps.forEach(app => {
155                     if (app.isChanged) {
156                         //$log.debug('NewUserModalCtrl::updateUserAppsRoles: app roles have changed; going to update: id: ', app.id, '; name: ', app.name);
157                         app.isUpdating = true;
158                         for(var i=0;i<app.appRoles.length;i++){
159                           if(app.appRoles[i].roleName.indexOf('*global_')!=-1){
160                                         app.appRoles[i].roleName=app.appRoles[i].roleName.replace('*','');
161                                                         
162                                                 }
163                             }
164                         var newUserAppRoles = {
165                                 orgUserId: this.selectedUser.orgUserId,
166                                 appId: app.id, 
167                                 appRoles: app.appRoles,
168                                 appName: app.name
169                         }; 
170                         usersService.updateUserAppRoles(newUserAppRoles).promise()
171                         .then(res => {
172                             //$log.debug('NewUserModalCtrl::updateUserAppsRoles: User app roles updated successfully on app: ',app.id);
173                             app.isUpdating = false;
174                             app.isDoneUpdating = true;
175                             this.numberAppsSucceeded++;
176                         }).catch(err => {
177                             $log.error(err);
178                             app.isErrorUpdating = true;
179                             confirmBoxService.showInformation(
180                                         'Failed to update the user application roles: ' + err.status)
181                                         .then(isConfirmed => {});
182                         }).finally(()=>{
183                             this.numberAppsProcessed++;
184                             if (this.numberAppsProcessed === this.adminApps.length) {
185                                 this.isSaving = false; // hide the spinner
186                             }
187                             if (this.numberAppsSucceeded === this.adminApps.length) {
188                                 $scope.$dismiss('cancel');//close and resolve dialog promise with true (to update the table)
189                             }
190                         })
191                     } else {
192                         //$log.debug('NewUserModalCtrl::updateUserAppsRoles: app roles have NOT changed; NOT going to update: id: ', app.id, '; name: ', app.name);
193                         app.noChanges = true;
194                         app.isError = false; //remove the error message; just show the No Changes messages
195                         this.numberAppsProcessed++;
196                         this.numberAppsSucceeded++;
197                         if (this.numberAppsProcessed === this.adminApps.length) {
198                             this.isSaving = false; // hide the spinner
199                         }
200                         if (this.numberAppsSucceeded === this.adminApps.length) {
201                                 $scope.$dismiss('cancel');//close and resolve dialog promise with true (to update the table)
202                         }
203                     }
204                 });
205             };
206
207             /**
208              * Navigate between dialog screens using step number: 1,2,...
209              */
210             this.navigateBack = () => {
211                 if (this.dialogState === 1) {
212                     //back from 1st screen?
213                 }
214                 if (this.dialogState === 3) {
215                     this.dialogState = 1;
216                 }
217             };
218
219             init();
220
221             $scope.$on('$stateChangeStart', e => {
222                 //Disable navigation when modal is opened
223                 //**Nabil - note: this will cause the history back state to be replaced with current state
224                 e.preventDefault();
225             });
226         }
227     }
228     NewUserModalCtrl.$inject = ['$scope', '$log', 'usersService', 'applicationsService', 'confirmBoxService', 'items'];
229     angular.module('ecompApp').controller('NewUserModalCtrl', NewUserModalCtrl);
230 })();