[Library | Trunk]: Fix redirect from login logout. Add fragment

git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@60653 d315682c-612b-4755-9ff5-7f18f6832af3
This commit is contained in:
k.triantafyllou 2021-03-17 09:21:37 +00:00
parent 50dda276c6
commit 10090b685c
7 changed files with 79 additions and 111 deletions

View File

@ -1,6 +1,9 @@
<div> <div>
<div [id]="(mainComponent)?'tm-main':''" [class]="(mainComponent)?'uk-section uk-padding-remove-top tm-middle':''"> <div [id]="(mainComponent)?'tm-main':''" [class]="(mainComponent)?'uk-section tm-middle':''">
<div uk-grid uk-grid> <div *ngIf="loading" class="uk-flex uk-flex-center">
<loading></loading>
</div>
<div *ngIf="!loading" class="uk-grid" uk-grid>
<div class="tm-main uk-width-1-1@s uk-width-1-1@m uk-width-1-1@l uk-row-first "> <div class="tm-main uk-width-1-1@s uk-width-1-1@m uk-width-1-1@l uk-row-first ">
<div *ngIf="!server" class="uk-container uk-container-small uk-position-relative"> <div *ngIf="!server" class="uk-container uk-container-small uk-position-relative">

View File

@ -18,6 +18,7 @@ import {StringUtils} from "../utils/string-utils.class";
export class UserComponent { export class UserComponent {
public user: User; public user: User;
public loading: boolean = true;
public loggedIn: boolean = false; public loggedIn: boolean = false;
public server: boolean = true; public server: boolean = true;
public errorMessage: string = ""; public errorMessage: string = "";
@ -27,7 +28,7 @@ export class UserComponent {
public redirectUrl: string = ""; public redirectUrl: string = "";
public routerHelper: RouterHelper = new RouterHelper(); public routerHelper: RouterHelper = new RouterHelper();
public loginUrl; public loginUrl;
properties: EnvProperties; public properties: EnvProperties = properties;
@Input() mainComponent = true; @Input() mainComponent = true;
constructor(private router: Router, constructor(private router: Router,
@ -35,35 +36,32 @@ export class UserComponent {
private _meta: Meta, private _meta: Meta,
private _title: Title, private _title: Title,
private userManagementsService: UserManagementService) { private userManagementsService: UserManagementService) {
this._title.setTitle("OpenAIRE | Login");
var title = "OpenAIRE | Login";
this._title.setTitle(title);
} }
ngOnInit() { ngOnInit() {
this.properties = properties;
this.loginUrl = this.properties.loginUrl; this.loginUrl = this.properties.loginUrl;
if (typeof document !== 'undefined') { if (typeof document !== 'undefined') {
this.server = false; this.server = false;
this.subscriptions.push(this.userManagementsService.getUserInfo().subscribe(user => { this.subscriptions.push(this.userManagementsService.getUserInfo(false).subscribe(user => {
this.user = user; this.user = user;
this.loggedIn = !!this.user; this.loggedIn = !!this.user;
this.errorMessage = ""; this.errorMessage = "";
this.loading = true;
this.subscriptions.push(this.route.queryParams.subscribe(params => { this.subscriptions.push(this.route.queryParams.subscribe(params => {
this.errorCode = params["errorCode"]; this.errorCode = params["errorCode"];
this.redirectUrl = params["redirectUrl"]; this.redirectUrl = params["redirectUrl"];
this.errorMessage = ""; this.errorMessage = "";
if (this.loggedIn && this.errorCode == '1') { if (this.loggedIn && this.errorCode == '1') {
this.redirect(); this.redirect();
} else {
this.loading = false;
} }
})); }));
})); }));
} }
} }
ngOnDestroy() { ngOnDestroy() {
this.subscriptions.forEach(subscription => { this.subscriptions.forEach(subscription => {
if (subscription instanceof Subscriber) { if (subscription instanceof Subscriber) {
@ -75,49 +73,13 @@ export class UserComponent {
redirect() { redirect() {
if (this.redirectUrl && this.redirectUrl != "") { if (this.redirectUrl && this.redirectUrl != "") {
this.redirectUrl = decodeURIComponent(this.redirectUrl); this.redirectUrl = decodeURIComponent(this.redirectUrl);
var route = this.redirectUrl; this.userManagementsService.setRedirectUrl(this.redirectUrl);
var queryParams = ""; this.router.navigate(['/reload']);
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]);
}
console.debug('here');
this.router.navigate([route], {queryParams: this.routerHelper.createQueryParams(paramsArray, valuesArray)});
} else {
this.router.navigate([route]);
}
} }
// else{
// this.router.navigate(['/']);
// }
} }
logIn() { logIn() {
if (this.redirectUrl && this.redirectUrl != "") { this.userManagementsService.login();
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[]) { getTheRolesFormatted(roles: string[]) {

View File

@ -8,10 +8,11 @@ import {UserRoutingModule} from './user-routing.module';
import {UserComponent} from './user.component'; import {UserComponent} from './user.component';
import {PreviousRouteRecorder} from '../utils/piwik/previousRouteRecorder.guard'; import {PreviousRouteRecorder} from '../utils/piwik/previousRouteRecorder.guard';
import {LoadingModule} from "../utils/loading/loading.module";
@NgModule({ @NgModule({
imports: [ imports: [
CommonModule, FormsModule, UserRoutingModule, RouterModule CommonModule, FormsModule, UserRoutingModule, RouterModule, LoadingModule
], ],
providers: [ providers: [
PreviousRouteRecorder PreviousRouteRecorder

View File

@ -1,11 +1,10 @@
import {Component, Input, OnChanges, OnInit, SimpleChanges} from '@angular/core'; import {Component, Input, OnChanges, OnInit, SimpleChanges} from '@angular/core';
import {ActivatedRoute, NavigationStart, Router} from '@angular/router'; import {ActivatedRoute, Router} from '@angular/router';
import {User, Session} from './utils/helper.class'; import {Session, User} from './utils/helper.class';
import {RouterHelper} from '../utils/routerHelper.class'; import {RouterHelper} from '../utils/routerHelper.class';
import {StringUtils} from '../utils/string-utils.class';
import {properties} from "../../../environments/environment";
import {Subscriber} from "rxjs"; import {Subscriber} from "rxjs";
import {MenuItem} from "../sharedComponents/menu"; import {MenuItem} from "../sharedComponents/menu";
import {UserManagementService} from "../services/user-management.service";
// declare var logoutClicked; // declare var logoutClicked;
@Component({ @Component({
@ -161,7 +160,6 @@ import {MenuItem} from "../sharedComponents/menu";
<div id="logout2"></div> <div id="logout2"></div>
` `
}) })
export class UserMiniComponent implements OnInit, OnChanges { export class UserMiniComponent implements OnInit, OnChanges {
@Input() user: User; @Input() user: User;
public loggedIn: boolean = false; public loggedIn: boolean = false;
@ -176,31 +174,22 @@ export class UserMiniComponent implements OnInit, OnChanges {
@Input() logOutUrl; @Input() logOutUrl;
@Input() cookieDomain; @Input() cookieDomain;
@Input() fixRedirectUrl: string; @Input() fixRedirectUrl: string;
@Input() redirectUrl: string;
@Input() dark: boolean = false; @Input() dark: boolean = false;
search: string = '';
subscriptions = []; subscriptions = [];
constructor(private router: Router, private route: ActivatedRoute) { constructor(private router: Router, private route: ActivatedRoute, private userManagementService: UserManagementService) {
this.subscriptions.push(this.router.events.forEach(event => {
if (event instanceof NavigationStart) {
this.initialize(event.url);
}
}));
} }
ngOnInit() { ngOnInit() {
if (typeof document !== 'undefined') { if (typeof document !== 'undefined') {
this.server = false; this.server = false;
} }
this.subscriptions.push(this.route.queryParams.subscribe(params => { this.initUser();
this.initialize();
}));
} }
ngOnChanges(changes: SimpleChanges): void { ngOnChanges(changes: SimpleChanges): void {
if (changes.user) { if (changes.user) {
this.initialize(); this.initUser();
} }
} }
@ -212,42 +201,21 @@ export class UserMiniComponent implements OnInit, OnChanges {
}); });
} }
initialize(url: string = null) { initUser() {
if (url) {
let parts = StringUtils.split(url, ['?']);
this.redirectUrl = properties.baseLink + parts[0];
if (parts.length == 2) {
this.search = parts[1];
} else {
this.search = null;
}
} else if (typeof location !== 'undefined') {
this.redirectUrl = location.pathname;
this.search = location.search;
}
if (this.user) { if (this.user) {
this.loggedIn = true; this.loggedIn = true;
this.parseName(); this.parseName();
this.isAuthorized = Session.isClaimsCurator(this.user) || Session.isPortalAdministrator(this.user); this.isAuthorized = Session.isClaimsCurator(this.user) || Session.isPortalAdministrator(this.user);
} else { } else {
this.loggedIn = false; this.loggedIn = false;
this.isAuthorized = false; this.isAuthorized = false;
this.user = null; this.user = null;
} }
} }
logOut() { logOut() {
if (this.user) { if (this.user) {
Session.removeUser(); this.userManagementService.logout();
// New method
if (properties.logoutUrl.includes('openid_logout')) {
Session.setReloadUrl(location.protocol + "//" + location.host, this.redirectUrl, this.search);
window.location.href = this.logOutUrl;
} else {
window.location.href = this.logOutUrl + StringUtils.URIEncode(location.href);
}
} }
this.loggedIn = false; this.loggedIn = false;
this.isAuthorized = false; this.isAuthorized = false;
@ -255,9 +223,7 @@ export class UserMiniComponent implements OnInit, OnChanges {
} }
logIn() { logIn() {
Session.setReloadUrl(location.protocol + "//" + location.host, this.userManagementService.login();
this.fixRedirectUrl ? (properties.baseLink + this.fixRedirectUrl) : this.redirectUrl, this.search);
window.location.href = this.logInUrl;
} }
isTheActiveSubMenu(menuItem: MenuItem): boolean { isTheActiveSubMenu(menuItem: MenuItem): boolean {

View File

@ -24,11 +24,12 @@ export class Session {
return (cookie != null && cookie != ""); return (cookie != null && cookie != "");
} }
public static setReloadUrl(host: string, path: string, params: string) { public static setReloadUrl(host: string, path: string, params: string, fragment: string) {
var URL = {}; var URL = {};
URL["host"] = host; URL["host"] = host;
URL["path"] = path; URL["path"] = path;
URL["params"] = params; URL["params"] = params;
URL["fragment"] = fragment;
COOKIE.setCookie("reloadURL", JSON.stringify(URL), -1); COOKIE.setCookie("reloadURL", JSON.stringify(URL), -1);
} }

View File

@ -17,42 +17,36 @@ export class ReloadComponent {
public ngOnInit() { public ngOnInit() {
HelperFunctions.scroll(); HelperFunctions.scroll();
let URL = Session.getReloadUrl(); let URL = Session.getReloadUrl();
if (URL && URL["path"] && URL["path"] != null && URL["path"] != "") { if (URL && URL["path"] && URL["path"] != "") {
let url: string = URL["path"]; let url: string = URL["path"];
let host = URL["host"]; let host = URL["host"];
let paramsObject = ((URL["params"] && URL["params"] != null) ? Session.getParamsObj(URL["params"]) : null); let paramsObject = ((URL["params"] && URL["params"] != null) ? Session.getParamsObj(URL["params"]) : null);
let hash = url.indexOf("#");
if (host == (location.protocol + "//" + location.host)) { if (host == (location.protocol + "//" + location.host)) {
let fragment = (hash !== -1)?url.slice(hash + 1):null;
if(fragment) {
url = url.slice(0, hash);
}
let baseUrl = (document && document.getElementsByTagName('base')) ? document.getElementsByTagName('base')[0].href.split(document.location.host)[1] : "/"; let baseUrl = (document && document.getElementsByTagName('base')) ? document.getElementsByTagName('base')[0].href.split(document.location.host)[1] : "/";
url = (baseUrl.length > 1 && url.indexOf(baseUrl) != -1) ? ("/" + url.split(baseUrl)[1]) : url; url = (baseUrl.length > 1 && url.indexOf(baseUrl) != -1) ? ("/" + url.split(baseUrl)[1]) : url;
if (paramsObject) { if (paramsObject) {
Session.setReloadUrl("", "", "") Session.setReloadUrl("", "", "", "");
if(fragment) { if(URL['fragment'] && URL['fragment'] !== '') {
this._router.navigate([url], {queryParams: paramsObject, fragment: fragment}); this._router.navigate([url], {queryParams: paramsObject, fragment: URL['fragment']});
} else { } else {
this._router.navigate([url], {queryParams: paramsObject}); this._router.navigate([url], {queryParams: paramsObject});
} }
} else { } else {
Session.setReloadUrl("", "", "") Session.setReloadUrl("", "", "", "");
if(fragment) { if(URL['fragment'] && URL['fragment'] !== '') {
this._router.navigate([url], {fragment: fragment}); this._router.navigate([url], {fragment: URL['fragment']});
} else { } else {
this._router.navigate([url]); this._router.navigate([url]);
} }
} }
} else { } else {
Session.setReloadUrl("", "", "") Session.setReloadUrl("", "", "", "");
window.location.href = host + url + ((URL["params"] && URL["params"] != null) ? ((URL["params"].indexOf("?") == -1 ? "?" : "") + URL["params"]) : ""); window.location.href = host + url + ((URL["params"] && URL["params"] != null) ? ((URL["params"].indexOf("?") == -1 ? "?" : "") + URL["params"]) : "");
} }
} else { } else {
Session.setReloadUrl("", "", "") Session.setReloadUrl("", "", "", "");
this._router.navigate(['/']); this._router.navigate(['/']);
} }
} }

View File

@ -1,10 +1,12 @@
import {Injectable} from '@angular/core'; import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http'; import {HttpClient} from '@angular/common/http';
import {BehaviorSubject, from, Observable, of} from "rxjs"; import {BehaviorSubject, from, Observable, of} from "rxjs";
import {COOKIE, User} from "../login/utils/helper.class"; import {COOKIE, Session, User} from "../login/utils/helper.class";
import {map} from "rxjs/operators"; import {map} from "rxjs/operators";
import {NavigationEnd, Router} from "@angular/router"; import {NavigationEnd, Router} from "@angular/router";
import {properties} from "../../../environments/environment"; import {properties} from "../../../environments/environment";
import {el} from "@angular/platform-browser/testing/src/browser_util";
import {StringUtils} from "../utils/string-utils.class";
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@ -15,6 +17,7 @@ export class UserManagementService{
private readonly promise: Promise<User>; private readonly promise: Promise<User>;
sub; sub;
routeSub; routeSub;
constructor(private http: HttpClient, constructor(private http: HttpClient,
private router: Router) { private router: Router) {
this.promise = new Promise<any>((resolve => { this.promise = new Promise<any>((resolve => {
@ -110,4 +113,42 @@ export class UserManagementService{
user.expirationDate = info.exp_date; user.expirationDate = info.exp_date;
return user; return user;
} }
public setRedirectUrl(url: string = null) {
if(url) {
let parts = url.split('?');
let path = properties.baseLink + parts[0];
let params = null;
if (parts.length == 2) {
params = parts[1];
}
let hash = path.indexOf("#");
let fragment = (hash !== -1)?path.slice(hash + 1):null;
if(fragment) {
path = path.slice(0, hash);
} else {
fragment = "";
}
if(!path.includes('/reload')) {
Session.setReloadUrl(location.protocol + "//" + location.host, path, params, fragment);
}
} else {
Session.setReloadUrl(location.protocol + "//" + location.host, location.pathname, location.search, location.hash);
}
}
public login() {
this.setRedirectUrl();
window.location.href = properties.loginUrl;
}
public logout() {
this.setRedirectUrl();
Session.removeUser();
if (properties.logoutUrl.includes('openid_logout')) {
window.location.href = properties.logoutUrl;
} else {
window.location.href = properties.logoutUrl + StringUtils.URIEncode(location.href);
}
}
} }