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, Output, EventEmitter } from '@angular/core';
39 import { NgbActiveModal, NgbModal } from '@ng-bootstrap/ng-bootstrap';
40 import { RoleFunction, Role } from 'src/app/shared/model';
41 import { ConfirmationModalComponent } from 'src/app/modals/confirmation-modal/confirmation-modal.component';
42 import { environment } from 'src/environments/environment';
43 import { HttpClient } from '@angular/common/http';
44 import { InformationModalComponent } from 'src/app/modals/information-modal/information-modal.component';
47 selector: 'app-role-function-modal',
48 templateUrl: './role-function-modal.component.html',
49 styleUrls: ['./role-function-modal.component.scss']
51 export class RoleFunctionModalComponent implements OnInit {
53 @Input() title: string;
55 @Input() dialogState: number;
56 @Input() currentRoleFunctions: any;
57 @Input() editRoleFunction: RoleFunction;
58 @Output() passBackRoleFunctionPopup: EventEmitter<any> = new EventEmitter();
59 roleFunction: RoleFunction;
60 otherTypeValue: string;
61 typeOptions: string[] = ['menu', 'url', 'other'];
62 api = environment.api;
67 createOrUpdate: string;
68 constructor(public activeModal: NgbActiveModal, public ngbModal: NgbModal, public http: HttpClient) { }
71 this.createOrUpdate = 'create';
72 this.selectedType = 'menu';
73 this.roleFunction = new RoleFunction(this.selectedType, '', '*', '');
74 this.otherTypeValue = '';
75 if (this.editRoleFunction) {
76 this.createOrUpdate = 'update';
77 this.editDisable = true;
78 this.selectedType = this.editRoleFunction.type;
79 if (this.editRoleFunction.type !== 'menu' && this.editRoleFunction.type !== 'url') {
80 this.selectedType = 'other';
81 this.otherTypeValue = this.editRoleFunction.type;
83 this.roleFunction = new RoleFunction(this.editRoleFunction.type, this.editRoleFunction.code, this.editRoleFunction.action, this.editRoleFunction.name);
88 if (/[^a-zA-Z0-9\-\.\_]/.test(this.roleFunction.type) && this.selectedType === 'other') {
89 this.openConfirmationModal('Confirmation', 'Type can only contain alphanumeric characters, dots(.) and underscores(_)');
92 this.roleFunction.type = this.selectedType;
94 if (this.roleFunction.action !== '*' && /[^a-zA-Z0-9\-\.\_]/.test(this.roleFunction.action)) {
95 this.openConfirmationModal('Confirmation', 'Action can only contain alphanumeric characters, hyphens(-), dots(.) and underscores(_) and single asterisk character(*)');
98 if (/[^a-zA-Z0-9\-\:\_\./*]/.test(this.roleFunction.code)) {
99 this.openConfirmationModal('Confirmation', 'Instance can only contain alphanumeric characters, hyphens(-), dots(.), colons(:), forwardSlash(/) , asterisk(*) and underscores(_)');
102 const modalInfoRef = this.ngbModal.open(InformationModalComponent);
103 modalInfoRef.componentInstance.title = 'Confirmation';
104 modalInfoRef.componentInstance.message = 'You are about to ' + this.createOrUpdate + ' the role function ' + this.roleFunction.name + '. Do you want to continue?';
105 modalInfoRef.result.then((_res) => {
107 this.showSpinner = true;
108 var uuu = this.api.saveRoleFunction.replace(':appId', this.appId);
109 var postData = this.roleFunction;
110 var exists = false, x;
111 for (x in this.currentRoleFunctions) {
112 if (this.currentRoleFunctions[x].type == this.roleFunction.type
113 && this.currentRoleFunctions[x].code == this.roleFunction.code
114 && this.currentRoleFunctions[x].action == this.roleFunction.action
115 && this.currentRoleFunctions[x].name == this.roleFunction.name) {
116 this.openConfirmationModal('Confirmation', "Role Function already exist.");
118 this.showSpinner = false;
121 if (!this.editDisable) {
122 if (this.currentRoleFunctions[x].type == this.roleFunction.type
123 && this.currentRoleFunctions[x].code == this.roleFunction.code
124 && this.currentRoleFunctions[x].action == this.roleFunction.action
126 this.openConfirmationModal('Confirmation', "Please make sure code, type and action is unique. Please create a role function with a different code or type or action to proceed.");
128 this.showSpinner = false;
133 if (this.selectedType === 'other')
134 this.roleFunction.type = this.otherTypeValue;
135 if (!exists && this.roleFunction.name.trim() != '' && this.roleFunction.code.trim() != '') {
136 this.http.post(uuu, JSON.stringify(postData)).toPromise().then((res: any) => {
137 if (res.status == 'OK') {
138 this.showSpinner = false;
139 if (this.editRoleFunction) {
140 this.editRoleFunction.name = this.roleFunction.name;
141 this.passBackRoleFunctionPopup.emit(this.editRoleFunction);
143 this.passBackRoleFunctionPopup.emit(this.roleFunction);
145 this.openConfirmationModal('Success', res.message);
147 this.showSpinner = false;
148 this.openConfirmationModal('Error', res.message);
160 openConfirmationModal(_title: string, _message: string) {
161 const modalInfoRef = this.ngbModal.open(ConfirmationModalComponent);
162 modalInfoRef.componentInstance.title = _title;
163 modalInfoRef.componentInstance.message = _message;