Skip to content

Commit

Permalink
115279: Replaced EndUserAgreeMent guards with functional guards
Browse files Browse the repository at this point in the history
  • Loading branch information
LotteHofstede committed May 23, 2024
1 parent ffe3062 commit 553bada
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 147 deletions.
55 changes: 27 additions & 28 deletions src/app/app-routes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
InMemoryScrollingOptions,
mapToCanActivate,
Route,
RouterConfigOptions,
} from '@angular/router';
Expand Down Expand Up @@ -29,7 +28,7 @@ import { authenticatedGuard } from './core/auth/authenticated.guard';
import { groupAdministratorGuard } from './core/data/feature-authorization/feature-authorization-guard/group-administrator.guard';
import { siteAdministratorGuard } from './core/data/feature-authorization/feature-authorization-guard/site-administrator.guard';
import { siteRegisterGuard } from './core/data/feature-authorization/feature-authorization-guard/site-register.guard';
import { EndUserAgreementCurrentUserGuard } from './core/end-user-agreement/end-user-agreement-current-user.guard';
import { endUserAgreementCurrentUserGuard } from './core/end-user-agreement/end-user-agreement-current-user.guard';
import { reloadGuard } from './core/reload/reload.guard';
import { forgotPasswordCheckGuard } from './core/rest-property/forgot-password-check-guard.guard';
import { ServerCheckGuard } from './core/server-check/server-check.guard';
Expand Down Expand Up @@ -66,25 +65,25 @@ export const APP_ROUTES: Route[] = [
.then((m) => m.ROUTES),
data: { showBreadcrumbs: false },
providers: [provideSuggestionNotificationsState()],
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
canActivate: [endUserAgreementCurrentUserGuard],
},
{
path: 'community-list',
loadChildren: () => import('./community-list-page/community-list-page-routes')
.then((m) => m.ROUTES),
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
canActivate: [endUserAgreementCurrentUserGuard],
},
{
path: 'id',
loadChildren: () => import('./lookup-by-id/lookup-by-id-routes')
.then((m) => m.ROUTES),
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
canActivate: [endUserAgreementCurrentUserGuard],
},
{
path: 'handle',
loadChildren: () => import('./lookup-by-id/lookup-by-id-routes')
.then((m) => m.ROUTES),
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
canActivate: [endUserAgreementCurrentUserGuard],
},
{
path: REGISTER_PATH,
Expand All @@ -96,75 +95,75 @@ export const APP_ROUTES: Route[] = [
path: FORGOT_PASSWORD_PATH,
loadChildren: () => import('./forgot-password/forgot-password-routes')
.then((m) => m.ROUTES),
canActivate: [EndUserAgreementCurrentUserGuard, forgotPasswordCheckGuard],
canActivate: [endUserAgreementCurrentUserGuard, forgotPasswordCheckGuard],
},
{
path: COMMUNITY_MODULE_PATH,
loadChildren: () => import('./community-page/community-page-routes')
.then((m) => m.ROUTES),
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
canActivate: [endUserAgreementCurrentUserGuard],
},
{
path: COLLECTION_MODULE_PATH,
loadChildren: () => import('./collection-page/collection-page-routes')
.then((m) => m.ROUTES),
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
canActivate: [endUserAgreementCurrentUserGuard],
},
{
path: ITEM_MODULE_PATH,
loadChildren: () => import('./item-page/item-page-routes')
.then((m) => m.ROUTES),
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
canActivate: [endUserAgreementCurrentUserGuard],
},
{
path: 'entities/:entity-type',
loadChildren: () => import('./item-page/item-page-routes')
.then((m) => m.ROUTES),
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
canActivate: [endUserAgreementCurrentUserGuard],
},
{
path: LEGACY_BITSTREAM_MODULE_PATH,
loadChildren: () => import('./bitstream-page/bitstream-page-routes')
.then((m) => m.ROUTES),
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
canActivate: [endUserAgreementCurrentUserGuard],
},
{
path: BITSTREAM_MODULE_PATH,
loadChildren: () => import('./bitstream-page/bitstream-page-routes')
.then((m) => m.ROUTES),
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
canActivate: [endUserAgreementCurrentUserGuard],
},
{
path: 'mydspace',
loadChildren: () => import('./my-dspace-page/my-dspace-page-routes')
.then((m) => m.ROUTES),
providers: [provideSuggestionNotificationsState()],
canActivate: [authenticatedGuard, ...mapToCanActivate([EndUserAgreementCurrentUserGuard])],
canActivate: [authenticatedGuard, endUserAgreementCurrentUserGuard],
},
{
path: 'search',
loadChildren: () => import('./search-page/search-page-routes')
.then((m) => m.ROUTES),
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
canActivate: [endUserAgreementCurrentUserGuard],
},
{
path: 'browse',
loadChildren: () => import('./browse-by/browse-by-page-routes')
.then((m) => m.ROUTES),
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
canActivate: [endUserAgreementCurrentUserGuard],
},
{
path: ADMIN_MODULE_PATH,
loadChildren: () => import('./admin/admin-routes')
.then((m) => m.ROUTES),
canActivate: [siteAdministratorGuard, EndUserAgreementCurrentUserGuard],
canActivate: [siteAdministratorGuard, endUserAgreementCurrentUserGuard],
},
{
path: NOTIFICATIONS_MODULE_PATH,
loadChildren: () => import('./quality-assurance-notifications-pages/notifications-pages-routes')
.then((m) => m.ROUTES),
providers: [provideSuggestionNotificationsState()],
canActivate: [authenticatedGuard, ...mapToCanActivate([EndUserAgreementCurrentUserGuard])],
canActivate: [authenticatedGuard, endUserAgreementCurrentUserGuard],
},
{
path: 'login',
Expand All @@ -181,47 +180,47 @@ export const APP_ROUTES: Route[] = [
loadChildren: () => import('./submit-page/submit-page-routes')
.then((m) => m.ROUTES),
providers: [provideSubmissionState()],
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
canActivate: [endUserAgreementCurrentUserGuard],
},
{
path: 'import-external',
loadChildren: () => import('./import-external-page/import-external-page-routes')
.then((m) => m.ROUTES),
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
canActivate: [endUserAgreementCurrentUserGuard],
},
{
path: 'workspaceitems',
loadChildren: () => import('./workspaceitems-edit-page/workspaceitems-edit-page-routes')
.then((m) => m.ROUTES),
providers: [provideSubmissionState()],
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
canActivate: [endUserAgreementCurrentUserGuard],
},
{
path: WORKFLOW_ITEM_MODULE_PATH,
providers: [provideSubmissionState()],
loadChildren: () => import('./workflowitems-edit-page/workflowitems-edit-page-routes')
.then((m) => m.ROUTES),
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
canActivate: [endUserAgreementCurrentUserGuard],
},
{
path: PROFILE_MODULE_PATH,
loadChildren: () => import('./profile-page/profile-page-routes')
.then((m) => m.ROUTES),
providers: [provideSuggestionNotificationsState()],
canActivate: [authenticatedGuard, ...mapToCanActivate([EndUserAgreementCurrentUserGuard])],
canActivate: [authenticatedGuard, endUserAgreementCurrentUserGuard],
},
{
path: PROCESS_MODULE_PATH,
loadChildren: () => import('./process-page/process-page-routes')
.then((m) => m.ROUTES),
canActivate: [authenticatedGuard, ...mapToCanActivate([EndUserAgreementCurrentUserGuard])],
canActivate: [authenticatedGuard, endUserAgreementCurrentUserGuard],
},
{
path: SUGGESTION_MODULE_PATH,
loadChildren: () => import('./suggestions-page/suggestions-page-routes')
.then((m) => m.ROUTES),
providers: [provideSuggestionNotificationsState()],
canActivate: [authenticatedGuard, ...mapToCanActivate([EndUserAgreementCurrentUserGuard])],
canActivate: [authenticatedGuard, endUserAgreementCurrentUserGuard],
},
{
path: INFO_MODULE_PATH,
Expand All @@ -230,7 +229,7 @@ export const APP_ROUTES: Route[] = [
{
path: REQUEST_COPY_MODULE_PATH,
loadChildren: () => import('./request-copy/request-copy-routes').then((m) => m.ROUTES),
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
canActivate: [endUserAgreementCurrentUserGuard],
},
{
path: FORBIDDEN_PATH,
Expand All @@ -240,7 +239,7 @@ export const APP_ROUTES: Route[] = [
path: 'statistics',
loadChildren: () => import('./statistics-page/statistics-page-routes')
.then((m) => m.ROUTES),
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
canActivate: [endUserAgreementCurrentUserGuard],
},
{
path: HEALTH_PAGE_PATH,
Expand All @@ -250,7 +249,7 @@ export const APP_ROUTES: Route[] = [
{
path: ACCESS_CONTROL_MODULE_PATH,
loadChildren: () => import('./access-control/access-control-routes').then((m) => m.ROUTES),
canActivate: [groupAdministratorGuard, EndUserAgreementCurrentUserGuard],
canActivate: [groupAdministratorGuard, endUserAgreementCurrentUserGuard],
},
{
path: 'subscriptions',
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { TestBed } from '@angular/core/testing';
import {
Router,
UrlTree,
} from '@angular/router';
import { Observable } from 'rxjs';

import { EndUserAgreementService } from './end-user-agreement.service';
import { EndUserAgreementCookieGuard } from './end-user-agreement-cookie.guard';
import { endUserAgreementCookieGuard } from './end-user-agreement-cookie.guard';

describe('EndUserAgreementCookieGuard', () => {
let guard: EndUserAgreementCookieGuard;
describe('endUserAgreementCookieGuard', () => {

let endUserAgreementService: EndUserAgreementService;
let router: Router;
Expand All @@ -21,14 +22,22 @@ describe('EndUserAgreementCookieGuard', () => {
parseUrl: new UrlTree(),
createUrlTree: new UrlTree(),
});

guard = new EndUserAgreementCookieGuard(endUserAgreementService, router);
TestBed.configureTestingModule({
providers: [
{ provide: Router, useValue: router },
{ provide: EndUserAgreementService, useValue: endUserAgreementService },
],
});
});

describe('canActivate', () => {
describe('when the cookie has been accepted', () => {
it('should return true', (done) => {
guard.canActivate(undefined, { url: Object.assign({ url: 'redirect' }) } as any).subscribe((result) => {
const result$ = TestBed.runInInjectionContext(() => {
return endUserAgreementCookieGuard(undefined, { url: Object.assign({ url: 'redirect' }) } as any);
}) as Observable<boolean | UrlTree>;

result$.subscribe((result) => {
expect(result).toEqual(true);
done();
});
Expand All @@ -41,7 +50,11 @@ describe('EndUserAgreementCookieGuard', () => {
});

it('should return a UrlTree', (done) => {
guard.canActivate(undefined, Object.assign({ url: 'redirect' })).subscribe((result) => {
const result$ = TestBed.runInInjectionContext(() => {
return endUserAgreementCookieGuard(undefined, { url: Object.assign({ url: 'redirect' }) } as any);
}) as Observable<boolean | UrlTree>;

result$.subscribe((result) => {
expect(result).toEqual(jasmine.any(UrlTree));
done();
});
Expand Down
38 changes: 14 additions & 24 deletions src/app/core/end-user-agreement/end-user-agreement-cookie.guard.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import {
Observable,
of as observableOf,
} from 'rxjs';
import { inject } from '@angular/core';
import { CanActivateFn } from '@angular/router';
import { of as observableOf } from 'rxjs';

import { AbstractEndUserAgreementGuard } from './abstract-end-user-agreement.guard';
import { endUserAgreementGuard } from './end-user-agreement.guard';
import { EndUserAgreementService } from './end-user-agreement.service';


/**
* A guard redirecting users to the end agreement page when the user agreement cookie hasn't been accepted
* Guard for preventing unauthorized access to certain pages
* requiring the end user agreement to have been accepted in a cookie
*/
@Injectable({ providedIn: 'root' })
export class EndUserAgreementCookieGuard extends AbstractEndUserAgreementGuard {

constructor(protected endUserAgreementService: EndUserAgreementService,
protected router: Router) {
super(router);
}

/**
* True when the user agreement cookie has been accepted
*/
hasAccepted(): Observable<boolean> {
return observableOf(this.endUserAgreementService.isCookieAccepted());
}

}
export const endUserAgreementCookieGuard: CanActivateFn =
endUserAgreementGuard(
() => {
const endUserAgreementService = inject(EndUserAgreementService);
return observableOf(endUserAgreementService.isCookieAccepted());
},
);
Loading

0 comments on commit 553bada

Please sign in to comment.