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 * ================================================================================
23 constructor($log, applicationsService, usersService, confirmBoxService, $scope, ngDialog) {
25 $scope.adminAppsIsNull = false;
26 $scope.appsIsDown = false;
27 $scope.noUsersInApp = false;
28 $scope.multiAppAdmin = false;
30 $log.info('UsersCtrl:: initializing...');
32 * Handle all active HTTP requests
33 * activeRequests @type {Array[requests with cancel option]}
35 let activeRequests = [];
36 let clearReq = (req) => {
37 activeRequests.splice(activeRequests.indexOf(req), 1);
41 this.isLoadingTable = false;
42 this.selectedApp = null;
43 this.isAppSelectDisabled = false;
44 this.selectApp = 'Select application';
45 this.adminApps = [{index: 0, id: 0, value: this.selectApp, title: this.selectApp}];
48 /*Table general configuration params*/
49 this.searchString = '';
51 this.usersTableHeaders = ['First Name', 'Last Name', 'User ID', 'Roles'];
52 this.accountUsers = [];
55 let getAdminApps = () => {
56 $log.debug('UsersCtrl::getAdminApps: - Starting getAdminApps');
58 this.isLoadingTable = true;
59 let adminAppsReq = applicationsService.getAdminApps();
60 adminAppsReq.promise().then(apps => {
61 if (!apps || !apps.length) {
62 $log.error('UsersCtrl::getAdminApps: - no apps found');
65 $log.debug('UsersCtrl::getAdminApps: Apps for this user are: ' + JSON.stringify(apps));
66 if (apps.length >= 2) {
67 $log.info('UsersCtrl::getAdminApps: - more than one app for this admin:', apps.length, ' apps');
68 $scope.multiAppAdmin = true;
72 let sortedApps = apps.sort(getSortOrder("name"));
74 for(let i=1; i<=sortedApps.length; i++){
77 id: sortedApps[i - 1].id,
78 value: sortedApps[i - 1].name,
79 title: sortedApps[i - 1].name
81 realAppIndex = realAppIndex + 1;
84 $log.debug('UsersCtrl::getAdminApps: Apps for this user are: ' + JSON.stringify(this.adminApps));
86 this.selectedApp = this.adminApps[0];
87 clearReq(adminAppsReq);
88 $scope.adminAppsIsNull = false;
90 $scope.adminAppsIsNull = true;
91 $log.error('UsersCtrl::getAdminApps: - getAdminApps() failed = '+ e.message);
92 clearReq(adminAppsReq);
93 confirmBoxService.showInformation('There was a problem retrieving the applications. ' +
94 'Please try again later.').then(isConfirmed => {});
97 this.isLoadingTable = false;
100 $scope.adminAppsIsNull = true;
101 $log.error('UsersCtrl::getAdminApps: - getAdminApps() failed!');
102 this.isLoadingTable = false;
106 let getSortOrder = (prop) => {
107 return function(a, b) {
108 if (a[prop] > b[prop]) {
110 } else if (a[prop] < b[prop]) {
117 this.updateUsersList = () => {
118 $scope.appsIsDown = false;
119 $scope.noUsersInApp = false;
120 // $log.debug('UsersCtrl::updateUsersList: Starting updateUsersList');
121 //reset search string
122 this.searchString = '';
123 //should i disable this too in case of moving between tabs?
124 this.isAppSelectDisabled = true;
126 this.isLoadingTable = true;
128 if(this.adminApps!=null && this.selectedApp!=null){
129 var tempSelected = null;
130 for(let i=0; i<=this.adminApps.length; i++){
131 if(typeof this.adminApps[i] != 'undefined' && this.selectedApp.value==this.adminApps[i].value){
132 tempSelected=_.clone(this.adminApps[i]);
135 if(tempSelected!=null){
136 this.selectedApp= tempSelected;
140 if (this.selectedApp.title != this.selectApp) { // 'Select Application'
141 usersService.getAccountUsers(this.selectedApp.id)
142 .then(accountUsers => {
143 $log.debug('UsersCtrl::updateUsersList accountUsers: '+ accountUsers);
144 if (angular.isObject(accountUsers)===false) {
145 $log.error('UsersCtrl::updateUsersList accountUsers: App is down!');
146 $scope.appsIsDown = true;
148 $log.debug('UsersCtrl::updateUsersList length: '+ Object.keys(accountUsers).length);
149 this.isAppSelectDisabled = false;
150 this.accountUsers = accountUsers;
151 if (angular.isObject(accountUsers) && Object.keys(accountUsers).length === 0) {
152 $log.debug('UsersCtrl::updateUsersList accountUsers: App has no users.');
153 $scope.noUsersInApp = true;
156 this.isAppSelectDisabled = false;
157 $log.error('UsersCtrl::updateUsersList error: ' + err);
158 confirmBoxService.showInformation('There was a problem updating the users List. ' +
159 'Please try again later.').then(isConfirmed => {});
160 $scope.appsIsDown = true;
162 this.isLoadingTable = false;
163 $scope.noAppSelected = false;
166 // this.selectedApp = this.adminApps[0];
167 this.isAppSelectDisabled = false;
168 this.isLoadingTable = false;
169 $scope.noUsersInApp = false;
170 $scope.noAppSelected = true;
175 this.openAddNewUserModal = (user) => {
181 orgUserId: user.orgUserId,
182 firstName: user.firstName,
183 lastName: user.lastName,
189 templateUrl: 'app/views/users/new-user-dialogs/new-user.modal.html',
190 controller: 'NewUserModalCtrl',
191 controllerAs: 'newUser',
193 }).closePromise.then(needUpdate => {
194 if (needUpdate.value === true) {
195 $log.debug('UsersCtrl::openAddNewUserModal updating table data...');
196 this.updateUsersList();
201 this.openBulkUserUploadModal = (adminApps) => {
206 selectedApplication: {
207 appid: adminApps[0].appid,
208 appName: adminApps[0].appName
213 templateUrl: 'app/views/users/new-user-dialogs/bulk-user.modal.html',
214 controller: 'BulkUserModalCtrl',
215 controllerAs: 'bulkUser',
217 }).closePromise.then(needUpdate => {
218 this.updateUsersList();
223 $scope.$watch('users.selectedApp.value', (newVal, oldVal) => {
224 if (!newVal || _.isEqual(newVal, oldVal)) {
227 $log.debug('UsersCtrl::openAddNewUserModal:$watch selectedApp -> Fire with: ', newVal);
228 this.accountUsers = []; //reset table and show swirl here
229 this.updateUsersList();
232 $scope.$on('$destroy', () => {
233 //cancel all active requests when closing the modal
234 activeRequests.forEach(req => {
242 UsersCtrl.$inject = ['$log', 'applicationsService', 'usersService', 'confirmBoxService', '$scope', 'ngDialog'];
243 angular.module('ecompApp').controller('UsersCtrl', UsersCtrl);