2 * ================================================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
21 * Created by nnaffar on 12/20/15.
25 class NewUserModalCtrl {
26 constructor($scope, $log, usersService, applicationsService, confirmBoxService, items) {
27 var extRequestValue = false;
29 //$log.info('NewUserModalCtrl::init');
30 this.isSaving = false;
31 this.anyChanges = false;
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();
42 this.isShowBack = true;
43 this.selectedUser = null;
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;
55 this.deleteApp = (app) => {
56 let appMessage = this.selectedUser.firstName + ' ' + this.selectedUser.lastName;
57 confirmBoxService.deleteItem(appMessage).then(isConfirmed => {
59 this.anyChanges = true;
61 app.isDeleted = true; // use this to hide the app in the display
62 app.appRoles.forEach(function(role){
63 role.isApplied = false;
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 => {});
73 this.getUserAppsRoles = () => {
74 if (!this.selectedUser || !this.selectedUser.orgUserId) {
75 $log.error('NewUserModalCtrl::getUserAppsRoles error: No user is selected');
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');
88 //$log.debug('NewUserModalCtrl::getUserAppsRoles: then for getAdminAppsSimpler: step 2');
89 //$log.debug('NewUserModalCtrl::getUserAppsRoles: admin apps: ', apps);
90 this.adminApps = apps;
92 this.userAppRoles = {};
93 this.numberAppsProcessed = 0;
94 this.isLoading = true;
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;
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++){
114 if(app.appRoles[i].roleName.indexOf('global_')!=-1){
115 app.appRoles[i].roleName='*'+app.appRoles[i].roleName;
123 app.isLoading = false;
124 app.errorMessage = err.headers('FEErrorString');
125 //$log.debug('NewUserModalCtrl::getUserAppsRoles: in new-user.controller: response header: '+err.headers('FEErrorString'));
127 this.numberAppsProcessed++;
128 if (this.numberAppsProcessed === this.adminApps.length) {
129 this.isLoading = false;
141 * Update the selected user apps with the new roles.
142 * If no roles remain, set the user to inactive.
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');
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 => {
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('*','');
164 var newUserAppRoles = {
165 orgUserId: this.selectedUser.orgUserId,
167 appRoles: app.appRoles,
170 usersService.updateUserAppRoles(newUserAppRoles).promise()
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++;
178 var errorMessage = 'Failed to update the user application roles: ' + err.status;
179 if(err.status == 504){
180 this.numberAppsSucceeded++;
181 errorMessage = 'Request is being processed, please check back later!';
183 app.isErrorUpdating = true;
185 confirmBoxService.showInformation(
187 .then(isConfirmed => {});
189 this.numberAppsProcessed++;
190 if (this.numberAppsProcessed === this.adminApps.length) {
191 this.isSaving = false; // hide the spinner
193 if (this.numberAppsSucceeded === this.adminApps.length) {
194 $scope.$dismiss('cancel');//close and resolve dialog promise with true (to update the table)
198 //$log.debug('NewUserModalCtrl::updateUserAppsRoles: app roles have NOT changed; NOT going to update: id: ', app.id, '; name: ', app.name);
199 app.noChanges = true;
200 app.isError = false; //remove the error message; just show the No Changes messages
201 this.numberAppsProcessed++;
202 this.numberAppsSucceeded++;
203 if (this.numberAppsProcessed === this.adminApps.length) {
204 this.isSaving = false; // hide the spinner
206 if (this.numberAppsSucceeded === this.adminApps.length) {
207 $scope.$dismiss('cancel');//close and resolve dialog promise with true (to update the table)
214 * Navigate between dialog screens using step number: 1,2,...
216 this.navigateBack = () => {
217 if (this.dialogState === 1) {
218 //back from 1st screen?
220 if (this.dialogState === 3) {
221 this.dialogState = 1;
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
234 NewUserModalCtrl.$inject = ['$scope', '$log', 'usersService', 'applicationsService', 'confirmBoxService', 'items'];
235 angular.module('ecompApp').controller('NewUserModalCtrl', NewUserModalCtrl);