From c17f6e5597a1bfe7698d4e7192ebaaf0d37d5b12 Mon Sep 17 00:00:00 2001 From: "k.triantafyllou" Date: Wed, 12 Aug 2020 15:16:09 +0000 Subject: [PATCH] [Library | Trunk]: Make PreviousRouteRecorder singleton. Add verification guard and component git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@59261 d315682c-612b-4755-9ff5-7f18f6832af3 --- login/user.component.html | 5 +-- login/verification.guard.ts | 44 ++++++++++++++++++++++ services/user-registry.service.ts | 5 +++ utils/piwik/previousRouteRecorder.guard.ts | 4 +- verification/verification.component.ts | 9 +++++ verification/verification.module.ts | 10 +++++ 6 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 login/verification.guard.ts create mode 100644 verification/verification.component.ts create mode 100644 verification/verification.module.ts diff --git a/login/user.component.html b/login/user.component.html index 5f6aa719..5f54a8c1 100644 --- a/login/user.component.html +++ b/login/user.component.html @@ -53,16 +53,13 @@ Please sign in to continue. -
+
You are not authorized to use the requested page.
The session has expired. Please sign in again or continue browsing as a guest.
-
- You are not authorized to use the requested page. -
There is no research community selected.
diff --git a/login/verification.guard.ts b/login/verification.guard.ts new file mode 100644 index 00000000..8fc3399f --- /dev/null +++ b/login/verification.guard.ts @@ -0,0 +1,44 @@ +import {Injectable} from '@angular/core'; +import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router'; +import {Observable} from 'rxjs/Observable'; +import {Session} from './utils/helper.class'; +import {LoginErrorCodes} from './utils/guardHelper.class'; +import {filter, map, mergeMap} from "rxjs/operators"; +import {UserManagementService} from "../services/user-management.service"; +import {UserRegistryService} from "../services/user-registry.service"; +import {of} from "rxjs"; + +@Injectable() +export class VerificationGuard implements CanActivate { + + constructor(private router: Router, + private userRegistryService: UserRegistryService, + private userManagementService: UserManagementService) { + } + + check(path: string): Observable | boolean { + if (Session.isLoggedIn()) { + const obs = this.userManagementService.getUserInfo(false).pipe(map(user => { + if(user) { + of(true); + } else { + return of(false); + } + }), mergeMap(authorized => { + return authorized; + })); + obs.pipe(filter(isLoggedIn => !isLoggedIn)).subscribe(() => { + this.router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_LOGIN, path}}); + }); + return obs; + } else { + this.router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_LOGIN, path}}); + return false; + } + } + + canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | boolean { + console.log(route); + return this.check(state.url); + } +} diff --git a/services/user-registry.service.ts b/services/user-registry.service.ts index e6468428..9785e02a 100644 --- a/services/user-registry.service.ts +++ b/services/user-registry.service.ts @@ -39,6 +39,11 @@ export class UserRegistryService { return this.http.get(properties.registryUrl + encodeURIComponent(type) + '/' + encodeURIComponent(id) + '/subscribers/email').pipe(map(response => response.response)); } + public getInvitation(id: string): Observable { + return this.http.get(properties.registryUrl + "verification/" + encodeURIComponent(id)) + .pipe(map(response => response.response), CustomOptions.registryOptions()); + } + public getManagersEmail(type: string, id: string): Observable { return this.http.get(properties.registryUrl + encodeURIComponent(type) + '/' + encodeURIComponent(id) + '/managers/email').pipe(map(response => response.response)); } diff --git a/utils/piwik/previousRouteRecorder.guard.ts b/utils/piwik/previousRouteRecorder.guard.ts index d10b7407..4f9e762e 100644 --- a/utils/piwik/previousRouteRecorder.guard.ts +++ b/utils/piwik/previousRouteRecorder.guard.ts @@ -3,7 +3,9 @@ import { Injectable } from '@angular/core'; import { CanDeactivate, Router} from '@angular/router'; import {Observable} from 'rxjs'; -@Injectable() // do not forget to register this class as a provider +@Injectable({ + providedIn: 'root' +}) // do not forget to register this class as a provider export class PreviousRouteRecorder implements CanDeactivate { constructor(private router: Router) { } diff --git a/verification/verification.component.ts b/verification/verification.component.ts new file mode 100644 index 00000000..5f6e5a7f --- /dev/null +++ b/verification/verification.component.ts @@ -0,0 +1,9 @@ +import {Component} from "@angular/core"; + +@Component({ + selector: 'verification', + template: `` +}) +export class VerificationComponent { + +} diff --git a/verification/verification.module.ts b/verification/verification.module.ts new file mode 100644 index 00000000..cb9fb5a2 --- /dev/null +++ b/verification/verification.module.ts @@ -0,0 +1,10 @@ +import {NgModule} from "@angular/core"; +import {CommonModule} from "@angular/common"; +import {VerificationComponent} from "./verification.component"; + +@NgModule({ + imports: [CommonModule], + declarations: [VerificationComponent], + exports: [VerificationComponent] +}) +export class VerificationModule {}