72541: End-User-Agreement redirect via store

This commit is contained in:
Kristof De Langhe
2020-08-28 13:05:02 +02:00
parent 76b9fd4702
commit cb3ef1dde4
4 changed files with 30 additions and 17 deletions

View File

@@ -2,23 +2,26 @@ import { EndUserAgreementGuard } from './end-user-agreement.guard';
import { EndUserAgreementService } from './end-user-agreement.service';
import { Router, UrlTree } from '@angular/router';
import { of as observableOf } from 'rxjs';
import { AuthService } from '../auth/auth.service';
describe('EndUserAgreementGuard', () => {
let guard: EndUserAgreementGuard;
let endUserAgreementService: EndUserAgreementService;
let authService: AuthService;
let router: Router;
beforeEach(() => {
endUserAgreementService = jasmine.createSpyObj('endUserAgreementService', {
hasCurrentUserAcceptedAgreement: observableOf(true)
});
authService = jasmine.createSpyObj('authService', ['setRedirectUrl']);
router = jasmine.createSpyObj('router', {
navigateByUrl: {},
parseUrl: new UrlTree()
});
guard = new EndUserAgreementGuard(endUserAgreementService, router);
guard = new EndUserAgreementGuard(endUserAgreementService, authService, router);
});
describe('canActivate', () => {
@@ -39,7 +42,8 @@ describe('EndUserAgreementGuard', () => {
it('should navigate the user with a redirect url', (done) => {
const redirect = 'redirect/url';
guard.canActivate(undefined, Object.assign({ url: redirect })).subscribe(() => {
expect(router.navigateByUrl).toHaveBeenCalledWith(jasmine.anything(), { state: { redirect } });
expect(authService.setRedirectUrl).toHaveBeenCalledWith(redirect);
expect(router.navigateByUrl).toHaveBeenCalled();
done();
});
});

View File

@@ -4,6 +4,7 @@ import { Observable } from 'rxjs/internal/Observable';
import { returnEndUserAgreementUrlTreeOnFalse } from '../shared/operators';
import { EndUserAgreementService } from './end-user-agreement.service';
import { tap } from 'rxjs/operators';
import { AuthService } from '../auth/auth.service';
/**
* A guard redirecting users to the end agreement page when they haven't accepted the latest user agreement
@@ -12,6 +13,7 @@ import { tap } from 'rxjs/operators';
export class EndUserAgreementGuard implements CanActivate {
constructor(protected endUserAgreementService: EndUserAgreementService,
protected authService: AuthService,
protected router: Router) {
}
@@ -26,7 +28,8 @@ export class EndUserAgreementGuard implements CanActivate {
returnEndUserAgreementUrlTreeOnFalse(this.router),
tap((result) => {
if (result instanceof UrlTree) {
this.router.navigateByUrl(result, { state: { redirect: state.url } })
this.authService.setRedirectUrl(state.url);
this.router.navigateByUrl(result);
}
})
);

View File

@@ -24,19 +24,19 @@ describe('EndUserAgreementComponent', () => {
let redirectUrl;
function init() {
redirectUrl = 'redirect/url';
endUserAgreementService = jasmine.createSpyObj('endUserAgreementService', {
hasCurrentUserAcceptedAgreement: observableOf(false),
setUserAcceptedAgreement: observableOf(true)
});
notificationsService = jasmine.createSpyObj('notificationsService', ['success', 'error']);
authService = jasmine.createSpyObj('authService', {
isAuthenticated: observableOf(true)
isAuthenticated: observableOf(true),
getRedirectUrl: observableOf(redirectUrl)
});
store = jasmine.createSpyObj('store', ['dispatch']);
router = jasmine.createSpyObj('router', ['navigate', 'navigateByUrl']);
redirectUrl = 'redirect/url';
window.history.pushState({ redirect: redirectUrl }, '');
}
beforeEach(async(() => {

View File

@@ -1,6 +1,6 @@
import { Component, OnInit } from '@angular/core';
import { AuthService } from '../../core/auth/auth.service';
import { take } from 'rxjs/operators';
import { switchMap, take } from 'rxjs/operators';
import { Router } from '@angular/router';
import { Store } from '@ngrx/store';
import { AppState } from '../../app.reducer';
@@ -8,7 +8,8 @@ import { LogOutAction } from '../../core/auth/auth.actions';
import { EndUserAgreementService } from '../../core/end-user-agreement/end-user-agreement.service';
import { NotificationsService } from '../../shared/notifications/notifications.service';
import { TranslateService } from '@ngx-translate/core';
import { hasValue } from '../../shared/empty.util';
import { of as observableOf } from 'rxjs';
import { isNotEmpty } from '../../shared/empty.util';
@Component({
selector: 'ds-end-user-agreement',
@@ -54,15 +55,20 @@ export class EndUserAgreementComponent implements OnInit {
* Set the End User Agreement, display a notification and (optionally) redirect the user back to their original destination
*/
submit() {
this.endUserAgreementService.setUserAcceptedAgreement(this.accepted).subscribe((success) => {
if (success) {
this.notificationsService.success(this.translate.instant('info.end-user-agreement.accept.success'));
const redirect = window.history.state.redirect;
if (hasValue(redirect)) {
this.router.navigateByUrl(redirect);
this.endUserAgreementService.setUserAcceptedAgreement(this.accepted).pipe(
switchMap((success) => {
if (success) {
this.notificationsService.success(this.translate.instant('info.end-user-agreement.accept.success'));
return this.authService.getRedirectUrl();
} else {
this.notificationsService.error(this.translate.instant('info.end-user-agreement.accept.error'));
return observableOf(undefined);
}
} else {
this.notificationsService.error(this.translate.instant('info.end-user-agreement.accept.error'));
}),
take(1)
).subscribe((redirectUrl) => {
if (isNotEmpty(redirectUrl)) {
this.router.navigateByUrl(redirectUrl);
}
});
}