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