Project

General

Profile

1
import {Component, ElementRef} from '@angular/core';
2
import {Observable}       from 'rxjs/Observable';
3
import {ActivatedRoute, Router} from '@angular/router';
4

    
5
// import {LoginService} from './login.service';
6
import {User,Session} from './utils/helper.class';
7
import {RouterHelper} from '../utils/routerHelper.class';
8
import { Meta} from '../../angular2-meta';
9
import {OpenaireProperties} from '../utils/properties/openaireProperties';
10

    
11
@Component({
12
    selector: 'user',
13
    template: `
14
    <div id="tm-main" class=" uk-section  uk-margin-small-top tm-middle"   >
15
      <div uk-grid uk-grid>
16
       <div class="tm-main uk-width-1-1@s uk-width-1-1@m  uk-width-1-1@l uk-row-first ">
17

    
18
<div *ngIf="!server" class="uk-margin-top uk-container uk-container-small uk-position-relative">
19

    
20

    
21
    <!--form *ngIf="!loggedIn" class=" ">
22

    
23
            <h3>Welcome to OpenAIRE's Discover Portal</h3>
24

    
25
               <p>This service uses the same ldap sign-in  as the OpenAIRE services so you can use the same
26
                   credentials.</p>
27

    
28
               <p>Login in. To see it in action.</p>
29
            <div class="uk-margin">
30
             <input class="uk-input uk-form-width-medium" placeholder="Username" type="text" name="username" [(ngModel)]="username">
31
          </div>
32

    
33
          <div class="uk-margin">
34
             <input placeholder="Password" class="uk-input uk-form-width-medium" type="password" name="password" [(ngModel)]="password">
35
          </div>
36
          <div class="uk-margin">
37
             <button (click)="login()" class=" uk-button uk-button-primary">Login</button>
38
          </div>
39
     </form-->
40
     <div *ngIf="errorCode == '1'" class="uk-alert uk-alert-warning">
41
      The requested page requires authentication. Please sign in.
42
      <span  *ngIf="!loggedIn">
43
          <a  class="loginLink"  [href]="loginUrl"  >Sign in</a>
44
       </span>
45
    </div>
46
    <div *ngIf="errorCode == '2'" class="uk-alert uk-alert-warning">
47
      You are not authorized to use the requested page
48
    </div>
49
    <div *ngIf="errorCode == '3'" class="uk-alert uk-alert-warning">
50
      The session has expired. Please sign in again or continue <a (click)="redirect();">browsing as a guest</a>.
51
      <span  *ngIf="!loggedIn">
52
          <a  class="loginLink"   [href]="loginUrl"   >Sign in</a>
53
       </span>
54
    </div>
55
    <div *ngIf="!loggedIn && errorMessage.length > 0" class="uk-alert uk-alert-danger">{{errorMessage}}</div>
56

    
57
    <div *ngIf="loggedIn">
58
        <div  class="uk-alert uk-alert-success">
59
        Hello {{user.fullname}}!
60
        </div>
61
        <button (click)="logout()" class=" uk-button uk-button-default">Log out</button>
62
    </div>
63
</div>
64
</div>
65
</div>
66
</div>
67
    `
68
})
69

    
70
export class UserComponent {
71
    public user: User;
72
    public loggedIn: boolean = false;
73
    public server: boolean = true;
74
    public errorMessage: string;
75
    public username: string = "";
76
    public password: string = "";
77
    private sub:any;private sublogin:any;
78
    public errorCode: string = "";
79
    public redirectUrl: string = "";
80
    public routerHelper:RouterHelper = new RouterHelper();
81
    public loginUrl= OpenaireProperties.getLoginURL();
82

    
83
    constructor( private router: Router,
84
                 private  route: ActivatedRoute, private _meta: Meta ) {
85
      this._meta.setTitle("OpenAIRE | Login");
86
    }
87

    
88
    ngOnInit() {
89
      if( typeof document !== 'undefined') {
90
        this.server = false;
91
      }
92
      this.loggedIn = Session.isLoggedIn();
93
      this.user = Session.getUser();
94
      this.errorMessage = "";
95
      this.sub =  this.route.queryParams.subscribe(params => {
96
        this.errorCode = params["errorCode"];
97
        this.redirectUrl = params["redirectUrl"];
98
        this.loggedIn = Session.isLoggedIn();
99
        this.user = Session.getUser();
100
        this.errorMessage = "";
101
      });
102
    }
103
    ngOnDestroy(){
104
      this.sub.unsubscribe();
105
      if(this.sublogin){
106
        this.sublogin.unsubscribe();
107
      }
108
    }
109
    logout(){
110
        if(Session.isLoggedIn()){
111
          Session.removeUser();
112
        }
113
        this.loggedIn = false;
114
        this.user = new User();
115
        this.username = "";
116
        this.password = "";
117
        this.redirect();
118

    
119
    }
120
    redirect(){
121
      if(this.redirectUrl && this.redirectUrl != null && this.redirectUrl != ""){
122
        this.redirectUrl = decodeURIComponent(this.redirectUrl);
123
        var baseUrl = this.redirectUrl;
124
        var queryParams = "";
125
        var paramsArray =[];
126
        var valuesArray =[];
127
        if(this.redirectUrl.indexOf('?') != -1){
128
          baseUrl = this.redirectUrl.split('?')[0];
129
          queryParams =  this.redirectUrl.split('?')[1];
130
        }
131
        if(queryParams != ""){
132
          var queryParamsArray = queryParams.split('&');
133
          for(var i = 0; i < queryParamsArray.length; i++){
134
            paramsArray.push(queryParamsArray[i].split("=")[0]);
135
            valuesArray.push(queryParamsArray[i].split("=")[1]);
136
          }
137
          this.router.navigate([baseUrl], { queryParams: this.routerHelper.createQueryParams(paramsArray,valuesArray)});
138
        }else{
139
          this.router.navigate([baseUrl]);
140
        }
141
      }else{
142
        this.router.navigate(['/search/find']);
143
      }
144
    }
145
    // login() {
146
    //     this.sublogin =this._loginService.authenticate(/*this.user*/this.username, this.password).subscribe(
147
    //         data => {
148
    //             this.user = data;
149
    //             this.loggedIn = true;
150
    //             this.username = "";
151
    //             this.password = "";
152
    //             this.errorCode = "";
153
    //             this.redirect();
154
    //
155
    //         },
156
    //         err => {
157
    //           console.log(err);
158
    //           if(err.status == "404") {
159
    //               this.errorMessage = "Wrong username";
160
    //           } else if(err.status == "401") {
161
    //               this.errorMessage = "Wrong password";
162
    //           }
163
    //           this.username = "";
164
    //           this.password = "";
165
    //           this.errorCode = "";
166
    //
167
    //         }
168
    //     );
169
    // }
170
}
(5-5/7)