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;
46 var isSystemUser = false;
48 $scope.ngRepeatDemo = [
49 {id: 'userButton', value: 'true', labelvalue: 'User'},
50 {id: 'systemUserButton', value: 'false', labelvalue: 'System'}
53 $scope.selectedvalueradioButtonGroup = {
59 //$log.info('NewUserModalCtrl::init');
60 this.isSaving = false;
61 this.anyChanges = false;
63 this.isGettingAdminApps = false;
64 if(items && items.selectedUser && items.dialogState){
65 this.selectedUser = items.selectedUser;
66 this.dialogState = items.dialogState;
67 this.isShowBack = false;
68 if(this.dialogState === 3){
69 this.getUserAppsRoles();
72 this.isShowBack = true;
73 this.selectedUser = null;
78 this.appChanged = (index) => {
79 let myApp = this.adminApps[index];
80 //$log.debug('NewUserModalCtrl::appChanged: index: ', index, '; app id: ', myApp.id, 'app name: ',myApp.name);
81 myApp.isChanged = true;
82 this.anyChanges = true;
85 this.deleteApp = (app) => {
86 let appMessage = this.selectedUser.firstName + ' ' + this.selectedUser.lastName;
87 confirmBoxService.deleteItem(appMessage).then(isConfirmed => {
89 this.anyChanges = true;
91 app.isDeleted = true; // use this to hide the app in the display
92 app.appRoles.forEach(function(role){
93 role.isApplied = false;
97 $log.error('NewUserModalCtrl::deleteApp error: ',err);
98 confirmBoxService.showInformation('There was a problem deleting the the applications. ' +
99 'Please try again later. Error: ' + err.status).then(isConfirmed => {});
103 this.getUserAppsRoles = () => {
104 if (!this.selectedUser || !this.selectedUser.orgUserId) {
105 $log.error('NewUserModalCtrl::getUserAppsRoles error: No user is selected');
106 this.dialogState = 1;
109 $log.debug('NewUserModalCtrl::getUserAppsRoles: about to call getAdminAppsSimpler');
110 this.isGettingAdminApps = true;
111 applicationsService.getAdminAppsSimpler().then((apps) => {
112 //$log.debug('NewUserModalCtrl::getUserAppsRoles: beginning of then for getAdminAppsSimpler');
113 this.isGettingAdminApps = false;
114 if (!apps || !apps.length) {
115 $log.error('NewUserModalCtrl::getUserAppsRoles error: no admin apps found');
118 //$log.debug('NewUserModalCtrl::getUserAppsRoles: then for getAdminAppsSimpler: step 2');
119 //$log.debug('NewUserModalCtrl::getUserAppsRoles: admin apps: ', apps);
120 this.adminApps = apps;
121 this.dialogState = 3;
122 this.userAppRoles = {};
123 this.numberAppsProcessed = 0;
124 this.isLoading = true;
125 apps.forEach(app => {
126 //$log.debug('NewUserModalCtrl::getUserAppsRoles: app: id: ', app.id, 'name: ',app.name);
127 // Keep track of which app has changed, so we know which apps to update using a BE API
128 app.isChanged = false;
129 // Each of these specifies a state, which corresponds to a different message and style that gets displayed
130 app.isLoading = true;
132 app.isDeleted = false;
133 app.printNoChanges = false;
134 app.isUpdating = false;
135 app.isErrorUpdating = false;
136 app.isDoneUpdating = false;
137 app.errorMessage = "";
138 if($scope.selectedvalueradioButtonGroup.type == 'false')
142 usersService.getUserAppRoles(app.id, this.selectedUser.orgUserId, extRequestValue,isSystemUser).promise().then((userAppRolesResult) => {
143 //$log.debug('NewUserModalCtrl::getUserAppsRoles: got a result for app: ',app.id,': ',app.name,': ',userAppRolesResult);
144 app.appRoles = userAppRolesResult;
145 app.isLoading = false;
146 for(var i=0;i<app.appRoles.length;i++){
148 if(app.appRoles[i].roleName.indexOf('global_')!=-1){
149 app.appRoles[i].roleName='*'+app.appRoles[i].roleName;
157 app.isLoading = false;
158 app.errorMessage = err.headers('FEErrorString');
159 //$log.debug('NewUserModalCtrl::getUserAppsRoles: in new-user.controller: response header: '+err.headers('FEErrorString'));
161 this.numberAppsProcessed++;
162 if (this.numberAppsProcessed === this.adminApps.length) {
163 this.isLoading = false;
175 * Update the selected user apps with the new roles.
176 * If no roles remain, set the user to inactive.
178 this.updateUserAppsRoles = () => {
179 // $log.debug('NewUserModalCtrl::updateUserAppsRoles: entering updateUserAppsRoles');
180 if(!this.selectedUser || !this.selectedUser.orgUserId || !this.adminApps){
181 $log.error('NewUserModalCtrl::updateUserAppsRoles: mmissing arguments');
184 this.isSaving = true;
185 //$log.debug('NewUserModalCtrl::updateUserAppsRoles: going to update user: ' + this.selectedUser.orgUserId);
186 this.numberAppsProcessed = 0;
187 this.numberAppsSucceeded = 0;
188 this.adminApps.forEach(app => {
190 //$log.debug('NewUserModalCtrl::updateUserAppsRoles: app roles have changed; going to update: id: ', app.id, '; name: ', app.name);
191 app.isUpdating = true;
192 for(var i=0;i<app.appRoles.length;i++){
193 if(app.appRoles[i].roleName.indexOf('*global_')!=-1){
194 app.appRoles[i].roleName=app.appRoles[i].roleName.replace('*','');
198 if($scope.selectedvalueradioButtonGroup.type == 'false')
202 isSystemUser = false;
204 var newUserAppRoles = {
205 orgUserId: this.selectedUser.orgUserId,
207 appRoles: app.appRoles,
209 isSystemUser : isSystemUser
211 usersService.updateUserAppRoles(newUserAppRoles).promise()
213 //$log.debug('NewUserModalCtrl::updateUserAppsRoles: User app roles updated successfully on app: ',app.id);
214 app.isUpdating = false;
215 app.isDoneUpdating = true;
216 this.numberAppsSucceeded++;
219 var errorMessage = 'Failed to update the user application roles: ' + err;
220 if(err.status == 504){
221 this.numberAppsSucceeded++;
222 errorMessage = 'Request is being processed, please check back later!';
224 app.isErrorUpdating = true;
226 confirmBoxService.showInformation(
228 .then(isConfirmed => {});
230 this.numberAppsProcessed++;
231 if (this.numberAppsProcessed === this.adminApps.length) {
232 this.isSaving = false; // hide the spinner
234 if (this.numberAppsSucceeded === this.adminApps.length) {
235 $scope.$dismiss('cancel');//close and resolve dialog promise with true (to update the table)
239 //$log.debug('NewUserModalCtrl::updateUserAppsRoles: app roles have NOT changed; NOT going to update: id: ', app.id, '; name: ', app.name);
240 app.noChanges = true;
241 app.isError = false; //remove the error message; just show the No Changes messages
242 this.numberAppsProcessed++;
243 this.numberAppsSucceeded++;
244 if (this.numberAppsProcessed === this.adminApps.length) {
245 this.isSaving = false; // hide the spinner
247 if (this.numberAppsSucceeded === this.adminApps.length) {
248 $scope.$dismiss('cancel');//close and resolve dialog promise with true (to update the table)
255 * Navigate between dialog screens using step number: 1,2,...
257 this.navigateBack = () => {
258 if (this.dialogState === 1) {
259 //back from 1st screen?
261 if (this.dialogState === 3) {
262 this.dialogState = 1;
268 $scope.$on('$stateChangeStart', e => {
269 //Disable navigation when modal is opened
270 //**Nabil - note: this will cause the history back state to be replaced with current state
275 NewUserModalCtrl.$inject = ['$scope', '$log', 'usersService', 'applicationsService', 'confirmBoxService', 'items'];
276 angular.module('ecompApp').controller('NewUserModalCtrl', NewUserModalCtrl);