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 $log.info('NewAdminModalCtrl::initiated');
47 let orderList = (apps) => {
49 for (var i = 0; i < apps.length; i++) {
50 if (apps[i].isAdmin) {
51 this.appsOrder.push(apps[i].id);
56 this.orderFilter = app => {
57 if (!app || !app.id || !this.appsOrder.length) {
60 return this.appsOrder.indexOf(app.id);
63 this.getAdminAppsRoles = () => {
64 if (!this.selectedUser || !this.selectedUser.orgUserId) {
65 $log.error('No user is selected / searchUsers is InProgress');
69 adminsService.getAdminAppsRoles(this.selectedUser.orgUserId).then(roles => {
70 $log.debug('apps roles res: ', JSON.stringify(roles));
71 if (!roles.appsRoles) {
75 this.adminAppsRoles = [];
76 for (var i = 0; i < roles.appsRoles.length; i++) {
77 if (!roles.appsRoles[i].restrictedApp) {
78 $log.debug('pushing: {id: ', roles.appsRoles[i].id,
79 'name: ', roles.appsRoles[i].appName,
80 'restrictedApp: ', roles.appsRoles[i].restrictedApp,
81 'isAdmin: ', roles.appsRoles[i].isAdmin, '}');
82 this.adminAppsRoles.push({
83 id: roles.appsRoles[i].id,
84 appName: roles.appsRoles[i].appName,
85 isAdmin: roles.appsRoles[i].isAdmin,
86 restrictedApp: roles.appsRoles[i].restrictedApp
91 this.adminAppsRoles = this.adminAppsRoles.sort(getSortOrder("appName"));
93 orderList(roles.appsRoles);
94 if (this.appsOrder != null) {
95 for (var j = 0; j < this.appsOrder.length; j++) {
96 this.originalApps.push(this.appsOrder[j]);
104 let getSortOrder = (prop) => {
105 return function (a, b) {
106 if (a[prop].toLowerCase() > b[prop].toLowerCase()) {
108 } else if (a[prop].toLowerCase() < b[prop].toLowerCase()) {
115 this.setSelectedUser = (user) => {
116 $log.debug('selected user: ', user);
117 this.selectedUser = user;
120 this.unadminApp = (app) => {
121 confirmBoxService.deleteItem(app.appName).then(confirmed => {
122 if (confirmed === true) {
124 this.appsOrder.splice(this.appsOrder.indexOf(app.id), 1);
131 this.updateAdminAppsRoles = () => {
132 if (!this.selectedUser || !this.selectedUser.orgUserId || !this.adminAppsRoles) {
135 this.isSaving = true;
136 $log.debug('going to update user: ' + this.selectedUser.orgUserId + ' with app roles: ' + JSON.stringify(this.adminAppsRoles));
137 confirmBoxService.makeAdminChanges('Are you sure you want to make these admin changes?')
139 adminsService.updateAdminAppsRoles({
140 orgUserId: this.selectedUser.orgUserId,
141 appsRoles: this.adminAppsRoles
144 $log.debug('Admin apps roles updated successfully!', res);
145 this.remindToAddUserIfNecessary();
146 $scope.closeThisDialog(true);
148 $log.error('NewAdminModalCtrl.updateAdminAppsRoles:: Failed - ' + err);
150 this.isSaving = false;
155 this.navigateBack = () => {
156 if (this.dialogState === 1) {
158 if (this.dialogState === 2) {
159 this.dialogState = 1;
165 $scope.$watch('newAdmin.selectedNewApp', (newVal) => {
166 if (!newVal || newVal.isAdmin === undefined) {
169 let app = _.find(this.adminAppsRoles, {id: newVal.id});
172 this.appsOrder.push(app.id);
174 this.selectedNewApp = null;
177 $scope.$on('$stateChangeStart', e => {
181 this.remindToAddUserIfNecessary = () => {
182 var adminAddedToNewApp = false;
183 if ((this.originalApps != null) && (this.originalApps.length > 0)) {
184 for (var i = 0; i < this.appsOrder.length; i++) {
185 var foundApp = false;
186 for (var j = 0; j < this.originalApps.length; j++) {
187 if (this.originalApps[j] == this.appsOrder[i]) {
191 if (foundApp == false) {
192 adminAddedToNewApp = true;
197 adminAddedToNewApp = true;
200 if (adminAddedToNewApp == true) {
201 confirmBoxService.confirm('Add this person as an application user? This allows them to access the application from OpenECOMP Portal. Press OK to go to the Add Users page.')
203 if (confirmed == true) {
204 $location.path('/users');
212 NewAdminModalCtrl.$inject = ['$log', 'adminsService', '$scope', 'confirmBoxService', 'utilsService', '$location'];
213 angular.module('ecompApp').controller('NewAdminModalCtrl', NewAdminModalCtrl);