Security/ Package Name changes
[portal.git] / ecomp-portal-FE-common / client / app / views / admins / admins.controller.js
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 'use strict';
39 (function () {
40     class AdminsCtrl {
41         constructor($log, adminsService, applicationsService, ngDialog, $modal) {
42
43             let allPortalsFilterObject = {index: 0, title: 'All applications', value: ''};
44
45             let updateTableData = () => {
46                 this.isLoadingTable = true;
47                 adminsService.getAccountAdmins().then(res=> {
48                     if (!res || !res.length) {
49                         $log.error('AdminsCtrl::updateTableData: no admins err handling');
50                         this.adminsTableData = [];
51                         return;
52                     }
53                     this.adminsTableData = res;
54                 }).catch(err=> {
55                     $log.error('AdminsCtrl::updateTableData error: ', err);
56                 }).finally(() => {
57                     this.isLoadingTable = false;
58                 });
59             };
60
61             let init = () => {
62                 //$log.info('AdminsCtrl:: ::initializing...');
63                 this.isLoadingTable = false;
64                 this.availableApps = [allPortalsFilterObject];
65                 this.filterByApp = this.availableApps[0];
66
67                 /*Table general configuration params*/
68                 this.searchString= '';
69                 /*Table data*/
70                 this.adminsTableHeaders = ['First Name', 'Last Name', 'User ID', 'Applications'];
71                 this.adminsTableData = [];
72                 updateTableData();
73             };
74
75             applicationsService.getAvailableApps().then(res=> {
76                 //if(!res || Object.prototype.toString.call(res) !== '[object Array]'){
77                 //    this.availableApps = [allPortalsFilterObject];
78                 //    return;
79                 //}
80                 //this part overrides index param to fix ABS select bug
81                 // (index has to be the same as location in array)
82                 // todo:change BE object to contain only id and name
83                 this.availableApps = [allPortalsFilterObject];
84                 var res1 = res.sort(getSortOrder("title"));
85                 var realAppIndex = 1;
86                 for(let i=1; i<=res1.length; i++){
87                     if (!res1[i-1].restrictedApp) {
88                         //$log.debug('AdminsCtrl:getAvailableApps:: pushing: {index: ', realAppIndex, 'title: ', res1[i - 1].title,
89                          //   '| value: ', res1[i -1].value, '}');
90                         this.availableApps.push({
91                             index: realAppIndex,
92                             title: res1[i - 1].title,
93                             value: res1[i - 1].value
94                         });
95                         realAppIndex = realAppIndex + 1;
96                     } else {
97                         // $log.debug('AdminsCtrl:getAvailableApps:: Restricted/URL only App will not be used = ' + res1[i - 1].title);
98                     }
99                 }
100             }).catch(err=> {
101                 $log.error('AdminsCtrl::ctor', err);
102                 this.availableApps = [allPortalsFilterObject];
103             });
104
105             // Refactor this into a directive
106             let getSortOrder = (prop) => {
107                 return function(a, b) {
108                     // $log.debug('a = ' + JSON.stringify(a) + "| b = " + JSON.stringify(b));
109                     if (a[prop].toLowerCase() > b[prop].toLowerCase()) {
110                         return 1;
111                     } else if (a[prop].toLowerCase() < b[prop].toLowerCase()) {
112                         return -1;
113                     }
114                     return 0;
115                 }
116             }
117
118             init();
119
120             //Filter function
121             this.portalsRowFilter = (input) => {
122                 if (this.filterByApp.value === '') {
123                     return true;
124                 }
125                 return _.find(input.apps, {appName: this.filterByApp.value}) !== undefined;
126             };
127
128             this.openAddNewAdminModal = (user) => {
129                 let data = null;
130                 if(user){
131                     data = {
132                         dialogState: 2,
133                         selectedUser:{
134                             orgUserId: user.orgUserId,
135                             firstName: user.firstName,
136                             lastName: user.lastName
137                         }
138                     }
139                 }
140                 var modalInstance = $modal.open({
141                     templateUrl: 'app/views/admins/add-admin-dialogs/new-admin.modal.html',
142                     controller: 'NewAdminModalCtrl as newAdmin',
143                     sizeClass: 'modal-medium', 
144                     resolve: {
145                                         items: function () {
146                                                 return data;
147                                 }
148                         }
149                 });
150                 
151                 modalInstance.result.finally(function () {
152                         updateTableData();  
153                 });
154             };
155         }
156     }
157     AdminsCtrl.$inject = ['$log', 'adminsService', 'applicationsService', 'ngDialog', '$modal'];
158     angular.module('ecompApp').controller('AdminsCtrl', AdminsCtrl);
159 })();