blob: 92fe3c346af959ba61e93d572934bfab27b8cf09 [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/**
20 * @file Page for Login component
21 */
22import { HttpErrorResponse } from '@angular/common/http';
23import { Component, Injector, OnInit } from '@angular/core';
24import { FormBuilder, FormGroup, Validators } from '@angular/forms';
25import { Router } from '@angular/router';
SANDHYA.JS1b17c432023-04-26 17:54:57 +053026import { TranslateService } from '@ngx-translate/core';
kumaran.m3b4814a2020-05-01 19:48:54 +053027import { AuthenticationService } from 'AuthenticationService';
SANDHYA.JS1b17c432023-04-26 17:54:57 +053028import { ERRORDATA } from 'CommonModel';
29import { environment } from 'environment';
30import { ToastrService } from 'ngx-toastr';
kumaran.m3b4814a2020-05-01 19:48:54 +053031import { RestService } from 'RestService';
32import { Observable } from 'rxjs';
SANDHYA.JSc84f1122024-06-04 21:50:03 +053033import { SharedService, isNullOrUndefined } from 'SharedService';
SANDHYA.JS1b17c432023-04-26 17:54:57 +053034import { UserDetail } from 'UserModel';
kumaran.m3b4814a2020-05-01 19:48:54 +053035
36/**
37 * Creating component
38 * @Component takes LoginComponent.html as template url
39 */
40@Component({
41 selector: 'app-login',
42 templateUrl: './LoginComponent.html',
43 styleUrls: ['./LoginComponent.scss']
44})
45/** Exporting a class @exports LoginComponent */
46export class LoginComponent implements OnInit {
47 /** Invoke service injectors @public */
48 public injector: Injector;
49
50 /** contains loginform group information @public */
51 public loginForm: FormGroup;
52
53 /** submitted set to boolean state @public */
54 public submitted: boolean = false;
55
56 /** contains return URL link @public */
57 public returnUrl: string;
58
59 /** Observable Hold the value of subscription @public */
60 public isLoggedIn$: Observable<boolean>;
61
SANDHYA.JSa9816552022-04-12 09:07:08 +053062 /** Observable Hold the value of subscription @public */
63 public isChangePassword$: Observable<boolean>;
64
kumaran.m3b4814a2020-05-01 19:48:54 +053065 /** contains access token information @public */
66 public accessToken: string;
67
68 /** Utilizes rest service for any CRUD operations @public */
69 public restService: RestService;
70
71 /** Check the loading results @public */
72 public isLoadingResults: boolean = false;
73
74 /** Give the message for the loading @public */
75 public message: string = 'PLEASEWAIT';
76
77 /** Contains all methods related to shared @public */
78 public sharedService: SharedService;
79
SANDHYA.JSa9816552022-04-12 09:07:08 +053080 /** contains the loggedIn observable value @public */
81 public loggedIn: boolean;
82
SANDHYA.JS1b17c432023-04-26 17:54:57 +053083 /** Contains Last Login information @public */
84 public lastLogin: string;
85
86 /** Holds Last Login Toaster Message @public */
87 public lastLoginMessage: string;
88
89 /** Holds Failed Attempts Toaster Message @public */
90 public failedAttemptsMessage: string;
91
92 /** Holds Password Expire Toaster Message @public */
93 public passwordExpireMessage: string;
94
95 /** Holds Account Expire Toaster Message @public */
96 public accountExpireMessage: string;
97
98 /** Holds password & account Toaster Message @public */
99 public daysMessage: string;
100
101 /** Holds account Days Toaster Message @public */
102 public accountMessage: string;
103
104 /** Holds password Days Toaster Message @public */
105 public passwordMessage: string;
106
107 /** Contains user details information @public */
108 public userDetails: UserDetail;
109
110 /** contains No of failed attempts values @public */
111 public failedAttempts: string;
112
113 /** contains No of days to expire account @public */
114 public accountNoOfDays: string;
115
116 /** contains No of days to expire password @public */
117 public passwordNoOfDays: string;
118
119 /** User Visibility Check @public */
120 public isUserShow: boolean;
121
122 /** Admin Visibility Check @public */
123 public isAdminShow: boolean;
124
SANDHYA.JSa9816552022-04-12 09:07:08 +0530125 /** contains the passwordIn observable value @public */
126 public changePassword: boolean;
127
SANDHYA.JS11322582023-10-12 16:23:38 +0530128 /** To show the visiblity of password @public */
129 public visiblePassword: boolean;
130
kumaran.m3b4814a2020-05-01 19:48:54 +0530131 /** Utilizes auth service for any auth operations @private */
132 private authService: AuthenticationService;
133
134 /** contians form builder module @private */
135 private formBuilder: FormBuilder;
136
137 /** Holds teh instance of AuthService class of type AuthService @private */
138 private router: Router;
139
SANDHYA.JS1b17c432023-04-26 17:54:57 +0530140 /** Contains tranlsate instance @private */
141 private translateService: TranslateService;
142
143 /** Contains toaster instance @private */
144 private toaster: ToastrService;
145
146 /** express number for expire days @private */
147 // eslint-disable-next-line @typescript-eslint/no-magic-numbers
148 private expireDays: number = 5;
149
150 /** express number for time manupulation 1000 */
151 // eslint-disable-next-line @typescript-eslint/no-magic-numbers
152 private epochTime1000: number = 1000;
153
154 /** contains toaster settings */
155 private toasterSettings: {} = {
156 enableHtml: true,
157 closeButton: true,
158 timeOut: 2000
159 };
160
kumaran.m3b4814a2020-05-01 19:48:54 +0530161 // creates instance of login component
162 constructor(injector: Injector) {
163 this.injector = injector;
164 this.restService = this.injector.get(RestService);
165 this.authService = this.injector.get(AuthenticationService);
166 this.formBuilder = this.injector.get(FormBuilder);
167 this.router = this.injector.get(Router);
168 this.sharedService = this.injector.get(SharedService);
SANDHYA.JS1b17c432023-04-26 17:54:57 +0530169 this.translateService = this.injector.get(TranslateService);
170 this.toaster = this.injector.get(ToastrService);
kumaran.m3b4814a2020-05-01 19:48:54 +0530171 }
172
173 /**
174 * Lifecyle Hooks the trigger before component is instantiate
175 */
176 public ngOnInit(): void {
177 this.isLoggedIn$ = this.authService.isLoggedIn;
SANDHYA.JSa9816552022-04-12 09:07:08 +0530178 this.isLoggedIn$.subscribe((res: boolean): void => {
179 this.loggedIn = res;
180 });
181 if (this.loggedIn === true) {
182 this.router.navigate(['/']).catch((): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530183 // Catch Navigation Error
184 });
185 }
SANDHYA.JSa9816552022-04-12 09:07:08 +0530186 this.isChangePassword$ = this.authService.isChangePassword;
187 this.isChangePassword$.subscribe((res: boolean): void => {
188 this.changePassword = res;
189 });
190 if (this.changePassword === true) {
191 this.router.navigate(['changepassword']).catch((): void => {
192 // Catch Navigation Error
193 });
194 }
195
kumaran.m3b4814a2020-05-01 19:48:54 +0530196 this.loginForm = this.formBuilder.group({
197 userName: ['', [Validators.required]],
198 password: ['', [Validators.required]]
199 });
SANDHYA.JS5b35bcd2023-04-27 15:11:06 +0530200 this.returnUrl = isNullOrUndefined(sessionStorage.getItem('returnUrl')) ? '/' : sessionStorage.getItem('returnUrl');
kumaran.m3b4814a2020-05-01 19:48:54 +0530201 }
202
203 /**
204 * called on form submit @private onSubmit
205 */
206 public onSubmit(): void {
207 this.submitted = true;
208 if (this.loginForm.invalid) {
209 return;
210 }
211 this.isLoadingResults = true;
212 this.sharedService.cleanForm(this.loginForm);
SANDHYA.JS1b17c432023-04-26 17:54:57 +0530213 this.isLoadingResults = false;
214 if (!this.loginForm.invalid) {
215 this.loginUser();
216 }
217 }
218
219 /** Login User @public */
220 public loginUser(): void {
kumaran.m3b4814a2020-05-01 19:48:54 +0530221 this.authService.login(this.loginForm.value.userName, this.loginForm.value.password).subscribe(
SANDHYA.JSa9816552022-04-12 09:07:08 +0530222 (data: {}): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530223 this.isLoadingResults = false;
SANDHYA.JSa9816552022-04-12 09:07:08 +0530224 if (this.changePassword === true && this.loggedIn === false) {
225 this.router.navigate(['/changepassword']).catch((): void => {
226 // Catch Navigation Error
227 });
228 } else {
229 this.router.navigate([this.returnUrl]).catch((): void => {
230 // Catch Navigation Error
231 });
SANDHYA.JS5b35bcd2023-04-27 15:11:06 +0530232 this.isAdminShow = sessionStorage.getItem('admin_show') === 'true' ? true : false;
233 this.isUserShow = sessionStorage.getItem('user_show') === 'true' ? true : false;
SANDHYA.JS1b17c432023-04-26 17:54:57 +0530234 setTimeout((): void => {
235 if (this.isAdminShow === true || this.isUserShow === true) {
236 this.generateData();
237 }
238 }, this.epochTime1000);
SANDHYA.JSa9816552022-04-12 09:07:08 +0530239 }
SANDHYA.JS5b35bcd2023-04-27 15:11:06 +0530240 sessionStorage.removeItem('returnUrl');
SANDHYA.JSa9816552022-04-12 09:07:08 +0530241 }, (err: HttpErrorResponse): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530242 this.isLoadingResults = false;
243 this.restService.handleError(err, 'post');
244 });
245 }
SANDHYA.JS1b17c432023-04-26 17:54:57 +0530246
247 /** Fetching the data from server to load it in toaster @public */
248 public generateData(): void {
SANDHYA.JS5b35bcd2023-04-27 15:11:06 +0530249 const userID: string = sessionStorage.getItem('user_id');
SANDHYA.JS1b17c432023-04-26 17:54:57 +0530250 if (userID !== '') {
251 this.isLoadingResults = true;
252 this.restService.getResource(environment.USERS_URL + '/' + userID).subscribe((userDetails: UserDetail): void => {
253 this.userDetails = userDetails;
254 if (!isNullOrUndefined(userDetails)) {
255 const account: string = this.sharedService.convertEpochTime(!isNullOrUndefined(userDetails._admin) ?
256 userDetails._admin.account_expire_time : null);
257 const password: string = this.sharedService.convertEpochTime(!isNullOrUndefined(userDetails._admin) ?
258 userDetails._admin.password_expire_time : null);
259 const accountExpire: number = this.sharedService.converEpochToDays(account);
260 const passwordExpire: number = this.sharedService.converEpochToDays(password);
261 if (accountExpire >= 0 && accountExpire <= this.expireDays) {
262 this.accountNoOfDays = String(accountExpire);
263 }
264 if (passwordExpire >= 0 && passwordExpire <= this.expireDays) {
265 this.passwordNoOfDays = String(passwordExpire);
266 }
267 this.lastLoginMessage = this.translateService.instant('PAGE.LOGIN.LASTACCESS');
268 this.failedAttemptsMessage = this.translateService.instant('PAGE.LOGIN.FAILED');
269 this.accountExpireMessage = this.translateService.instant('PAGE.LOGIN.ACCOUNTEXPIRE');
270 this.passwordExpireMessage = this.translateService.instant('PAGE.LOGIN.PASSWORDEXPIRE');
271 this.daysMessage = this.translateService.instant('PAGE.LOGIN.DAYS');
SANDHYA.JS5b35bcd2023-04-27 15:11:06 +0530272 this.lastLogin = sessionStorage.getItem('last_login');
273 this.failedAttempts = sessionStorage.getItem('failed_count');
SANDHYA.JS1b17c432023-04-26 17:54:57 +0530274 if (this.accountNoOfDays !== '0' && this.passwordNoOfDays !== '0' &&
275 this.accountNoOfDays !== '1' && this.passwordNoOfDays !== '1') {
276 this.showToaster();
277 }
278 this.passwordExpiryToaster();
279 this.accountExpiryToaster();
280 }
281 this.isLoadingResults = false;
282 }, (error: ERRORDATA): void => {
283 this.isLoadingResults = false;
284 this.restService.handleError(error, 'get');
285 });
286 }
287 }
288
SANDHYA.JS11322582023-10-12 16:23:38 +0530289 /** To Show or Hide the Password @public */
290 public onShowPassword(): void {
291 this.visiblePassword = !this.visiblePassword;
292 }
293
SANDHYA.JS1b17c432023-04-26 17:54:57 +0530294 /** To display password expiry Toaster with required data @public */
295 public passwordExpiryToaster(): void {
296 if ((this.accountNoOfDays === '1' && this.passwordNoOfDays === '1') ||
297 (this.accountNoOfDays === '0' && this.passwordNoOfDays === '0')) {
298 this.passwordExpireMessage = this.translateService.instant('PAGE.LOGIN.PASSWORDEXPIRETODAY');
299 this.accountExpireMessage = this.translateService.instant('PAGE.LOGIN.ACCOUNTEXPIRETODAY');
300 if (this.accountNoOfDays === '1') {
301 this.passwordExpireMessage = this.translateService.instant('PAGE.LOGIN.PASSWORDEXPIRETOMORROW');
302 this.accountExpireMessage = this.translateService.instant('PAGE.LOGIN.ACCOUNTEXPIRETOMORROW');
303 }
304 this.passwordMessage = '';
305 this.accountMessage = '';
306 this.accountNoOfDays = '';
307 this.passwordNoOfDays = '';
308 this.sharedService.showToaster(this.lastLogin, this.failedAttempts, this.passwordNoOfDays, this.accountNoOfDays,
309 this.passwordExpireMessage, this.accountExpireMessage, this.passwordMessage, this.accountMessage);
310 } else if (!isNullOrUndefined(this.passwordNoOfDays)) {
311 if ((this.passwordNoOfDays === '0') || this.passwordNoOfDays === '1' ||
312 (this.passwordNoOfDays === '0' && (isNullOrUndefined(this.accountNoOfDays) || !isNullOrUndefined(this.accountNoOfDays))) ||
313 (this.passwordNoOfDays === '1' && (isNullOrUndefined(this.accountNoOfDays) || !isNullOrUndefined(this.accountNoOfDays)))
314 ) {
315 if (this.passwordNoOfDays === '1') {
316 this.passwordExpireMessage = this.translateService.instant('PAGE.LOGIN.PASSWORDEXPIRETOMORROW');
317 this.passwordMessage = '';
318 this.passwordNoOfDays = '';
319 } else if (this.passwordNoOfDays === '0') {
320 this.passwordExpireMessage = this.translateService.instant('PAGE.LOGIN.PASSWORDEXPIRETODAY');
321 this.passwordMessage = '';
322 this.passwordNoOfDays = '';
323 }
324 if (isNullOrUndefined(this.accountNoOfDays)) {
325 this.sharedService.passwordToaster(this.lastLogin, this.failedAttempts, this.passwordNoOfDays,
326 this.passwordExpireMessage, this.passwordMessage);
327 } else {
SANDHYA.JSc6c72e32023-10-12 11:29:52 +0530328 this.accountDaysCheck();
SANDHYA.JS1b17c432023-04-26 17:54:57 +0530329 }
330 }
331 }
332 }
SANDHYA.JSc6c72e32023-10-12 11:29:52 +0530333 /** To check account no.of days with 0 & 1 @public */
334 public accountDaysCheck(): void {
335 if (this.accountNoOfDays === '1') {
336 this.accountExpireMessage = this.translateService.instant('PAGE.LOGIN.ACCOUNTEXPIRETOMORROW');
337 this.accountMessage = '';
338 this.accountNoOfDays = '';
339 } else if (this.accountNoOfDays === '0') {
340 this.accountExpireMessage = this.translateService.instant('PAGE.LOGIN.ACCOUNTEXPIRETODAY');
341 this.accountMessage = '';
342 this.accountNoOfDays = '';
343 } else {
344 this.accountExpireMessage = this.accountExpireMessage = this.translateService.instant('PAGE.LOGIN.ACCOUNTEXPIRE');
345 this.accountMessage = this.translateService.instant('PAGE.LOGIN.DAYS');
346 }
347 this.sharedService.showToaster(this.lastLogin, this.failedAttempts, this.passwordNoOfDays, this.accountNoOfDays,
348 this.passwordExpireMessage, this.accountExpireMessage, this.passwordMessage, this.accountMessage);
349 }
SANDHYA.JS1b17c432023-04-26 17:54:57 +0530350 /** To display account expiry Toaster with required data @public */
351 public accountExpiryToaster(): void {
352 if (!isNullOrUndefined(this.accountNoOfDays)) {
353 if ((this.accountNoOfDays === '0') || (this.accountNoOfDays === '1') || ((this.accountNoOfDays === '0') &&
354 (isNullOrUndefined(this.passwordNoOfDays) || !isNullOrUndefined(this.passwordNoOfDays))) ||
355 ((this.accountNoOfDays === '1') && (isNullOrUndefined(this.passwordNoOfDays) || !isNullOrUndefined(this.passwordNoOfDays)))
356 && this.passwordNoOfDays !== '0' && this.passwordNoOfDays !== '1') {
357 if (this.accountNoOfDays === '1') {
358 this.accountExpireMessage = this.translateService.instant('PAGE.LOGIN.ACCOUNTEXPIRETOMORROW');
359 this.accountMessage = '';
360 this.accountNoOfDays = '';
361 } else if (this.accountNoOfDays === '0') {
362 this.accountExpireMessage = this.translateService.instant('PAGE.LOGIN.ACCOUNTEXPIRETODAY');
363 this.accountMessage = '';
364 this.accountNoOfDays = '';
365 }
366 if (isNullOrUndefined(this.passwordNoOfDays)) {
367 this.sharedService.accountToaster(this.lastLogin, this.failedAttempts,
368 this.accountNoOfDays, this.accountExpireMessage, this.accountMessage);
369 } else {
SANDHYA.JSc6c72e32023-10-12 11:29:52 +0530370 this.passwordDaysCheck();
SANDHYA.JS1b17c432023-04-26 17:54:57 +0530371 }
372 }
373 }
374 }
SANDHYA.JSc6c72e32023-10-12 11:29:52 +0530375 /** To check password no.of days with 0 & 1 @public */
376 public passwordDaysCheck(): void {
377 if (this.passwordNoOfDays === '1') {
378 this.passwordExpireMessage = this.translateService.instant('PAGE.LOGIN.PASSWORDEXPIRETOMORROW');
379 this.passwordMessage = '';
380 this.passwordNoOfDays = '';
381 } else if (this.passwordNoOfDays === '0') {
382 this.passwordExpireMessage = this.translateService.instant('PAGE.LOGIN.PASSWORDEXPIRETODAY');
383 this.passwordMessage = '';
384 this.passwordNoOfDays = '';
385 } else {
386 this.passwordExpireMessage = this.translateService.instant('PAGE.LOGIN.PASSWORDEXPIRE');
387 this.passwordMessage = this.translateService.instant('PAGE.LOGIN.DAYS');
388 }
389 this.sharedService.showToaster(this.lastLogin, this.failedAttempts, this.passwordNoOfDays, this.accountNoOfDays,
390 this.passwordExpireMessage, this.accountExpireMessage, this.passwordMessage, this.accountMessage);
391 }
SANDHYA.JS1b17c432023-04-26 17:54:57 +0530392 /** To display password & account expiry Toaster with required data @public */
393 public showToaster(): void {
394 if (!isNullOrUndefined(this.accountNoOfDays) && !isNullOrUndefined(this.passwordNoOfDays)) {
395 this.toaster.info(this.lastLoginMessage + ':' + '&nbsp' + this.lastLogin +
396 '</br>' + this.failedAttemptsMessage + ':' + '&nbsp' + this.failedAttempts +
397 '</br>' + this.passwordExpireMessage + '&nbsp' + this.passwordNoOfDays + '&nbsp' + this.daysMessage +
398 '</br>' + this.accountExpireMessage + '&nbsp' + this.accountNoOfDays + '&nbsp' + this.daysMessage,
399 this.translateService.instant('PAGE.LOGIN.LOGINHISTORY'), this.toasterSettings);
400 } else if (!isNullOrUndefined(this.accountNoOfDays) || !isNullOrUndefined(this.passwordNoOfDays)) {
401 if (!isNullOrUndefined(this.passwordNoOfDays)) {
402 this.toaster.info(this.lastLoginMessage + ':' + '&nbsp' + this.lastLogin +
403 '</br>' + this.failedAttemptsMessage + ':' + '&nbsp' + this.failedAttempts +
404 '</br>' + this.passwordExpireMessage + '&nbsp' + this.passwordNoOfDays + '&nbsp' + this.daysMessage,
405 this.translateService.instant('PAGE.LOGIN.LOGINHISTORY'), this.toasterSettings);
406 } else if (!isNullOrUndefined(this.accountNoOfDays)) {
407 this.toaster.info(
408 this.lastLoginMessage + ':' + '&nbsp' + this.lastLogin +
409 '</br>' + this.failedAttemptsMessage + ':' + '&nbsp' + this.failedAttempts +
410 '</br>' + this.accountExpireMessage + '&nbsp' + this.accountNoOfDays + '&nbsp' + this.daysMessage,
411 this.translateService.instant('PAGE.LOGIN.LOGINHISTORY'), this.toasterSettings);
412 }
413 } else {
414 this.toaster.info(this.lastLoginMessage + ':' + '&nbsp' + this.lastLogin +
415 '</br>' + this.failedAttemptsMessage + ':' + '&nbsp' + this.failedAttempts,
416 this.translateService.instant('PAGE.LOGIN.LOGINHISTORY'), this.toasterSettings);
417 }
418 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530419}