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, items) {
26 this.isSaving = false;
27 this.originalApps = [];
28 /* istanbul ignore if */
29 if (items && items.selectedUser && items.dialogState) {
30 this.selectedUser = items.selectedUser;
31 this.dialogState = items.dialogState;
32 this.isShowBack = false;
33 if (this.dialogState === 2) {
34 this.getAdminAppsRoles();
37 this.isShowBack = true;
38 this.selectedUser = null;
42 //this.searchUsersInProgress = false;
43 //this.showNewAdminAppDropdown = false;
44 $log.info('NewAdminModalCtrl::initiated');
48 let orderList = (apps) => {
50 for (var i = 0; i < apps.length; i++) {
51 if (apps[i].isAdmin) {
52 this.appsOrder.push(apps[i].id);
57 this.orderFilter = app => {
58 if (!app || !app.id || !this.appsOrder.length) {
61 return this.appsOrder.indexOf(app.id);
65 * this function get the selected admin apps roles
67 this.getAdminAppsRoles = () => {
68 if (!this.selectedUser || !this.selectedUser.orgUserId) {
69 $log.error('No user is selected / searchUsers is InProgress');
73 adminsService.getAdminAppsRoles(this.selectedUser.orgUserId).then(roles => {
74 $log.debug('apps roles res: ', JSON.stringify(roles));
75 if (!roles.appsRoles) {
79 this.adminAppsRoles = [];
80 for (var i = 0; i < roles.appsRoles.length; i++) {
81 if (!roles.appsRoles[i].restrictedApp) {
82 $log.debug('pushing: {id: ', roles.appsRoles[i].id,
83 'name: ', roles.appsRoles[i].appName,
84 'restrictedApp: ', roles.appsRoles[i].restrictedApp,
85 'isAdmin: ', roles.appsRoles[i].isAdmin, '}');
86 this.adminAppsRoles.push({
87 id: roles.appsRoles[i].id,
88 appName: roles.appsRoles[i].appName,
89 isAdmin: roles.appsRoles[i].isAdmin,
90 restrictedApp: roles.appsRoles[i].restrictedApp
95 this.adminAppsRoles = this.adminAppsRoles.sort(getSortOrder("appName"));
97 orderList(roles.appsRoles);
98 if (this.appsOrder != null) {
99 for (var j = 0; j < this.appsOrder.length; j++) {
100 this.originalApps.push(this.appsOrder[j]);
108 // Refactor this into a directive
109 let getSortOrder = (prop) => {
110 return function (a, b) {
111 if (a[prop].toLowerCase() > b[prop].toLowerCase()) {
113 } else if (a[prop].toLowerCase() < b[prop].toLowerCase()) {
121 * this function set the selected user
122 * @param user: selected user object
124 this.setSelectedUser = (user) => {
125 $log.debug('selected user: ', user);
126 this.selectedUser = user;
130 * Mark the user as not admin of the selected app
131 * @param app: selected app object
133 this.unadminApp = (app) => {
134 confirmBoxService.deleteItem(app.appName).then(confirmed => {
135 if (confirmed === true) {
137 this.appsOrder.splice(this.appsOrder.indexOf(app.id), 1);
145 * update the selected admin app with the new roles
147 this.updateAdminAppsRoles = () => {
148 if (!this.selectedUser || !this.selectedUser.orgUserId || !this.adminAppsRoles) {
151 this.isSaving = true;
152 $log.debug('going to update user: ' + this.selectedUser.orgUserId + ' with app roles: ' + JSON.stringify(this.adminAppsRoles));
153 confirmBoxService.makeAdminChanges('Are you sure you want to make these admin changes?')
155 if(confirmed === true){
156 adminsService.updateAdminAppsRoles({
157 orgUserId: this.selectedUser.orgUserId,
158 appsRoles: this.adminAppsRoles
161 $log.debug('Admin apps roles updated successfully!', res);
162 //close and resolve dialog promise with true (to update the table)
163 this.remindToAddUserIfNecessary();
164 $scope.$dismiss('cancel');
166 $log.error('NewAdminModalCtrl.updateAdminAppsRoles:: Failed - ' + err);
168 this.isSaving = false;
171 this.isSaving = false;
177 * Navigate between dialog screens using step number: 1,2,...
179 this.navigateBack = () => {
180 if (this.dialogState === 1) {
181 //back from 1st screen?
183 if (this.dialogState === 2) {
184 this.dialogState = 1;
191 * each time new app is selected in the drop down,
192 * add it to the user administrated apps list
194 $scope.$watch('newAdmin.selectedNewApp.value', (newVal) => {
195 if(newVal) var newVal= JSON.parse(newVal);
196 if (!newVal || newVal.isAdmin === undefined) {
199 //newVal.isAdmin = true; - track by ruined this, here is the workaround:
200 let app = _.find(this.adminAppsRoles, {id: newVal.id});
203 this.appsOrder.push(app.id);
205 this.selectedNewApp = null;
206 //this.showNewAdminAppDropdown = false;
209 $scope.$on('$stateChangeStart', e => {
210 //Disable navigation when modal is opened
211 //**Nabil - note: this will cause the history back state to be replaced with current state
216 * If an Admin was added for an application remind the portal admin to add the admin as a user
218 this.remindToAddUserIfNecessary = () => {
220 var adminAddedToNewApp = false;
221 if ((this.originalApps != null) && (this.originalApps.length > 0)) {
222 for (var i = 0; i < this.appsOrder.length; i++) {
223 var foundApp = false;
224 for (var j = 0; j < this.originalApps.length; j++) {
225 if (this.originalApps[j] === this.appsOrder[i]) {
229 if (foundApp === false) {
230 adminAddedToNewApp = true;
235 adminAddedToNewApp = true;
238 if (adminAddedToNewApp === true) {
239 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.')
241 if (confirmed === true) {
242 $location.path('/users');
250 NewAdminModalCtrl.$inject = ['$log', 'adminsService', '$scope', 'confirmBoxService', 'utilsService', '$location', 'items'];
251 angular.module('ecompApp').controller('NewAdminModalCtrl', NewAdminModalCtrl);