Project

General

Profile

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

    
5
import {Session, User} 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 {RESPONSE} from "@nguniversal/express-engine/tokens";
11

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

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

    
32
  constructor(private router: Router,
33
              private route: ActivatedRoute,
34
              private _meta: Meta,
35
              private _title: Title,
36
              private userManagementsService: UserManagementService,
37
              @Optional() @Inject(RESPONSE) private response: any) {
38

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

    
43
  ngOnInit() {
44
    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
    }
53
    this.userManagementsService.getUserInfo(this.properties.userInfoUrl).subscribe(user => {
54
      this.user = user;
55
      this.loggedIn = !!this.user;
56
      this.errorMessage = "";
57
      this.sub = this.route.queryParams.subscribe(params => {
58
        this.errorCode = params["errorCode"];
59
        this.redirectUrl = params["redirectUrl"];
60
        this.errorMessage = "";
61
        if (this.loggedIn && this.errorCode == '1') {
62
          this.redirect();
63
        } else {
64
          if (this.errorCode == '1') {
65
            this.response.statusCode = 403;
66
            this.response.statusMessage = '403 - Forbidden';
67
          } else if (this.errorCode == '2' || this.errorCode == '3' || this.errorCode == '5') {
68
            this.response.statusCode = 401;
69
            this.response.statusMessage = '401 - Unauthorized';
70
          } else {
71
            this.response.statusCode = 400;
72
            this.response.statusMessage = '400 - Bad Request';
73
          }
74
        }
75
      });
76
    });
77
  }
78

    
79
  ngOnDestroy() {
80
    this.sub.unsubscribe();
81
    if (this.sublogin) {
82
      this.sublogin.unsubscribe();
83
    }
84
  }
85

    
86
  redirect() {
87
    if (this.redirectUrl && this.redirectUrl != "") {
88
      this.redirectUrl = decodeURIComponent(this.redirectUrl);
89
      var baseUrl = this.redirectUrl;
90
      var queryParams = "";
91
      var paramsArray = [];
92
      var valuesArray = [];
93
      if (this.redirectUrl.indexOf('?') != -1) {
94
        baseUrl = this.redirectUrl.split('?')[0];
95
        queryParams = this.redirectUrl.split('?')[1];
96
      }
97
      if (queryParams != "") {
98
        var queryParamsArray = queryParams.split('&');
99
        for (var i = 0; i < queryParamsArray.length; i++) {
100
          paramsArray.push(queryParamsArray[i].split("=")[0]);
101
          valuesArray.push(queryParamsArray[i].split("=")[1]);
102
        }
103
        this.router.navigate([baseUrl], {queryParams: this.routerHelper.createQueryParams(paramsArray, valuesArray)});
104
      } else {
105
        this.router.navigate([baseUrl]);
106
      }
107
    }
108
    // else{
109
    //   this.router.navigate(['/']);
110
    // }
111
  }
112

    
113
  logIn() {
114
    if (this.redirectUrl && this.redirectUrl != "") {
115
      this.redirectUrl = decodeURIComponent(this.redirectUrl);
116
      var baseUrl = this.redirectUrl;
117
      var queryParams = "";
118
      if (this.redirectUrl.indexOf('?') != -1) {
119
        var splits = this.redirectUrl.split('?');
120
        if (splits.length > 0) {
121
          baseUrl = splits[0];
122
        }
123
        if (splits.length > 1) {
124
          queryParams = splits[1];
125
        }
126
      }
127
      Session.setReloadUrl(location.protocol + "//" + location.host, baseUrl, queryParams);
128
    }
129

    
130
    window.location.href = this.properties.loginUrl;
131
  }
132

    
133
  getTheRolesFormatted(roles: string[]) {
134
    let formattedRoles = [];
135
    for (let role of roles) {
136
      let formattedRole = role.split("urn:geant:openaire.eu:group:")[1];
137
      formattedRole = formattedRole.split("#aai.openaire.eu")[0]
138
      formattedRole = formattedRole.replace("+", " ");
139
      formattedRole = formattedRole.split("+").join(" ");
140
      formattedRoles.push(formattedRole);
141
    }
142
    return formattedRoles.join(", ");
143
  }
144

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

    
149
}
(7-7/9)