Project

General

Profile

1
import {Component, Input} from '@angular/core';
2
import {ActivatedRoute, Router} from '@angular/router';
3
import {Title, Meta} from '@angular/platform-browser';
4

    
5
import {User, Session} from './utils/helper.class';
6
import {RouterHelper} from '../utils/routerHelper.class';
7

    
8
import {EnvProperties} from '../utils/properties/env-properties';
9
import {UserManagementService} from "../services/user-management.service";
10

    
11
@Component({
12
  selector: 'user',
13
  templateUrl: 'user.component.html'
14
})
15

    
16
export class UserComponent {
17
  public user: User;
18
  public loggedIn: boolean = false;
19
  public server: boolean = true;
20
  public errorMessage: string = "";
21
  public password: string = "";
22
  private sub: any;
23
  private sublogin: any;
24
  public errorCode: string = "";
25
  public redirectUrl: string = "";
26
  public routerHelper: RouterHelper = new RouterHelper();
27
  public loginUrl;
28
  properties: EnvProperties;
29
  @Input() mainComponent = true;
30

    
31
  constructor(private router: Router,
32
              private route: ActivatedRoute,
33
              private _meta: Meta,
34
              private _title: Title,
35
              private userManagementsService: UserManagementService) {
36

    
37
    var title = "OpenAIRE | Login";
38
    this._title.setTitle(title);
39
  }
40

    
41
  ngOnInit() {
42
    this.route.data
43
      .subscribe((data: { envSpecific: EnvProperties }) => {
44
        this.properties = data.envSpecific;
45
        this.loginUrl = this.properties.loginUrl;
46

    
47
      });
48
    if (typeof document !== 'undefined') {
49
      this.server = false;
50
      this.userManagementsService.getUserInfo().subscribe(user => {
51
        this.user = user;
52
        this.loggedIn = !!this.user;
53
        this.errorMessage = "";
54
        this.sub = this.route.queryParams.subscribe(params => {
55
          this.errorCode = params["errorCode"];
56
          this.redirectUrl = params["redirectUrl"];
57
          this.errorMessage = "";
58
          if (this.loggedIn && this.errorCode == '1') {
59
            this.redirect();
60
          }
61
        });
62
      });
63
    }
64
  }
65

    
66

    
67
  ngOnDestroy() {
68
    if(this.sub) {
69
      this.sub.unsubscribe();
70
    }
71
    if (this.sublogin) {
72
      this.sublogin.unsubscribe();
73
    }
74
  }
75

    
76
  redirect() {
77
    if (this.redirectUrl && this.redirectUrl != "") {
78
      this.redirectUrl = decodeURIComponent(this.redirectUrl);
79
      var baseUrl = this.redirectUrl;
80
      var queryParams = "";
81
      var paramsArray = [];
82
      var valuesArray = [];
83
      if (this.redirectUrl.indexOf('?') != -1) {
84
        baseUrl = this.redirectUrl.split('?')[0];
85
        queryParams = this.redirectUrl.split('?')[1];
86
      }
87
      if (queryParams != "") {
88
        var queryParamsArray = queryParams.split('&');
89
        for (var i = 0; i < queryParamsArray.length; i++) {
90
          paramsArray.push(queryParamsArray[i].split("=")[0]);
91
          valuesArray.push(queryParamsArray[i].split("=")[1]);
92
        }
93
        this.router.navigate([baseUrl], {queryParams: this.routerHelper.createQueryParams(paramsArray, valuesArray)});
94
      } else {
95
        this.router.navigate([baseUrl]);
96
      }
97
    }
98
    // else{
99
    //   this.router.navigate(['/']);
100
    // }
101
  }
102

    
103
  logIn() {
104
    if (this.redirectUrl && this.redirectUrl != "") {
105
      this.redirectUrl = decodeURIComponent(this.redirectUrl);
106
      var baseUrl = this.redirectUrl;
107
      var queryParams = "";
108
      if (this.redirectUrl.indexOf('?') != -1) {
109
        var splits = this.redirectUrl.split('?');
110
        if (splits.length > 0) {
111
          baseUrl = splits[0];
112
        }
113
        if (splits.length > 1) {
114
          queryParams = splits[1];
115
        }
116
      }
117
      Session.setReloadUrl(location.protocol + "//" + location.host, baseUrl, queryParams);
118
    }
119

    
120
    window.location.href = this.properties.loginUrl;
121
  }
122

    
123
  getTheRolesFormatted(roles: string[]) {
124
    let formattedRoles = [];
125
    for (let role of roles) {
126
      let formattedRole = role.split("urn:geant:openaire.eu:group:")[1];
127
      formattedRole = formattedRole.split("#aai.openaire.eu")[0]
128
      formattedRole = formattedRole.replace("+", " ");
129
      formattedRole = formattedRole.split("+").join(" ");
130
      formattedRoles.push(formattedRole);
131
    }
132
    return formattedRoles.join(", ");
133
  }
134

    
135
  isUserManager() {
136
    return Session.isUserManager(this.user);
137
  }
138

    
139
}
(7-7/10)