From 0309e7a8c1e39b29d8b238e4acb0a7176ce253dd Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Tue, 4 Apr 2023 09:37:54 +0200 Subject: [PATCH] MOBILE-4303 iframe: Support auto-login in core-iframe component --- src/addons/mod/url/components/index/index.ts | 14 +------------- src/core/components/iframe/iframe.ts | 10 +++++++++- .../h5p/components/h5p-iframe/h5p-iframe.ts | 5 +---- .../features/viewer/pages/iframe/iframe.html | 4 ++-- src/core/features/viewer/pages/iframe/iframe.ts | 16 ---------------- 5 files changed, 13 insertions(+), 36 deletions(-) diff --git a/src/addons/mod/url/components/index/index.ts b/src/addons/mod/url/components/index/index.ts index 0739552df..de2899f36 100644 --- a/src/addons/mod/url/components/index/index.ts +++ b/src/addons/mod/url/components/index/index.ts @@ -18,7 +18,6 @@ import { CoreError } from '@classes/errors/error'; import { CoreCourseModuleMainResourceComponent } from '@features/course/classes/main-resource-component'; import { CoreCourseContentsPage } from '@features/course/pages/contents/contents'; import { CoreCourse } from '@features/course/services/course'; -import { CoreSites } from '@services/sites'; import { CoreMimetypeUtils } from '@services/utils/mimetype'; import { CoreTextUtils } from '@services/utils/text'; import { AddonModUrl, AddonModUrlDisplayOptions, AddonModUrlProvider, AddonModUrlUrl } from '../../services/url'; @@ -109,8 +108,7 @@ export class AddonModUrlIndexComponent extends CoreCourseModuleMainResourceCompo } catch { // Fallback in case is not prefetched. - const mod = - await CoreCourse.getModule(this.module.id, this.courseId, undefined, false, false, undefined, 'url'); + const mod = await CoreCourse.getModule(this.module.id, this.courseId, undefined, false, false, undefined, 'url'); this.name = mod.name; this.description = mod.description; @@ -146,16 +144,6 @@ export class AddonModUrlIndexComponent extends CoreCourseModuleMainResourceCompo this.isVideo = CoreMimetypeUtils.isExtensionInGroup(extension, ['web_video']); this.isOther = !this.isImage && !this.isAudio && !this.isVideo; } - - if (this.shouldIframe || (this.shouldEmbed && !this.isImage && !this.isAudio && !this.isVideo)) { - // Will be displayed in an iframe. Check if we need to auto-login. - const currentSite = CoreSites.getCurrentSite(); - - if (currentSite && this.url) { - // Format the URL to add auto-login if needed. - this.url = await currentSite.getAutoLoginUrl(this.url, false); - } - } } /** diff --git a/src/core/components/iframe/iframe.ts b/src/core/components/iframe/iframe.ts index 1201da20a..515daec03 100644 --- a/src/core/components/iframe/iframe.ts +++ b/src/core/components/iframe/iframe.ts @@ -28,6 +28,7 @@ import { CoreScreen, CoreScreenOrientation } from '@services/screen'; import { Subscription } from 'rxjs'; import { filter } from 'rxjs/operators'; import { NavigationStart } from '@angular/router'; +import { CoreSites } from '@services/sites'; @Component({ selector: 'core-iframe', @@ -45,6 +46,7 @@ export class CoreIframeComponent implements OnChanges, OnDestroy { @Input() allowFullscreen?: boolean | string; @Input() showFullscreenOnToolbar?: boolean | string; @Input() autoFullscreenOnRotate?: boolean | string; + @Input() allowAutoLogin = true; @Output() loaded: EventEmitter = new EventEmitter(); loading?: boolean; @@ -154,9 +156,15 @@ export class CoreIframeComponent implements OnChanges, OnDestroy { */ async ngOnChanges(changes: {[name: string]: SimpleChange }): Promise { if (changes.src) { - const url = CoreUrlUtils.getYoutubeEmbedUrl(changes.src.currentValue) || changes.src.currentValue; + let url = CoreUrlUtils.getYoutubeEmbedUrl(changes.src.currentValue) || changes.src.currentValue; this.displayHelp = CoreIframeUtils.shouldDisplayHelpForUrl(url); + const currentSite = CoreSites.getCurrentSite(); + if (this.allowAutoLogin && currentSite) { + // Format the URL to add auto-login if needed. + url = await currentSite.getAutoLoginUrl(url, false); + } + await CoreIframeUtils.fixIframeCookies(url); this.safeUrl = DomSanitizer.bypassSecurityTrustResourceUrl(CoreFile.convertFileSrc(url)); diff --git a/src/core/features/h5p/components/h5p-iframe/h5p-iframe.ts b/src/core/features/h5p/components/h5p-iframe/h5p-iframe.ts index a6e0d41d0..ea7bb3e38 100644 --- a/src/core/features/h5p/components/h5p-iframe/h5p-iframe.ts +++ b/src/core/features/h5p/components/h5p-iframe/h5p-iframe.ts @@ -131,11 +131,8 @@ export class CoreH5PIframeComponent implements OnChanges, OnDestroy { CoreH5PCore.DISPLAY_OPTION_DOWNLOAD + '=0', ); - // Get auto-login URL so the user is automatically authenticated if needed. - const url = await this.site.getAutoLoginUrl(src, false); - // Add the preventredirect param so the user can authenticate. - this.iframeSrc = CoreUrlUtils.addParamsToUrl(url, { preventredirect: false }); + this.iframeSrc = CoreUrlUtils.addParamsToUrl(src, { preventredirect: false }); } } catch (error) { CoreDomUtils.showErrorModalDefault(error, 'Error loading H5P package.', true); diff --git a/src/core/features/viewer/pages/iframe/iframe.html b/src/core/features/viewer/pages/iframe/iframe.html index 213a23e16..16ecc4b39 100644 --- a/src/core/features/viewer/pages/iframe/iframe.html +++ b/src/core/features/viewer/pages/iframe/iframe.html @@ -9,7 +9,7 @@ - - + + diff --git a/src/core/features/viewer/pages/iframe/iframe.ts b/src/core/features/viewer/pages/iframe/iframe.ts index 7383a50d2..5b6ef0d13 100644 --- a/src/core/features/viewer/pages/iframe/iframe.ts +++ b/src/core/features/viewer/pages/iframe/iframe.ts @@ -15,8 +15,6 @@ import { Component, OnInit } from '@angular/core'; import { CoreNavigator } from '@services/navigator'; -import { CoreSites } from '@services/sites'; - /** * Page to display a URL in an iframe. */ @@ -29,7 +27,6 @@ export class CoreViewerIframePage implements OnInit { title?: string; // Page title. url?: string; // Iframe URL. autoLogin?: boolean; // Whether to try to use auto-login. - finalUrl?: string; async ngOnInit(): Promise { this.title = CoreNavigator.getRouteParam('title'); @@ -38,19 +35,6 @@ export class CoreViewerIframePage implements OnInit { this.autoLogin = typeof autoLoginParam === 'boolean' ? autoLoginParam : autoLoginParam !== 'no'; // Support deprecated values yes/no/check. - - if (!this.url) { - return; - } - - const currentSite = CoreSites.getCurrentSite(); - - if (currentSite && this.autoLogin) { - // Format the URL to add auto-login. - this.finalUrl = await currentSite.getAutoLoginUrl(this.url, false); - } else { - this.finalUrl = this.url; - } } }