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 * bulk user upload controller
43 class BulkUserModalCtrl {
44 constructor($scope, $log, $filter, $q, usersService, applicationsService, confirmBoxService, functionalMenuService, ngDialog,$modal) {
46 // Set to true for copious console output
48 // Roles fetched from app service
49 var appRolesResult = [];
50 // Users fetched from user service
51 var userCheckResult = [];
52 // Requests for user-role assignment built by validator
53 var appUserRolesRequest = [];
57 $log.debug('BulkUserModalCtrl::init');
58 // Angular insists on this.
59 $scope.fileModel = {};
60 // Model for drop-down
61 $scope.adminApps = [];
62 // Enable modal controls
64 this.fileSelected = false;
66 // Flag that indicates background work is proceeding
67 $scope.isProcessing = true;
69 // Load user's admin applications
70 applicationsService.getAdminApps().promise().then(apps => {
72 $log.debug('BulkUserModalCtrl::init: getAdminApps returned' + JSON.stringify(apps));
73 if (!apps || typeof(apps) != 'object') {
74 $log.error('BulkUserModalCtrl::init: getAdminApps returned unexpected data');
78 $log.debug('BulkUserModalCtrl::init: admin apps length is ', apps.length);
80 // Sort app names and populate the drop-down model
81 let sortedApps = apps.sort(getSortOrder('name', true));
82 for (let i = 0; i < sortedApps.length; ++i) {
83 $scope.adminApps.push({
86 value: sortedApps[i].name,
87 title: sortedApps[i].name
90 // Pick the first one in the list
91 $scope.selectedApplication = $scope.adminApps[0];
93 $scope.isProcessing = false;
95 $log.error('BulkUserModalCtrl::init: getAdminApps threw', err);
96 $scope.isProcessing = false;
101 // Answers a function that compares properties with the specified name.
102 let getSortOrder = (prop, foldCase) => {
103 return function(a, b) {
104 let aProp = foldCase ? a[prop].toLowerCase() : a[prop];
105 let bProp = foldCase ? b[prop].toLowerCase() : b[prop];
108 else if (aProp < bProp)
115 //This is a fix for dropdown selection, due to b2b dropdown only update value field
116 $scope.$watch('selectedApplication.value', (newVal, oldVal) => {
117 for(var i=0;i<$scope.adminApps.length;i++){
118 if($scope.adminApps[i].value==newVal){
119 $scope.selectedApplication=angular.copy($scope.adminApps[i]);;
124 // Invoked when user picks an app on the drop-down.
125 $scope.appSelected = () => {
127 $log.debug('BulkUserModalCtrl::appSelected: selectedApplication.id is ' + $scope.selectedApplication.id);
128 this.appSelected = true;
131 // Caches the file name supplied by the event handler.
132 $scope.fileChangeHandler = (event, files) => {
133 this.fileSelected = true;
134 this.fileToRead = files[0];
136 $log.debug("BulkUserModalCtrl::fileChangeHandler: file is ", this.fileToRead);
137 }; // file change handler
140 * Reads the contents of the file, calls portal endpoints
141 * to validate roles, userIds and existing role assignments;
142 * ultimately builds array of requests to be sent.
143 * Creates scope variable with input file contents for
144 * communication with functions.
146 * This function performs a synchronous step-by-step process
147 * using asynchronous promises. The code could all be inline
148 * here but the nesting becomes unwieldy.
150 $scope.readValidateFile = () => {
151 $scope.isProcessing = true;
152 $scope.progressMsg = 'Reading upload file..';
153 var reader = new FileReader();
154 reader.onload = function(event) {
155 $scope.uploadFile = $filter('csvToObj')(reader.result);
157 $log.debug('BulkUserModalCtrl::readValidateFile onload: data length is ' + $scope.uploadFile.length);
158 // sort input by orgUserId
159 $scope.uploadFile.sort(getSortOrder('orgUserId', true));
161 let appid = $scope.selectedApplication.id;
162 $scope.progressMsg = 'Fetching application roles..';
163 functionalMenuService.getManagedRolesMenu(appid).then(function (rolesObj) {
165 $log.debug("BulkUserModalCtrl::readValidateFile: managedRolesMenu returned " + JSON.stringify(rolesObj));
166 appRolesResult = rolesObj;
167 $scope.progressMsg = 'Validating application roles..';
168 $scope.verifyRoles();
170 let userPromises = $scope.buildUserChecks();
172 $log.debug('BulkUserModalCtrl::readValidateFile: userPromises length is ' + userPromises.length);
173 $scope.progressMsg = 'Validating Org Users..';
174 $q.all(userPromises).then(function() {
176 $log.debug('BulkUserModalCtrl::readValidateFile: userCheckResult length is ' + userCheckResult.length);
177 $scope.evalUserCheckResults();
179 let appPromises = $scope.buildAppRoleChecks();
181 $log.debug('BulkUserModalCtrl::readValidateFile: appPromises length is ' + appPromises.length);
182 $scope.progressMsg = 'Querying application for user roles..';
183 $q.all(appPromises).then( function() {
185 $log.debug('BulkUserModalCtrl::readValidateFile: appUserRolesRequest length is ' + appUserRolesRequest.length);
186 $scope.evalAppRoleCheckResults();
188 // Re sort by line for the confirmation dialog
189 $scope.uploadFile.sort(getSortOrder('line', false));
190 // We're done, confirm box may show the table
192 $log.debug('BulkUserModalCtrl::readValidateFile inner-then ends');
193 $scope.progressMsg = 'Done.';
194 $scope.isProcessing = false;
197 $log.error('BulkUserModalCtrl::readValidateFile: failed retrieving user-app roles');
198 $scope.isProcessing = false;
200 ); // then of app promises
203 $log.error('BulkUserModalCtrl::readValidateFile: failed retrieving user info');
204 $scope.isProcessing = false;
206 ); // then of user promises
209 $log.error('BulkUserModalCtrl::readValidateFile: failed retrieving app role info');
210 $scope.isProcessing = false;
212 ); // then of role promise
216 // Invoke the reader on the selected file
217 reader.readAsText(this.fileToRead);
221 * Evaluates the result set returned by the app role service.
222 * Sets an uploadFile array element status if a role is not defined.
223 * Reads and writes scope variable uploadFile.
224 * Reads closure variable appRolesResult.
226 $scope.verifyRoles = () => {
228 $log.debug('BulkUserModalCtrl::verifyRoles: appRoles is ' + JSON.stringify(appRolesResult));
229 // check roles in upload file against defined app roles
230 $scope.uploadFile.forEach( function (uploadRow) {
231 // skip rows that already have a defined status: headers etc.
232 if (uploadRow.status) {
234 $log.debug('BulkUserModalCtrl::verifyRoles: skip row ' + uploadRow.line);
237 uploadRow.role = uploadRow.role.trim();
239 for (var i=0; i < appRolesResult.length; i++) {
240 if (uploadRow.role.toUpperCase() === appRolesResult[i].rolename.trim().toUpperCase()) {
242 $log.debug('BulkUserModalCtrl::verifyRoles: match on role ' + uploadRow.role);
249 $log.debug('BulkUserModalCtrl::verifyRoles: NO match on role ' + uploadRow.role);
250 uploadRow.status = 'Invalid role';
256 * Builds and returns an array of promises to invoke the
257 * searchUsers service for each unique Org User UID in the input.
258 * Reads and writes scope variable uploadFile, which must be sorted by Org User UID.
259 * The promise function writes to closure variable userCheckResult
261 $scope.buildUserChecks = () => {
263 $log.debug('BulkUserModalCtrl::buildUserChecks: uploadFile length is ' + $scope.uploadFile.length);
264 userCheckResult = [];
267 $scope.uploadFile.forEach(function (uploadRow) {
268 if (uploadRow.status) {
270 $log.debug('BulkUserModalCtrl::buildUserChecks: skip row ' + uploadRow.line);
273 // detect repeated UIDs
274 if (prevRow == null || prevRow.orgUserId.toLowerCase() !== uploadRow.orgUserId.toLowerCase()) {
276 $log.debug('BulkUserModalCtrl::buildUserChecks: create request for orgUserId ' + uploadRow.orgUserId);
277 let userPromise = usersService.searchUsers(uploadRow.orgUserId).promise().then( (usersList) => {
278 if (typeof usersList[0] !== "undefined") {
279 userCheckResult.push({
280 orgUserId: usersList[0].orgUserId,
281 firstName: usersList[0].firstName,
282 lastName: usersList[0].lastName,
283 jobTitle: usersList[0].jobTitle
289 $log.debug('BulkUserModalCtrl::buildUserChecks: searchUsers returned null');
292 $log.error('BulkUserModalCtrl::buildUserChecks: searchUsers failed ' + JSON.stringify(error));
294 promises.push(userPromise);
298 $log.debug('BulkUserModalCtrl::buildUserChecks: skip repeated orgUserId ' + uploadRow.orgUserId);
303 }; // buildUserChecks
306 * Evaluates the result set returned by the user service to set
307 * the uploadFile array element status if the user was not found.
308 * Reads and writes scope variable uploadFile.
309 * Reads closure variable userCheckResult.
311 $scope.evalUserCheckResults = () => {
313 $log.debug('BulkUserModalCtrl::evalUserCheckResult: uploadFile length is ' + $scope.uploadFile.length);
314 $scope.uploadFile.forEach(function (uploadRow) {
315 if (uploadRow.status) {
317 $log.debug('BulkUserModalCtrl::evalUserCheckResults: skip row ' + uploadRow.line);
320 let foundorgUserId = false;
321 userCheckResult.forEach(function(userItem) {
322 if (uploadRow.orgUserId.toLowerCase() === userItem.orgUserId.toLowerCase()) {
324 $log.debug('BulkUserModalCtrl::evalUserCheckResults: found orgUserId ' + uploadRow.orgUserId);
328 if (!foundorgUserId) {
330 $log.debug('BulkUserModalCtrl::evalUserCheckResults: NO match on orgUserId ' + uploadRow.orgUserId);
331 uploadRow.status = 'Invalid orgUserId';
334 }; // evalUserCheckResults
337 * Builds and returns an array of promises to invoke the getUserAppRoles
338 * service for each unique Org User in the input file.
339 * Each promise creates an update to be sent to the remote application
340 * with all role names.
341 * Reads scope variable uploadFile, which must be sorted by Org User.
342 * The promise function writes to closure variable appUserRolesRequest
344 $scope.buildAppRoleChecks = () => {
346 $log.debug('BulkUserModalCtrl::buildAppRoleChecks: uploadFile length is ' + $scope.uploadFile.length);
347 appUserRolesRequest = [];
348 let appId = $scope.selectedApplication.id;
351 $scope.uploadFile.forEach( function (uploadRow) {
352 if (uploadRow.status) {
354 $log.debug('BulkUserModalCtrl::buildAppRoleChecks: skip row ' + uploadRow.line);
357 // Because the input is sorted, generate only one request for each Org User
358 if (prevRow == null || prevRow.orgUserId.toLowerCase() !== uploadRow.orgUserId.toLowerCase()) {
360 $log.debug('BulkUserModalCtrl::buildAppRoleChecks: create request for orgUserId ' + uploadRow.orgUserId);
361 let appPromise = usersService.getUserAppRoles(appId, uploadRow.orgUserId,true).promise().then( (userAppRolesResult) => {
362 // Reply for unknown user has all defined roles with isApplied=false on each.
363 if (typeof userAppRolesResult[0] !== "undefined") {
365 $log.debug('BulkUserModalCtrl::buildAppRoleChecks: adding result '
366 + JSON.stringify(userAppRolesResult));
367 appUserRolesRequest.push({
368 orgUserId: uploadRow.orgUserId,
369 userAppRoles: userAppRolesResult
372 $log.error('BulkUserModalCtrl::buildAppRoleChecks: getUserAppRoles returned ' + JSON.stringify(userAppRolesResult));
375 $log.error('BulkUserModalCtrl::buildAppRoleChecks: getUserAppRoles failed ', error);
377 promises.push(appPromise);
380 $log.debug('BulkUserModalCtrl::buildAppRoleChecks: duplicate orgUserId, skip: '+ uploadRow.orgUserId);
385 }; // buildAppRoleChecks
388 * Evaluates the result set returned by the app service and adjusts
389 * the list of updates to be sent to the remote application by setting
390 * isApplied=true for each role name found in the upload file.
391 * Reads and writes scope variable uploadFile.
392 * Reads closure variable appUserRolesRequest.
394 $scope.evalAppRoleCheckResults = () => {
396 $log.debug('BulkUserModalCtrl::evalAppRoleCheckResults: uploadFile length is ' + $scope.uploadFile.length);
397 $scope.uploadFile.forEach(function (uploadRow) {
398 if (uploadRow.status) {
400 $log.debug('BulkUserModalCtrl::evalAppRoleCheckResults: skip row ' + uploadRow.line);
403 // Search for the match in the app-user-roles array
404 appUserRolesRequest.forEach( function (appUserRoleObj) {
405 if (uploadRow.orgUserId.toLowerCase() === appUserRoleObj.orgUserId.toLowerCase()) {
407 $log.debug('BulkUserModalCtrl::evalAppRoleCheckResults: match on orgUserId ' + uploadRow.orgUserId);
408 let roles = appUserRoleObj.userAppRoles;
409 roles.forEach(function (appRoleItem) {
411 // $log.debug('BulkUserModalCtrl::evalAppRoleCheckResults: checking uploadRow.role='
412 // + uploadRow.role + ', appRoleItem.roleName= ' + appRoleItem.roleName);
413 if (uploadRow.role === appRoleItem.roleName) {
414 if (appRoleItem.isApplied) {
416 $log.debug('BulkUserModalCtrl::evalAppRoleCheckResults: existing role '
417 + appRoleItem.roleName);
418 uploadRow.status = 'Role exists';
422 $log.debug('BulkUserModalCtrl::evalAppRoleCheckResults: new role '
423 + appRoleItem.roleName);
424 // After much back-and-forth I decided a clear indicator
425 // is better than blank in the table status column.
426 uploadRow.status = 'OK';
427 appRoleItem.isApplied = true;
429 // This count is not especially interesting.
430 // numberUserRolesSucceeded++;
434 }); // for each result
436 }; // evalAppRoleCheckResults
439 * Sends requests to Portal requesting user role assignment.
440 * That endpoint handles creation of the user at the remote app if necessary.
441 * Reads closure variable appUserRolesRequest.
442 * Invoked by the Next button on the confirmation dialog.
444 $scope.updateDB = () => {
445 $scope.isProcessing = true;
446 $scope.progressMsg = 'Sending requests to application..';
448 $log.debug('BulkUserModalCtrl::updateDB: request length is ' + appUserRolesRequest.length);
449 var numberUsersSucceeded = 0;
451 appUserRolesRequest.forEach(function(appUserRoleObj) {
453 $log.debug('BulkUserModalCtrl::updateDB: appUserRoleObj is ' + JSON.stringify(appUserRoleObj));
454 let updateRequest = {
455 orgUserId: appUserRoleObj.orgUserId,
456 appId: $scope.selectedApplication.id,
457 appRoles: appUserRoleObj.userAppRoles
460 $log.debug('BulkUserModalCtrl::updateDB: updateRequest is ' + JSON.stringify(updateRequest));
461 let updatePromise = usersService.updateUserAppRoles(updateRequest).promise().then(res => {
463 $log.debug('BulkUserModalCtrl::updateDB: updated successfully: ' + JSON.stringify(res));
464 numberUsersSucceeded++;
466 // What to do if one of many fails??
467 $log.error('BulkUserModalCtrl::updateDB failed: ', err);
468 confirmBoxService.showInformation(
469 'Failed to update the user application roles. ' +
470 'Error: ' + err.status).then(isConfirmed => { });
472 // $log.debug('BulkUserModalCtrl::updateDB: finally()');
474 promises.push(updatePromise);
477 // Run all the promises
478 $q.all(promises).then(function(){
479 $scope.isProcessing = false;
480 confirmBoxService.showInformation('Processed ' + numberUsersSucceeded + ' users.').then(isConfirmed => {
481 // Close the upload-confirm dialog
487 // Sets the variable that hides/reveals the user controls
488 $scope.step2 = () => {
489 this.fileSelected = false;
490 $scope.selectedFile = null;
491 $scope.fileModel = null;
495 // Navigate between dialog screens using step number: 1,2,...
496 $scope.navigateBack = () => {
498 this.fileSelected = false;
501 // Opens a dialog to show the data to be uploaded.
502 // Invoked by the upload button on the bulk user dialog.
503 $scope.confirmUpload = () => {
505 $scope.readValidateFile();
506 // Dialog shows progress
508 templateUrl: 'app/views/users/new-user-dialogs/bulk-user.confirm.html',
510 sizeClass: 'modal-medium',
516 // Invoked by the Cancel button on the confirmation dialog.
517 $scope.cancelUpload = () => {
524 BulkUserModalCtrl.$inject = ['$scope', '$log', '$filter', '$q', 'usersService', 'applicationsService', 'confirmBoxService', 'functionalMenuService', 'ngDialog','$modal'];
525 angular.module('ecompApp').controller('BulkUserModalCtrl', BulkUserModalCtrl);
527 angular.module('ecompApp').directive('fileChange', ['$parse', function($parse){
531 link : function($scope, element, attrs, ngModel) {
532 var attrHandler = $parse(attrs['fileChange']);
533 var handler=function(e) {
534 $scope.$apply(function() {
535 attrHandler($scope, { $event:e, files:e.target.files } );
536 $scope.selectedFile = e.target.files[0].name;
539 element[0].addEventListener('change',handler,false);
544 angular.module('ecompApp').filter('csvToObj',function() {
545 return function(input) {
548 var lines = input.split('\n');
549 // Need 1-based index below
550 for (len = lines.length, i = 1; i <= len; ++i) {
551 // Use 0-based index for array
552 line = lines[i - 1].trim();
553 if (line.length == 0) {
554 // console.log("Skipping blank line");
564 if (o.length !== 2) {
565 // other lengths not valid for upload
570 status: 'Failed to find 2 comma-separated values'
574 // console.log("Valid line: ", val);
579 // leave status undefined, this could be valid.
581 if (o[0].toLowerCase() === 'orgUserId') {
582 // not valid for upload, so set status
583 entry.status = 'Header';
585 else if (o[0].trim() == '' || o[1].trim() == '') {
586 // defend against line with only a single comma etc.
587 entry.status = 'Failed to find 2 non-empty values';