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
    }
51
    this.userManagementsService.getUserInfo(this.properties.userInfoUrl).subscribe(user => {
52
      this.user = user;
53
      this.loggedIn = !!this.user;
54
      this.errorMessage = "";
55
      this.sub = this.route.queryParams.subscribe(params => {
56
        this.errorCode = params["errorCode"];
57
        this.redirectUrl = params["redirectUrl"];
58
        this.errorMessage = "";
59
        if (this.loggedIn && this.errorCode == '1') {
60
          this.redirect();
61
        }
62
      });
63
    });
64
  }
65

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

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

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

    
117
    window.location.href = this.properties.loginUrl;
118
  }
119

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

    
132
  isUserManager() {
133
    return Session.isUserManager(this.user);
134
  }
135

    
136
}
(7-7/9)