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, Input, EventEmitter, Output } from '@angular/core';
39 import { NgbActiveModal, NgbModal } from '@ng-bootstrap/ng-bootstrap';
40 import { ApplicationsService, UsersService } from 'src/app/shared/services';
41 import { MatTableDataSource, MatRadioChange } from '@angular/material';
42 import { UserAdminApps, UserAccessRoles } from 'src/app/shared/model';
43 import { ConfirmationModalComponent } from 'src/app/modals/confirmation-modal/confirmation-modal.component';
44 import { InformationModalComponent } from 'src/app/modals/information-modal/information-modal.component';
45 import { HttpErrorResponse } from '@angular/common/http';
48 selector: 'app-new-user-modal',
49 templateUrl: './new-user-modal.component.html',
50 styleUrls: ['./new-user-modal.component.scss']
52 export class NewUserModalComponent implements OnInit {
53 @Input() dialogState: number;
54 @Input() userTitle: string;
55 @Input() disableBack: boolean;
56 @Input() adminModalData: any;
57 @Input() userModalData: any;
58 @Output() passBackNewUserPopup: EventEmitter<any> = new EventEmitter();
59 changedSelectedUser: any;
61 isGettingAdminApps: boolean;
63 modelSelectedRoles: any;
64 appRoles: Array<UserAccessRoles>;
65 numberAppsProcessed: number;
66 isSystemUserCheck = false;
67 extRequestValue = false;
68 searchTitleText = 'Enter First Name, Last Name or Org-ID';
69 placeholderText = 'Search';
71 { id: 'userButton', value: 'true', labelValue: 'User' },
72 { id: 'systemUserButton', value: 'false', labelValue: 'System' }
74 selectedvalueradioButtonGroup = {
77 userRadioSearchButton = this.ngRepeatDemo[0].labelValue;
78 displayedColumns: string[] = ['applications', 'roles', 'delete'];
79 userAdminAppsSource = new MatTableDataSource(this.adminApps);
80 showAppSpinner: boolean;
83 originalSelectedRoles: any;
84 numberAppsSucceeded: number;
86 orgUserIdValue: string;
87 constructor(public ngbModal: NgbModal, public activeModal: NgbActiveModal, private applicationsService: ApplicationsService, private usersService: UsersService) { }
92 this.anyChanges = false;
93 this.modelSelectedRoles = [];
94 this.originalSelectedRoles = [];
95 this.changedSelectedUser = null;
96 if (this.dialogState === 2) {
97 this.changedSelectedUser = this.userModalData;
98 this.orgUserIdValue = this.changedSelectedUser.orgUserId;
99 this.getUserAppsRoles();
103 addUserFlag : boolean = false;
105 addNewUser(newUserFlag : boolean){
106 console.log("New user flag ", newUserFlag);
107 this.addUserFlag = true;
110 changeSelectedUser(user: any) {
111 this.changedSelectedUser = user;
112 if (this.changedSelectedUser.firstName === undefined || this.changedSelectedUser.lastName === undefined) {
113 this.userTitle = this.changedSelectedUser;
114 this.orgUserIdValue = this.changedSelectedUser;
117 this.orgUserIdValue = this.changedSelectedUser.orgUserId;
118 this.userTitle = `${this.changedSelectedUser.firstName}, ` + ` ${this.changedSelectedUser.lastName} ` + ` (${this.changedSelectedUser.orgUserId})`;
122 searchUserRadioChange($event: MatRadioChange) {
123 if ($event.value === 'System') {
124 this.searchTitleText = 'Enter System UserID';
125 this.placeholderText = 'xxxxxx@org.com';
126 this.isSystemUserCheck = true;
128 this.searchTitleText = 'Enter First Name, Last Name or ATTUID';
129 this.placeholderText = 'Search';
130 this.isSystemUserCheck = false;
135 this.anyChanges = false;
136 this.dialogState = 1;
139 roleSelectChange(element: any) { // update this.adminApps list when user select roles from dropdown
140 let existingSelectedRoles = this.modelSelectedRoles;
141 this.modelSelectedRoles = [];
142 this.anyChanges = true;
143 this.adminApps.forEach(_item => {
144 if (_item.id === element.id) {
145 _item['isChanged'] = true;
147 let appRoleList = _item.appRoles;
148 if (appRoleList != undefined) {
149 appRoleList.forEach(_itemRole => {
150 if (_itemRole.appId === element.id) {
151 const index = existingSelectedRoles.indexOf(_itemRole);
153 _itemRole.isApplied = true;
155 _itemRole.isApplied = false;
158 if (_itemRole.isApplied)
159 this.modelSelectedRoles.push(_itemRole);
166 if (this.changedSelectedUser === undefined) {
167 this.dialogState = 1;
170 this.isGettingAdminApps = true;
171 this.applicationsService.getAdminApps().subscribe((apps: Array<UserAdminApps>) => {
172 this.isGettingAdminApps = false;
173 if (!apps || !apps.length) {
176 this.adminApps = apps;
177 this.dialogState = 2;
178 this.numberAppsProcessed = 0;
179 this.showSpinner = true;
180 apps.forEach(app => {
181 //$log.debug('NewUserModalCtrl::getUserAppsRoles: app: id: ', app.id, 'name: ',app.name);
182 // Keep track of which app has changed, so we know which apps to update using a BE API
183 app['isChanged'] = false;
184 // Each of these specifies a state, which corresponds to a different message and style that gets displayed
185 app['showSpinner'] = true;
186 app['isError'] = false;
187 app['isDeleted'] = false;
188 app['printNoChanges'] = false;
189 app['isUpdating'] = false;
190 app['isErrorUpdating'] = false;
191 app['isDoneUpdating'] = false;
192 app['errorMessage'] = "";
193 this.usersService.getUserAppRoles(app.id, this.orgUserIdValue, this.extRequestValue, this.isSystemUserCheck).toPromise().then((userAppRolesResult) => {
194 app['appRoles'] = userAppRolesResult;
195 app['showSpinner'] = false;
196 for (var i = 0; i < app['appRoles'].length; i++) {
197 app['appRoles'][i]['appId'] = app.id;
198 if (app['appRoles'][i].roleName.indexOf('global_') != -1) {
199 app['appRoles'][i].roleName = '*' + app['appRoles'][i].roleName;
201 if (app['appRoles'][i].isApplied)
202 this.modelSelectedRoles.push(app['appRoles'][i]);
204 }).catch((err: HttpErrorResponse) => {
205 app['isError'] = true;
206 app['showSpinner'] = false;
207 if (err.status == 200 || err.message.toLowerCase().includes("rollback"))
208 app['errorMessage'] = 'Error: ' + 500;
210 app['errorMessage'] = 'Error: ' + err.status;
212 this.numberAppsProcessed++;
213 if (this.numberAppsProcessed === this.adminApps.length) {
214 this.originalSelectedRoles = this.modelSelectedRoles;
215 this.showSpinner = false;
219 this.userAdminAppsSource = new MatTableDataSource(this.adminApps);
225 updateUserAppsRoles() {
226 this.anyChanges = false;
227 if (!this.changedSelectedUser ||
228 (this.changedSelectedUser.orgUserId == undefined && !this.isSystemUserCheck) ||
232 this.showSpinner = true;
233 this.numberAppsProcessed = 0;
234 this.numberAppsSucceeded = 0;
235 this.adminApps.forEach(app => {
237 app.isUpdating = true;
238 for (var i = 0; i < app.appRoles.length; i++) {
239 if (app.appRoles[i].roleName.indexOf('*global_') != -1) {
240 app.appRoles[i].roleName = app.appRoles[i].roleName.replace('*', '');
243 var newUserAppRoles = {
244 orgUserId: this.orgUserIdValue,
246 appRoles: app.appRoles,
248 isSystemUser: this.isSystemUserCheck
250 this.usersService.updateUserAppRoles(newUserAppRoles).toPromise()
251 .then((res: any) => {
252 if (res.status && res.status.toLowerCase() === 'error') {
253 const modalErrorRef = this.ngbModal.open(ConfirmationModalComponent);
254 modalErrorRef.componentInstance.message = "Error: " + res.message;
256 app.isUpdating = false;
257 app.isDoneUpdating = true;
258 this.numberAppsSucceeded++;
260 var errorMessage = 'Failed to update the user application roles: ' + err;
261 if (err.status == 504) {
262 this.numberAppsSucceeded++;
263 errorMessage = 'Request is being processed, please check back later!';
265 app.isErrorUpdating = true;
267 const modalErrorRef = this.ngbModal.open(ConfirmationModalComponent);
268 modalErrorRef.componentInstance.message = errorMessage;
270 this.numberAppsProcessed++;
271 if (this.numberAppsProcessed === this.adminApps.length) {
272 this.showSpinner = false; // hide the spinner
274 if (this.numberAppsSucceeded === this.adminApps.length) {
275 this.passBackNewUserPopup.emit();
276 this.activeModal.close('Close');//close and resolve dialog promise with true (to update the table)
280 app.noChanges = true;
281 app.isError = false; //remove the error message; just show the No Changes messages
282 this.numberAppsProcessed++;
283 this.numberAppsSucceeded++;
284 if (this.numberAppsProcessed === this.adminApps.length) {
285 this.showSpinner = false; // hide the spinner
287 if (this.numberAppsSucceeded === this.adminApps.length) {
288 this.activeModal.close('Close');
295 let appMessage = this.changedSelectedUser.firstName + ' ' + this.changedSelectedUser.lastName;
296 const ngbModalConfirm = this.ngbModal.open(InformationModalComponent);
297 ngbModalConfirm.componentInstance.title = 'Confirmation';
298 ngbModalConfirm.componentInstance.message = 'Are you sure you want to delete ' + appMessage;
299 ngbModalConfirm.result.then((_res) => {
301 this.anyChanges = true;
302 app.isChanged = true;
303 app.isDeleted = true; // use this to hide the app in the display
304 app.appRoles.forEach((role) => {
305 role.isApplied = false;
307 // remove app roles if user app delete option is selected
308 this.modelSelectedRoles.forEach((_role, index) => {
309 if (_role.appId === app.id) this.modelSelectedRoles.splice(index, 1);
311 this.roleSelectChange(app);
314 // $log.error('NewUserModalCtrl::deleteApp error: ',err);
315 const ngbModalError = this.ngbModal.open(InformationModalComponent);
316 ngbModalError.componentInstance.title = 'Error';
317 ngbModalError.componentInstance.message = 'There was a problem deleting the the applications. ' +
318 'Please try again later. Error: ' + err.status;