1- import {
2- AsyncPipe ,
3- isPlatformServer ,
4- Location ,
5- } from '@angular/common' ;
6- import {
7- Component ,
8- Inject ,
9- OnInit ,
10- PLATFORM_ID ,
11- } from '@angular/core' ;
12- import {
13- ActivatedRoute ,
14- Router ,
15- } from '@angular/router' ;
1+ import { AsyncPipe , isPlatformServer , Location , } from '@angular/common' ;
2+ import { Component , Inject , inject , OnInit , PLATFORM_ID , } from '@angular/core' ;
3+ import { ActivatedRoute , Params , Router , } from '@angular/router' ;
164import { TranslateModule } from '@ngx-translate/core' ;
17- import {
18- combineLatest as observableCombineLatest ,
19- Observable ,
20- of as observableOf ,
21- } from 'rxjs' ;
22- import {
23- filter ,
24- map ,
25- switchMap ,
26- take ,
27- } from 'rxjs/operators' ;
5+ import { combineLatest as observableCombineLatest , Observable , of as observableOf , } from 'rxjs' ;
6+ import { filter , map , switchMap , take , } from 'rxjs/operators' ;
287
298import { getForbiddenRoute } from '../../app-routing-paths' ;
309import { AuthService } from '../../core/auth/auth.service' ;
3110import { DSONameService } from '../../core/breadcrumbs/dso-name.service' ;
11+ import { ConfigurationDataService } from '../../core/data/configuration-data.service' ;
3212import { AuthorizationDataService } from '../../core/data/feature-authorization/authorization-data.service' ;
3313import { FeatureID } from '../../core/data/feature-authorization/feature-id' ;
3414import { RemoteData } from '../../core/data/remote-data' ;
@@ -40,10 +20,7 @@ import { redirectOn4xx } from '../../core/shared/authorized.operators';
4020import { Bitstream } from '../../core/shared/bitstream.model' ;
4121import { FileService } from '../../core/shared/file.service' ;
4222import { getRemoteDataPayload } from '../../core/shared/operators' ;
43- import {
44- hasValue ,
45- isNotEmpty ,
46- } from '../../shared/empty.util' ;
23+ import { hasValue , isNotEmpty , } from '../../shared/empty.util' ;
4724import { MatomoService } from '../../statistics/matomo.service' ;
4825
4926@Component ( {
@@ -63,6 +40,8 @@ export class BitstreamDownloadPageComponent implements OnInit {
6340 bitstream$ : Observable < Bitstream > ;
6441 bitstreamRD$ : Observable < RemoteData < Bitstream > > ;
6542
43+ configService = inject ( ConfigurationDataService ) ;
44+
6645 constructor (
6746 private route : ActivatedRoute ,
6847 protected router : Router ,
@@ -85,6 +64,10 @@ export class BitstreamDownloadPageComponent implements OnInit {
8564 }
8665
8766 ngOnInit ( ) : void {
67+ const accessToken$ : Observable < string > = this . route . queryParams . pipe (
68+ map ( ( queryParams : Params ) => queryParams ?. accessToken || null ) ,
69+ take ( 1 ) ,
70+ ) ;
8871
8972 this . bitstreamRD$ = this . route . data . pipe (
9073 map ( ( data ) => data . bitstream ) ) ;
@@ -98,38 +81,49 @@ export class BitstreamDownloadPageComponent implements OnInit {
9881 switchMap ( ( bitstream : Bitstream ) => {
9982 const isAuthorized$ = this . authorizationService . isAuthorized ( FeatureID . CanDownload , isNotEmpty ( bitstream ) ? bitstream . self : undefined ) ;
10083 const isLoggedIn$ = this . auth . isAuthenticated ( ) ;
101- return observableCombineLatest ( [ isAuthorized$ , isLoggedIn$ , observableOf ( bitstream ) ] ) ;
84+ const isMatomoEnabled$ = this . matomoService . isMatomoEnabled$ ( ) ;
85+ return observableCombineLatest ( [ isAuthorized$ , isLoggedIn$ , isMatomoEnabled$ , accessToken$ , observableOf ( bitstream ) ] ) ;
10286 } ) ,
103- filter ( ( [ isAuthorized , isLoggedIn , bitstream ] : [ boolean , boolean , Bitstream ] ) => hasValue ( isAuthorized ) && hasValue ( isLoggedIn ) ) ,
87+ filter ( ( [ isAuthorized , isLoggedIn , isMatomoEnabled , accessToken , bitstream ] : [ boolean , boolean , boolean , string , Bitstream ] ) => ( hasValue ( isAuthorized ) && hasValue ( isLoggedIn ) ) || hasValue ( accessToken ) ) ,
10488 take ( 1 ) ,
105- switchMap ( ( [ isAuthorized , isLoggedIn , bitstream ] : [ boolean , boolean , Bitstream ] ) => {
89+ switchMap ( ( [ isAuthorized , isLoggedIn , isMatomoEnabled , accessToken , bitstream ] : [ boolean , boolean , boolean , string , Bitstream ] ) => {
10690 if ( isAuthorized && isLoggedIn ) {
10791 return this . fileService . retrieveFileDownloadLink ( bitstream . _links . content . href ) . pipe (
10892 filter ( ( fileLink ) => hasValue ( fileLink ) ) ,
10993 take ( 1 ) ,
11094 map ( ( fileLink ) => {
111- return [ isAuthorized , isLoggedIn , bitstream , fileLink ] ;
95+ return [ isAuthorized , isLoggedIn , isMatomoEnabled , bitstream , fileLink ] ;
11296 } ) ) ;
97+ } else if ( hasValue ( accessToken ) ) {
98+ return [ [ isAuthorized , ! isLoggedIn , isMatomoEnabled , bitstream , '' , accessToken ] ] ;
11399 } else {
114- return [ [ isAuthorized , isLoggedIn , bitstream , bitstream . _links . content . href ] ] ;
100+ return [ [ isAuthorized , isLoggedIn , isMatomoEnabled , bitstream , bitstream . _links . content . href ] ] ;
115101 }
116102 } ) ,
117- switchMap ( ( [ isAuthorized , isLoggedIn , bitstream , fileLink ] : [ boolean , boolean , Bitstream , string ] ) =>
118- this . matomoService . appendVisitorId ( fileLink )
119- . pipe (
120- map ( ( fileLinkWithVisitorId ) => [ isAuthorized , isLoggedIn , bitstream , fileLinkWithVisitorId ] ) ,
121- ) ,
122- ) ,
123- ) . subscribe ( ( [ isAuthorized , isLoggedIn , , fileLink ] : [ boolean , boolean , Bitstream , string ] ) => {
103+ switchMap ( ( [ isAuthorized , isLoggedIn , isMatomoEnabled , bitstream , fileLink , accessToken ] : [ boolean , boolean , boolean , Bitstream , string , string ] ) => {
104+ if ( isMatomoEnabled ) {
105+ return this . matomoService . appendVisitorId ( fileLink ) . pipe (
106+ map ( ( fileLinkWithVisitorId ) => [ isAuthorized , isLoggedIn , bitstream , fileLinkWithVisitorId , accessToken ] ) ,
107+ ) ;
108+ }
109+ return observableOf ( [ isAuthorized , isLoggedIn , bitstream , fileLink , accessToken ] ) ;
110+ } ) ,
111+ ) . subscribe ( ( [ isAuthorized , isLoggedIn , bitstream , fileLink , accessToken ] : [ boolean , boolean , Bitstream , string , string ] ) => {
124112 if ( isAuthorized && isLoggedIn && isNotEmpty ( fileLink ) ) {
125113 this . hardRedirectService . redirect ( fileLink ) ;
126- } else if ( isAuthorized && ! isLoggedIn ) {
114+ } else if ( isAuthorized && ! isLoggedIn && ! hasValue ( accessToken ) ) {
127115 this . hardRedirectService . redirect ( fileLink ) ;
128- } else if ( ! isAuthorized && isLoggedIn ) {
129- this . router . navigateByUrl ( getForbiddenRoute ( ) , { skipLocationChange : true } ) ;
130- } else if ( ! isAuthorized && ! isLoggedIn ) {
131- this . auth . setRedirectUrl ( this . router . url ) ;
132- this . router . navigateByUrl ( 'login' ) ;
116+ } else if ( ! isAuthorized ) {
117+ // Either we have an access token, or we are logged in, or we are not logged in.
118+ // For now, the access token does not care if we are logged in or not.
119+ if ( hasValue ( accessToken ) ) {
120+ this . hardRedirectService . redirect ( bitstream . _links . content . href + '?accessToken=' + accessToken ) ;
121+ } else if ( isLoggedIn ) {
122+ this . router . navigateByUrl ( getForbiddenRoute ( ) , { skipLocationChange : true } ) ;
123+ } else if ( ! isLoggedIn ) {
124+ this . auth . setRedirectUrl ( this . router . url ) ;
125+ this . router . navigateByUrl ( 'login' ) ;
126+ }
133127 }
134128 } ) ;
135129 }
0 commit comments