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)
20 * @file Page for Login component
22 import { HttpErrorResponse } from '@angular/common/http';
23 import { Component, Injector, OnInit } from '@angular/core';
24 import { FormBuilder, FormGroup, Validators } from '@angular/forms';
25 import { Router } from '@angular/router';
26 import { TranslateService } from '@ngx-translate/core';
27 import { AuthenticationService } from 'AuthenticationService';
28 import { ERRORDATA } from 'CommonModel';
29 import { environment } from 'environment';
30 import { ToastrService } from 'ngx-toastr';
31 import { RestService } from 'RestService';
32 import { Observable } from 'rxjs';
33 import { SharedService, isNullOrUndefined } from 'SharedService';
34 import { UserDetail } from 'UserModel';
38 * @Component takes LoginComponent.html as template url
41 selector: 'app-login',
42 templateUrl: './LoginComponent.html',
43 styleUrls: ['./LoginComponent.scss']
45 /** Exporting a class @exports LoginComponent */
46 export class LoginComponent implements OnInit {
47 /** Invoke service injectors @public */
48 public injector: Injector;
50 /** contains loginform group information @public */
51 public loginForm: FormGroup;
53 /** submitted set to boolean state @public */
54 public submitted: boolean = false;
56 /** contains return URL link @public */
57 public returnUrl: string;
59 /** Observable Hold the value of subscription @public */
60 public isLoggedIn$: Observable<boolean>;
62 /** Observable Hold the value of subscription @public */
63 public isChangePassword$: Observable<boolean>;
65 /** contains access token information @public */
66 public accessToken: string;
68 /** Utilizes rest service for any CRUD operations @public */
69 public restService: RestService;
71 /** Check the loading results @public */
72 public isLoadingResults: boolean = false;
74 /** Give the message for the loading @public */
75 public message: string = 'PLEASEWAIT';
77 /** Contains all methods related to shared @public */
78 public sharedService: SharedService;
80 /** contains the loggedIn observable value @public */
81 public loggedIn: boolean;
83 /** Contains Last Login information @public */
84 public lastLogin: string;
86 /** Holds Last Login Toaster Message @public */
87 public lastLoginMessage: string;
89 /** Holds Failed Attempts Toaster Message @public */
90 public failedAttemptsMessage: string;
92 /** Holds Password Expire Toaster Message @public */
93 public passwordExpireMessage: string;
95 /** Holds Account Expire Toaster Message @public */
96 public accountExpireMessage: string;
98 /** Holds password & account Toaster Message @public */
99 public daysMessage: string;
101 /** Holds account Days Toaster Message @public */
102 public accountMessage: string;
104 /** Holds password Days Toaster Message @public */
105 public passwordMessage: string;
107 /** Contains user details information @public */
108 public userDetails: UserDetail;
110 /** contains No of failed attempts values @public */
111 public failedAttempts: string;
113 /** contains No of days to expire account @public */
114 public accountNoOfDays: string;
116 /** contains No of days to expire password @public */
117 public passwordNoOfDays: string;
119 /** User Visibility Check @public */
120 public isUserShow: boolean;
122 /** Admin Visibility Check @public */
123 public isAdminShow: boolean;
125 /** contains the passwordIn observable value @public */
126 public changePassword: boolean;
128 /** To show the visiblity of password @public */
129 public visiblePassword: boolean;
131 /** Utilizes auth service for any auth operations @private */
132 private authService: AuthenticationService;
134 /** contians form builder module @private */
135 private formBuilder: FormBuilder;
137 /** Holds teh instance of AuthService class of type AuthService @private */
138 private router: Router;
140 /** Contains tranlsate instance @private */
141 private translateService: TranslateService;
143 /** Contains toaster instance @private */
144 private toaster: ToastrService;
146 /** express number for expire days @private */
147 // eslint-disable-next-line @typescript-eslint/no-magic-numbers
148 private expireDays: number = 5;
150 /** express number for time manupulation 1000 */
151 // eslint-disable-next-line @typescript-eslint/no-magic-numbers
152 private epochTime1000: number = 1000;
154 /** contains toaster settings */
155 private toasterSettings: {} = {
161 // 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);
169 this.translateService = this.injector.get(TranslateService);
170 this.toaster = this.injector.get(ToastrService);
174 * Lifecyle Hooks the trigger before component is instantiate
176 public ngOnInit(): void {
177 this.isLoggedIn$ = this.authService.isLoggedIn;
178 this.isLoggedIn$.subscribe((res: boolean): void => {
181 if (this.loggedIn === true) {
182 this.router.navigate(['/']).catch((): void => {
183 // Catch Navigation Error
186 this.isChangePassword$ = this.authService.isChangePassword;
187 this.isChangePassword$.subscribe((res: boolean): void => {
188 this.changePassword = res;
190 if (this.changePassword === true) {
191 this.router.navigate(['changepassword']).catch((): void => {
192 // Catch Navigation Error
196 this.loginForm = this.formBuilder.group({
197 userName: ['', [Validators.required]],
198 password: ['', [Validators.required]]
200 this.returnUrl = isNullOrUndefined(sessionStorage.getItem('returnUrl')) ? '/' : sessionStorage.getItem('returnUrl');
204 * called on form submit @private onSubmit
206 public onSubmit(): void {
207 this.submitted = true;
208 if (this.loginForm.invalid) {
211 this.isLoadingResults = true;
212 this.sharedService.cleanForm(this.loginForm);
213 this.isLoadingResults = false;
214 if (!this.loginForm.invalid) {
219 /** Login User @public */
220 public loginUser(): void {
221 this.authService.login(this.loginForm.value.userName, this.loginForm.value.password).subscribe(
222 (data: {}): void => {
223 this.isLoadingResults = false;
224 if (this.changePassword === true && this.loggedIn === false) {
225 this.router.navigate(['/changepassword']).catch((): void => {
226 // Catch Navigation Error
229 this.router.navigate([this.returnUrl]).catch((): void => {
230 // Catch Navigation Error
232 this.isAdminShow = sessionStorage.getItem('admin_show') === 'true' ? true : false;
233 this.isUserShow = sessionStorage.getItem('user_show') === 'true' ? true : false;
234 setTimeout((): void => {
235 if (this.isAdminShow === true || this.isUserShow === true) {
238 }, this.epochTime1000);
240 sessionStorage.removeItem('returnUrl');
241 }, (err: HttpErrorResponse): void => {
242 this.isLoadingResults = false;
243 this.restService.handleError(err, 'post');
247 /** Fetching the data from server to load it in toaster @public */
248 public generateData(): void {
249 const userID: string = sessionStorage.getItem('user_id');
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);
264 if (passwordExpire >= 0 && passwordExpire <= this.expireDays) {
265 this.passwordNoOfDays = String(passwordExpire);
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');
272 this.lastLogin = sessionStorage.getItem('last_login');
273 this.failedAttempts = sessionStorage.getItem('failed_count');
274 if (this.accountNoOfDays !== '0' && this.passwordNoOfDays !== '0' &&
275 this.accountNoOfDays !== '1' && this.passwordNoOfDays !== '1') {
278 this.passwordExpiryToaster();
279 this.accountExpiryToaster();
281 this.isLoadingResults = false;
282 }, (error: ERRORDATA): void => {
283 this.isLoadingResults = false;
284 this.restService.handleError(error, 'get');
289 /** To Show or Hide the Password @public */
290 public onShowPassword(): void {
291 this.visiblePassword = !this.visiblePassword;
294 /** 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');
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)))
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 = '';
324 if (isNullOrUndefined(this.accountNoOfDays)) {
325 this.sharedService.passwordToaster(this.lastLogin, this.failedAttempts, this.passwordNoOfDays,
326 this.passwordExpireMessage, this.passwordMessage);
328 this.accountDaysCheck();
333 /** 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 = '';
344 this.accountExpireMessage = this.accountExpireMessage = this.translateService.instant('PAGE.LOGIN.ACCOUNTEXPIRE');
345 this.accountMessage = this.translateService.instant('PAGE.LOGIN.DAYS');
347 this.sharedService.showToaster(this.lastLogin, this.failedAttempts, this.passwordNoOfDays, this.accountNoOfDays,
348 this.passwordExpireMessage, this.accountExpireMessage, this.passwordMessage, this.accountMessage);
350 /** 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 = '';
366 if (isNullOrUndefined(this.passwordNoOfDays)) {
367 this.sharedService.accountToaster(this.lastLogin, this.failedAttempts,
368 this.accountNoOfDays, this.accountExpireMessage, this.accountMessage);
370 this.passwordDaysCheck();
375 /** 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 = '';
386 this.passwordExpireMessage = this.translateService.instant('PAGE.LOGIN.PASSWORDEXPIRE');
387 this.passwordMessage = this.translateService.instant('PAGE.LOGIN.DAYS');
389 this.sharedService.showToaster(this.lastLogin, this.failedAttempts, this.passwordNoOfDays, this.accountNoOfDays,
390 this.passwordExpireMessage, this.accountExpireMessage, this.passwordMessage, this.accountMessage);
392 /** 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 + ':' + ' ' + this.lastLogin +
396 '</br>' + this.failedAttemptsMessage + ':' + ' ' + this.failedAttempts +
397 '</br>' + this.passwordExpireMessage + ' ' + this.passwordNoOfDays + ' ' + this.daysMessage +
398 '</br>' + this.accountExpireMessage + ' ' + this.accountNoOfDays + ' ' + 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 + ':' + ' ' + this.lastLogin +
403 '</br>' + this.failedAttemptsMessage + ':' + ' ' + this.failedAttempts +
404 '</br>' + this.passwordExpireMessage + ' ' + this.passwordNoOfDays + ' ' + this.daysMessage,
405 this.translateService.instant('PAGE.LOGIN.LOGINHISTORY'), this.toasterSettings);
406 } else if (!isNullOrUndefined(this.accountNoOfDays)) {
408 this.lastLoginMessage + ':' + ' ' + this.lastLogin +
409 '</br>' + this.failedAttemptsMessage + ':' + ' ' + this.failedAttempts +
410 '</br>' + this.accountExpireMessage + ' ' + this.accountNoOfDays + ' ' + this.daysMessage,
411 this.translateService.instant('PAGE.LOGIN.LOGINHISTORY'), this.toasterSettings);
414 this.toaster.info(this.lastLoginMessage + ':' + ' ' + this.lastLogin +
415 '</br>' + this.failedAttemptsMessage + ':' + ' ' + this.failedAttempts,
416 this.translateService.instant('PAGE.LOGIN.LOGINHISTORY'), this.toasterSettings);