blob: a816c624de3b92ab61907c69c8f441635f177311 [file] [log] [blame]
kumaran.m3b4814a2020-05-01 19:48:54 +05301/*
2 Copyright 2020 TATA ELXSI
3
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
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
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.
15
16 Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in)
17*/
18/**
19 * @file Add Edit Component.
20 */
21import { HttpHeaders } from '@angular/common/http';
22import { Component, Injector, Input, OnInit } from '@angular/core';
23import { AbstractControl, FormBuilder, FormGroup, Validators } from '@angular/forms';
24import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
25import { TranslateService } from '@ngx-translate/core';
26import { NotifierService } from 'angular-notifier';
27import { AuthenticationService } from 'AuthenticationService';
Barath Kumar R16070582021-02-08 18:19:35 +053028import { APIURLHEADER, ERRORDATA, LOGINPARAMS, MODALCLOSERESPONSEDATA, TYPESECTION } from 'CommonModel';
kumaran.m3b4814a2020-05-01 19:48:54 +053029import { environment } from 'environment';
30import { RestService } from 'RestService';
SANDHYA.JSc84f1122024-06-04 21:50:03 +053031import { SharedService, isNullOrUndefined } from 'SharedService';
kumaran.m3b4814a2020-05-01 19:48:54 +053032
33/**
34 * Creating component
35 * @Component takes AddEditUserComponent.html as template url
36 */
37@Component({
SANDHYA.JSa9816552022-04-12 09:07:08 +053038 selector: 'app-add-edit-user',
Barath Kumar R16070582021-02-08 18:19:35 +053039 templateUrl: './AddEditUserComponent.html',
40 styleUrls: ['./AddEditUserComponent.scss']
kumaran.m3b4814a2020-05-01 19:48:54 +053041})
42/** Exporting a class @exports AddEditUserComponent */
43export class AddEditUserComponent implements OnInit {
Barath Kumar R16070582021-02-08 18:19:35 +053044 /** To inject services @public */
45 public injector: Injector;
kumaran.m3b4814a2020-05-01 19:48:54 +053046
Barath Kumar R16070582021-02-08 18:19:35 +053047 /** Instance for active modal service @public */
48 public activeModal: NgbActiveModal;
kumaran.m3b4814a2020-05-01 19:48:54 +053049
Barath Kumar R16070582021-02-08 18:19:35 +053050 /** FormGroup user Edit Account added to the form @ html @public */
51 public userForm: FormGroup;
kumaran.m3b4814a2020-05-01 19:48:54 +053052
Barath Kumar R16070582021-02-08 18:19:35 +053053 /** Form submission Add */
54 public submitted: boolean = false;
kumaran.m3b4814a2020-05-01 19:48:54 +053055
Barath Kumar R16070582021-02-08 18:19:35 +053056 /** Input contains Modal dialog component Instance @public */
57 @Input() public userTitle: string;
kumaran.m3b4814a2020-05-01 19:48:54 +053058
Barath Kumar R16070582021-02-08 18:19:35 +053059 /** Input contains Modal dialog component Instance @public */
60 @Input() public userType: string;
kumaran.m3b4814a2020-05-01 19:48:54 +053061
Barath Kumar R16070582021-02-08 18:19:35 +053062 /** Input contains Modal dialog component Instance @public */
63 @Input() public userID: string;
kumaran.m3b4814a2020-05-01 19:48:54 +053064
Barath Kumar R16070582021-02-08 18:19:35 +053065 /** Input contains Modal dialog component Instance @public */
66 @Input() public userName: string;
kumaran.m3b4814a2020-05-01 19:48:54 +053067
Barath Kumar R16070582021-02-08 18:19:35 +053068 /** Check the loading results for loader status @public */
69 public isLoadingResults: boolean = false;
kumaran.m3b4814a2020-05-01 19:48:54 +053070
Barath Kumar R16070582021-02-08 18:19:35 +053071 /** Give the message for the loading @public */
72 public message: string = 'PLEASEWAIT';
kumaran.m3b4814a2020-05-01 19:48:54 +053073
Barath Kumar R16070582021-02-08 18:19:35 +053074 /** Holds list of domains @public */
75 public domains: TYPESECTION[] = [];
kumaran.m3b4814a2020-05-01 19:48:54 +053076
SANDHYA.JSa9816552022-04-12 09:07:08 +053077 /** Variable contains type is changepassword or not @public */
78 public isPassword: boolean;
79
80 /** Variable holds value for first login user @public */
SANDHYA.JS5b35bcd2023-04-27 15:11:06 +053081 public isFirstLogin: boolean = Boolean(sessionStorage.getItem('firstLogin') === 'true');
SANDHYA.JSa9816552022-04-12 09:07:08 +053082
Barath Kumar R16070582021-02-08 18:19:35 +053083 /** Instance of the rest service @private */
84 private restService: RestService;
kumaran.m3b4814a2020-05-01 19:48:54 +053085
Barath Kumar R16070582021-02-08 18:19:35 +053086 /** FormBuilder instance added to the formBuilder @private */
87 private formBuilder: FormBuilder;
kumaran.m3b4814a2020-05-01 19:48:54 +053088
Barath Kumar R16070582021-02-08 18:19:35 +053089 /** Controls the header form @private */
90 private headers: HttpHeaders;
kumaran.m3b4814a2020-05-01 19:48:54 +053091
Barath Kumar R16070582021-02-08 18:19:35 +053092 /** Notifier service to popup notification @private */
93 private notifierService: NotifierService;
kumaran.m3b4814a2020-05-01 19:48:54 +053094
Barath Kumar R16070582021-02-08 18:19:35 +053095 /** Contains tranlsate instance @private */
96 private translateService: TranslateService;
kumaran.m3b4814a2020-05-01 19:48:54 +053097
Barath Kumar R16070582021-02-08 18:19:35 +053098 /** Contains all methods related to shared @private */
99 private sharedService: SharedService;
kumaran.m3b4814a2020-05-01 19:48:54 +0530100
Barath Kumar R16070582021-02-08 18:19:35 +0530101 /** ModalData instance of modal @private */
102 private modalData: MODALCLOSERESPONSEDATA;
kumaran.m3b4814a2020-05-01 19:48:54 +0530103
Barath Kumar R16070582021-02-08 18:19:35 +0530104 /** Utilizes auth service for any auth operations @private */
105 private authService: AuthenticationService;
kumaran.m3b4814a2020-05-01 19:48:54 +0530106
Barath Kumar R16070582021-02-08 18:19:35 +0530107 constructor(injector: Injector) {
108 this.injector = injector;
109 this.formBuilder = this.injector.get(FormBuilder);
110 this.restService = this.injector.get(RestService);
111 this.activeModal = this.injector.get(NgbActiveModal);
112 this.notifierService = this.injector.get(NotifierService);
113 this.translateService = this.injector.get(TranslateService);
114 this.sharedService = this.injector.get(SharedService);
115 this.authService = this.injector.get(AuthenticationService);
kumaran.m3b4814a2020-05-01 19:48:54 +0530116
Barath Kumar R16070582021-02-08 18:19:35 +0530117 /** Initializing Form Action */
118 this.userForm = this.formBuilder.group({
119 userName: ['', Validators.required],
120 password: [null, [Validators.required, Validators.pattern(this.sharedService.REGX_PASSWORD_PATTERN)]],
121 password2: [null, Validators.required],
SANDHYA.JSa9816552022-04-12 09:07:08 +0530122 old_password: [null, Validators.required],
Barath Kumar R16070582021-02-08 18:19:35 +0530123 domain_name: [null]
124 });
kumaran.m3b4814a2020-05-01 19:48:54 +0530125 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530126
Barath Kumar R16070582021-02-08 18:19:35 +0530127 /** convenience getter for easy access to form fields */
128 get f(): FormGroup['controls'] { return this.userForm.controls; }
kumaran.m3b4814a2020-05-01 19:48:54 +0530129
Barath Kumar R16070582021-02-08 18:19:35 +0530130 /** Lifecyle Hooks the trigger before component is instantiate @public */
131 public ngOnInit(): void {
132 this.headers = new HttpHeaders({
133 'Content-Type': 'application/json',
134 Accept: 'application/json',
135 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
136 });
137 if (this.userType === 'add') {
138 this.getDomainList();
139 } else if (this.userType === 'editUserName') {
140 this.userForm.patchValue({ userName: this.userName });
SANDHYA.JSa9816552022-04-12 09:07:08 +0530141 } else if (this.isFirstLogin) {
142 this.isPassword = true;
kumaran.m3b4814a2020-05-01 19:48:54 +0530143 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530144 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530145
Barath Kumar R16070582021-02-08 18:19:35 +0530146 /** On modal submit users acction will called @public */
147 public userAction(userType: string): void {
148 if (userType === 'editPassword') {
149 this.getFormControl('userName').setValidators([]);
150 this.getFormControl('userName').updateValueAndValidity();
SANDHYA.JSa9816552022-04-12 09:07:08 +0530151 this.getFormControl('old_password').setValidators([]);
152 this.getFormControl('old_password').updateValueAndValidity();
Barath Kumar R16070582021-02-08 18:19:35 +0530153 } else if (userType === 'editUserName') {
154 this.getFormControl('password').setValidators([]);
155 this.getFormControl('password').updateValueAndValidity();
156 this.getFormControl('password2').setValidators([]);
157 this.getFormControl('password2').updateValueAndValidity();
SANDHYA.JSa9816552022-04-12 09:07:08 +0530158 this.getFormControl('old_password').setValidators([]);
159 this.getFormControl('old_password').updateValueAndValidity();
160 } else if (userType === 'changePassword') {
161 this.getFormControl('userName').setValidators([]);
162 this.getFormControl('userName').updateValueAndValidity();
163 } else if (userType === 'add') {
164 this.getFormControl('old_password').setValidators([]);
165 this.getFormControl('old_password').updateValueAndValidity();
Barath Kumar R16070582021-02-08 18:19:35 +0530166 }
167 this.submitted = true;
168 this.modalData = {
169 message: 'Done'
170 };
171 this.sharedService.cleanForm(this.userForm);
172 if (!this.userForm.invalid) {
173 if (this.userForm.value.password !== this.userForm.value.password2) {
174 this.notifierService.notify('error', this.translateService.instant('PAGE.USERS.PASSWORDCONFLICT'));
175 return;
176 }
177 if (userType === 'add') {
178 this.addUser();
SANDHYA.JSa9816552022-04-12 09:07:08 +0530179 } else {
Barath Kumar R16070582021-02-08 18:19:35 +0530180 this.editUser();
181 }
182 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530183 }
Barath Kumar R16070582021-02-08 18:19:35 +0530184
185 /** Add user @public */
186 public addUser(): void {
187 this.isLoadingResults = true;
188 const payLoad: {} = JSON.stringify({
189 username: (this.userForm.value.userName).toLowerCase(),
190 password: (this.userForm.value.password),
191 domain_name: !isNullOrUndefined(this.userForm.value.domain_name) ? this.userForm.value.domain_name : undefined
192 });
193 const apiURLHeader: APIURLHEADER = {
194 url: environment.USERS_URL,
195 httpOptions: { headers: this.headers }
196 };
197 this.restService.postResource(apiURLHeader, payLoad).subscribe((result: {}): void => {
198 this.activeModal.close(this.modalData);
199 this.isLoadingResults = false;
200 this.notifierService.notify('success', this.translateService.instant('PAGE.USERS.CREATEDSUCCESSFULLY'));
201 }, (error: ERRORDATA): void => {
202 this.restService.handleError(error, 'post');
203 this.isLoadingResults = false;
204 });
205 }
206
207 /** Edit user @public */
208 public editUser(): void {
209 this.isLoadingResults = true;
210 const payLoad: LOGINPARAMS = {};
211 if (this.userType === 'editPassword') {
212 payLoad.password = (this.userForm.value.password);
SANDHYA.JSa9816552022-04-12 09:07:08 +0530213 } else if (this.userType === 'changePassword') {
214 payLoad.password = (this.userForm.value.password);
215 payLoad.old_password = (this.userForm.value.old_password);
Barath Kumar R16070582021-02-08 18:19:35 +0530216 } else {
217 payLoad.username = this.userForm.value.userName.toLowerCase();
218 }
219 const apiURLHeader: APIURLHEADER = {
220 url: environment.USERS_URL + '/' + this.userID,
221 httpOptions: { headers: this.headers }
222 };
223 this.restService.patchResource(apiURLHeader, payLoad).subscribe((result: {}): void => {
224 this.checkUsername(payLoad);
225 this.activeModal.close(this.modalData);
SANDHYA.JSa9816552022-04-12 09:07:08 +0530226 if (this.isFirstLogin) {
227 this.notifierService.notify('success', this.translateService.instant('PAGE.USERS.CHANGEPASSWORD'));
228 this.authService.destoryToken();
SANDHYA.JS1b17c432023-04-26 17:54:57 +0530229 } else if (this.userType === 'changePassword' && (!this.isFirstLogin)) {
230 this.notifierService.notify('success', this.translateService.instant('PAGE.USERS.CHANGEDSUCCESSFULLY'));
SANDHYA.JSa9816552022-04-12 09:07:08 +0530231 } else {
232 this.notifierService.notify('success', this.translateService.instant('PAGE.USERS.EDITEDSUCCESSFULLY'));
233 }
Barath Kumar R16070582021-02-08 18:19:35 +0530234 this.isLoadingResults = false;
Barath Kumar R16070582021-02-08 18:19:35 +0530235 }, (error: ERRORDATA): void => {
SANDHYA.JSa9816552022-04-12 09:07:08 +0530236 if (this.isFirstLogin) {
237 this.notifierService.notify('error', error.error.detail);
238 this.activeModal.close(this.modalData);
239 this.authService.destoryToken();
240 } else {
241 this.restService.handleError(error, 'put');
242 }
Barath Kumar R16070582021-02-08 18:19:35 +0530243 this.isLoadingResults = false;
244 });
245 }
SANDHYA.JSa9816552022-04-12 09:07:08 +0530246 /** Close the modal and destroy subscribe @public */
247 public close(): void {
248 if (this.isFirstLogin) {
249 this.activeModal.close(this.modalData);
250 this.authService.destoryToken();
251 } else {
252 this.activeModal.close(this.modalData);
253 }
254 }
Barath Kumar R16070582021-02-08 18:19:35 +0530255 /** Get domain name list @private */
256 private getDomainList(): void {
257 this.isLoadingResults = true;
258 this.sharedService.getDomainName().subscribe((domainList: TYPESECTION[]): void => {
259 this.domains = domainList;
260 this.isLoadingResults = false;
261 }, (error: ERRORDATA): void => {
262 this.isLoadingResults = false;
263 this.restService.handleError(error, 'get');
264 });
265 }
266
267 /** Used to get the AbstractControl of controlName passed @private */
268 private getFormControl(controlName: string): AbstractControl {
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530269 // eslint-disable-next-line security/detect-object-injection
Barath Kumar R16070582021-02-08 18:19:35 +0530270 return this.userForm.controls[controlName];
271 }
272
273 /** Method to check loggedin username and update @private */
274 private checkUsername(payLoad: LOGINPARAMS): void {
SANDHYA.JS5b35bcd2023-04-27 15:11:06 +0530275 const logUsername: string = sessionStorage.getItem('username');
Barath Kumar R16070582021-02-08 18:19:35 +0530276 if (this.userType === 'editUserName' && logUsername === this.userName) {
277 this.authService.userName.next(payLoad.username);
SANDHYA.JS5b35bcd2023-04-27 15:11:06 +0530278 sessionStorage.setItem('username', payLoad.username);
Barath Kumar R16070582021-02-08 18:19:35 +0530279 }
280 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530281}