Business Card Integration
[portal.git] / portal-FE-common / src / app / layout / components / header / header.component.ts
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright � 2019 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 import { Component, OnInit } from '@angular/core';
39 import { Router, NavigationEnd } from '@angular/router';
40 import { UserProfileService, MenusService } from 'src/app/shared/services';
41 import { CookieService } from 'ngx-cookie-service';
42 import { environment } from 'src/environments/environment';
43
44 @Component({
45     selector: 'app-header',
46     templateUrl: './header.component.html',
47     styleUrls: ['./header.component.scss']
48 })
49 export class HeaderComponent implements OnInit {
50     public pushRightClass: string;
51     firstName: string;
52     lastName: string;
53     loginSnippetUserid: any;
54     lastLogin: number;
55     loginSnippetEmail: any;
56     userapproles: any[];
57     displayUserAppRoles: any;
58     isLoading: boolean;
59     api = environment.api;
60     brandName: string;
61     brandLogoImagePath: string;
62     isSystemUser: boolean = false;
63
64     constructor(public router: Router, private userProfileService: UserProfileService, private menusService: MenusService, private cookieService: CookieService) {
65
66         this.router.events.subscribe(val => {
67             if (
68                 val instanceof NavigationEnd &&
69                 window.innerWidth <= 992 &&
70                 this.isToggled()
71             ) {
72                 this.toggleSidebar();
73             }
74         });
75     }
76
77     ngOnInit() {
78         this.pushRightClass = 'push-right';
79         this.getUserInformation();
80         
81         this.brandName = "ONAP Portal";
82         if(this.api.brandName != ''){
83             this.brandName = this.api.brandName;
84          }
85
86         this.brandLogoImagePath = "assets/images/global.logo";
87         if(this.api.brandLogoImagePath != ''){
88            this.brandLogoImagePath = this.api.brandLogoImagePath;
89         }
90     }
91
92     getUserInformation() {
93         this.userProfileService.getFunctionalMenuStaticInfo().toPromise().then((res: any) => {
94             if (res === null || res.firstName === null || res.firstName === '' || res.lastName === null || res.lastName === '') {
95                 // $log.info('HeaderCtrl: failed to get all required data, trying user profile');
96                 this.userProfileService.getUserProfile().toPromise().then((profile: any) => {
97                     this.firstName = profile.firstName;
98                     this.lastName = profile.lastName;
99                 }, (err) => {
100                     // $log.error('Header Controller:: getUserProfile() failed: ' + err);
101                 });
102             } else {
103                 this.firstName = res.firstName;
104                 this.lastName = res.lastName;
105                 this.loginSnippetEmail = res.email;
106                 this.loginSnippetUserid = res.userId;
107                 this.lastLogin = Date.parse(res.last_login);
108             }
109             if(res != null && res.isSystemUser === 'true'){
110                 this.isSystemUser = true;
111             }
112             sessionStorage.userId = res.userId;
113             this.menusService.getFunctionalMenuForUser().toPromise().then((jsonHeaderMenu: any) => {
114                 // $scope.menuItems = unflatten(jsonHeaderMenu);
115                 // $scope.megaMenuDataObject = $scope.menuItems;
116             }, (err) => {
117                 // $log.error('HeaderCtrl::GetFunctionalMenuForUser: HeaderCtrl json returned: ' + err);
118             });
119
120         }, (err) => {
121             // $log.error('HeaderCtrl::getFunctionalMenuStaticInfo failed: ' + err);
122         })
123     }
124
125     //     unflatten = function( array, parent, tree ){
126
127     //     tree = typeof tree !== 'undefined' ? tree : [];
128     //     parent = typeof parent !== 'undefined' ? parent : { menuId: null };
129     //     var children = _.filter( array, function(child){ return child.parentMenuId == parent.menuId; });
130
131     //     if( !_.isEmpty( children )  ){
132     //       if( parent.menuId === null ){
133     //         tree = children;
134     //       }else{
135     //         parent['children'] = children
136     //       }
137     //       _.each( children, function( child ){ unflatten( array, child ) } );
138     //     }
139
140     //     return tree;
141     // }
142
143     getUserApplicationRoles() {
144         this.userapproles = [];
145         if (this.displayUserAppRoles) {
146             this.displayUserAppRoles = false;
147         } else {
148             this.displayUserAppRoles = true;
149             this.isLoading = true;
150             this.userProfileService.getUserAppRoles(this.loginSnippetUserid)
151             .subscribe((res: any) => {
152                 this.isLoading = false;
153                 for (var i = 0; i < res.length; i++) {
154                     var userapprole = {
155                         App: res[i].appName,
156                         Roles: res[i].roleNames,
157                     };
158                     this.userapproles.push(userapprole);
159                 }
160             }, (err) => {
161                 this.isLoading = false;
162             });
163         }
164     }
165
166     allAppsLogout() {
167         this.firstName="";
168         this.lastName="";
169         this.displayUserAppRoles=false;         
170         var cookieTabs = this.cookieService.get("visInVisCookieTabs").toString;
171          if(cookieTabs!=null){
172              for(var t in cookieTabs){
173              
174                  var url = cookieTabs[t].content;
175                  if(url != "") {
176                      this.menusService.logout(url);
177                    }
178              }
179          }
180          // wait for individual applications to log out before the portal logout
181          setTimeout(function() {
182              window.location.href = "logout.htm";
183          }, 2000);
184     }
185
186     isToggled(): boolean {
187         const dom: Element = document.querySelector('body');
188         return dom.classList.contains(this.pushRightClass);
189     }
190
191     toggleSidebar() {
192         const dom: any = document.querySelector('body');
193         dom.classList.toggle(this.pushRightClass);
194     }
195
196     onLoggedout() {
197         localStorage.removeItem('isLoggedin');
198     }
199 }