2 Copyright 2020 TATA ELXSI
4 Licensed under the Apache License, Version 2.0 (the 'License');
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
16 Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in)
19 * @file Roles Create and Edit Component
21 import { HttpClient, HttpHeaders } from '@angular/common/http';
22 import { Component, Injector, Input, OnInit } from '@angular/core';
23 import { FormBuilder, FormGroup, Validators } from '@angular/forms';
24 import { ActivatedRoute, Router } from '@angular/router';
25 import { TranslateService } from '@ngx-translate/core';
26 import { NotifierService } from 'angular-notifier';
27 import { APIURLHEADER, ERRORDATA } from 'CommonModel';
28 import { environment } from 'environment';
29 import * as jsonpath from 'jsonpath';
30 import { RestService } from 'RestService';
31 import { Permission, PermissionGroup, RoleConfig, RoleData } from 'RolesModel';
32 import { SharedService, isNullOrUndefined } from 'SharedService';
36 * @Component takes RolesCreateEditComponent.html as template url
39 selector: 'app-roles-create-edit',
40 templateUrl: './RolesCreateEditComponent.html',
41 styleUrls: ['./RolesCreateEditComponent.scss']
43 /** Exporting a class @exports RolesCreateEditComponent */
44 export class RolesCreateEditComponent implements OnInit {
45 /** To inject services @public */
46 public injector: Injector;
48 /** contains role permissions from config file @public */
49 public rolePermissions: {}[];
51 /** contains role form group information @public */
52 public roleForm: FormGroup;
54 /** Instance of the rest service @public */
55 public restService: RestService;
57 /** Check the loading results for loader status @public */
58 public isLoadingResults: boolean = false;
60 /** Give the message for the loading @public */
61 public message: string = 'PLEASEWAIT';
63 /** Instance to check role form submitted status @public */
64 public submitted: boolean = false;
66 /** Contains role create or edit value @public */
67 public getRoleType: string;
69 /** Contains view selection value either text or preview @public */
70 public viewMode: string = 'text';
72 /** Contains current permissions data @private */
73 private currentPermissions: string;
75 /** Contains current role name @private */
76 private currentRoleName: string;
78 /** Contains modified role name @private */
79 private modifiedRoleName: string;
81 /** Contains role id value @private */
82 private roleRef: string;
84 /** Notifier service to popup notification @private */
85 private notifierService: NotifierService;
87 /** Contains tranlsate instance @private */
88 private translateService: TranslateService;
90 /** contians form builder module @private */
91 private formBuilder: FormBuilder;
93 /** Contains all methods related to shared @private */
94 private sharedService: SharedService;
96 /** Controls the header form @private */
97 private headers: HttpHeaders;
99 /** Varaibles to hold http client @private */
100 private httpClient: HttpClient;
102 /** Holds the instance of activatedRoute of router service @private */
103 private activatedRoute: ActivatedRoute;
105 /** Holds the instance of roter service @private */
106 private router: Router;
108 constructor(injector: Injector) {
109 this.injector = injector;
110 this.restService = this.injector.get(RestService);
111 this.notifierService = this.injector.get(NotifierService);
112 this.translateService = this.injector.get(TranslateService);
113 this.formBuilder = this.injector.get(FormBuilder);
114 this.sharedService = this.injector.get(SharedService);
115 this.httpClient = this.injector.get(HttpClient);
116 this.activatedRoute = this.injector.get(ActivatedRoute);
117 this.router = this.injector.get(Router);
120 /** Lifecyle Hooks the trigger before component is instantiate @public */
121 public ngOnInit(): void {
122 this.headers = new HttpHeaders({
123 'Content-Type': 'application/json',
124 Accept: 'application/json',
125 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
127 this.roleForm = this.formBuilder.group({
128 roleName: ['', [Validators.required]],
131 this.getRolePermissions();
134 /** Get role permission information based on action @public */
135 public getRolePermissions(): void {
136 this.isLoadingResults = true;
137 this.loadPermissions().then((response: RoleConfig) => {
138 this.rolePermissions = response.rolePermissions;
139 if (this.activatedRoute.snapshot.url[0].path === 'create') {
140 this.getRoleType = 'Add';
141 this.isLoadingResults = false;
143 this.getRoleType = 'Edit';
151 /** Check Role Create or Edit and proceed action @public */
152 public roleCheck(): void {
153 this.submitted = true;
154 if (!this.roleForm.valid) {
155 const errorIp: Element = document.querySelector('.ng-invalid[formControlName]');
157 errorIp.scrollIntoView({ behavior: 'smooth', block: 'center' });
160 if (this.getRoleType === 'Add') {
168 /** Convenience getter for easy access to form fields */
169 get f(): FormGroup['controls'] { return this.roleForm.controls; }
171 /** Create role @private */
172 public createRole(): void {
173 const apiURLHeader: APIURLHEADER = {
174 url: environment.ROLES_URL,
175 httpOptions: { headers: this.headers }
177 let permissionData: Object = {};
178 this.sharedService.cleanForm(this.roleForm);
179 if (!this.roleForm.invalid) {
180 if (this.viewMode === 'preview') {
181 this.isLoadingResults = true;
182 permissionData = this.generatePermissions();
183 this.roleCreateAPI(apiURLHeader, permissionData);
185 if (this.checkPermission()) {
186 this.isLoadingResults = true;
187 permissionData = this.roleForm.value.permissions !== '' ? JSON.parse(this.roleForm.value.permissions) : {};
188 this.roleCreateAPI(apiURLHeader, permissionData);
194 /** Method to handle role create API call @public */
195 public roleCreateAPI(apiURLHeader: APIURLHEADER, permissionData: {}): void {
196 const postData: {} = {
197 name: this.roleForm.value.roleName,
198 permissions: !isNullOrUndefined(permissionData) ? permissionData : {}
200 this.restService.postResource(apiURLHeader, postData).subscribe(() => {
201 this.notifierService.notify('success', this.translateService.instant('PAGE.ROLES.CREATEDSUCCESSFULLY'));
202 this.router.navigate(['/roles/details']).catch((): void => {
203 // Catch Navigation Error
205 }, (error: ERRORDATA) => {
206 this.isLoadingResults = false;
207 this.restService.handleError(error, 'post');
211 /** Edit role @private */
212 public editRole(): void {
213 const apiURLHeader: APIURLHEADER = {
214 url: environment.ROLES_URL + '/' + this.roleRef,
215 httpOptions: { headers: this.headers }
217 this.modifiedRoleName = this.roleForm.value.roleName;
218 let permissionData: Object = {};
219 this.sharedService.cleanForm(this.roleForm);
220 if (!this.roleForm.invalid) {
221 if (this.viewMode === 'preview') {
222 permissionData = this.generatePermissions();
223 this.roleEditAPI(apiURLHeader, permissionData);
225 if (this.checkPermission()) {
226 permissionData = this.roleForm.value.permissions !== '' ? JSON.parse(this.roleForm.value.permissions) : {};
227 this.roleEditAPI(apiURLHeader, permissionData);
233 /** Method to handle role edit API call */
234 public roleEditAPI(apiURLHeader: APIURLHEADER, permissionData: {}): void {
235 if ((JSON.stringify(permissionData, null, '\t') === this.currentPermissions) && (this.modifiedRoleName === this.currentRoleName)) {
236 this.notifierService.notify('warning', this.translateService.instant('PAGE.TOPOLOGY.DATAEMPTY'));
239 this.isLoadingResults = true;
240 const postData: {} = {
241 name: this.roleForm.value.roleName,
242 permissions: !isNullOrUndefined(permissionData) ? permissionData : {}
244 this.restService.patchResource(apiURLHeader, postData).subscribe(() => {
245 this.notifierService.notify('success', this.translateService.instant('PAGE.ROLES.UPDATEDSUCCESSFULLY'));
246 this.router.navigate(['/roles/details']).catch((): void => {
247 // Catch Navigation Error
249 }, (error: ERRORDATA) => {
250 this.isLoadingResults = false;
251 this.restService.handleError(error, 'patch');
255 /** Method to handle toggle role view selection @public */
256 public viewSelection(): void {
257 if (this.viewMode === 'text' && this.checkPermission()) {
258 this.loadPermissions().then((response: RoleConfig) => {
259 this.rolePermissions = response.rolePermissions;
260 const permissions: {} = this.roleForm.value.permissions !== '' ? JSON.parse(this.roleForm.value.permissions) : {};
261 this.filterRoleData(permissions);
262 this.viewMode = 'preview';
267 const rolePermission: {} = this.generatePermissions();
268 if (Object.keys(rolePermission).length !== 0) {
269 this.roleForm.patchValue({ permissions: JSON.stringify(rolePermission, null, '\t') });
271 this.viewMode = 'text';
275 /** Generate role permission post data @private */
276 private generatePermissions(): Object {
277 const permissions: Object = {};
278 this.rolePermissions.forEach((permissionGroup: PermissionGroup) => {
279 if (!isNullOrUndefined(permissionGroup)) {
280 permissionGroup.permissions.forEach((permission: Permission) => {
281 if (!isNullOrUndefined(permission.value) && permission.value !== 'NA') {
282 permissions[permission.operation] = permission.value;
290 /** Validation method for check permission json string @private */
291 private checkPermission(): boolean {
292 if (this.roleForm.value.permissions) {
293 if (!this.sharedService.checkJson(this.roleForm.value.permissions)) {
294 this.notifierService.notify('error', this.translateService.instant('PAGE.ROLES.ROLEJSONERROR'));
297 this.roleForm.value.permissions = this.roleForm.value.permissions.replace(/'/g, '"');
298 const rolePermission: {} = JSON.parse(this.roleForm.value.permissions);
299 for (const key of Object.keys(rolePermission)) {
300 // eslint-disable-next-line security/detect-object-injection
301 if (typeof rolePermission[key] !== 'boolean') {
302 this.notifierService.notify('error', this.translateService.instant('PAGE.ROLES.ROLEKEYERROR', { roleKey: key }));
311 /** Get role data from NBI based on ID and apply filter @private */
312 private getRoleData(): void {
313 this.roleRef = this.activatedRoute.snapshot.paramMap.get('id');
314 if (!isNullOrUndefined(this.roleRef)) {
315 this.restService.getResource(environment.ROLES_URL + '/' + this.roleRef).subscribe((data: RoleData) => {
316 this.roleForm.setValue({ roleName: data.name, permissions: JSON.stringify(data.permissions, null, '\t') });
317 this.filterRoleData(data.permissions);
318 this.isLoadingResults = false;
319 this.currentRoleName = data.name;
320 this.currentPermissions = JSON.stringify(data.permissions, null, '\t');
321 }, (error: ERRORDATA) => {
322 this.router.navigate(['/roles/details']).catch((): void => {
323 // Catch Navigation Error
325 this.restService.handleError(error, 'get');
330 /** Method to filter role data @private */
331 private filterRoleData(permissions: {}): void {
332 Object.keys(permissions).forEach((permission: string) => {
333 jsonpath.apply(this.rolePermissions, '$..permissions[?(@.operation == "' + permission + '")]', (response: Permission) => {
334 if (response.operation === permission) {
335 // eslint-disable-next-line security/detect-object-injection
336 response.value = permissions[permission];
343 /** Method to load the role permission Json file @private */
344 private async loadPermissions(): Promise<Object> {
345 const jsonFile: string = environment.PERMISSIONS_CONFIG_FILE + '?cb=' + new Date().getTime();
346 return new Promise<Object>((resolve: Function, reject: Function): void => {
347 this.httpClient.get(jsonFile).subscribe((response: Response) => {