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 { isNullOrUndefined } from 'util';
22 import { HttpClient, HttpHeaders } from '@angular/common/http';
23 import { Component, Injector, Input, OnInit } from '@angular/core';
24 import { FormBuilder, FormGroup, Validators } from '@angular/forms';
25 import { ActivatedRoute, Router } from '@angular/router';
26 import { TranslateService } from '@ngx-translate/core';
27 import { NotifierService } from 'angular-notifier';
28 import { APIURLHEADER, ERRORDATA } from 'CommonModel';
29 import { environment } from 'environment';
30 import * as jsonpath from 'jsonpath';
31 import { RestService } from 'RestService';
32 import { Permission, PermissionGroup, RoleConfig, RoleData } from 'RolesModel';
33 import { SharedService } from 'SharedService';
37 * @Component takes RolesCreateEditComponent.html as template url
40 selector: 'app-roles-create-edit',
41 templateUrl: './RolesCreateEditComponent.html',
42 styleUrls: ['./RolesCreateEditComponent.scss']
44 /** Exporting a class @exports RolesCreateEditComponent */
45 export class RolesCreateEditComponent implements OnInit {
46 /** To inject services @public */
47 public injector: Injector;
49 /** contains role permissions from config file @public */
50 public rolePermissions: {}[];
52 /** contains role form group information @public */
53 public roleForm: FormGroup;
55 /** Instance of the rest service @public */
56 public restService: RestService;
58 /** Check the loading results for loader status @public */
59 public isLoadingResults: boolean = false;
61 /** Give the message for the loading @public */
62 public message: string = 'PLEASEWAIT';
64 /** Instance to check role form submitted status @public */
65 public submitted: boolean = false;
67 /** Contains role create or edit value @public */
68 public getRoleType: string;
70 /** Contains view selection value either text or preview @public */
71 public viewMode: string = 'text';
73 /** Contains role id value @private */
74 private roleRef: string;
76 /** Notifier service to popup notification @private */
77 private notifierService: NotifierService;
79 /** Contains tranlsate instance @private */
80 private translateService: TranslateService;
82 /** contians form builder module @private */
83 private formBuilder: FormBuilder;
85 /** Contains all methods related to shared @private */
86 private sharedService: SharedService;
88 /** Controls the header form @private */
89 private headers: HttpHeaders;
91 /** Varaibles to hold http client @private */
92 private httpClient: HttpClient;
94 /** Holds the instance of activatedRoute of router service @private */
95 private activatedRoute: ActivatedRoute;
97 /** Holds the instance of roter service @private */
98 private router: Router;
100 constructor(injector: Injector) {
101 this.injector = injector;
102 this.restService = this.injector.get(RestService);
103 this.notifierService = this.injector.get(NotifierService);
104 this.translateService = this.injector.get(TranslateService);
105 this.formBuilder = this.injector.get(FormBuilder);
106 this.sharedService = this.injector.get(SharedService);
107 this.httpClient = this.injector.get(HttpClient);
108 this.activatedRoute = this.injector.get(ActivatedRoute);
109 this.router = this.injector.get(Router);
112 /** Lifecyle Hooks the trigger before component is instantiate @public */
113 public ngOnInit(): void {
114 this.headers = new HttpHeaders({
115 'Content-Type': 'application/json',
116 Accept: 'application/json',
117 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
119 this.roleForm = this.formBuilder.group({
120 roleName: ['', [Validators.required]],
123 this.getRolePermissions();
126 /** Get role permission information based on action @public */
127 public getRolePermissions(): void {
128 this.isLoadingResults = true;
129 this.loadPermissions().then((response: RoleConfig) => {
130 this.rolePermissions = response.rolePermissions;
131 if (this.activatedRoute.snapshot.url[0].path === 'create') {
132 this.getRoleType = 'Add';
133 this.isLoadingResults = false;
135 this.getRoleType = 'Edit';
143 /** Check Role Create or Edit and proceed action @public */
144 public roleCheck(): void {
145 this.submitted = true;
146 if (!this.roleForm.valid) {
147 const errorIp: Element = document.querySelector('.ng-invalid[formControlName]');
149 errorIp.scrollIntoView({ behavior: 'smooth', block: 'center' });
152 if (this.getRoleType === 'Add') {
160 /** Convenience getter for easy access to form fields */
161 get f(): FormGroup['controls'] { return this.roleForm.controls; }
163 /** Create role @private */
164 public createRole(): void {
165 const apiURLHeader: APIURLHEADER = {
166 url: environment.ROLES_URL,
167 httpOptions: { headers: this.headers }
169 let permissionData: Object = {};
170 this.sharedService.cleanForm(this.roleForm);
171 if (!this.roleForm.invalid) {
172 if (this.viewMode === 'preview') {
173 this.isLoadingResults = true;
174 permissionData = this.generatePermissions();
175 this.roleCreateAPI(apiURLHeader, permissionData);
177 if (this.checkPermission()) {
178 this.isLoadingResults = true;
179 permissionData = this.roleForm.value.permissions !== '' ? JSON.parse(this.roleForm.value.permissions) : {};
180 this.roleCreateAPI(apiURLHeader, permissionData);
186 /** Method to handle role create API call @public */
187 public roleCreateAPI(apiURLHeader: APIURLHEADER, permissionData: {}): void {
188 const postData: {} = {
189 name: this.roleForm.value.roleName,
190 permissions: !isNullOrUndefined(permissionData) ? permissionData : {}
192 this.restService.postResource(apiURLHeader, postData).subscribe(() => {
193 this.notifierService.notify('success', this.translateService.instant('PAGE.ROLES.CREATEDSUCCESSFULLY'));
194 this.router.navigate(['/roles/details']).catch((): void => {
195 // Catch Navigation Error
197 }, (error: ERRORDATA) => {
198 this.isLoadingResults = false;
199 this.restService.handleError(error, 'post');
203 /** Edit role @private */
204 public editRole(): void {
205 const apiURLHeader: APIURLHEADER = {
206 url: environment.ROLES_URL + '/' + this.roleRef,
207 httpOptions: { headers: this.headers }
209 let permissionData: Object = {};
210 this.sharedService.cleanForm(this.roleForm);
211 if (!this.roleForm.invalid) {
212 if (this.viewMode === 'preview') {
213 this.isLoadingResults = true;
214 permissionData = this.generatePermissions();
215 this.roleEditAPI(apiURLHeader, permissionData);
217 if (this.checkPermission()) {
218 this.isLoadingResults = true;
219 permissionData = this.roleForm.value.permissions !== '' ? JSON.parse(this.roleForm.value.permissions) : {};
220 this.roleEditAPI(apiURLHeader, permissionData);
226 /** Method to handle role edit API call */
227 public roleEditAPI(apiURLHeader: APIURLHEADER, permissionData: {}): void {
228 const postData: {} = {
229 name: this.roleForm.value.roleName,
230 permissions: !isNullOrUndefined(permissionData) ? permissionData : {}
232 this.restService.patchResource(apiURLHeader, postData).subscribe(() => {
233 this.notifierService.notify('success', this.translateService.instant('PAGE.ROLES.UPDATEDSUCCESSFULLY'));
234 this.router.navigate(['/roles/details']).catch((): void => {
235 // Catch Navigation Error
237 }, (error: ERRORDATA) => {
238 this.isLoadingResults = false;
239 this.restService.handleError(error, 'patch');
243 /** Method to handle toggle role view selection @public */
244 public viewSelection(): void {
245 if (this.viewMode === 'text' && this.checkPermission()) {
246 this.loadPermissions().then((response: RoleConfig) => {
247 this.rolePermissions = response.rolePermissions;
248 const permissions: {} = this.roleForm.value.permissions !== '' ? JSON.parse(this.roleForm.value.permissions) : {};
249 this.filterRoleData(permissions);
250 this.viewMode = 'preview';
255 const rolePermission: {} = this.generatePermissions();
256 if (Object.keys(rolePermission).length !== 0) {
257 this.roleForm.patchValue({ permissions: JSON.stringify(rolePermission, null, '\t') });
259 this.viewMode = 'text';
263 /** Generate role permission post data @private */
264 private generatePermissions(): Object {
265 const permissions: Object = {};
266 this.rolePermissions.forEach((permissionGroup: PermissionGroup) => {
267 if (!isNullOrUndefined(permissionGroup)) {
268 permissionGroup.permissions.forEach((permission: Permission) => {
269 if (!isNullOrUndefined(permission.value) && permission.value !== 'NA') {
270 permissions[permission.operation] = permission.value;
278 /** Validation method for check permission json string @private */
279 private checkPermission(): boolean {
280 if (this.roleForm.value.permissions) {
281 if (!this.sharedService.checkJson(this.roleForm.value.permissions)) {
282 this.notifierService.notify('error', this.translateService.instant('PAGE.ROLES.ROLEJSONERROR'));
285 this.roleForm.value.permissions = this.roleForm.value.permissions.replace(/'/g, '"');
286 const rolePermission: {} = JSON.parse(this.roleForm.value.permissions);
287 for (const key of Object.keys(rolePermission)) {
288 // eslint-disable-next-line security/detect-object-injection
289 if (typeof rolePermission[key] !== 'boolean') {
290 this.notifierService.notify('error', this.translateService.instant('PAGE.ROLES.ROLEKEYERROR', { roleKey: key }));
299 /** Get role data from NBI based on ID and apply filter @private */
300 private getRoleData(): void {
301 this.roleRef = this.activatedRoute.snapshot.paramMap.get('id');
302 if (!isNullOrUndefined(this.roleRef)) {
303 this.restService.getResource(environment.ROLES_URL + '/' + this.roleRef).subscribe((data: RoleData) => {
304 this.roleForm.setValue({ roleName: data.name, permissions: JSON.stringify(data.permissions, null, '\t') });
305 this.filterRoleData(data.permissions);
306 this.isLoadingResults = false;
307 }, (error: ERRORDATA) => {
308 this.router.navigate(['/roles/details']).catch((): void => {
309 // Catch Navigation Error
311 this.restService.handleError(error, 'get');
316 /** Method to filter role data @private */
317 private filterRoleData(permissions: {}): void {
318 Object.keys(permissions).forEach((permission: string) => {
319 jsonpath.apply(this.rolePermissions, '$..permissions[?(@.operation == "' + permission + '")]', (response: Permission) => {
320 if (response.operation === permission) {
321 // eslint-disable-next-line security/detect-object-injection
322 response.value = permissions[permission];
329 /** Method to load the role permission Json file @private */
330 private async loadPermissions(): Promise<Object> {
331 const jsonFile: string = environment.PERMISSIONS_CONFIG_FILE + '?cb=' + new Date().getTime();
332 return new Promise<Object>((resolve: Function, reject: Function): void => {
333 this.httpClient.get(jsonFile).subscribe((response: Response) => {