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
|
|
10
|
@Component({
|
11
|
selector: 'user',
|
12
|
templateUrl: 'user.component.html'
|
13
|
})
|
14
|
|
15
|
export class UserComponent {
|
16
|
public user: User;
|
17
|
public loggedIn: boolean = false;
|
18
|
public server: boolean = true;
|
19
|
public errorMessage: string = "";
|
20
|
public password: string = "";
|
21
|
private sub:any;
|
22
|
private sublogin:any;
|
23
|
public errorCode: string = "";
|
24
|
public redirectUrl: string = "";
|
25
|
public routerHelper:RouterHelper = new RouterHelper();
|
26
|
public loginUrl;
|
27
|
properties:EnvProperties;
|
28
|
@Input() mainComponent = true;
|
29
|
|
30
|
constructor( private router: Router,
|
31
|
private route: ActivatedRoute,
|
32
|
private _meta: Meta,
|
33
|
private _title: Title) {
|
34
|
|
35
|
var title = "OpenAIRE | Login";
|
36
|
this._title.setTitle(title);
|
37
|
}
|
38
|
|
39
|
ngOnInit() {
|
40
|
this.route.data
|
41
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
42
|
this.properties = data.envSpecific;
|
43
|
this.loginUrl = this.properties.loginUrl;
|
44
|
|
45
|
});
|
46
|
if( typeof document !== 'undefined') {
|
47
|
this.server = false;
|
48
|
}
|
49
|
this.loggedIn = Session.isLoggedIn();
|
50
|
this.user = Session.getUser();
|
51
|
this.errorMessage = "";
|
52
|
this.sub = this.route.queryParams.subscribe(params => {
|
53
|
this.errorCode = params["errorCode"];
|
54
|
this.redirectUrl = params["redirectUrl"];
|
55
|
this.loggedIn = Session.isLoggedIn();
|
56
|
this.user = Session.getUser();
|
57
|
this.errorMessage = "";
|
58
|
if(this.loggedIn && this.errorCode == '1'){
|
59
|
this.redirect();
|
60
|
}
|
61
|
});
|
62
|
}
|
63
|
ngOnDestroy(){
|
64
|
this.sub.unsubscribe();
|
65
|
if(this.sublogin){
|
66
|
this.sublogin.unsubscribe();
|
67
|
}
|
68
|
}
|
69
|
|
70
|
redirect(){
|
71
|
if(this.redirectUrl && this.redirectUrl != null && this.redirectUrl != ""){
|
72
|
this.redirectUrl = decodeURIComponent(this.redirectUrl);
|
73
|
var baseUrl = this.redirectUrl;
|
74
|
var queryParams = "";
|
75
|
var paramsArray =[];
|
76
|
var valuesArray =[];
|
77
|
if(this.redirectUrl.indexOf('?') != -1){
|
78
|
baseUrl = this.redirectUrl.split('?')[0];
|
79
|
queryParams = this.redirectUrl.split('?')[1];
|
80
|
}
|
81
|
if(queryParams != ""){
|
82
|
var queryParamsArray = queryParams.split('&');
|
83
|
for(var i = 0; i < queryParamsArray.length; i++){
|
84
|
paramsArray.push(queryParamsArray[i].split("=")[0]);
|
85
|
valuesArray.push(queryParamsArray[i].split("=")[1]);
|
86
|
}
|
87
|
this.router.navigate([baseUrl], { queryParams: this.routerHelper.createQueryParams(paramsArray,valuesArray)});
|
88
|
}else{
|
89
|
this.router.navigate([baseUrl]);
|
90
|
}
|
91
|
}
|
92
|
// else{
|
93
|
// this.router.navigate(['/']);
|
94
|
// }
|
95
|
}
|
96
|
|
97
|
logIn(){
|
98
|
if(this.redirectUrl && this.redirectUrl != null && this.redirectUrl != ""){
|
99
|
this.redirectUrl = decodeURIComponent(this.redirectUrl);
|
100
|
var baseUrl = this.redirectUrl;
|
101
|
var queryParams = "";
|
102
|
if(this.redirectUrl.indexOf('?') != -1){
|
103
|
var splits =this.redirectUrl.split('?');
|
104
|
if(splits.length>0){
|
105
|
baseUrl = splits[0];
|
106
|
}
|
107
|
if(splits.length >1){
|
108
|
queryParams = splits[1];
|
109
|
}
|
110
|
}
|
111
|
Session.setReloadUrl(location.protocol +"//"+location.host, baseUrl, queryParams);
|
112
|
}
|
113
|
|
114
|
window.location.href = this.properties.loginUrl;
|
115
|
}
|
116
|
getTheRolesFormatted(roles:string[]){
|
117
|
let formattedRoles = [];
|
118
|
for(let role of roles ){
|
119
|
let formattedRole = role.split("urn:geant:openaire.eu:group:")[1];
|
120
|
formattedRole=formattedRole.split("#aai.openaire.eu")[0]
|
121
|
formattedRole = formattedRole.replace("+"," ");
|
122
|
formattedRole = formattedRole.split("+").join(" ");
|
123
|
formattedRoles.push(formattedRole);
|
124
|
}
|
125
|
return formattedRoles.join(", ");
|
126
|
}
|
127
|
isUSerManager(){
|
128
|
return Session.isUserManager();
|
129
|
}
|
130
|
|
131
|
}
|