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 class NewAdminModalCtrl {
24 constructor($log, adminsService, $scope, confirmBoxService, utilsService, $location) {
27 this.isSaving = false;
28 this.originalApps = [];
29 /* istanbul ignore if */
30 if ($scope.ngDialogData && $scope.ngDialogData.selectedUser && $scope.ngDialogData.dialogState) {
31 this.selectedUser = $scope.ngDialogData.selectedUser;
32 this.dialogState = $scope.ngDialogData.dialogState;
33 this.isShowBack = false;
34 if (this.dialogState === 2) {
35 this.getAdminAppsRoles();
38 this.isShowBack = true;
39 this.selectedUser = null;
43 //this.searchUsersInProgress = false;
44 //this.showNewAdminAppDropdown = false;
45 $log.info('NewAdminModalCtrl::initiated');
49 let orderList = (apps) => {
51 for (var i = 0; i < apps.length; i++) {
52 if (apps[i].isAdmin) {
53 this.appsOrder.push(apps[i].id);
58 this.orderFilter = app => {
59 if (!app || !app.id || !this.appsOrder.length) {
62 return this.appsOrder.indexOf(app.id);
66 * this function get the selected admin apps roles
68 this.getAdminAppsRoles = () => {
69 if (!this.selectedUser || !this.selectedUser.orgUserId) {
70 $log.error('No user is selected / searchUsers is InProgress');
74 adminsService.getAdminAppsRoles(this.selectedUser.orgUserId).then(roles => {
75 $log.debug('apps roles res: ', JSON.stringify(roles));
76 if (!roles.appsRoles) {
80 this.adminAppsRoles = [];
81 for (var i = 0; i < roles.appsRoles.length; i++) {
82 if (!roles.appsRoles[i].restrictedApp) {
83 $log.debug('pushing: {id: ', roles.appsRoles[i].id,
84 'name: ', roles.appsRoles[i].appName,
85 'restrictedApp: ', roles.appsRoles[i].restrictedApp,
86 'isAdmin: ', roles.appsRoles[i].isAdmin, '}');
87 this.adminAppsRoles.push({
88 id: roles.appsRoles[i].id,
89 appName: roles.appsRoles[i].appName,
90 isAdmin: roles.appsRoles[i].isAdmin,
91 restrictedApp: roles.appsRoles[i].restrictedApp
96 this.adminAppsRoles = this.adminAppsRoles.sort(getSortOrder("appName"));
98 orderList(roles.appsRoles);
99 if (this.appsOrder != null) {
100 for (var j = 0; j < this.appsOrder.length; j++) {
101 this.originalApps.push(this.appsOrder[j]);
109 // Refactor this into a directive
110 let getSortOrder = (prop) => {
111 return function (a, b) {
112 if (a[prop].toLowerCase() > b[prop].toLowerCase()) {
114 } else if (a[prop].toLowerCase() < b[prop].toLowerCase()) {
122 * this function set the selected user
123 * @param user: selected user object
125 this.setSelectedUser = (user) => {
126 $log.debug('selected user: ', user);
127 this.selectedUser = user;
131 * Mark the user as not admin of the selected app
132 * @param app: selected app object
134 this.unadminApp = (app) => {
135 confirmBoxService.deleteItem(app.appName).then(confirmed => {
136 if (confirmed === true) {
138 this.appsOrder.splice(this.appsOrder.indexOf(app.id), 1);
146 * update the selected admin app with the new roles
148 this.updateAdminAppsRoles = () => {
149 if (!this.selectedUser || !this.selectedUser.orgUserId || !this.adminAppsRoles) {
152 this.isSaving = true;
153 $log.debug('going to update user: ' + this.selectedUser.orgUserId + ' with app roles: ' + JSON.stringify(this.adminAppsRoles));
154 confirmBoxService.makeAdminChanges('Are you sure you want to make these admin changes?')
156 if(confirmed === true){
157 adminsService.updateAdminAppsRoles({
158 orgUserId: this.selectedUser.orgUserId,
159 appsRoles: this.adminAppsRoles
162 $log.debug('Admin apps roles updated successfully!', res);
163 //close and resolve dialog promise with true (to update the table)
164 this.remindToAddUserIfNecessary();
165 $scope.closeThisDialog(true);
167 $log.error('NewAdminModalCtrl.updateAdminAppsRoles:: Failed - ' + err);
169 this.isSaving = false;
172 this.isSaving = false;
178 * Navigate between dialog screens using step number: 1,2,...
180 this.navigateBack = () => {
181 if (this.dialogState === 1) {
182 //back from 1st screen?
184 if (this.dialogState === 2) {
185 this.dialogState = 1;
192 * each time new app is selected in the drop down,
193 * add it to the user administrated apps list
195 $scope.$watch('newAdmin.selectedNewApp.value', (newVal) => {
196 var newVal= JSON.parse(newVal);
197 if (!newVal || newVal.isAdmin === undefined) {
200 //newVal.isAdmin = true; - track by ruined this, here is the workaround:
201 let app = _.find(this.adminAppsRoles, {id: newVal.id});
204 this.appsOrder.push(app.id);
206 this.selectedNewApp = null;
207 //this.showNewAdminAppDropdown = false;
210 $scope.$on('$stateChangeStart', e => {
211 //Disable navigation when modal is opened
212 //**Nabil - note: this will cause the history back state to be replaced with current state
217 * If an Admin was added for an application remind the portal admin to add the admin as a user
219 this.remindToAddUserIfNecessary = () => {
221 var adminAddedToNewApp = false;
222 if ((this.originalApps != null) && (this.originalApps.length > 0)) {
223 for (var i = 0; i < this.appsOrder.length; i++) {
224 var foundApp = false;
225 for (var j = 0; j < this.originalApps.length; j++) {
226 if (this.originalApps[j] === this.appsOrder[i]) {
230 if (foundApp === false) {
231 adminAddedToNewApp = true;
236 adminAddedToNewApp = true;
239 if (adminAddedToNewApp === true) {
240 confirmBoxService.confirm('Add this person as an application user? This allows them to access the application from ECOMP Portal. Press OK to go to the Add Users page.')
242 if (confirmed === true) {
243 $location.path('/users');
251 NewAdminModalCtrl.$inject = ['$log', 'adminsService', '$scope', 'confirmBoxService', 'utilsService', '$location'];
252 angular.module('ecompApp').controller('NewAdminModalCtrl', NewAdminModalCtrl);