import {Component, Input} from '@angular/core'; import {ActivatedRoute, Router} from '@angular/router'; import {Title, Meta} from '@angular/platform-browser'; import {User, Session} from './utils/helper.class'; import {RouterHelper} from '../utils/routerHelper.class'; import {EnvProperties} from '../utils/properties/env-properties'; import {UserManagementService} from "../services/user-management.service"; import {properties} from "../../../environments/environment"; import {Subscriber} from "rxjs"; import {StringUtils} from "../utils/string-utils.class"; @Component({ selector: 'user', templateUrl: 'user.component.html' }) export class UserComponent { public user: User; public loggedIn: boolean = false; public server: boolean = true; public errorMessage: string = ""; public password: string = ""; private subscriptions = []; public errorCode: string = ""; public redirectUrl: string = ""; public routerHelper: RouterHelper = new RouterHelper(); public loginUrl; properties: EnvProperties; @Input() mainComponent = true; constructor(private router: Router, private route: ActivatedRoute, private _meta: Meta, private _title: Title, private userManagementsService: UserManagementService) { var title = "OpenAIRE | Login"; this._title.setTitle(title); } ngOnInit() { this.subscriptions.push(this.route.data .subscribe((data: { envSpecific: EnvProperties }) => { this.properties = data.envSpecific; this.loginUrl = this.properties.loginUrl; })); if (typeof document !== 'undefined') { this.server = false; this.subscriptions.push(this.userManagementsService.getUserInfo().subscribe(user => { this.user = user; this.loggedIn = !!this.user; this.errorMessage = ""; this.subscriptions.push(this.route.queryParams.subscribe(params => { this.errorCode = params["errorCode"]; this.redirectUrl = params["redirectUrl"]; this.errorMessage = ""; if (this.loggedIn && this.errorCode == '1') { this.redirect(); } })); })); } } ngOnDestroy() { this.subscriptions.forEach(subscription => { if (subscription instanceof Subscriber) { subscription.unsubscribe(); } }); } redirect() { if (this.redirectUrl && this.redirectUrl != "") { this.redirectUrl = decodeURIComponent(this.redirectUrl); var route = this.redirectUrl; var queryParams = ""; var paramsArray = []; var valuesArray = []; if (this.redirectUrl.indexOf('?') != -1) { route = this.redirectUrl.split('?')[0]; queryParams = this.redirectUrl.split('?')[1]; } if (queryParams != "") { var queryParamsArray = queryParams.split('&'); for (var i = 0; i < queryParamsArray.length; i++) { paramsArray.push(queryParamsArray[i].split("=")[0]); valuesArray.push(queryParamsArray[i].split("=")[1]); } this.router.navigate([route], {queryParams: this.routerHelper.createQueryParams(paramsArray, valuesArray)}); } else { this.router.navigate([route]); } } // else{ // this.router.navigate(['/']); // } } logIn() { if (this.redirectUrl && this.redirectUrl != "") { this.redirectUrl = decodeURIComponent(this.redirectUrl); var route = this.redirectUrl; var queryParams = ""; if (this.redirectUrl.indexOf('?') != -1) { var splits = this.redirectUrl.split('?'); if (splits.length > 0) { route = splits[0]; } if (splits.length > 1) { queryParams = splits[1]; } } Session.setReloadUrl(location.protocol + "//" + location.host,properties.baseLink + route, queryParams); } console.log(Session.getReloadUrl()); window.location.href = this.properties.loginUrl; } getTheRolesFormatted(roles: string[]) { let formattedRoles = []; for (let role of roles) { if(role.includes("urn:geant:openaire.eu:group:")) { let formattedRole = role.split("urn:geant:openaire.eu:group:")[1]; formattedRole = formattedRole.split("#aai.openaire.eu")[0] formattedRole = formattedRole.replace("+", " "); formattedRole = formattedRole.split("+").join(" "); formattedRoles.push(formattedRole); }else{ if(role.indexOf("_MANAGER")!=-1){ formattedRoles.push("Manager of " + role.split("_")[1]); }else if((["FUNDER","COMMUNITY","INSTITUTION","PROJECT"]).indexOf(role.split("_")[0])!=-1){ formattedRoles.push("Member of " + role.split("_")[1]); }else{ formattedRoles.splice(0,0,StringUtils.capitalize(role.split('_').join(' ').toLowerCase())); } } } return formattedRoles.join(", "); } isUserManager() { return Session.isUserManager(this.user); } }