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

    
45
    this.properties = properties;
46
    this.loginUrl = this.properties.loginUrl;
47

    
48
    if (typeof document !== 'undefined') {
49
      this.server = false;
50
      this.subscriptions.push(this.userManagementsService.getUserInfo().subscribe(user => {
51
        this.user = user;
52
        this.loggedIn = !!this.user;
53
        this.errorMessage = "";
54
        this.subscriptions.push(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
    this.subscriptions.forEach(subscription => {
69
      if (subscription instanceof Subscriber) {
70
        subscription.unsubscribe();
71
      }
72
    });
73
  }
74

    
75
  redirect() {
76
    if (this.redirectUrl && this.redirectUrl != "") {
77
      this.redirectUrl = decodeURIComponent(this.redirectUrl);
78
      var route = this.redirectUrl;
79
      var queryParams = "";
80
      var paramsArray = [];
81
      var valuesArray = [];
82
      if (this.redirectUrl.indexOf('?') != -1) {
83
        route = this.redirectUrl.split('?')[0];
84
        queryParams = this.redirectUrl.split('?')[1];
85
      }
86
      if (queryParams != "") {
87
        var queryParamsArray = queryParams.split('&');
88
        for (var i = 0; i < queryParamsArray.length; i++) {
89
          paramsArray.push(queryParamsArray[i].split("=")[0]);
90
          valuesArray.push(queryParamsArray[i].split("=")[1]);
91
        }
92
        console.debug('here');
93
        this.router.navigate([route], {queryParams: this.routerHelper.createQueryParams(paramsArray, valuesArray)});
94
      } else {
95
        this.router.navigate([route]);
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 route = this.redirectUrl;
107
      var queryParams = "";
108
      if (this.redirectUrl.indexOf('?') != -1) {
109
        var splits = this.redirectUrl.split('?');
110
        if (splits.length > 0) {
111
          route = splits[0];
112
        }
113
        if (splits.length > 1) {
114
          queryParams = splits[1];
115
        }
116
      }
117
      Session.setReloadUrl(location.protocol + "//" + location.host,properties.baseLink +  route, queryParams);
118
    }
119
    console.log(Session.getReloadUrl());
120
    window.location.href = this.properties.loginUrl;
121
  }
122
  
123
  getTheRolesFormatted(roles: string[]) {
124
    let formattedRoles = [];
125
    for (let role of roles) {
126
      if(role.includes("urn:geant:openaire.eu:group:")) {
127
        let formattedRole = role.split("urn:geant:openaire.eu:group:")[1];
128
        formattedRole = formattedRole.split("#aai.openaire.eu")[0]
129
        formattedRole = formattedRole.replace("+", " ");
130
        formattedRole = formattedRole.split("+").join(" ");
131
        formattedRoles.push(formattedRole);
132
      }else{
133
        if(role.indexOf("_MANAGER")!=-1){
134
          formattedRoles.push("Manager of " +  role.split("_")[1]);
135
        }else if((["FUNDER","COMMUNITY","INSTITUTION","PROJECT"]).indexOf(role.split("_")[0])!=-1){
136
          formattedRoles.push("Member of " +  role.split("_")[1]);
137
        }else{
138
          formattedRoles.splice(0,0,StringUtils.capitalize(role.split('_').join(' ').toLowerCase()));
139
        }
140
      }
141
    }
142
    return formattedRoles.join(", ");
143
  }
144

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

    
149
}
(7-7/11)