61d13186fa8f8db25730bd5d325943f7d9bbfe57
[sdc.git] / catalog-ui / src / app / ng2 / services / cookie.service.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 import {Injectable, Inject} from '@angular/core';
22 import {ICookie} from "../../models/app-config";
23 import {SdcConfigToken, ISdcConfig} from "../config/sdc-config.config";
24
25 @Injectable()
26 export class Cookie2Service {
27
28     private cookie:ICookie;
29     private cookiePrefix:string;
30
31     constructor(@Inject(SdcConfigToken) sdcConfig:ISdcConfig) {
32         this.cookie = sdcConfig.cookie;
33
34         this.cookiePrefix = '';
35         let junctionName:string = this.getCookieByName(this.cookie.junctionName);
36         if ((junctionName !== null) && (junctionName !== '')) {
37             this.cookiePrefix = this.cookie.prefix + junctionName + '!';
38         }
39         console.log("junctionName:" + junctionName);
40     }
41
42     private getCookieByName = (cookieName:string):string => {
43         cookieName += '=';
44         let cookies:Array<string> = document.cookie.split(';');
45         let cookieVal:string = '';
46         cookies.forEach((cookie:string) => {
47             while (cookie.charAt(0) === ' ') {
48                 cookie = cookie.substring(1);
49             }
50             if (cookie.indexOf(cookieName) === 0) {
51                 cookieVal = cookie.substring(cookieName.length, cookie.length);
52                 return;
53             }
54         });
55         return cookieVal;
56     };
57
58     public getUserIdSuffix = ():string => {
59         return this.cookie.userIdSuffix;
60     };
61
62     public getUserId = ():string => {
63         let userIdCookieName:string = this.cookiePrefix + this.cookie.userIdSuffix;
64         let userId:string = this.getCookieByName(userIdCookieName);
65         return userId;
66     };
67
68     public getFirstName = ():string => {
69         let firstNameCookieName:string = this.cookiePrefix + this.cookie.userFirstName;
70         let firstName:string = this.getCookieByName(firstNameCookieName);
71         return firstName;
72     };
73
74     public getLastName = ():string => {
75         let lastNameCookieName:string = this.cookiePrefix + this.cookie.userLastName;
76         let lastName:string = this.getCookieByName(lastNameCookieName);
77         return lastName;
78     };
79
80     public getEmail = ():string => {
81         let emailCookieName:string = this.cookiePrefix + this.cookie.userEmail;
82         let email:string = this.getCookieByName(emailCookieName);
83         return email;
84     }
85 }