2 * ================================================================================
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
21 * Created by nnaffar on 12/20/15.
\r
25 class NewUserModalCtrl {
\r
26 constructor($scope, $log, usersService, applicationsService, confirmBoxService) {
\r
28 //$log.info('NewUserModalCtrl::init');
\r
29 this.isSaving = false;
\r
30 this.anyChanges = false;
\r
31 this.adminApps = [];
\r
32 this.isGettingAdminApps = false;
\r
33 if($scope.ngDialogData && $scope.ngDialogData.selectedUser && $scope.ngDialogData.dialogState){
\r
34 this.selectedUser = $scope.ngDialogData.selectedUser;
\r
35 this.dialogState = $scope.ngDialogData.dialogState;
\r
36 this.isShowBack = false;
\r
37 if(this.dialogState === 3){
\r
38 this.getUserAppsRoles();
\r
41 this.isShowBack = true;
\r
42 this.selectedUser = null;
\r
43 this.dialogState = 1;
\r
47 this.appChanged = (index) => {
\r
48 let myApp = this.adminApps[index];
\r
49 //$log.debug('NewUserModalCtrl::appChanged: index: ', index, '; app id: ', myApp.id, 'app name: ',myApp.name);
\r
50 myApp.isChanged = true;
\r
51 this.anyChanges = true;
\r
54 this.deleteApp = (app) => {
\r
55 let appMessage = this.selectedUser.firstName + ' ' + this.selectedUser.lastName;
\r
56 confirmBoxService.deleteItem(appMessage).then(isConfirmed => {
\r
58 this.anyChanges = true;
\r
59 app.isChanged = true;
\r
60 app.isDeleted = true; // use this to hide the app in the display
\r
61 app.appRoles.forEach(function(role){
\r
62 role.isApplied = false;
\r
66 $log.error('NewUserModalCtrl::deleteApp error: ',err);
\r
67 confirmBoxService.showInformation('There was a problem deleting the the applications. ' +
\r
68 'Please try again later. Error: ' + err.status).then(isConfirmed => {});
\r
72 this.getUserAppsRoles = () => {
\r
73 if (!this.selectedUser || !this.selectedUser.orgUserId) {
\r
74 $log.error('NewUserModalCtrl::getUserAppsRoles error: No user is selected');
\r
75 this.dialogState = 1;
\r
78 //$log.debug('NewUserModalCtrl::getUserAppsRoles: about to call getAdminAppsSimpler');
\r
79 this.isGettingAdminApps = true;
\r
80 applicationsService.getAdminAppsSimpler().then((apps) => {
\r
81 //$log.debug('NewUserModalCtrl::getUserAppsRoles: beginning of then for getAdminAppsSimpler');
\r
82 this.isGettingAdminApps = false;
\r
83 if (!apps || !apps.length) {
\r
84 $log.error('NewUserModalCtrl::getUserAppsRoles error: no admin apps found');
\r
87 //$log.debug('NewUserModalCtrl::getUserAppsRoles: then for getAdminAppsSimpler: step 2');
\r
88 //$log.debug('NewUserModalCtrl::getUserAppsRoles: admin apps: ', apps);
\r
89 this.adminApps = apps;
\r
90 this.dialogState = 3;
\r
91 this.userAppRoles = {};
\r
92 this.numberAppsProcessed = 0;
\r
93 this.isLoading = true;
\r
94 apps.forEach(app => {
\r
95 //$log.debug('NewUserModalCtrl::getUserAppsRoles: app: id: ', app.id, 'name: ',app.name);
\r
96 // Keep track of which app has changed, so we know which apps to update using a BE API
\r
97 app.isChanged = false;
\r
98 // Each of these specifies a state, which corresponds to a different message and style that gets displayed
\r
99 app.isLoading = true;
\r
100 app.isError = false;
\r
101 app.isDeleted = false;
\r
102 app.printNoChanges = false;
\r
103 app.isUpdating = false;
\r
104 app.isErrorUpdating = false;
\r
105 app.isDoneUpdating = false;
\r
106 app.errorMessage = "";
\r
107 usersService.getUserAppRoles(app.id, this.selectedUser.orgUserId).promise().then((userAppRolesResult) => {
\r
108 //$log.debug('NewUserModalCtrl::getUserAppsRoles: got a result for app: ',app.id,': ',app.name,': ',userAppRolesResult);
\r
109 app.appRoles = userAppRolesResult;
\r
110 app.isLoading = false;
\r
114 app.isError = true;
\r
115 app.isLoading = false;
\r
116 app.errorMessage = err.headers('FEErrorString');
\r
117 //$log.debug('NewUserModalCtrl::getUserAppsRoles: in new-user.controller: response header: '+err.headers('FEErrorString'));
\r
119 this.numberAppsProcessed++;
\r
120 if (this.numberAppsProcessed === this.adminApps.length) {
\r
121 this.isLoading = false;
\r
133 * Update the selected user apps with the new roles.
\r
134 * If no roles remain, set the user to inactive.
\r
136 this.updateUserAppsRoles = () => {
\r
137 // $log.debug('NewUserModalCtrl::updateUserAppsRoles: entering updateUserAppsRoles');
\r
138 if(!this.selectedUser || !this.selectedUser.orgUserId || !this.adminApps){
\r
139 $log.error('NewUserModalCtrl::updateUserAppsRoles: mmissing arguments');
\r
142 this.isSaving = true;
\r
143 //$log.debug('NewUserModalCtrl::updateUserAppsRoles: going to update user: ' + this.selectedUser.orgUserId);
\r
144 this.numberAppsProcessed = 0;
\r
145 this.numberAppsSucceeded = 0;
\r
146 this.adminApps.forEach(app => {
\r
147 if (app.isChanged) {
\r
148 //$log.debug('NewUserModalCtrl::updateUserAppsRoles: app roles have changed; going to update: id: ', app.id, '; name: ', app.name);
\r
149 app.isUpdating = true;
\r
150 var newUserAppRoles = {
\r
151 orgUserId: this.selectedUser.orgUserId,
\r
153 appRoles: app.appRoles,
\r
156 usersService.updateUserAppRoles(newUserAppRoles).promise()
\r
158 //$log.debug('NewUserModalCtrl::updateUserAppsRoles: User app roles updated successfully on app: ',app.id);
\r
159 app.isUpdating = false;
\r
160 app.isDoneUpdating = true;
\r
161 this.numberAppsSucceeded++;
\r
164 app.isErrorUpdating = true;
\r
165 confirmBoxService.showInformation(
\r
166 'Failed to update the user application roles: ' + err.status)
\r
167 .then(isConfirmed => {});
\r
169 this.numberAppsProcessed++;
\r
170 if (this.numberAppsProcessed === this.adminApps.length) {
\r
171 this.isSaving = false; // hide the spinner
\r
173 if (this.numberAppsSucceeded === this.adminApps.length) {
\r
174 $scope.closeThisDialog(true);//close and resolve dialog promise with true (to update the table)
\r
178 //$log.debug('NewUserModalCtrl::updateUserAppsRoles: app roles have NOT changed; NOT going to update: id: ', app.id, '; name: ', app.name);
\r
179 app.noChanges = true;
\r
180 app.isError = false; //remove the error message; just show the No Changes messages
\r
181 this.numberAppsProcessed++;
\r
182 this.numberAppsSucceeded++;
\r
183 if (this.numberAppsProcessed === this.adminApps.length) {
\r
184 this.isSaving = false; // hide the spinner
\r
186 if (this.numberAppsSucceeded === this.adminApps.length) {
\r
187 $scope.closeThisDialog(true);//close and resolve dialog promise with true (to update the table)
\r
194 * Navigate between dialog screens using step number: 1,2,...
\r
196 this.navigateBack = () => {
\r
197 if (this.dialogState === 1) {
\r
198 //back from 1st screen?
\r
200 if (this.dialogState === 3) {
\r
201 this.dialogState = 1;
\r
207 $scope.$on('$stateChangeStart', e => {
\r
208 //Disable navigation when modal is opened
\r
209 //**Nabil - note: this will cause the history back state to be replaced with current state
\r
210 e.preventDefault();
\r
214 NewUserModalCtrl.$inject = ['$scope', '$log', 'usersService', 'applicationsService', 'confirmBoxService'];
\r
215 angular.module('ecompApp').controller('NewUserModalCtrl', NewUserModalCtrl);
\r