blob: 2931d7917665598bd3833c7767834913c720bb20 [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 */
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +053021import { isNullOrUndefined } from 'util';
kumaran.m3b4814a2020-05-01 19:48:54 +053022import { HttpHeaders } from '@angular/common/http';
23import { Component, Injector, Input, OnInit } from '@angular/core';
24import { AbstractControl, FormBuilder, FormGroup, Validators } from '@angular/forms';
25import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
26import { TranslateService } from '@ngx-translate/core';
27import { NotifierService } from 'angular-notifier';
28import { AuthenticationService } from 'AuthenticationService';
Barath Kumar R16070582021-02-08 18:19:35 +053029import { APIURLHEADER, ERRORDATA, LOGINPARAMS, MODALCLOSERESPONSEDATA, TYPESECTION } from 'CommonModel';
kumaran.m3b4814a2020-05-01 19:48:54 +053030import { environment } from 'environment';
31import { RestService } from 'RestService';
32import { SharedService } from 'SharedService';
kumaran.m3b4814a2020-05-01 19:48:54 +053033
34/**
35 * Creating component
36 * @Component takes AddEditUserComponent.html as template url
37 */
38@Component({
SANDHYA.JSa9816552022-04-12 09:07:08 +053039 selector: 'app-add-edit-user',
Barath Kumar R16070582021-02-08 18:19:35 +053040 templateUrl: './AddEditUserComponent.html',
41 styleUrls: ['./AddEditUserComponent.scss']
kumaran.m3b4814a2020-05-01 19:48:54 +053042})
43/** Exporting a class @exports AddEditUserComponent */
44export class AddEditUserComponent implements OnInit {
Barath Kumar R16070582021-02-08 18:19:35 +053045 /** To inject services @public */
46 public injector: Injector;
kumaran.m3b4814a2020-05-01 19:48:54 +053047
Barath Kumar R16070582021-02-08 18:19:35 +053048 /** Instance for active modal service @public */
49 public activeModal: NgbActiveModal;
kumaran.m3b4814a2020-05-01 19:48:54 +053050
Barath Kumar R16070582021-02-08 18:19:35 +053051 /** FormGroup user Edit Account added to the form @ html @public */
52 public userForm: FormGroup;
kumaran.m3b4814a2020-05-01 19:48:54 +053053
Barath Kumar R16070582021-02-08 18:19:35 +053054 /** Form submission Add */
55 public submitted: boolean = false;
kumaran.m3b4814a2020-05-01 19:48:54 +053056
Barath Kumar R16070582021-02-08 18:19:35 +053057 /** Input contains Modal dialog component Instance @public */
58 @Input() public userTitle: string;
kumaran.m3b4814a2020-05-01 19:48:54 +053059
Barath Kumar R16070582021-02-08 18:19:35 +053060 /** Input contains Modal dialog component Instance @public */
61 @Input() public userType: string;
kumaran.m3b4814a2020-05-01 19:48:54 +053062
Barath Kumar R16070582021-02-08 18:19:35 +053063 /** Input contains Modal dialog component Instance @public */
64 @Input() public userID: string;
kumaran.m3b4814a2020-05-01 19:48:54 +053065
Barath Kumar R16070582021-02-08 18:19:35 +053066 /** Input contains Modal dialog component Instance @public */
67 @Input() public userName: string;
kumaran.m3b4814a2020-05-01 19:48:54 +053068
Barath Kumar R16070582021-02-08 18:19:35 +053069 /** Check the loading results for loader status @public */
70 public isLoadingResults: boolean = false;
kumaran.m3b4814a2020-05-01 19:48:54 +053071
Barath Kumar R16070582021-02-08 18:19:35 +053072 /** Give the message for the loading @public */
73 public message: string = 'PLEASEWAIT';
kumaran.m3b4814a2020-05-01 19:48:54 +053074
Barath Kumar R16070582021-02-08 18:19:35 +053075 /** Holds list of domains @public */
76 public domains: TYPESECTION[] = [];
kumaran.m3b4814a2020-05-01 19:48:54 +053077
SANDHYA.JSa9816552022-04-12 09:07:08 +053078 /** Variable contains type is changepassword or not @public */
79 public isPassword: boolean;
80
81 /** Variable holds value for first login user @public */
SANDHYA.JS5b35bcd2023-04-27 15:11:06 +053082 public isFirstLogin: boolean = Boolean(sessionStorage.getItem('firstLogin') === 'true');
SANDHYA.JSa9816552022-04-12 09:07:08 +053083
Barath Kumar R16070582021-02-08 18:19:35 +053084 /** Instance of the rest service @private */
85 private restService: RestService;
kumaran.m3b4814a2020-05-01 19:48:54 +053086
Barath Kumar R16070582021-02-08 18:19:35 +053087 /** FormBuilder instance added to the formBuilder @private */
88 private formBuilder: FormBuilder;
kumaran.m3b4814a2020-05-01 19:48:54 +053089
Barath Kumar R16070582021-02-08 18:19:35 +053090 /** Controls the header form @private */
91 private headers: HttpHeaders;
kumaran.m3b4814a2020-05-01 19:48:54 +053092
Barath Kumar R16070582021-02-08 18:19:35 +053093 /** Notifier service to popup notification @private */
94 private notifierService: NotifierService;
kumaran.m3b4814a2020-05-01 19:48:54 +053095
Barath Kumar R16070582021-02-08 18:19:35 +053096 /** Contains tranlsate instance @private */
97 private translateService: TranslateService;
kumaran.m3b4814a2020-05-01 19:48:54 +053098
Barath Kumar R16070582021-02-08 18:19:35 +053099 /** Contains all methods related to shared @private */
100 private sharedService: SharedService;
kumaran.m3b4814a2020-05-01 19:48:54 +0530101
Barath Kumar R16070582021-02-08 18:19:35 +0530102 /** ModalData instance of modal @private */
103 private modalData: MODALCLOSERESPONSEDATA;
kumaran.m3b4814a2020-05-01 19:48:54 +0530104
Barath Kumar R16070582021-02-08 18:19:35 +0530105 /** Utilizes auth service for any auth operations @private */
106 private authService: AuthenticationService;
kumaran.m3b4814a2020-05-01 19:48:54 +0530107
Barath Kumar R16070582021-02-08 18:19:35 +0530108 constructor(injector: Injector) {
109 this.injector = injector;
110 this.formBuilder = this.injector.get(FormBuilder);
111 this.restService = this.injector.get(RestService);
112 this.activeModal = this.injector.get(NgbActiveModal);
113 this.notifierService = this.injector.get(NotifierService);
114 this.translateService = this.injector.get(TranslateService);
115 this.sharedService = this.injector.get(SharedService);
116 this.authService = this.injector.get(AuthenticationService);
kumaran.m3b4814a2020-05-01 19:48:54 +0530117
Barath Kumar R16070582021-02-08 18:19:35 +0530118 /** Initializing Form Action */
119 this.userForm = this.formBuilder.group({
120 userName: ['', Validators.required],
121 password: [null, [Validators.required, Validators.pattern(this.sharedService.REGX_PASSWORD_PATTERN)]],
122 password2: [null, Validators.required],
SANDHYA.JSa9816552022-04-12 09:07:08 +0530123 old_password: [null, Validators.required],
Barath Kumar R16070582021-02-08 18:19:35 +0530124 domain_name: [null]
125 });
kumaran.m3b4814a2020-05-01 19:48:54 +0530126 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530127
Barath Kumar R16070582021-02-08 18:19:35 +0530128 /** convenience getter for easy access to form fields */
129 get f(): FormGroup['controls'] { return this.userForm.controls; }
kumaran.m3b4814a2020-05-01 19:48:54 +0530130
Barath Kumar R16070582021-02-08 18:19:35 +0530131 /** Lifecyle Hooks the trigger before component is instantiate @public */
132 public ngOnInit(): void {
133 this.headers = new HttpHeaders({
134 'Content-Type': 'application/json',
135 Accept: 'application/json',
136 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
137 });
138 if (this.userType === 'add') {
139 this.getDomainList();
140 } else if (this.userType === 'editUserName') {
141 this.userForm.patchValue({ userName: this.userName });
SANDHYA.JSa9816552022-04-12 09:07:08 +0530142 } else if (this.isFirstLogin) {
143 this.isPassword = true;
kumaran.m3b4814a2020-05-01 19:48:54 +0530144 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530145 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530146
Barath Kumar R16070582021-02-08 18:19:35 +0530147 /** On modal submit users acction will called @public */
148 public userAction(userType: string): void {
149 if (userType === 'editPassword') {
150 this.getFormControl('userName').setValidators([]);
151 this.getFormControl('userName').updateValueAndValidity();
SANDHYA.JSa9816552022-04-12 09:07:08 +0530152 this.getFormControl('old_password').setValidators([]);
153 this.getFormControl('old_password').updateValueAndValidity();
Barath Kumar R16070582021-02-08 18:19:35 +0530154 } else if (userType === 'editUserName') {
155 this.getFormControl('password').setValidators([]);
156 this.getFormControl('password').updateValueAndValidity();
157 this.getFormControl('password2').setValidators([]);
158 this.getFormControl('password2').updateValueAndValidity();
SANDHYA.JSa9816552022-04-12 09:07:08 +0530159 this.getFormControl('old_password').setValidators([]);
160 this.getFormControl('old_password').updateValueAndValidity();
161 } else if (userType === 'changePassword') {
162 this.getFormControl('userName').setValidators([]);
163 this.getFormControl('userName').updateValueAndValidity();
164 } else if (userType === 'add') {
165 this.getFormControl('old_password').setValidators([]);
166 this.getFormControl('old_password').updateValueAndValidity();
Barath Kumar R16070582021-02-08 18:19:35 +0530167 }
168 this.submitted = true;
169 this.modalData = {
170 message: 'Done'
171 };
172 this.sharedService.cleanForm(this.userForm);
173 if (!this.userForm.invalid) {
174 if (this.userForm.value.password !== this.userForm.value.password2) {
175 this.notifierService.notify('error', this.translateService.instant('PAGE.USERS.PASSWORDCONFLICT'));
176 return;
177 }
178 if (userType === 'add') {
179 this.addUser();
SANDHYA.JSa9816552022-04-12 09:07:08 +0530180 } else {
Barath Kumar R16070582021-02-08 18:19:35 +0530181 this.editUser();
182 }
183 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530184 }
Barath Kumar R16070582021-02-08 18:19:35 +0530185
186 /** Add user @public */
187 public addUser(): void {
188 this.isLoadingResults = true;
189 const payLoad: {} = JSON.stringify({
190 username: (this.userForm.value.userName).toLowerCase(),
191 password: (this.userForm.value.password),
192 domain_name: !isNullOrUndefined(this.userForm.value.domain_name) ? this.userForm.value.domain_name : undefined
193 });
194 const apiURLHeader: APIURLHEADER = {
195 url: environment.USERS_URL,
196 httpOptions: { headers: this.headers }
197 };
198 this.restService.postResource(apiURLHeader, payLoad).subscribe((result: {}): void => {
199 this.activeModal.close(this.modalData);
200 this.isLoadingResults = false;
201 this.notifierService.notify('success', this.translateService.instant('PAGE.USERS.CREATEDSUCCESSFULLY'));
202 }, (error: ERRORDATA): void => {
203 this.restService.handleError(error, 'post');
204 this.isLoadingResults = false;
205 });
206 }
207
208 /** Edit user @public */
209 public editUser(): void {
210 this.isLoadingResults = true;
211 const payLoad: LOGINPARAMS = {};
212 if (this.userType === 'editPassword') {
213 payLoad.password = (this.userForm.value.password);
SANDHYA.JSa9816552022-04-12 09:07:08 +0530214 } else if (this.userType === 'changePassword') {
215 payLoad.password = (this.userForm.value.password);
216 payLoad.old_password = (this.userForm.value.old_password);
Barath Kumar R16070582021-02-08 18:19:35 +0530217 } else {
218 payLoad.username = this.userForm.value.userName.toLowerCase();
219 }
220 const apiURLHeader: APIURLHEADER = {
221 url: environment.USERS_URL + '/' + this.userID,
222 httpOptions: { headers: this.headers }
223 };
224 this.restService.patchResource(apiURLHeader, payLoad).subscribe((result: {}): void => {
225 this.checkUsername(payLoad);
226 this.activeModal.close(this.modalData);
SANDHYA.JSa9816552022-04-12 09:07:08 +0530227 if (this.isFirstLogin) {
228 this.notifierService.notify('success', this.translateService.instant('PAGE.USERS.CHANGEPASSWORD'));
229 this.authService.destoryToken();
SANDHYA.JS1b17c432023-04-26 17:54:57 +0530230 } else if (this.userType === 'changePassword' && (!this.isFirstLogin)) {
231 this.notifierService.notify('success', this.translateService.instant('PAGE.USERS.CHANGEDSUCCESSFULLY'));
SANDHYA.JSa9816552022-04-12 09:07:08 +0530232 } else {
233 this.notifierService.notify('success', this.translateService.instant('PAGE.USERS.EDITEDSUCCESSFULLY'));
234 }
Barath Kumar R16070582021-02-08 18:19:35 +0530235 this.isLoadingResults = false;
Barath Kumar R16070582021-02-08 18:19:35 +0530236 }, (error: ERRORDATA): void => {
SANDHYA.JSa9816552022-04-12 09:07:08 +0530237 if (this.isFirstLogin) {
238 this.notifierService.notify('error', error.error.detail);
239 this.activeModal.close(this.modalData);
240 this.authService.destoryToken();
241 } else {
242 this.restService.handleError(error, 'put');
243 }
Barath Kumar R16070582021-02-08 18:19:35 +0530244 this.isLoadingResults = false;
245 });
246 }
SANDHYA.JSa9816552022-04-12 09:07:08 +0530247 /** Close the modal and destroy subscribe @public */
248 public close(): void {
249 if (this.isFirstLogin) {
250 this.activeModal.close(this.modalData);
251 this.authService.destoryToken();
252 } else {
253 this.activeModal.close(this.modalData);
254 }
255 }
Barath Kumar R16070582021-02-08 18:19:35 +0530256 /** Get domain name list @private */
257 private getDomainList(): void {
258 this.isLoadingResults = true;
259 this.sharedService.getDomainName().subscribe((domainList: TYPESECTION[]): void => {
260 this.domains = domainList;
261 this.isLoadingResults = false;
262 }, (error: ERRORDATA): void => {
263 this.isLoadingResults = false;
264 this.restService.handleError(error, 'get');
265 });
266 }
267
268 /** Used to get the AbstractControl of controlName passed @private */
269 private getFormControl(controlName: string): AbstractControl {
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530270 // eslint-disable-next-line security/detect-object-injection
Barath Kumar R16070582021-02-08 18:19:35 +0530271 return this.userForm.controls[controlName];
272 }
273
274 /** Method to check loggedin username and update @private */
275 private checkUsername(payLoad: LOGINPARAMS): void {
SANDHYA.JS5b35bcd2023-04-27 15:11:06 +0530276 const logUsername: string = sessionStorage.getItem('username');
Barath Kumar R16070582021-02-08 18:19:35 +0530277 if (this.userType === 'editUserName' && logUsername === this.userName) {
278 this.authService.userName.next(payLoad.username);
SANDHYA.JS5b35bcd2023-04-27 15:11:06 +0530279 sessionStorage.setItem('username', payLoad.username);
Barath Kumar R16070582021-02-08 18:19:35 +0530280 }
281 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530282}