update hasConsent Guard logic, remove loginGuard from app routing module

This commit is contained in:
argirok 2023-12-20 18:18:32 +02:00
parent c6234de801
commit d03d92f027
4 changed files with 45 additions and 30 deletions

View File

@ -4,7 +4,6 @@ import {Irish} from "./shared/irish";
import {ErrorPageComponent} from "./openaireLibrary/error/errorPage.component";
import {AdminLoginGuard} from "./openaireLibrary/login/adminLoginGuard.guard";
import {HasConsentGuard} from "./shared/hasConsent.guard";
import {LoginGuard} from "./openaireLibrary/login/loginGuard.guard";
const routes: Routes = [
{
@ -40,7 +39,7 @@ const routes: Routes = [
{
path: 'upload-dois',
loadChildren: () => import('./upload-dois/upload-dois.module').then(m => m.UploadDoisModule),
data: {title: Irish.METADATA_PREFIX}, canActivate: [LoginGuard, HasConsentGuard]
data: {title: Irish.METADATA_PREFIX}, canActivate: [HasConsentGuard]
},
{
path: 'admin',
@ -50,13 +49,13 @@ const routes: Routes = [
},
{path: 'public-logs', loadChildren: () => import('./public-logs/public-logs.module').then(m => m.PublicLogsModule)},
{path: 'user-policy', loadChildren: () => import('./user-policy/user-policy.module').then(m => m.UserPolicyModule)},
{path: 'participate', loadChildren: () => import('./claims/claims.module').then(m => m.ClaimsModule), canActivate: [LoginGuard, HasConsentGuard]},
{path: 'participate', loadChildren: () => import('./claims/claims.module').then(m => m.ClaimsModule), canActivate: [HasConsentGuard]},
{
path: '',
loadChildren: () => import('./national/national.module').then(m => m.NationalModule)
},
{path: 'orcid', loadChildren: () => import('./orcid/orcid.module').then(m => m.LibOrcidModule), canActivate: [LoginGuard, HasConsentGuard]},
{path: 'my-orcid-links', loadChildren: () => import('./orcid/my-orcid-links/myOrcidLinks.module').then(m => m.LibMyOrcidLinksModule), canActivate: [LoginGuard, HasConsentGuard]},
{path: 'orcid', loadChildren: () => import('./orcid/orcid.module').then(m => m.LibOrcidModule), canActivate: [HasConsentGuard]},
{path: 'my-orcid-links', loadChildren: () => import('./orcid/my-orcid-links/myOrcidLinks.module').then(m => m.LibMyOrcidLinksModule), canActivate: [HasConsentGuard]},
{path: 'error', component: ErrorPageComponent},
{path: '**', pathMatch: 'full', component: ErrorPageComponent}
];

View File

@ -5,8 +5,6 @@ import {SEOService} from '../openaireLibrary/sharedComponents/SEO/SEO.service';
import {PiwikService} from '../openaireLibrary/utils/piwik/piwik.service';
import {Meta, Title} from '@angular/platform-browser';
import {LogService} from "../openaireLibrary/utils/log/log.service";
import {UserManagementService} from "../openaireLibrary/services/user-management.service";
import {CookieLawService} from "../openaireLibrary/sharedComponents/cookie-law/cookie-law.service";
@Component({
selector: 'public-logs',
@ -90,8 +88,7 @@ export class PublicLogsComponent extends BaseComponent implements OnInit {
protected _piwikService: PiwikService,
protected _title: Title,
protected _meta: Meta,
private _logService:LogService,
private userManagementsService: UserManagementService, private cookieLawService: CookieLawService) {
private _logService:LogService) {
super();
this.startDate.setFullYear(2023,11,1)
this.months = Array.from({ length:12 }, (_, index) => 12 - index);

View File

@ -1,32 +1,48 @@
import {ActivatedRouteSnapshot, Route, Router, RouterStateSnapshot, UrlTree} from '@angular/router';
import {Observable} from 'rxjs';
import {ActivatedRouteSnapshot, Router, RouterStateSnapshot, UrlTree} from '@angular/router';
import {Observable, merge, forkJoin} from 'rxjs';
import {UserProfileService} from "../openaireLibrary/services/userProfile.service";
import {map, tap} from "rxjs/operators";
import {filter, map, take} from "rxjs/operators";
import {Injectable} from "@angular/core";
import {UserManagementService} from "../openaireLibrary/services/user-management.service";
import {LoginErrorCodes} from "../openaireLibrary/login/utils/guardHelper.class";
@Injectable({
providedIn: 'root'
})
export class HasConsentGuard {
constructor(private router: Router,
private _userProfileService: UserProfileService) {
private _userProfileService: UserProfileService,
private userManagementService: UserManagementService) {
}
check(path: string): Observable<boolean> | boolean {
return this._userProfileService.getUserProfile().pipe(map(userProfile => {
return userProfile.consent;
}),tap(hasConsent => {
if(!hasConsent) {
return forkJoin([
this.userManagementService.getUserInfo().pipe(take(1)),
this._userProfileService.getUserProfile().pipe(take(1))
]).pipe(
map(([user, userProfile]) => {
if (user) {
if(userProfile && userProfile.consent){
return true;
}else{
this.router.navigate(['/user-policy'], {
queryParams: {
'redirectUrl': path
}
});
return false;
}
}));
}else{
this.router.navigate(['/user-info'], {
queryParams: {
'errorCode': LoginErrorCodes.NOT_LOGIN,
'redirectUrl': path
}
})
return false;
}
})
);
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
@ -36,8 +52,8 @@ export class HasConsentGuard {
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return this.check(state.url);
}
/*
canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean {
return this.check('/' + route.path);
}
}*/
}

View File

@ -11,7 +11,7 @@ import {UserProfileService} from "../openaireLibrary/services/userProfile.servic
@Component({
selector: 'user-policy',
template: `
<div class="uk-container uk-container-large uk-margin-top">
<div *ngIf="!loading" class="uk-container uk-container-large uk-margin-top">
<h3>User Policy</h3>
<div class="">
<div>
@ -22,7 +22,6 @@ import {UserProfileService} from "../openaireLibrary/services/userProfile.servic
<input type="checkbox" (change)="value = !value"> Accept policy</div>
<button class="uk-button uk-button-primary uk-margin-top" [class.uk-disabled]="!value" (click)="accept()"> Accept and proceed</button>
</div>
</div>
@ -34,6 +33,7 @@ export class UserPolicyComponent extends BaseComponent implements OnInit {
hasConsent = false;
value = false;
redirectEnabled =true;
loading = true;
constructor(protected _router: Router,
protected _route: ActivatedRoute,
protected seoService: SEOService,
@ -46,6 +46,7 @@ export class UserPolicyComponent extends BaseComponent implements OnInit {
}
ngOnInit() {
this.loading = true;
this.title = 'User policy';
this.description = 'OA Monitor Ireland - User policy';
this.setMetadata();
@ -58,8 +59,10 @@ export class UserPolicyComponent extends BaseComponent implements OnInit {
if(this.hasConsent && this.redirectEnabled){
this.redirect();
}
this.loading = false;
}, error => {
this.hasConsent = false;
this.loading = false;
}));
}
accept(){