summaryrefslogtreecommitdiffstats
path: root/portal-FE-common/src/app/pages/admins/new-admin/new-admin.component.ts
blob: 8f80138e880544bbb307cc0a5f87ffb16066bfb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*-
 * ============LICENSE_START==========================================
 * ONAP Portal
 * ===================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * ===================================================================
 *
 * Unless otherwise specified, all software contained herein is licensed
 * under the Apache License, Version 2.0 (the "License");
 * you may not use this software except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *             http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Unless otherwise specified, all documentation contained herein is licensed
 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
 * you may not use this documentation except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *             https://creativecommons.org/licenses/by/4.0/
 *
 * Unless required by applicable law or agreed to in writing, documentation
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * ============LICENSE_END============================================
 *
 * 
 */
import { Component, OnInit, Output, EventEmitter, Input } from '@angular/core';
import { AdminsService } from 'src/app/shared/services';
import { HttpErrorResponse } from '@angular/common/http';
import { ConfirmationModalComponent } from 'src/app/modals/confirmation-modal/confirmation-modal.component';
import { NgbModal, NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
import { PortalAdmin } from 'src/app/shared/model';
import { MatTableDataSource } from '@angular/material';
import { InformationModalComponent } from 'src/app/modals/information-modal/information-modal.component';
import { Router } from '@angular/router';

@Component({
  selector: 'app-new-admin',
  templateUrl: './new-admin.component.html',
  styleUrls: ['./new-admin.component.scss']
})
export class NewAdminComponent implements OnInit {

  @Input() dialogState: number;
  @Input() userTitle: string;
  @Input() disableBack: boolean;
  @Input() adminModalData: any;
  @Output() passBackNewAdminPopup: EventEmitter<any> = new EventEmitter();
  searchTitleText = 'Enter First Name, Last Name or Org User Id';
  placeholderText = 'Search';
  changedSelectedUser: PortalAdmin;
  adminAppsRoles: any;
  adminDropdownApps: any;
  isLoading: boolean;
  newAppSelected: boolean;
  adminAppSelectAndUnselectData: any;
  displayedColumns: string[] = ['applications'];
  adminsAppsSource = new MatTableDataSource(this.adminAppsRoles);
  constructor(public router: Router, private adminsService: AdminsService, public ngModal: NgbModal, public activeModal: NgbActiveModal) { }

  ngOnInit() {
    this.adminAppsRoles = [];
    this.changedSelectedUser = null;
    if (this.disableBack){
      this.changedSelectedUser = this.adminModalData;
      this.getAdminAppsRoles();
    }
    this.adminDropdownApps = [];
    this.adminAppSelectAndUnselectData = [];
  }

  changeSelectedUser(user: PortalAdmin) {
    this.changedSelectedUser = user;
    this.userTitle = `${this.changedSelectedUser.firstName}, ` + ` ${this.changedSelectedUser.lastName} ` + ` (${this.changedSelectedUser.orgUserId})`;
  }

  getAdminAppsRoles() {
    this.isLoading = true;
    this.adminsService.getAdminAppsRoles(this.changedSelectedUser.orgUserId).subscribe((_res: any) => {
      JSON.stringify(_res);
      if (!_res.appsRoles) {
        return;
      }
      this.adminAppsRoles = [];
      for (var i = 0; i < _res.appsRoles.length; i++) {
        if (!_res.appsRoles[i].restrictedApp && _res.appsRoles[i].isAdmin) {
          this.adminAppsRoles.push({
            id: _res.appsRoles[i].id,
            appName: _res.appsRoles[i].appName,
            isAdmin: _res.appsRoles[i].isAdmin,
            restrictedApp: _res.appsRoles[i].restrictedApp
          });
        } else if (!_res.appsRoles[i].restrictedApp) {
          this.adminDropdownApps.push({
            id: _res.appsRoles[i].id,
            appName: _res.appsRoles[i].appName,
            isAdmin: _res.appsRoles[i].isAdmin,
            restrictedApp: _res.appsRoles[i].restrictedApp
          });
        }
      }
      this.isLoading = false;
      this.newAppSelected = false;
      this.dialogState = 2;
      this.adminsAppsSource = new MatTableDataSource(this.adminAppsRoles);
    });
  }

  navigateBack() {
    this.dialogState = 1;
  }

  removeAdminApp(app: any) {
    const modalRef = this.ngModal.open(InformationModalComponent);
    modalRef.componentInstance.title = "Confirmation";
    modalRef.componentInstance.message = `Are you sure you want to delete ${app.appName}?`;
    modalRef.result.then((result) => {
      if (result === 'Ok') {
        this.adminAppsRoles.forEach((item: any, index: any) => {
          if (item === app) this.adminAppsRoles.splice(index, 1);
        });
        //call from delete admin app
        this.updateDropdown(app, false);
        this.adminsAppsSource = new MatTableDataSource(this.adminAppsRoles);
      }
    }, (resut) => {
      return;
    })
  }

  updateDropdown(_newValue: any, isDropdownCall: boolean) {
    // app is selected from dropdown
    if (isDropdownCall) {
      this.adminDropdownApps.forEach((item: any, index: any) => {
        if (item === _newValue) this.adminDropdownApps.splice(index, 1);
      });
      this.getadminAppSelectAndUnselectedData(_newValue);
      _newValue.isAdmin = true;
      this.adminAppsRoles.push(_newValue);
      this.adminsAppsSource = new MatTableDataSource(this.adminAppsRoles);
    } else {    // app is removed from the admin list
      this.getadminAppSelectAndUnselectedData(_newValue);
      _newValue.isAdmin = false;
      this.adminDropdownApps.push(_newValue);
    }

    // disable save button if nothing new in the admin list
    if (this.adminAppSelectAndUnselectData.length > 0)
      this.newAppSelected = true;
    else
      this.newAppSelected = false;

  }

  private getadminAppSelectAndUnselectedData(_newValue: any) {
    const index: number = this.adminAppSelectAndUnselectData.indexOf(_newValue);
    if (index !== -1) {
      this.adminAppSelectAndUnselectData.splice(index, 1); // if found, remove selected app from dropdown in the list
    }
    else {
      this.adminAppSelectAndUnselectData.push(_newValue);
    }
  }

  remindToAddUserIfNecessary() {
    let adminAddedToNewApp = true;
    if ((this.adminAppsRoles != null) && (this.adminAppsRoles.length > 0)) {
      for (var i = 0; i < this.adminAppSelectAndUnselectData.length; i++) {
        var foundApp = false;
        for (var j = 0; j < this.adminAppsRoles.length; j++) {
          if (this.adminAppsRoles[j] === this.adminAppSelectAndUnselectData[i]) {
            foundApp = true;
          }
        }
        if (foundApp === false) {
          adminAddedToNewApp = true;
          break;
        }
      }
    } else {
      adminAddedToNewApp = true;
    }
    if (adminAddedToNewApp === true) {
      const modalRef = this.ngModal.open(InformationModalComponent);
      modalRef.componentInstance.title = "Confirmation";
      modalRef.componentInstance.message = 'Add this person as an application user? This allows them to access the application from ONAP Portal. Press OK to go to the Add Users page.';
      modalRef.result.then((_res) => {
        if (_res === 'Ok') {
          this.router.navigate(['/users']);
        }
      });
    }
  }

  updateAdminAppsRoles() {
    const modalRef = this.ngModal.open(InformationModalComponent);
    modalRef.componentInstance.title = "Admin Update";
    modalRef.componentInstance.message = 'Are you sure you want to make these admin changes?';
    modalRef.result.then((result) => {
      if (result === 'Ok') {
        this.adminsService.updateAdminAppsRoles({ orgUserId: this.changedSelectedUser.orgUserId, appsRoles: this.adminAppsRoles }).subscribe(_data => {
          this.passBackNewAdminPopup.emit(_data);
          this.remindToAddUserIfNecessary();
        }, (_err: HttpErrorResponse) => {
          this.passBackNewAdminPopup.emit(_err);
          const modalErrorRef = this.ngModal.open(ConfirmationModalComponent);
          modalErrorRef.componentInstance.title = "Error";
          if (_err.status) {
            modalErrorRef.componentInstance.message = "There was a unknown problem while adding admin to selected application(s)." + "Please try again later. Error Status: " + _err.status;
          }
        });
      }
    }, (reason) => {
      return;
    });
  }
}