-
Notifications
You must be signed in to change notification settings - Fork 532
[DSpace-CRIS] Download button & Allow authors to download restricted files #5132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5bd912c
[DURACOM-455] add bitstream download redirect guard
FrancescoMolinaro 64e866e
[DURACOM-455] add download button
FrancescoMolinaro 4f72074
[DURACOM-455] add advanced attachment
FrancescoMolinaro 6878cd6
[DURACOM-455] add config example
FrancescoMolinaro d361cc3
[DURACOM-455] fix tests
FrancescoMolinaro e738016
Merge remote-tracking branch 'gitHub/main' into task/main/DURACOM-455
FrancescoMolinaro a77a7a4
[DURACOM-455] refactor config property, add example
FrancescoMolinaro 7ad401d
[DURACOM-455] set default as false, fix issue with missing default li…
FrancescoMolinaro de6955b
Merge remote-tracking branch 'gitHub/main' into task/main/DURACOM-455
FrancescoMolinaro 04a1e9f
[DURACOM-455] optimize file download link template
FrancescoMolinaro 3684066
[DURACOM-455] clean up, fix config example, add inline documentation
FrancescoMolinaro 8bceb03
[DURACOM-455] remove unnecessary pagination configuration for attachm…
FrancescoMolinaro f2e0b3b
[DURACOM-455] clean up config example
FrancescoMolinaro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
223 changes: 223 additions & 0 deletions
223
src/app/bitstream-page/bitstream-download-redirect.guard.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,223 @@ | ||
| import { PLATFORM_ID } from '@angular/core'; | ||
| import { | ||
| TestBed, | ||
| waitForAsync, | ||
| } from '@angular/core/testing'; | ||
| import { Router } from '@angular/router'; | ||
| import { NotificationsService } from '@dspace/core/notification-system/notifications.service'; | ||
| import { getForbiddenRoute } from '@dspace/core/router/core-routing-paths'; | ||
| import { createSuccessfulRemoteDataObject$ } from '@dspace/core/utilities/remote-data.utils'; | ||
| import { Store } from '@ngrx/store'; | ||
| import { of } from 'rxjs'; | ||
|
|
||
| import { AuthService } from '../core/auth/auth.service'; | ||
| import { RemoteDataBuildService } from '../core/cache/builders/remote-data-build.service'; | ||
| import { ObjectCacheService } from '../core/cache/object-cache.service'; | ||
| import { BitstreamDataService } from '../core/data/bitstream-data.service'; | ||
| import { BitstreamFormatDataService } from '../core/data/bitstream-format-data.service'; | ||
| import { DSOChangeAnalyzer } from '../core/data/dso-change-analyzer.service'; | ||
| import { AuthorizationDataService } from '../core/data/feature-authorization/authorization-data.service'; | ||
| import { SignpostingDataService } from '../core/data/signposting-data.service'; | ||
| import { HardRedirectService } from '../core/services/hard-redirect.service'; | ||
| import { ServerResponseService } from '../core/services/server-response.service'; | ||
| import { | ||
| NativeWindowRef, | ||
| NativeWindowService, | ||
| } from '../core/services/window.service'; | ||
| import { Bitstream } from '../core/shared/bitstream.model'; | ||
| import { FileService } from '../core/shared/file.service'; | ||
| import { HALEndpointService } from '../core/shared/hal-endpoint.service'; | ||
| import { UUIDService } from '../core/shared/uuid.service'; | ||
| import { bitstreamDownloadRedirectGuard } from './bitstream-download-redirect.guard'; | ||
|
|
||
| describe('BitstreamDownloadRedirectGuard', () => { | ||
| let resolver: any; | ||
|
|
||
| let authService: AuthService; | ||
| let authorizationService: AuthorizationDataService; | ||
| let bitstreamDataService: BitstreamDataService; | ||
| let fileService: FileService; | ||
| let halEndpointService: HALEndpointService; | ||
| let hardRedirectService: HardRedirectService; | ||
| let remoteDataBuildService: RemoteDataBuildService; | ||
| let uuidService: UUIDService; | ||
| let objectCacheService: ObjectCacheService; | ||
| let router: Router; | ||
| let store: Store; | ||
| let bitstream: Bitstream; | ||
| let serverResponseService: jasmine.SpyObj<ServerResponseService>; | ||
| let signpostingDataService: jasmine.SpyObj<SignpostingDataService>; | ||
|
|
||
| let route = { | ||
| params: {}, | ||
| queryParams: {}, | ||
| }; | ||
| let state = {}; | ||
|
|
||
| const mocklink = { | ||
| href: 'http://test.org', | ||
| rel: 'test', | ||
| type: 'test', | ||
| }; | ||
|
|
||
| const mocklink2 = { | ||
| href: 'http://test2.org', | ||
| rel: 'test', | ||
| type: 'test', | ||
| }; | ||
|
|
||
| function init() { | ||
| authService = jasmine.createSpyObj('authService', { | ||
| isAuthenticated: of(true), | ||
| setRedirectUrl: {}, | ||
| }); | ||
| authorizationService = jasmine.createSpyObj('authorizationSerivice', { | ||
| isAuthorized: of(true), | ||
| }); | ||
|
|
||
| fileService = jasmine.createSpyObj('fileService', { | ||
| retrieveFileDownloadLink: of('content-url-with-headers'), | ||
| }); | ||
|
|
||
| hardRedirectService = jasmine.createSpyObj('fileService', { | ||
| redirect: {}, | ||
| }); | ||
|
|
||
| halEndpointService = jasmine.createSpyObj('halEndpointService', { | ||
| getEndpoint: of('https://rest.api/core'), | ||
| }); | ||
|
|
||
| remoteDataBuildService = jasmine.createSpyObj('remoteDataBuildService', { | ||
| buildSingle: of(new Bitstream()), | ||
| }); | ||
|
|
||
| uuidService = jasmine.createSpyObj('uuidService', { | ||
| generate: 'test-id', | ||
| }); | ||
|
|
||
| bitstream = Object.assign(new Bitstream(), { | ||
| uuid: 'bitstreamUuid', | ||
| _links: { | ||
| content: { href: 'bitstream-content-link' }, | ||
| self: { href: 'bitstream-self-link' }, | ||
| }, | ||
| }); | ||
|
|
||
| router = jasmine.createSpyObj('router', ['navigateByUrl', 'createUrlTree']); | ||
|
|
||
| store = jasmine.createSpyObj('store', { | ||
| dispatch: {}, | ||
| pipe: of(true), | ||
| }); | ||
|
|
||
| serverResponseService = jasmine.createSpyObj('ServerResponseService', { | ||
| setHeader: jasmine.createSpy('setHeader'), | ||
| }); | ||
|
|
||
| signpostingDataService = jasmine.createSpyObj('SignpostingDataService', { | ||
| getLinks: of([mocklink, mocklink2]), | ||
| }); | ||
|
|
||
| objectCacheService = jasmine.createSpyObj('objectCacheService', { | ||
| getByHref: of(null), | ||
| }); | ||
|
|
||
| bitstreamDataService = jasmine.createSpyObj('bitstreamDataService', { | ||
| findById: createSuccessfulRemoteDataObject$(Object.assign(new Bitstream(), { | ||
| _links: { | ||
| content: { href: 'bitstream-content-link' }, | ||
| self: { href: 'bitstream-self-link' }, | ||
| }, | ||
| })), | ||
| }); | ||
|
|
||
| resolver = bitstreamDownloadRedirectGuard; | ||
| } | ||
|
|
||
| function initTestbed() { | ||
| TestBed.configureTestingModule({ | ||
| providers: [ | ||
| { provide: NativeWindowService, useValue: new NativeWindowRef() }, | ||
| { provide: Router, useValue: router }, | ||
| { provide: AuthorizationDataService, useValue: authorizationService }, | ||
| { provide: AuthService, useValue: authService }, | ||
| { provide: FileService, useValue: fileService }, | ||
| { provide: HardRedirectService, useValue: hardRedirectService }, | ||
| { provide: ServerResponseService, useValue: serverResponseService }, | ||
| { provide: SignpostingDataService, useValue: signpostingDataService }, | ||
| { provide: ObjectCacheService, useValue: objectCacheService }, | ||
| { provide: PLATFORM_ID, useValue: 'server' }, | ||
| { provide: UUIDService, useValue: uuidService }, | ||
| { provide: Store, useValue: store }, | ||
| { provide: RemoteDataBuildService, useValue: remoteDataBuildService }, | ||
| { provide: HALEndpointService, useValue: halEndpointService }, | ||
| { provide: DSOChangeAnalyzer, useValue: {} }, | ||
| { provide: BitstreamFormatDataService, useValue: {} }, | ||
| { provide: NotificationsService, useValue: {} }, | ||
| { provide: BitstreamDataService, useValue: bitstreamDataService }, | ||
| ], | ||
| }); | ||
| } | ||
|
|
||
| describe('bitstream retrieval', () => { | ||
| describe('when the user is authorized and not logged in', () => { | ||
| beforeEach(() => { | ||
| init(); | ||
| (authService.isAuthenticated as jasmine.Spy).and.returnValue(of(false)); | ||
| initTestbed(); | ||
| }); | ||
| it('should redirect to the content link', waitForAsync(() => { | ||
| TestBed.runInInjectionContext(() => { | ||
| resolver(route, state).subscribe(() => { | ||
| expect(hardRedirectService.redirect).toHaveBeenCalledWith('bitstream-content-link', null, true); | ||
| }, | ||
| ); | ||
| }); | ||
| })); | ||
| }); | ||
| describe('when the user is authorized and logged in', () => { | ||
| beforeEach(() => { | ||
| init(); | ||
| initTestbed(); | ||
| }); | ||
| it('should redirect to an updated content link', waitForAsync(() => { | ||
| TestBed.runInInjectionContext(() => { | ||
| resolver(route, state).subscribe(() => { | ||
| expect(hardRedirectService.redirect).toHaveBeenCalledWith('content-url-with-headers', null, true); | ||
| }); | ||
| }); | ||
| })); | ||
| }); | ||
| describe('when the user is not authorized and logged in', () => { | ||
| beforeEach(() => { | ||
| init(); | ||
| (authorizationService.isAuthorized as jasmine.Spy).and.returnValue(of(false)); | ||
| initTestbed(); | ||
| }); | ||
| it('should navigate to the forbidden route', waitForAsync(() => { | ||
| TestBed.runInInjectionContext(() => { | ||
| resolver(route, state).subscribe(() => { | ||
| expect(router.createUrlTree).toHaveBeenCalledWith([getForbiddenRoute()]); | ||
| }); | ||
| }); | ||
| })); | ||
| }); | ||
| describe('when the user is not authorized and not logged in', () => { | ||
| beforeEach(() => { | ||
| init(); | ||
| (authService.isAuthenticated as jasmine.Spy).and.returnValue(of(false)); | ||
| (authorizationService.isAuthorized as jasmine.Spy).and.returnValue(of(false)); | ||
| initTestbed(); | ||
| }); | ||
| it('should navigate to the login page', waitForAsync(() => { | ||
|
|
||
| TestBed.runInInjectionContext(() => { | ||
| resolver(route, state).subscribe(() => { | ||
| expect(authService.setRedirectUrl).toHaveBeenCalled(); | ||
| expect(router.createUrlTree).toHaveBeenCalledWith(['login']); | ||
| }); | ||
| }); | ||
| })); | ||
| }); | ||
| }); | ||
| }); |
102 changes: 102 additions & 0 deletions
102
src/app/bitstream-page/bitstream-download-redirect.guard.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import { inject } from '@angular/core'; | ||
| import { | ||
| ActivatedRouteSnapshot, | ||
| CanActivateFn, | ||
| Router, | ||
| RouterStateSnapshot, | ||
| UrlTree, | ||
| } from '@angular/router'; | ||
| import { getForbiddenRoute } from '@dspace/core/router/core-routing-paths'; | ||
| import { | ||
| combineLatest, | ||
| Observable, | ||
| of, | ||
| } from 'rxjs'; | ||
| import { | ||
| filter, | ||
| map, | ||
| switchMap, | ||
| take, | ||
| } from 'rxjs/operators'; | ||
|
|
||
| import { AuthService } from '../core/auth/auth.service'; | ||
| import { BitstreamDataService } from '../core/data/bitstream-data.service'; | ||
| import { AuthorizationDataService } from '../core/data/feature-authorization/authorization-data.service'; | ||
| import { FeatureID } from '../core/data/feature-authorization/feature-id'; | ||
| import { RemoteData } from '../core/data/remote-data'; | ||
| import { HardRedirectService } from '../core/services/hard-redirect.service'; | ||
| import { redirectOn4xx } from '../core/shared/authorized.operators'; | ||
| import { | ||
| Bitstream, | ||
| BITSTREAM_PAGE_LINKS_TO_FOLLOW, | ||
| } from '../core/shared/bitstream.model'; | ||
| import { FileService } from '../core/shared/file.service'; | ||
| import { getFirstCompletedRemoteData } from '../core/shared/operators'; | ||
| import { | ||
| hasValue, | ||
| isNotEmpty, | ||
| } from '../utils/empty.util'; | ||
|
|
||
|
|
||
| export const bitstreamDownloadRedirectGuard: CanActivateFn = ( | ||
|
tdonohue marked this conversation as resolved.
|
||
| route: ActivatedRouteSnapshot, | ||
| state: RouterStateSnapshot, | ||
| bitstreamDataService: BitstreamDataService = inject(BitstreamDataService), | ||
| authorizationService: AuthorizationDataService = inject(AuthorizationDataService), | ||
| auth: AuthService = inject(AuthService), | ||
| fileService: FileService = inject(FileService), | ||
| hardRedirectService: HardRedirectService = inject(HardRedirectService), | ||
| router: Router = inject(Router), | ||
| ): Observable<UrlTree | boolean> => { | ||
|
|
||
| const bitstreamId = route.params.id; | ||
| const accessToken: string = route.queryParams.accessToken; | ||
|
|
||
| return bitstreamDataService.findById(bitstreamId, true, false, ...BITSTREAM_PAGE_LINKS_TO_FOLLOW).pipe( | ||
| getFirstCompletedRemoteData(), | ||
| redirectOn4xx(router, auth), | ||
| switchMap((rd: RemoteData<Bitstream>) => { | ||
| if (rd.hasSucceeded && !rd.hasNoContent) { | ||
| const bitstream = rd.payload; | ||
| const isAuthorized$ = authorizationService.isAuthorized(FeatureID.CanDownload, bitstream.self); | ||
| const isLoggedIn$ = auth.isAuthenticated(); | ||
| return combineLatest([isAuthorized$, isLoggedIn$, of(bitstream)]); | ||
| } else { | ||
| return of([false, false, null]); | ||
| } | ||
| }), | ||
| filter(([isAuthorized, isLoggedIn, bitstream]: [boolean, boolean, Bitstream]) => hasValue(isAuthorized) && hasValue(isLoggedIn)), | ||
| take(1), | ||
| switchMap(([isAuthorized, isLoggedIn, bitstream]: [boolean, boolean, Bitstream]) => { | ||
| if (isAuthorized && isLoggedIn) { | ||
| return fileService.retrieveFileDownloadLink(bitstream._links.content.href).pipe( | ||
| filter((fileLink) => hasValue(fileLink)), | ||
| take(1), | ||
| map((fileLink) => { | ||
| return [isAuthorized, isLoggedIn, bitstream, fileLink]; | ||
| })); | ||
| } else { | ||
| return of([isAuthorized, isLoggedIn, bitstream, '']); | ||
| } | ||
| }), | ||
| map(([isAuthorized, isLoggedIn, bitstream, fileLink]: [boolean, boolean, Bitstream, string]) => { | ||
| if (isAuthorized && isLoggedIn && isNotEmpty(fileLink)) { | ||
| hardRedirectService.redirect(fileLink, null, true); | ||
| return false; | ||
| } else if (isAuthorized && !isLoggedIn && !hasValue(accessToken)) { | ||
| hardRedirectService.redirect(bitstream._links.content.href, null, true); | ||
| return false; | ||
| } else if (!isAuthorized) { | ||
| if (hasValue(accessToken)) { | ||
| hardRedirectService.redirect(bitstream._links.content.href + '?accessToken=' + accessToken, null, true); | ||
| return false; | ||
| } else if (isLoggedIn) { | ||
| return router.createUrlTree([getForbiddenRoute()]); | ||
| } else if (!isLoggedIn) { | ||
| auth.setRedirectUrl(router.url); | ||
| return router.createUrlTree(['login']); | ||
| } | ||
| } | ||
| }), | ||
| ); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.