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, ViewChild } from '@angular/core';
39 import { RoleService, ApplicationsService } from 'src/app/shared/services';
40 import { HttpErrorResponse, HttpClient } from '@angular/common/http';
41 import { MatTableDataSource, MatSort, MatPaginator } from '@angular/material';
42 import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
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 { environment } from 'src/environments/environment';
46 import { BulkUploadRoleComponent } from './bulk-upload-role/bulk-upload-role.component';
47 import { AddRoleComponent } from './add-role/add-role.component';
51 templateUrl: './role.component.html',
52 styleUrls: ['./role.component.scss']
54 export class RoleComponent implements OnInit {
56 selectedCentralizedApp: any;
59 availableRoles: any[];
60 syncRolesApplied: boolean;
61 displayedColumns: string[] = ['name', 'priority', 'active', 'edit', 'delete'];
62 roleDataSource = new MatTableDataSource(this.availableRoles);
63 @ViewChild(MatSort) sort: MatSort;
64 @ViewChild(MatPaginator) paginator: MatPaginator;
66 api = environment.api;
67 availableRoleFunctions: any;
68 constructor(private roleService: RoleService, private applicationsService: ApplicationsService, public ngbModal: NgbModal, public http: HttpClient) { }
71 this.centralizedApps = [];
72 this.availableRoles = [];
74 this.selectedCentralizedApp = '';
75 this.getCentralizedApps(sessionStorage.userId);
78 toggleRole(_element) {
79 let activeOrInactive = (_element.active) ? 'activate' : 'inactivate';
80 const modalInfoRef = this.ngbModal.open(InformationModalComponent);
81 modalInfoRef.componentInstance.title = 'Confirmation';
82 modalInfoRef.componentInstance.message = 'You are about to ' + activeOrInactive + ' the role ' + _element.name + '. Do you want to continue?';
83 modalInfoRef.result.then((_res) => {
85 var uuu = this.api.toggleRole + '/' + this.selectedCentralizedApp + '/' + _element.id;
87 appId: this.selectedCentralizedApp,
90 this.http.post(uuu, postData).toPromise().then((data: any) => {
91 if (typeof data === 'object' && data.restcallStatus == 'Success') {
92 this.availableRoles = data.availableRoles;
93 this.roleDataSource = new MatTableDataSource(this.availableRoles);
94 this.roleDataSource.sort = this.sort;
95 this.roleDataSource.paginator = this.paginator;
96 // $log.debug('role::availableRoles:'+$scope.availableRoles);
98 _element.active = !_element.active;
99 const modalErrorRef = this.ngbModal.open(ConfirmationModalComponent);
100 modalErrorRef.componentInstance.title = 'Error';
101 modalErrorRef.componentInstance.message = 'Error while saving. ' + data.restCallStatus;
105 // debug.log('response:'+response);
106 _element.active = !_element.active;
107 const modalErrorRef = this.ngbModal.open(ConfirmationModalComponent);
108 modalErrorRef.componentInstance.title = 'Error';
109 modalErrorRef.componentInstance.message = 'Error while saving. ' + response.restCallStatus;
112 _element.active = !_element.active;
120 openBulkUploadRolesAndFunctionsModal() {
121 const modalBulkUploadRole = this.ngbModal.open(BulkUploadRoleComponent);
122 modalBulkUploadRole.componentInstance.title = 'Bulk Upload Role-Function';
123 modalBulkUploadRole.componentInstance.dialogState = 1;
124 modalBulkUploadRole.componentInstance.appId = this.selectedCentralizedApp;
127 editRoleModalPopup(_element) {
128 this.showSpinner = true;
129 this.roleService.getRole(this.selectedCentralizedApp, _element.id).toPromise().then((data: any) => {
130 this.showSpinner = false;
131 var response = JSON.parse(data.data);
132 var availableRoleFunctions = JSON.parse(response.availableRoleFunctions);
133 const ngbModalCreateRole = this.ngbModal.open(AddRoleComponent);
134 ngbModalCreateRole.componentInstance.title = 'Role';
135 ngbModalCreateRole.componentInstance.dialogState = 2;
136 ngbModalCreateRole.componentInstance.availableRole = _element;
137 ngbModalCreateRole.componentInstance.appRoleFunctions = availableRoleFunctions;
138 ngbModalCreateRole.componentInstance.appId = this.selectedCentralizedApp;
139 ngbModalCreateRole.componentInstance.passBackAddRolePopup.subscribe((_result: any) => {
140 this.showSpinner = true;
141 this.getAppRoles(_result);
142 }, (_reason: any) => {
146 this.showSpinner = false;
150 addRoleModalPopup() {
151 const ngbModalCreateRole = this.ngbModal.open(AddRoleComponent);
152 ngbModalCreateRole.componentInstance.title = 'Role';
153 ngbModalCreateRole.componentInstance.appId = this.selectedCentralizedApp;
154 ngbModalCreateRole.componentInstance.passBackAddRolePopup.subscribe((_result: any) => {
155 this.showSpinner = true;
156 this.getAppRoles(_result);
157 }, (_reason: any) => {
163 removeRole(_element) {
164 if ((this.selectedCentralizedApp !== 1) && (_element.name.indexOf('global_') !== -1)) {
165 const modalInfoRef = this.ngbModal.open(ConfirmationModalComponent);
166 modalInfoRef.componentInstance.title = 'Confirmation';
167 modalInfoRef.componentInstance.message = 'Global role cannot be deleted.';
170 const modalInfoRef = this.ngbModal.open(InformationModalComponent);
171 modalInfoRef.componentInstance.title = 'Confirmation';
172 modalInfoRef.componentInstance.message = 'You are about to delete the role ' + _element.name + ' . Do you want to continue?';
173 modalInfoRef.result.then((_res) => {
175 var uuu = this.api.removeRole + '/' + this.selectedCentralizedApp + '/' + _element.id;
177 appId: this.selectedCentralizedApp,
178 availableRoleId: _element.id
180 this.http.post(uuu, postData).toPromise().then((data: any) => {
181 if (typeof data === 'object' && data.restCallStatus == 'Success') {
182 this.availableRoles = data.availableRoles;
183 this.roleDataSource = new MatTableDataSource(this.availableRoles);
184 this.roleDataSource.sort = this.sort;
185 this.roleDataSource.paginator = this.paginator;
187 const modalErrorRef = this.ngbModal.open(ConfirmationModalComponent);
188 modalErrorRef.componentInstance.title = 'Error';
189 modalErrorRef.componentInstance.message = 'Failed to remove role. ' + data.error;
192 const modalErrorRef = this.ngbModal.open(ConfirmationModalComponent);
193 modalErrorRef.componentInstance.title = 'Error';
194 modalErrorRef.componentInstance.message = 'Error while deleting: ' + _err.error;
204 getCentralizedApps(userId) {
205 this.showSpinner = true;
206 this.roleService.getCentralizedApps(userId).toPromise().then((res: any) => {
207 if (res.length > 0) {
208 this.centralizedApps = res;
209 this.selectedCentralizedApp = this.centralizedApps[0].appId;
210 this.getRolesForSelectedCentralizedApp(this.centralizedApps[0].appId);
213 this.showSpinner = false;
214 // $log.error('RoleListCtrl::centralizedApps retrieval error: ', err);
218 syncRolesFromExternalAuthSystem() {
219 this.applicationsService.syncRolesEcompFromExtAuthSystem(this.selectedCentralizedApp).toPromise().then((res: any) => {
220 if (res.status == 'OK') {
221 const modalInfoRef = this.ngbModal.open(InformationModalComponent);
222 modalInfoRef.componentInstance.title = 'Success';
223 modalInfoRef.componentInstance.message = 'Sync operation completed successfully!';
224 modalInfoRef.result.then((_res) => {
226 this.getRolesForSelectedCentralizedApp(this.selectedCentralizedApp);
231 const modalErrorRef = this.ngbModal.open(ConfirmationModalComponent);
232 modalErrorRef.componentInstance.title = 'Error';
233 modalErrorRef.componentInstance.message = 'Sync operation failed for ' + this.appName + 'res.message';
236 const modalErrorRef = this.ngbModal.open(ConfirmationModalComponent);
237 modalErrorRef.componentInstance.title = 'Error';
238 modalErrorRef.componentInstance.message = 'Sync operation failed for ' + this.appName + 'err.message';
242 getRolesForSelectedCentralizedApp(val) {
243 this.showSpinner = true;
244 this.availableRoles = [];
245 this.roleDataSource = new MatTableDataSource(this.availableRoles);
246 this.applicationsService.getSingleAppInfoById(val).subscribe((res: any) => {
247 this.appName = res.name;
248 if (res.rolesInAAF == true) {
249 this.syncRolesApplied = true;
252 this.getAppRoles(val);
255 private getAppRoles(val: any) {
256 this.roleService.getRoles(val).subscribe((data: any) => {
259 j = JSON.parse(j.data);
260 this.availableRoles = j.availableRoles;
261 this.roleDataSource = new MatTableDataSource(this.availableRoles);
262 this.roleDataSource.sort = this.sort;
263 this.roleDataSource.paginator = this.paginator;
264 this.showSpinner = false;
267 this.showSpinner = false;
268 const modalErrorRef = this.ngbModal.open(ConfirmationModalComponent);
269 modalErrorRef.componentInstance.title = 'Error';
270 modalErrorRef.componentInstance.message = 'Failed to get ' + this.appName + ' roles. Please try again later!';
272 }, (error: HttpErrorResponse) => {
273 this.showSpinner = false;
274 const modalErrorRef = this.ngbModal.open(ConfirmationModalComponent);
275 modalErrorRef.componentInstance.title = 'Error';
276 modalErrorRef.componentInstance.message = 'Failed to get ' + this.appName + ' roles. Please try again later!';