Feature 10941: User Management Enhancements
[osm/NG-UI.git] / src / app / layouts / header / HeaderComponent.ts
1 /*
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 Header Component
21  */
22 import { Component, Injector, OnInit } from '@angular/core';
23 import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
24 import { TranslateService } from '@ngx-translate/core';
25 import { AddEditUserComponent } from 'AddEditUserComponent';
26 import { AuthenticationService } from 'AuthenticationService';
27 import { MODALCLOSERESPONSEDATA } from 'CommonModel';
28 import { environment } from 'environment';
29 import { ProjectService } from 'ProjectService';
30 import { Observable } from 'rxjs';
31 import { SharedService } from 'SharedService';
32 import { UserSettingsComponent } from 'UserSettingsComponent';
33
34 /**
35  * Creating component
36  * @Component takes HeaderComponent.html as template url
37  */
38 @Component({
39     selector: 'app-header',
40     templateUrl: './HeaderComponent.html',
41     styleUrls: ['./HeaderComponent.scss']
42 })
43 /** Exporting a class @exports HeaderComponent */
44 export class HeaderComponent implements OnInit {
45     /** Invoke service injectors @public */
46     public injector: Injector;
47
48     /** Variables holds all the projects @public */
49     public projectList$: Observable<{}[]>;
50
51     /** Observable holds logined value  @public */
52     public username$: Observable<string>;
53
54     /** Variables holds admin is logged or not @public */
55     public isAdmin: boolean;
56
57     /** Variables holds the selected project @public */
58     public selectedProject: Observable<string>;
59
60     /** project @public */
61     public getSelectedProject: string;
62
63     /** Version holds packages version @public */
64     public PACKAGEVERSION: string;
65
66     /** To check the role of the user is systemadmin or not @public */
67     public isSystemAdmin: boolean;
68
69     /** Contains all methods related to shared @public */
70     public sharedService: SharedService;
71
72     /** Property contains to show new version tag shared @public */
73     public toShowNewTag: Boolean = false;
74
75     /** handle translate @public */
76     public translateService: TranslateService;
77
78     /** Utilizes auth service for any auth operations @private */
79     private authService: AuthenticationService;
80
81     /** Holds all project details @private */
82     private projectService: ProjectService;
83
84     /** Utilizes modal service for any modal operations @private */
85     private modalService: NgbModal;
86
87     constructor(injector: Injector) {
88         this.injector = injector;
89         this.authService = this.injector.get(AuthenticationService);
90         this.modalService = this.injector.get(NgbModal);
91         this.projectService = this.injector.get(ProjectService);
92         this.sharedService = this.injector.get(SharedService);
93         this.translateService = this.injector.get(TranslateService);
94     }
95
96     /** Lifecyle Hooks the trigger before component is instantiate @public */
97     public ngOnInit(): void {
98         this.isAdmin = (localStorage.getItem('isAdmin') === 'true') ? true : false;
99         this.isSystemAdmin = localStorage.getItem('admin_show') === 'true' ? true : false;
100         this.selectedProject = this.authService.ProjectName;
101         this.authService.ProjectName.subscribe((projectNameFinal: string): void => {
102             this.getSelectedProject = projectNameFinal;
103         });
104         this.username$ = this.authService.username;
105         this.projectService.setHeaderProjects();
106         this.projectList$ = this.projectService.projectList;
107         this.PACKAGEVERSION = environment.packageVersion;
108         const getLocalStorageVersion: string = localStorage.getItem('osmVersion');
109         if (getLocalStorageVersion === null) {
110             this.showNewVersion();
111         } else if (getLocalStorageVersion !== this.sharedService.osmVersion) {
112             this.showNewVersion();
113         }
114     }
115
116     /** Logout function  @public */
117     public logout(): void {
118         this.authService.logout();
119     }
120
121     /** Show Version function  @public */
122     public showNewVersion(): void {
123         this.toShowNewTag = true;
124     }
125
126     /** Close Version and add in local storage  @public */
127     public closeVersion(): void {
128         this.toShowNewTag = false;
129         localStorage.setItem('osmVersion', this.sharedService.osmVersion);
130     }
131
132     /** Implementation of model for UserSettings options.@public */
133     public userSettings(): void {
134         // eslint-disable-next-line security/detect-non-literal-fs-filename
135         this.modalService.open(UserSettingsComponent, { backdrop: 'static' });
136     }
137
138     /** ChangePassword Function @public */
139     public changePassword(): void {
140         // eslint-disable-next-line security/detect-non-literal-fs-filename
141         const modalRef: NgbModalRef = this.modalService.open(AddEditUserComponent, { backdrop: 'static' });
142         modalRef.componentInstance.userID = localStorage.getItem('user_id');
143         modalRef.componentInstance.userTitle = this.translateService.instant('PAGE.USERS.EDITCREDENTIALS');
144         modalRef.componentInstance.userType = 'changePassword';
145         modalRef.result.then((result: MODALCLOSERESPONSEDATA): void => {
146             if (result) {
147                 this.sharedService.callData();
148             }
149         }).catch((): void => {
150             // Catch Navigation Error
151         });
152     }
153 }