@@ -6,11 +6,13 @@ import {
66import {
77 Component ,
88 Inject ,
9+ inject ,
910 OnInit ,
1011 PLATFORM_ID ,
1112} from '@angular/core' ;
1213import {
1314 ActivatedRoute ,
15+ Params ,
1416 Router ,
1517} from '@angular/router' ;
1618import { TranslateModule } from '@ngx-translate/core' ;
@@ -29,6 +31,7 @@ import {
2931import { getForbiddenRoute } from '../../app-routing-paths' ;
3032import { AuthService } from '../../core/auth/auth.service' ;
3133import { DSONameService } from '../../core/breadcrumbs/dso-name.service' ;
34+ import { ConfigurationDataService } from '../../core/data/configuration-data.service' ;
3235import { AuthorizationDataService } from '../../core/data/feature-authorization/authorization-data.service' ;
3336import { FeatureID } from '../../core/data/feature-authorization/feature-id' ;
3437import { RemoteData } from '../../core/data/remote-data' ;
@@ -63,6 +66,8 @@ export class BitstreamDownloadPageComponent implements OnInit {
6366 bitstream$ : Observable < Bitstream > ;
6467 bitstreamRD$ : Observable < RemoteData < Bitstream > > ;
6568
69+ configService = inject ( ConfigurationDataService ) ;
70+
6671 constructor (
6772 private route : ActivatedRoute ,
6873 protected router : Router ,
@@ -85,6 +90,10 @@ export class BitstreamDownloadPageComponent implements OnInit {
8590 }
8691
8792 ngOnInit ( ) : void {
93+ const accessToken$ : Observable < string > = this . route . queryParams . pipe (
94+ map ( ( queryParams : Params ) => queryParams ?. accessToken || null ) ,
95+ take ( 1 ) ,
96+ ) ;
8897
8998 this . bitstreamRD$ = this . route . data . pipe (
9099 map ( ( data ) => data . bitstream ) ) ;
@@ -98,38 +107,49 @@ export class BitstreamDownloadPageComponent implements OnInit {
98107 switchMap ( ( bitstream : Bitstream ) => {
99108 const isAuthorized$ = this . authorizationService . isAuthorized ( FeatureID . CanDownload , isNotEmpty ( bitstream ) ? bitstream . self : undefined ) ;
100109 const isLoggedIn$ = this . auth . isAuthenticated ( ) ;
101- return observableCombineLatest ( [ isAuthorized$ , isLoggedIn$ , observableOf ( bitstream ) ] ) ;
110+ const isMatomoEnabled$ = this . matomoService . isMatomoEnabled$ ( ) ;
111+ return observableCombineLatest ( [ isAuthorized$ , isLoggedIn$ , isMatomoEnabled$ , accessToken$ , observableOf ( bitstream ) ] ) ;
102112 } ) ,
103- filter ( ( [ isAuthorized , isLoggedIn , bitstream ] : [ boolean , boolean , Bitstream ] ) => hasValue ( isAuthorized ) && hasValue ( isLoggedIn ) ) ,
113+ filter ( ( [ isAuthorized , isLoggedIn , isMatomoEnabled , accessToken , bitstream ] : [ boolean , boolean , boolean , string , Bitstream ] ) => ( hasValue ( isAuthorized ) && hasValue ( isLoggedIn ) ) || hasValue ( accessToken ) ) ,
104114 take ( 1 ) ,
105- switchMap ( ( [ isAuthorized , isLoggedIn , bitstream ] : [ boolean , boolean , Bitstream ] ) => {
115+ switchMap ( ( [ isAuthorized , isLoggedIn , isMatomoEnabled , accessToken , bitstream ] : [ boolean , boolean , boolean , string , Bitstream ] ) => {
106116 if ( isAuthorized && isLoggedIn ) {
107117 return this . fileService . retrieveFileDownloadLink ( bitstream . _links . content . href ) . pipe (
108118 filter ( ( fileLink ) => hasValue ( fileLink ) ) ,
109119 take ( 1 ) ,
110120 map ( ( fileLink ) => {
111- return [ isAuthorized , isLoggedIn , bitstream , fileLink ] ;
121+ return [ isAuthorized , isLoggedIn , isMatomoEnabled , bitstream , fileLink ] ;
112122 } ) ) ;
123+ } else if ( hasValue ( accessToken ) ) {
124+ return [ [ isAuthorized , ! isLoggedIn , isMatomoEnabled , bitstream , '' , accessToken ] ] ;
113125 } else {
114- return [ [ isAuthorized , isLoggedIn , bitstream , bitstream . _links . content . href ] ] ;
126+ return [ [ isAuthorized , isLoggedIn , isMatomoEnabled , bitstream , bitstream . _links . content . href ] ] ;
115127 }
116128 } ) ,
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 ] ) => {
129+ switchMap ( ( [ isAuthorized , isLoggedIn , isMatomoEnabled , bitstream , fileLink , accessToken ] : [ boolean , boolean , boolean , Bitstream , string , string ] ) => {
130+ if ( isMatomoEnabled ) {
131+ return this . matomoService . appendVisitorId ( fileLink ) . pipe (
132+ map ( ( fileLinkWithVisitorId ) => [ isAuthorized , isLoggedIn , bitstream , fileLinkWithVisitorId , accessToken ] ) ,
133+ ) ;
134+ }
135+ return observableOf ( [ isAuthorized , isLoggedIn , bitstream , fileLink , accessToken ] ) ;
136+ } ) ,
137+ ) . subscribe ( ( [ isAuthorized , isLoggedIn , bitstream , fileLink , accessToken ] : [ boolean , boolean , Bitstream , string , string ] ) => {
124138 if ( isAuthorized && isLoggedIn && isNotEmpty ( fileLink ) ) {
125139 this . hardRedirectService . redirect ( fileLink ) ;
126- } else if ( isAuthorized && ! isLoggedIn ) {
140+ } else if ( isAuthorized && ! isLoggedIn && ! hasValue ( accessToken ) ) {
127141 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' ) ;
142+ } else if ( ! isAuthorized ) {
143+ // Either we have an access token, or we are logged in, or we are not logged in.
144+ // For now, the access token does not care if we are logged in or not.
145+ if ( hasValue ( accessToken ) ) {
146+ this . hardRedirectService . redirect ( bitstream . _links . content . href + '?accessToken=' + accessToken ) ;
147+ } else if ( isLoggedIn ) {
148+ this . router . navigateByUrl ( getForbiddenRoute ( ) , { skipLocationChange : true } ) ;
149+ } else if ( ! isLoggedIn ) {
150+ this . auth . setRedirectUrl ( this . router . url ) ;
151+ this . router . navigateByUrl ( 'login' ) ;
152+ }
133153 }
134154 } ) ;
135155 }
0 commit comments