2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
39 * Created by nnaffar on 12/20/15.
43 class NewUserModalCtrl {
44 constructor($scope, $log, usersService, applicationsService, confirmBoxService, items) {
45 var extRequestValue = false;
47 //$log.info('NewUserModalCtrl::init');
48 this.isSaving = false;
49 this.anyChanges = false;
51 this.isGettingAdminApps = false;
52 if(items && items.selectedUser && items.dialogState){
53 this.selectedUser = items.selectedUser;
54 this.dialogState = items.dialogState;
55 this.isShowBack = false;
56 if(this.dialogState === 3){
57 this.getUserAppsRoles();
60 this.isShowBack = true;
61 this.selectedUser = null;
66 this.appChanged = (index) => {
67 let myApp = this.adminApps[index];
68 //$log.debug('NewUserModalCtrl::appChanged: index: ', index, '; app id: ', myApp.id, 'app name: ',myApp.name);
69 myApp.isChanged = true;
70 this.anyChanges = true;
73 this.deleteApp = (app) => {
74 let appMessage = this.selectedUser.firstName + ' ' + this.selectedUser.lastName;
75 confirmBoxService.deleteItem(appMessage).then(isConfirmed => {
77 this.anyChanges = true;
79 app.isDeleted = true; // use this to hide the app in the display
80 app.appRoles.forEach(function(role){
81 role.isApplied = false;
85 $log.error('NewUserModalCtrl::deleteApp error: ',err);
86 confirmBoxService.showInformation('There was a problem deleting the the applications. ' +
87 'Please try again later. Error: ' + err.status).then(isConfirmed => {});
91 this.getUserAppsRoles = () => {
92 if (!this.selectedUser || !this.selectedUser.orgUserId) {
93 $log.error('NewUserModalCtrl::getUserAppsRoles error: No user is selected');
97 //$log.debug('NewUserModalCtrl::getUserAppsRoles: about to call getAdminAppsSimpler');
98 this.isGettingAdminApps = true;
99 applicationsService.getAdminAppsSimpler().then((apps) => {
100 //$log.debug('NewUserModalCtrl::getUserAppsRoles: beginning of then for getAdminAppsSimpler');
101 this.isGettingAdminApps = false;
102 if (!apps || !apps.length) {
103 $log.error('NewUserModalCtrl::getUserAppsRoles error: no admin apps found');
106 //$log.debug('NewUserModalCtrl::getUserAppsRoles: then for getAdminAppsSimpler: step 2');
107 //$log.debug('NewUserModalCtrl::getUserAppsRoles: admin apps: ', apps);
108 this.adminApps = apps;
109 this.dialogState = 3;
110 this.userAppRoles = {};
111 this.numberAppsProcessed = 0;
112 this.isLoading = true;
113 apps.forEach(app => {
114 //$log.debug('NewUserModalCtrl::getUserAppsRoles: app: id: ', app.id, 'name: ',app.name);
115 // Keep track of which app has changed, so we know which apps to update using a BE API
116 app.isChanged = false;
117 // Each of these specifies a state, which corresponds to a different message and style that gets displayed
118 app.isLoading = true;
120 app.isDeleted = false;
121 app.printNoChanges = false;
122 app.isUpdating = false;
123 app.isErrorUpdating = false;
124 app.isDoneUpdating = false;
125 app.errorMessage = "";
126 usersService.getUserAppRoles(app.id, this.selectedUser.orgUserId, extRequestValue).promise().then((userAppRolesResult) => {
127 //$log.debug('NewUserModalCtrl::getUserAppsRoles: got a result for app: ',app.id,': ',app.name,': ',userAppRolesResult);
128 app.appRoles = userAppRolesResult;
129 app.isLoading = false;
130 for(var i=0;i<app.appRoles.length;i++){
132 if(app.appRoles[i].roleName.indexOf('global_')!=-1){
133 app.appRoles[i].roleName='*'+app.appRoles[i].roleName;
141 app.isLoading = false;
142 app.errorMessage = err.headers('FEErrorString');
143 //$log.debug('NewUserModalCtrl::getUserAppsRoles: in new-user.controller: response header: '+err.headers('FEErrorString'));
145 this.numberAppsProcessed++;
146 if (this.numberAppsProcessed === this.adminApps.length) {
147 this.isLoading = false;
159 * Update the selected user apps with the new roles.
160 * If no roles remain, set the user to inactive.
162 this.updateUserAppsRoles = () => {
163 // $log.debug('NewUserModalCtrl::updateUserAppsRoles: entering updateUserAppsRoles');
164 if(!this.selectedUser || !this.selectedUser.orgUserId || !this.adminApps){
165 $log.error('NewUserModalCtrl::updateUserAppsRoles: mmissing arguments');
168 this.isSaving = true;
169 //$log.debug('NewUserModalCtrl::updateUserAppsRoles: going to update user: ' + this.selectedUser.orgUserId);
170 this.numberAppsProcessed = 0;
171 this.numberAppsSucceeded = 0;
172 this.adminApps.forEach(app => {
174 //$log.debug('NewUserModalCtrl::updateUserAppsRoles: app roles have changed; going to update: id: ', app.id, '; name: ', app.name);
175 app.isUpdating = true;
176 for(var i=0;i<app.appRoles.length;i++){
177 if(app.appRoles[i].roleName.indexOf('*global_')!=-1){
178 app.appRoles[i].roleName=app.appRoles[i].roleName.replace('*','');
182 var newUserAppRoles = {
183 orgUserId: this.selectedUser.orgUserId,
185 appRoles: app.appRoles,
188 usersService.updateUserAppRoles(newUserAppRoles).promise()
190 //$log.debug('NewUserModalCtrl::updateUserAppsRoles: User app roles updated successfully on app: ',app.id);
191 app.isUpdating = false;
192 app.isDoneUpdating = true;
193 this.numberAppsSucceeded++;
196 var errorMessage = 'Failed to update the user application roles: ' + err;
197 if(err.status == 504){
198 this.numberAppsSucceeded++;
199 errorMessage = 'Request is being processed, please check back later!';
201 app.isErrorUpdating = true;
203 confirmBoxService.showInformation(
205 .then(isConfirmed => {});
207 this.numberAppsProcessed++;
208 if (this.numberAppsProcessed === this.adminApps.length) {
209 this.isSaving = false; // hide the spinner
211 if (this.numberAppsSucceeded === this.adminApps.length) {
212 $scope.$dismiss('cancel');//close and resolve dialog promise with true (to update the table)
216 //$log.debug('NewUserModalCtrl::updateUserAppsRoles: app roles have NOT changed; NOT going to update: id: ', app.id, '; name: ', app.name);
217 app.noChanges = true;
218 app.isError = false; //remove the error message; just show the No Changes messages
219 this.numberAppsProcessed++;
220 this.numberAppsSucceeded++;
221 if (this.numberAppsProcessed === this.adminApps.length) {
222 this.isSaving = false; // hide the spinner
224 if (this.numberAppsSucceeded === this.adminApps.length) {
225 $scope.$dismiss('cancel');//close and resolve dialog promise with true (to update the table)
232 * Navigate between dialog screens using step number: 1,2,...
234 this.navigateBack = () => {
235 if (this.dialogState === 1) {
236 //back from 1st screen?
238 if (this.dialogState === 3) {
239 this.dialogState = 1;
245 $scope.$on('$stateChangeStart', e => {
246 //Disable navigation when modal is opened
247 //**Nabil - note: this will cause the history back state to be replaced with current state
252 NewUserModalCtrl.$inject = ['$scope', '$log', 'usersService', 'applicationsService', 'confirmBoxService', 'items'];
253 angular.module('ecompApp').controller('NewUserModalCtrl', NewUserModalCtrl);