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
import {properties} from "../../../environments/environment";
11
import {Subscriber} from "rxjs";
12
import {StringUtils} from "../utils/string-utils.class";
13

    
14
@Component({
15
  selector: 'user',
16
  templateUrl: 'user.component.html'
17
})
18

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

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

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

    
43
  ngOnInit() {
44
    this.subscriptions.push(this.route.data
45
      .subscribe((data: { envSpecific: EnvProperties }) => {
46
        this.properties = data.envSpecific;
47
        this.loginUrl = this.properties.loginUrl;
48

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

    
68

    
69
  ngOnDestroy() {
70
    this.subscriptions.forEach(subscription => {
71
      if (subscription instanceof Subscriber) {
72
        subscription.unsubscribe();
73
      }
74
    });
75
  }
76

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

    
104
  logIn() {
105
    if (this.redirectUrl && this.redirectUrl != "") {
106
      this.redirectUrl = decodeURIComponent(this.redirectUrl);
107
      var route = this.redirectUrl;
108
      var queryParams = "";
109
      if (this.redirectUrl.indexOf('?') != -1) {
110
        var splits = this.redirectUrl.split('?');
111
        if (splits.length > 0) {
112
          route = splits[0];
113
        }
114
        if (splits.length > 1) {
115
          queryParams = splits[1];
116
        }
117
      }
118
      Session.setReloadUrl(location.protocol + "//" + location.host,properties.baseLink +  route, queryParams);
119
    }
120
    console.log(Session.getReloadUrl());
121
    window.location.href = this.properties.loginUrl;
122
  }
123
  
124
  getTheRolesFormatted(roles: string[]) {
125
    let formattedRoles = [];
126
    for (let role of roles) {
127
      if(role.includes("urn:geant:openaire.eu:group:")) {
128
        let formattedRole = role.split("urn:geant:openaire.eu:group:")[1];
129
        formattedRole = formattedRole.split("#aai.openaire.eu")[0]
130
        formattedRole = formattedRole.replace("+", " ");
131
        formattedRole = formattedRole.split("+").join(" ");
132
        formattedRoles.push(formattedRole);
133
      }else{
134
        if(role.indexOf("_MANAGER")!=-1){
135
          formattedRoles.push("Manager of " +  role.split("_")[1]);
136
        }else if((["FUNDER","COMMUNITY","INSTITUTION","PROJECT"]).indexOf(role.split("_")[0])!=-1){
137
          formattedRoles.push("Member of " +  role.split("_")[1]);
138
        }else{
139
          formattedRoles.splice(0,0,StringUtils.capitalize(role.split('_').join(' ').toLowerCase()));
140
        }
141
      }
142
    }
143
    return formattedRoles.join(", ");
144
  }
145

    
146
  isUserManager() {
147
    return Session.isUserManager(this.user);
148
  }
149

    
150
}
(7-7/11)