Skip to content

Commit 553bada

Browse files
committed
115279: Replaced EndUserAgreeMent guards with functional guards
1 parent ffe3062 commit 553bada

8 files changed

Lines changed: 152 additions & 147 deletions

src/app/app-routes.ts

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
InMemoryScrollingOptions,
3-
mapToCanActivate,
43
Route,
54
RouterConfigOptions,
65
} from '@angular/router';
@@ -29,7 +28,7 @@ import { authenticatedGuard } from './core/auth/authenticated.guard';
2928
import { groupAdministratorGuard } from './core/data/feature-authorization/feature-authorization-guard/group-administrator.guard';
3029
import { siteAdministratorGuard } from './core/data/feature-authorization/feature-authorization-guard/site-administrator.guard';
3130
import { siteRegisterGuard } from './core/data/feature-authorization/feature-authorization-guard/site-register.guard';
32-
import { EndUserAgreementCurrentUserGuard } from './core/end-user-agreement/end-user-agreement-current-user.guard';
31+
import { endUserAgreementCurrentUserGuard } from './core/end-user-agreement/end-user-agreement-current-user.guard';
3332
import { reloadGuard } from './core/reload/reload.guard';
3433
import { forgotPasswordCheckGuard } from './core/rest-property/forgot-password-check-guard.guard';
3534
import { ServerCheckGuard } from './core/server-check/server-check.guard';
@@ -66,25 +65,25 @@ export const APP_ROUTES: Route[] = [
6665
.then((m) => m.ROUTES),
6766
data: { showBreadcrumbs: false },
6867
providers: [provideSuggestionNotificationsState()],
69-
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
68+
canActivate: [endUserAgreementCurrentUserGuard],
7069
},
7170
{
7271
path: 'community-list',
7372
loadChildren: () => import('./community-list-page/community-list-page-routes')
7473
.then((m) => m.ROUTES),
75-
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
74+
canActivate: [endUserAgreementCurrentUserGuard],
7675
},
7776
{
7877
path: 'id',
7978
loadChildren: () => import('./lookup-by-id/lookup-by-id-routes')
8079
.then((m) => m.ROUTES),
81-
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
80+
canActivate: [endUserAgreementCurrentUserGuard],
8281
},
8382
{
8483
path: 'handle',
8584
loadChildren: () => import('./lookup-by-id/lookup-by-id-routes')
8685
.then((m) => m.ROUTES),
87-
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
86+
canActivate: [endUserAgreementCurrentUserGuard],
8887
},
8988
{
9089
path: REGISTER_PATH,
@@ -96,75 +95,75 @@ export const APP_ROUTES: Route[] = [
9695
path: FORGOT_PASSWORD_PATH,
9796
loadChildren: () => import('./forgot-password/forgot-password-routes')
9897
.then((m) => m.ROUTES),
99-
canActivate: [EndUserAgreementCurrentUserGuard, forgotPasswordCheckGuard],
98+
canActivate: [endUserAgreementCurrentUserGuard, forgotPasswordCheckGuard],
10099
},
101100
{
102101
path: COMMUNITY_MODULE_PATH,
103102
loadChildren: () => import('./community-page/community-page-routes')
104103
.then((m) => m.ROUTES),
105-
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
104+
canActivate: [endUserAgreementCurrentUserGuard],
106105
},
107106
{
108107
path: COLLECTION_MODULE_PATH,
109108
loadChildren: () => import('./collection-page/collection-page-routes')
110109
.then((m) => m.ROUTES),
111-
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
110+
canActivate: [endUserAgreementCurrentUserGuard],
112111
},
113112
{
114113
path: ITEM_MODULE_PATH,
115114
loadChildren: () => import('./item-page/item-page-routes')
116115
.then((m) => m.ROUTES),
117-
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
116+
canActivate: [endUserAgreementCurrentUserGuard],
118117
},
119118
{
120119
path: 'entities/:entity-type',
121120
loadChildren: () => import('./item-page/item-page-routes')
122121
.then((m) => m.ROUTES),
123-
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
122+
canActivate: [endUserAgreementCurrentUserGuard],
124123
},
125124
{
126125
path: LEGACY_BITSTREAM_MODULE_PATH,
127126
loadChildren: () => import('./bitstream-page/bitstream-page-routes')
128127
.then((m) => m.ROUTES),
129-
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
128+
canActivate: [endUserAgreementCurrentUserGuard],
130129
},
131130
{
132131
path: BITSTREAM_MODULE_PATH,
133132
loadChildren: () => import('./bitstream-page/bitstream-page-routes')
134133
.then((m) => m.ROUTES),
135-
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
134+
canActivate: [endUserAgreementCurrentUserGuard],
136135
},
137136
{
138137
path: 'mydspace',
139138
loadChildren: () => import('./my-dspace-page/my-dspace-page-routes')
140139
.then((m) => m.ROUTES),
141140
providers: [provideSuggestionNotificationsState()],
142-
canActivate: [authenticatedGuard, ...mapToCanActivate([EndUserAgreementCurrentUserGuard])],
141+
canActivate: [authenticatedGuard, endUserAgreementCurrentUserGuard],
143142
},
144143
{
145144
path: 'search',
146145
loadChildren: () => import('./search-page/search-page-routes')
147146
.then((m) => m.ROUTES),
148-
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
147+
canActivate: [endUserAgreementCurrentUserGuard],
149148
},
150149
{
151150
path: 'browse',
152151
loadChildren: () => import('./browse-by/browse-by-page-routes')
153152
.then((m) => m.ROUTES),
154-
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
153+
canActivate: [endUserAgreementCurrentUserGuard],
155154
},
156155
{
157156
path: ADMIN_MODULE_PATH,
158157
loadChildren: () => import('./admin/admin-routes')
159158
.then((m) => m.ROUTES),
160-
canActivate: [siteAdministratorGuard, EndUserAgreementCurrentUserGuard],
159+
canActivate: [siteAdministratorGuard, endUserAgreementCurrentUserGuard],
161160
},
162161
{
163162
path: NOTIFICATIONS_MODULE_PATH,
164163
loadChildren: () => import('./quality-assurance-notifications-pages/notifications-pages-routes')
165164
.then((m) => m.ROUTES),
166165
providers: [provideSuggestionNotificationsState()],
167-
canActivate: [authenticatedGuard, ...mapToCanActivate([EndUserAgreementCurrentUserGuard])],
166+
canActivate: [authenticatedGuard, endUserAgreementCurrentUserGuard],
168167
},
169168
{
170169
path: 'login',
@@ -181,47 +180,47 @@ export const APP_ROUTES: Route[] = [
181180
loadChildren: () => import('./submit-page/submit-page-routes')
182181
.then((m) => m.ROUTES),
183182
providers: [provideSubmissionState()],
184-
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
183+
canActivate: [endUserAgreementCurrentUserGuard],
185184
},
186185
{
187186
path: 'import-external',
188187
loadChildren: () => import('./import-external-page/import-external-page-routes')
189188
.then((m) => m.ROUTES),
190-
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
189+
canActivate: [endUserAgreementCurrentUserGuard],
191190
},
192191
{
193192
path: 'workspaceitems',
194193
loadChildren: () => import('./workspaceitems-edit-page/workspaceitems-edit-page-routes')
195194
.then((m) => m.ROUTES),
196195
providers: [provideSubmissionState()],
197-
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
196+
canActivate: [endUserAgreementCurrentUserGuard],
198197
},
199198
{
200199
path: WORKFLOW_ITEM_MODULE_PATH,
201200
providers: [provideSubmissionState()],
202201
loadChildren: () => import('./workflowitems-edit-page/workflowitems-edit-page-routes')
203202
.then((m) => m.ROUTES),
204-
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
203+
canActivate: [endUserAgreementCurrentUserGuard],
205204
},
206205
{
207206
path: PROFILE_MODULE_PATH,
208207
loadChildren: () => import('./profile-page/profile-page-routes')
209208
.then((m) => m.ROUTES),
210209
providers: [provideSuggestionNotificationsState()],
211-
canActivate: [authenticatedGuard, ...mapToCanActivate([EndUserAgreementCurrentUserGuard])],
210+
canActivate: [authenticatedGuard, endUserAgreementCurrentUserGuard],
212211
},
213212
{
214213
path: PROCESS_MODULE_PATH,
215214
loadChildren: () => import('./process-page/process-page-routes')
216215
.then((m) => m.ROUTES),
217-
canActivate: [authenticatedGuard, ...mapToCanActivate([EndUserAgreementCurrentUserGuard])],
216+
canActivate: [authenticatedGuard, endUserAgreementCurrentUserGuard],
218217
},
219218
{
220219
path: SUGGESTION_MODULE_PATH,
221220
loadChildren: () => import('./suggestions-page/suggestions-page-routes')
222221
.then((m) => m.ROUTES),
223222
providers: [provideSuggestionNotificationsState()],
224-
canActivate: [authenticatedGuard, ...mapToCanActivate([EndUserAgreementCurrentUserGuard])],
223+
canActivate: [authenticatedGuard, endUserAgreementCurrentUserGuard],
225224
},
226225
{
227226
path: INFO_MODULE_PATH,
@@ -230,7 +229,7 @@ export const APP_ROUTES: Route[] = [
230229
{
231230
path: REQUEST_COPY_MODULE_PATH,
232231
loadChildren: () => import('./request-copy/request-copy-routes').then((m) => m.ROUTES),
233-
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
232+
canActivate: [endUserAgreementCurrentUserGuard],
234233
},
235234
{
236235
path: FORBIDDEN_PATH,
@@ -240,7 +239,7 @@ export const APP_ROUTES: Route[] = [
240239
path: 'statistics',
241240
loadChildren: () => import('./statistics-page/statistics-page-routes')
242241
.then((m) => m.ROUTES),
243-
canActivate: mapToCanActivate([EndUserAgreementCurrentUserGuard]),
242+
canActivate: [endUserAgreementCurrentUserGuard],
244243
},
245244
{
246245
path: HEALTH_PAGE_PATH,
@@ -250,7 +249,7 @@ export const APP_ROUTES: Route[] = [
250249
{
251250
path: ACCESS_CONTROL_MODULE_PATH,
252251
loadChildren: () => import('./access-control/access-control-routes').then((m) => m.ROUTES),
253-
canActivate: [groupAdministratorGuard, EndUserAgreementCurrentUserGuard],
252+
canActivate: [groupAdministratorGuard, endUserAgreementCurrentUserGuard],
254253
},
255254
{
256255
path: 'subscriptions',

src/app/core/end-user-agreement/abstract-end-user-agreement.guard.ts

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/app/core/end-user-agreement/end-user-agreement-cookie.guard.spec.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
import { TestBed } from '@angular/core/testing';
12
import {
23
Router,
34
UrlTree,
45
} from '@angular/router';
6+
import { Observable } from 'rxjs';
57

68
import { EndUserAgreementService } from './end-user-agreement.service';
7-
import { EndUserAgreementCookieGuard } from './end-user-agreement-cookie.guard';
9+
import { endUserAgreementCookieGuard } from './end-user-agreement-cookie.guard';
810

9-
describe('EndUserAgreementCookieGuard', () => {
10-
let guard: EndUserAgreementCookieGuard;
11+
describe('endUserAgreementCookieGuard', () => {
1112

1213
let endUserAgreementService: EndUserAgreementService;
1314
let router: Router;
@@ -21,14 +22,22 @@ describe('EndUserAgreementCookieGuard', () => {
2122
parseUrl: new UrlTree(),
2223
createUrlTree: new UrlTree(),
2324
});
24-
25-
guard = new EndUserAgreementCookieGuard(endUserAgreementService, router);
25+
TestBed.configureTestingModule({
26+
providers: [
27+
{ provide: Router, useValue: router },
28+
{ provide: EndUserAgreementService, useValue: endUserAgreementService },
29+
],
30+
});
2631
});
2732

2833
describe('canActivate', () => {
2934
describe('when the cookie has been accepted', () => {
3035
it('should return true', (done) => {
31-
guard.canActivate(undefined, { url: Object.assign({ url: 'redirect' }) } as any).subscribe((result) => {
36+
const result$ = TestBed.runInInjectionContext(() => {
37+
return endUserAgreementCookieGuard(undefined, { url: Object.assign({ url: 'redirect' }) } as any);
38+
}) as Observable<boolean | UrlTree>;
39+
40+
result$.subscribe((result) => {
3241
expect(result).toEqual(true);
3342
done();
3443
});
@@ -41,7 +50,11 @@ describe('EndUserAgreementCookieGuard', () => {
4150
});
4251

4352
it('should return a UrlTree', (done) => {
44-
guard.canActivate(undefined, Object.assign({ url: 'redirect' })).subscribe((result) => {
53+
const result$ = TestBed.runInInjectionContext(() => {
54+
return endUserAgreementCookieGuard(undefined, { url: Object.assign({ url: 'redirect' }) } as any);
55+
}) as Observable<boolean | UrlTree>;
56+
57+
result$.subscribe((result) => {
4558
expect(result).toEqual(jasmine.any(UrlTree));
4659
done();
4760
});
Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,19 @@
1-
import { Injectable } from '@angular/core';
2-
import { Router } from '@angular/router';
3-
import {
4-
Observable,
5-
of as observableOf,
6-
} from 'rxjs';
1+
import { inject } from '@angular/core';
2+
import { CanActivateFn } from '@angular/router';
3+
import { of as observableOf } from 'rxjs';
74

8-
import { AbstractEndUserAgreementGuard } from './abstract-end-user-agreement.guard';
5+
import { endUserAgreementGuard } from './end-user-agreement.guard';
96
import { EndUserAgreementService } from './end-user-agreement.service';
107

8+
119
/**
12-
* A guard redirecting users to the end agreement page when the user agreement cookie hasn't been accepted
10+
* Guard for preventing unauthorized access to certain pages
11+
* requiring the end user agreement to have been accepted in a cookie
1312
*/
14-
@Injectable({ providedIn: 'root' })
15-
export class EndUserAgreementCookieGuard extends AbstractEndUserAgreementGuard {
16-
17-
constructor(protected endUserAgreementService: EndUserAgreementService,
18-
protected router: Router) {
19-
super(router);
20-
}
21-
22-
/**
23-
* True when the user agreement cookie has been accepted
24-
*/
25-
hasAccepted(): Observable<boolean> {
26-
return observableOf(this.endUserAgreementService.isCookieAccepted());
27-
}
28-
29-
}
13+
export const endUserAgreementCookieGuard: CanActivateFn =
14+
endUserAgreementGuard(
15+
() => {
16+
const endUserAgreementService = inject(EndUserAgreementService);
17+
return observableOf(endUserAgreementService.isCookieAccepted());
18+
},
19+
);

0 commit comments

Comments
 (0)