2 * ================================================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ================================================================================
\r
23 constructor($log, applicationsService, usersService, confirmBoxService, $scope, ngDialog) {
\r
25 $scope.adminAppsIsNull = false;
\r
26 $scope.appsIsDown = false;
\r
27 $scope.noUsersInApp = false;
\r
28 $scope.multiAppAdmin = false;
\r
30 $log.info('UsersCtrl:: initializing...');
\r
32 * Handle all active HTTP requests
\r
33 * activeRequests @type {Array[requests with cancel option]}
\r
35 let activeRequests = [];
\r
36 let clearReq = (req) => {
\r
37 activeRequests.splice(activeRequests.indexOf(req), 1);
\r
41 this.isLoadingTable = false;
\r
42 this.selectedApp = null;
\r
43 this.isAppSelectDisabled = false;
\r
44 this.selectApp = 'Select application';
\r
45 this.adminApps = [{index: 0, id: 0, value: this.selectApp, title: this.selectApp}];
\r
48 /*Table general configuration params*/
\r
49 this.searchString = '';
\r
51 this.usersTableHeaders = ['First Name', 'Last Name', 'User ID', 'Roles'];
\r
52 this.accountUsers = [];
\r
55 let getAdminApps = () => {
\r
56 $log.debug('UsersCtrl::getAdminApps: - Starting getAdminApps');
\r
58 this.isLoadingTable = true;
\r
59 let adminAppsReq = applicationsService.getAdminApps();
\r
60 adminAppsReq.promise().then(apps => {
\r
61 if (!apps || !apps.length) {
\r
62 $log.error('UsersCtrl::getAdminApps: - no apps found');
\r
65 $log.debug('UsersCtrl::getAdminApps: Apps for this user are: ' + JSON.stringify(apps));
\r
66 if (apps.length >= 2) {
\r
67 $log.info('UsersCtrl::getAdminApps: - more than one app for this admin:', apps.length, ' apps');
\r
68 $scope.multiAppAdmin = true;
\r
70 this.adminApps = [] ;
\r
72 let sortedApps = apps.sort(getSortOrder("name"));
\r
73 let realAppIndex = 1;
\r
74 for(let i=1; i<=sortedApps.length; i++){
\r
75 this.adminApps.push({
\r
76 index: realAppIndex,
\r
77 id: sortedApps[i - 1].id,
\r
78 value: sortedApps[i - 1].name,
\r
79 title: sortedApps[i - 1].name
\r
81 realAppIndex = realAppIndex + 1;
\r
84 $log.debug('UsersCtrl::getAdminApps: Apps for this user are: ' + JSON.stringify(this.adminApps));
\r
86 this.selectedApp = this.adminApps[0];
\r
87 clearReq(adminAppsReq);
\r
88 $scope.adminAppsIsNull = false;
\r
90 $scope.adminAppsIsNull = true;
\r
91 $log.error('UsersCtrl::getAdminApps: - getAdminApps() failed = '+ e.message);
\r
92 clearReq(adminAppsReq);
\r
93 confirmBoxService.showInformation('There was a problem retrieving the applications. ' +
\r
94 'Please try again later.').then(isConfirmed => {});
\r
97 this.isLoadingTable = false;
\r
100 $scope.adminAppsIsNull = true;
\r
101 $log.error('UsersCtrl::getAdminApps: - getAdminApps() failed!');
\r
102 this.isLoadingTable = false;
\r
106 let getSortOrder = (prop) => {
\r
107 return function(a, b) {
\r
108 if (a[prop] > b[prop]) {
\r
110 } else if (a[prop] < b[prop]) {
\r
117 this.updateUsersList = () => {
\r
118 $scope.appsIsDown = false;
\r
119 $scope.noUsersInApp = false;
\r
120 // $log.debug('UsersCtrl::updateUsersList: Starting updateUsersList');
\r
121 //reset search string
\r
122 this.searchString = '';
\r
123 //should i disable this too in case of moving between tabs?
\r
124 this.isAppSelectDisabled = true;
\r
126 this.isLoadingTable = true;
\r
128 if(this.adminApps!=null && this.selectedApp!=null){
\r
129 var tempSelected = null;
\r
130 for(let i=0; i<=this.adminApps.length; i++){
\r
131 if(typeof this.adminApps[i] != 'undefined' && this.selectedApp.value==this.adminApps[i].value){
\r
132 tempSelected=_.clone(this.adminApps[i]);
\r
135 if(tempSelected!=null){
\r
136 this.selectedApp= tempSelected;
\r
140 if (this.selectedApp.title != this.selectApp) { // 'Select Application'
\r
141 usersService.getAccountUsers(this.selectedApp.id)
\r
142 .then(accountUsers => {
\r
143 $log.debug('UsersCtrl::updateUsersList accountUsers: '+ accountUsers);
\r
144 if (angular.isObject(accountUsers)===false) {
\r
145 $log.error('UsersCtrl::updateUsersList accountUsers: App is down!');
\r
146 $scope.appsIsDown = true;
\r
148 $log.debug('UsersCtrl::updateUsersList length: '+ Object.keys(accountUsers).length);
\r
149 this.isAppSelectDisabled = false;
\r
150 this.accountUsers = accountUsers;
\r
151 if (angular.isObject(accountUsers) && Object.keys(accountUsers).length === 0) {
\r
152 $log.debug('UsersCtrl::updateUsersList accountUsers: App has no users.');
\r
153 $scope.noUsersInApp = true;
\r
156 this.isAppSelectDisabled = false;
\r
157 $log.error('UsersCtrl::updateUsersList error: ' + err);
\r
158 confirmBoxService.showInformation('There was a problem updating the users List. ' +
\r
159 'Please try again later.').then(isConfirmed => {});
\r
160 $scope.appsIsDown = true;
\r
162 this.isLoadingTable = false;
\r
163 $scope.noAppSelected = false;
\r
166 // this.selectedApp = this.adminApps[0];
\r
167 this.isAppSelectDisabled = false;
\r
168 this.isLoadingTable = false;
\r
169 $scope.noUsersInApp = false;
\r
170 $scope.noAppSelected = true;
\r
175 this.openAddNewUserModal = (user) => {
\r
181 orgUserId: user.orgUserId,
\r
182 firstName: user.firstName,
\r
183 lastName: user.lastName
\r
188 templateUrl: 'app/views/users/new-user-dialogs/new-user.modal.html',
\r
189 controller: 'NewUserModalCtrl',
\r
190 controllerAs: 'newUser',
\r
192 }).closePromise.then(needUpdate => {
\r
193 if (needUpdate.value === true) {
\r
194 $log.debug('UsersCtrl::openAddNewUserModal updating table data...');
\r
195 this.updateUsersList();
\r
200 this.openBulkUserUploadModal = (adminApps) => {
\r
205 selectedApplication: {
\r
206 appid: adminApps[0].appid,
\r
207 appName: adminApps[0].appName
\r
212 templateUrl: 'app/views/users/new-user-dialogs/bulk-user.modal.html',
\r
213 controller: 'BulkUserModalCtrl',
\r
214 controllerAs: 'bulkUser',
\r
216 }).closePromise.then(needUpdate => {
\r
217 this.updateUsersList();
\r
222 $scope.$watch('users.selectedApp.value', (newVal, oldVal) => {
\r
223 if (!newVal || _.isEqual(newVal, oldVal)) {
\r
226 $log.debug('UsersCtrl::openAddNewUserModal:$watch selectedApp -> Fire with: ', newVal);
\r
227 this.accountUsers = []; //reset table and show swirl here
\r
228 this.updateUsersList();
\r
231 $scope.$on('$destroy', () => {
\r
232 //cancel all active requests when closing the modal
\r
233 activeRequests.forEach(req => {
\r
241 UsersCtrl.$inject = ['$log', 'applicationsService', 'usersService', 'confirmBoxService', '$scope', 'ngDialog'];
\r
242 angular.module('ecompApp').controller('UsersCtrl', UsersCtrl);
\r