2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2019 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============================================
38 import { Component, OnInit, Output, EventEmitter, Input } from '@angular/core';
39 import { AdminsService } from 'src/app/shared/services';
40 import { HttpErrorResponse } from '@angular/common/http';
41 import { ConfirmationModalComponent } from 'src/app/modals/confirmation-modal/confirmation-modal.component';
42 import { NgbModal, NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
43 import { PortalAdmin } from 'src/app/shared/model';
44 import { MatTableDataSource } from '@angular/material';
45 import { InformationModalComponent } from 'src/app/modals/information-modal/information-modal.component';
46 import { Router } from '@angular/router';
49 selector: 'app-new-admin',
50 templateUrl: './new-admin.component.html',
51 styleUrls: ['./new-admin.component.scss']
53 export class NewAdminComponent implements OnInit {
55 @Input() dialogState: number;
56 @Input() userTitle: string;
57 @Input() disableBack: boolean;
58 @Input() adminModalData: any;
59 @Output() passBackNewAdminPopup: EventEmitter<any> = new EventEmitter();
60 searchTitleText = 'Enter First Name, Last Name or Org User Id';
61 placeholderText = 'Search';
62 changedSelectedUser: PortalAdmin;
64 adminDropdownApps: any;
66 newAppSelected: boolean;
67 adminAppSelectAndUnselectData: any;
68 displayedColumns: string[] = ['applications'];
69 adminsAppsSource = new MatTableDataSource(this.adminAppsRoles);
70 constructor(public router: Router, private adminsService: AdminsService, public ngModal: NgbModal, public activeModal: NgbActiveModal) { }
73 this.adminAppsRoles = [];
74 this.changedSelectedUser = null;
75 if (this.disableBack){
76 this.changedSelectedUser = this.adminModalData;
77 this.getAdminAppsRoles();
79 this.adminDropdownApps = [];
80 this.adminAppSelectAndUnselectData = [];
83 changeSelectedUser(user: PortalAdmin) {
84 this.changedSelectedUser = user;
85 this.userTitle = `${this.changedSelectedUser.firstName}, ` + ` ${this.changedSelectedUser.lastName} ` + ` (${this.changedSelectedUser.orgUserId})`;
89 this.isLoading = true;
90 this.adminsService.getAdminAppsRoles(this.changedSelectedUser.orgUserId).subscribe((_res: any) => {
92 if (!_res.appsRoles) {
95 this.adminAppsRoles = [];
96 for (var i = 0; i < _res.appsRoles.length; i++) {
97 if (!_res.appsRoles[i].restrictedApp && _res.appsRoles[i].isAdmin) {
98 this.adminAppsRoles.push({
99 id: _res.appsRoles[i].id,
100 appName: _res.appsRoles[i].appName,
101 isAdmin: _res.appsRoles[i].isAdmin,
102 restrictedApp: _res.appsRoles[i].restrictedApp
104 } else if (!_res.appsRoles[i].restrictedApp) {
105 this.adminDropdownApps.push({
106 id: _res.appsRoles[i].id,
107 appName: _res.appsRoles[i].appName,
108 isAdmin: _res.appsRoles[i].isAdmin,
109 restrictedApp: _res.appsRoles[i].restrictedApp
113 this.isLoading = false;
114 this.newAppSelected = false;
115 this.dialogState = 2;
116 this.adminsAppsSource = new MatTableDataSource(this.adminAppsRoles);
121 this.dialogState = 1;
124 removeAdminApp(app: any) {
125 const modalRef = this.ngModal.open(InformationModalComponent);
126 modalRef.componentInstance.title = "Confirmation";
127 modalRef.componentInstance.message = `Are you sure you want to delete ${app.appName}?`;
128 modalRef.result.then((result) => {
129 if (result === 'Ok') {
130 this.adminAppsRoles.forEach((item: any, index: any) => {
131 if (item === app) this.adminAppsRoles.splice(index, 1);
133 //call from delete admin app
134 this.updateDropdown(app, false);
135 this.adminsAppsSource = new MatTableDataSource(this.adminAppsRoles);
142 updateDropdown(_newValue: any, isDropdownCall: boolean) {
143 // app is selected from dropdown
144 if (isDropdownCall) {
145 this.adminDropdownApps.forEach((item: any, index: any) => {
146 if (item === _newValue) this.adminDropdownApps.splice(index, 1);
148 this.getadminAppSelectAndUnselectedData(_newValue);
149 _newValue.isAdmin = true;
150 this.adminAppsRoles.push(_newValue);
151 this.adminsAppsSource = new MatTableDataSource(this.adminAppsRoles);
152 } else { // app is removed from the admin list
153 this.getadminAppSelectAndUnselectedData(_newValue);
154 _newValue.isAdmin = false;
155 this.adminDropdownApps.push(_newValue);
158 // disable save button if nothing new in the admin list
159 if (this.adminAppSelectAndUnselectData.length > 0)
160 this.newAppSelected = true;
162 this.newAppSelected = false;
166 private getadminAppSelectAndUnselectedData(_newValue: any) {
167 const index: number = this.adminAppSelectAndUnselectData.indexOf(_newValue);
169 this.adminAppSelectAndUnselectData.splice(index, 1); // if found, remove selected app from dropdown in the list
172 this.adminAppSelectAndUnselectData.push(_newValue);
176 remindToAddUserIfNecessary() {
177 let adminAddedToNewApp = true;
178 if ((this.adminAppsRoles != null) && (this.adminAppsRoles.length > 0)) {
179 for (var i = 0; i < this.adminAppSelectAndUnselectData.length; i++) {
180 var foundApp = false;
181 for (var j = 0; j < this.adminAppsRoles.length; j++) {
182 if (this.adminAppsRoles[j] === this.adminAppSelectAndUnselectData[i]) {
186 if (foundApp === false) {
187 adminAddedToNewApp = true;
192 adminAddedToNewApp = true;
194 if (adminAddedToNewApp === true) {
195 const modalRef = this.ngModal.open(InformationModalComponent);
196 modalRef.componentInstance.title = "Confirmation";
197 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.';
198 modalRef.result.then((_res) => {
200 this.router.navigate(['/users']);
206 updateAdminAppsRoles() {
207 const modalRef = this.ngModal.open(InformationModalComponent);
208 modalRef.componentInstance.title = "Admin Update";
209 modalRef.componentInstance.message = 'Are you sure you want to make these admin changes?';
210 modalRef.result.then((result) => {
211 if (result === 'Ok') {
212 this.adminsService.updateAdminAppsRoles({ orgUserId: this.changedSelectedUser.orgUserId, appsRoles: this.adminAppsRoles }).subscribe(_data => {
213 this.passBackNewAdminPopup.emit(_data);
214 this.remindToAddUserIfNecessary();
215 }, (_err: HttpErrorResponse) => {
216 this.passBackNewAdminPopup.emit(_err);
217 const modalErrorRef = this.ngModal.open(ConfirmationModalComponent);
218 modalErrorRef.componentInstance.title = "Error";
220 modalErrorRef.componentInstance.message = "There was a unknown problem while adding admin to selected application(s)." + "Please try again later. Error Status: " + _err.status;