2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
41 class NewAdminModalCtrl {
42 constructor($log, adminsService, $scope, confirmBoxService, utilsService, $location, items) {
44 this.isSaving = false;
45 this.originalApps = [];
46 /* istanbul ignore if */
47 if (items && items.selectedUser && items.dialogState) {
48 this.selectedUser = items.selectedUser;
49 this.dialogState = items.dialogState;
50 this.isShowBack = false;
51 if (this.dialogState === 2) {
52 this.getAdminAppsRoles();
55 this.isShowBack = true;
56 this.selectedUser = null;
60 //this.searchUsersInProgress = false;
61 //this.showNewAdminAppDropdown = false;
62 $log.info('NewAdminModalCtrl::initiated');
66 let orderList = (apps) => {
68 for (var i = 0; i < apps.length; i++) {
69 if (apps[i].isAdmin) {
70 this.appsOrder.push(apps[i].id);
75 this.orderFilter = app => {
76 if (!app || !app.id || !this.appsOrder.length) {
79 return this.appsOrder.indexOf(app.id);
83 * this function get the selected admin apps roles
85 this.getAdminAppsRoles = () => {
86 if (!this.selectedUser || !this.selectedUser.orgUserId) {
87 $log.error('No user is selected / searchUsers is InProgress');
91 adminsService.getAdminAppsRoles(this.selectedUser.orgUserId).then(roles => {
92 $log.debug('apps roles res: ', JSON.stringify(roles));
93 if (!roles.appsRoles) {
97 this.adminAppsRoles = [];
98 for (var i = 0; i < roles.appsRoles.length; i++) {
99 if (!roles.appsRoles[i].restrictedApp) {
100 $log.debug('pushing: {id: ', roles.appsRoles[i].id,
101 'name: ', roles.appsRoles[i].appName,
102 'restrictedApp: ', roles.appsRoles[i].restrictedApp,
103 'isAdmin: ', roles.appsRoles[i].isAdmin, '}');
104 this.adminAppsRoles.push({
105 id: roles.appsRoles[i].id,
106 appName: roles.appsRoles[i].appName,
107 isAdmin: roles.appsRoles[i].isAdmin,
108 restrictedApp: roles.appsRoles[i].restrictedApp
112 this.dialogState = 2;
113 this.adminAppsRoles = this.adminAppsRoles.sort(getSortOrder("appName"));
115 orderList(roles.appsRoles);
116 if (this.appsOrder != null) {
117 for (var j = 0; j < this.appsOrder.length; j++) {
118 this.originalApps.push(this.appsOrder[j]);
126 // Refactor this into a directive
127 let getSortOrder = (prop) => {
128 return function (a, b) {
129 if (a[prop].toLowerCase() > b[prop].toLowerCase()) {
131 } else if (a[prop].toLowerCase() < b[prop].toLowerCase()) {
139 * this function set the selected user
140 * @param user: selected user object
142 this.setSelectedUser = (user) => {
143 $log.debug('selected user: ', user);
144 this.selectedUser = user;
148 * Mark the user as not admin of the selected app
149 * @param app: selected app object
151 this.unadminApp = (app) => {
152 confirmBoxService.deleteItem(app.appName).then(confirmed => {
153 if (confirmed === true) {
155 this.appsOrder.splice(this.appsOrder.indexOf(app.id), 1);
163 * update the selected admin app with the new roles
165 this.updateAdminAppsRoles = () => {
166 if (!this.selectedUser || !this.selectedUser.orgUserId || !this.adminAppsRoles) {
169 this.isSaving = true;
170 $log.debug('going to update user: ' + this.selectedUser.orgUserId + ' with app roles: ' + JSON.stringify(this.adminAppsRoles));
171 confirmBoxService.makeAdminChanges('Are you sure you want to make these admin changes?')
173 if(confirmed === true){
174 adminsService.updateAdminAppsRoles({
175 orgUserId: this.selectedUser.orgUserId,
176 appsRoles: this.adminAppsRoles
179 $log.debug('Admin apps roles updated successfully!', res);
180 //close and resolve dialog promise with true (to update the table)
181 this.remindToAddUserIfNecessary();
182 $scope.$dismiss('cancel');
184 $log.error('NewAdminModalCtrl.updateAdminAppsRoles:: Failed - ' + err);
186 this.isSaving = false;
189 this.isSaving = false;
195 * Navigate between dialog screens using step number: 1,2,...
197 this.navigateBack = () => {
198 if (this.dialogState === 1) {
199 //back from 1st screen?
201 if (this.dialogState === 2) {
202 this.dialogState = 1;
209 * each time new app is selected in the drop down,
210 * add it to the user administrated apps list
212 $scope.$watch('newAdmin.selectedNewApp.value', (newVal) => {
213 if(newVal) var newVal= JSON.parse(newVal);
214 if (!newVal || newVal.isAdmin === undefined) {
217 //newVal.isAdmin = true; - track by ruined this, here is the workaround:
218 let app = _.find(this.adminAppsRoles, {id: newVal.id});
221 this.appsOrder.push(app.id);
223 //this.showNewAdminAppDropdown = false;
225 this.selectedNewApp = null;
227 $scope.$on('$stateChangeStart', e => {
228 //Disable navigation when modal is opened
229 //**Nabil - note: this will cause the history back state to be replaced with current state
234 * If an Admin was added for an application remind the portal admin to add the admin as a user
236 this.remindToAddUserIfNecessary = () => {
238 var adminAddedToNewApp = false;
239 if ((this.originalApps != null) && (this.originalApps.length > 0)) {
240 for (var i = 0; i < this.appsOrder.length; i++) {
241 var foundApp = false;
242 for (var j = 0; j < this.originalApps.length; j++) {
243 if (this.originalApps[j] === this.appsOrder[i]) {
247 if (foundApp === false) {
248 adminAddedToNewApp = true;
253 adminAddedToNewApp = true;
256 if (adminAddedToNewApp === true) {
257 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.')
259 if (confirmed === true) {
260 $location.path('/users');
268 NewAdminModalCtrl.$inject = ['$log', 'adminsService', '$scope', 'confirmBoxService', 'utilsService', '$location', 'items'];
269 angular.module('ecompApp').controller('NewAdminModalCtrl', NewAdminModalCtrl);