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 loading: boolean = true;
|
22
|
public loggedIn: boolean = false;
|
23
|
public server: boolean = true;
|
24
|
public errorMessage: string = "";
|
25
|
public password: string = "";
|
26
|
private subscriptions = [];
|
27
|
public errorCode: string = "";
|
28
|
public redirectUrl: string = "";
|
29
|
public routerHelper: RouterHelper = new RouterHelper();
|
30
|
public loginUrl;
|
31
|
public properties: EnvProperties = properties;
|
32
|
@Input() mainComponent = true;
|
33
|
|
34
|
constructor(private router: Router,
|
35
|
private route: ActivatedRoute,
|
36
|
private _meta: Meta,
|
37
|
private _title: Title,
|
38
|
private userManagementsService: UserManagementService) {
|
39
|
this._title.setTitle("OpenAIRE | Login");
|
40
|
}
|
41
|
|
42
|
ngOnInit() {
|
43
|
this.loginUrl = this.properties.loginUrl;
|
44
|
if (typeof document !== 'undefined') {
|
45
|
this.server = false;
|
46
|
this.subscriptions.push(this.userManagementsService.getUserInfo(false).subscribe(user => {
|
47
|
this.user = user;
|
48
|
this.loggedIn = !!this.user;
|
49
|
this.errorMessage = "";
|
50
|
this.loading = true;
|
51
|
this.subscriptions.push(this.route.queryParams.subscribe(params => {
|
52
|
this.errorCode = params["errorCode"];
|
53
|
this.redirectUrl = params["redirectUrl"];
|
54
|
this.errorMessage = "";
|
55
|
if (this.loggedIn && (this.errorCode == '1' || this.errorCode == '3')) {
|
56
|
this.redirect();
|
57
|
} else {
|
58
|
this.loading = false;
|
59
|
}
|
60
|
}));
|
61
|
}));
|
62
|
}
|
63
|
}
|
64
|
|
65
|
ngOnDestroy() {
|
66
|
this.subscriptions.forEach(subscription => {
|
67
|
if (subscription instanceof Subscriber) {
|
68
|
subscription.unsubscribe();
|
69
|
}
|
70
|
});
|
71
|
}
|
72
|
|
73
|
redirect() {
|
74
|
if (this.redirectUrl && this.redirectUrl != "") {
|
75
|
this.redirectUrl = decodeURIComponent(this.redirectUrl);
|
76
|
this.userManagementsService.setRedirectUrl(this.redirectUrl);
|
77
|
this.router.navigate(['/reload']);
|
78
|
}
|
79
|
}
|
80
|
|
81
|
logIn() {
|
82
|
this.userManagementsService.login();
|
83
|
}
|
84
|
|
85
|
getTheRolesFormatted(roles: string[]) {
|
86
|
let formattedRoles = [];
|
87
|
for (let role of roles) {
|
88
|
if(role.indexOf("_MANAGER")!=-1){
|
89
|
formattedRoles.push("Manager of " + role.split("_")[1]);
|
90
|
}else if((["FUNDER","COMMUNITY","INSTITUTION","PROJECT"]).indexOf(role.split("_")[0])!=-1){
|
91
|
formattedRoles.push("Member of " + role.split("_")[1]);
|
92
|
}else{
|
93
|
formattedRoles.splice(0,0,StringUtils.capitalize(role.split('_').join(' ').toLowerCase()));
|
94
|
}
|
95
|
}
|
96
|
return formattedRoles.join(", ");
|
97
|
}
|
98
|
|
99
|
isUserManager() {
|
100
|
return Session.isUserManager(this.user);
|
101
|
}
|
102
|
|
103
|
}
|