From 23b93986aafd8a47e26457abaef87b3c20fafc04 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Mon, 20 Aug 2018 09:38:31 +0200 Subject: [PATCH] MOBILE-2533 core: Apply auto-login to embedded iframes --- src/classes/site.ts | 14 +++++++++++--- src/core/viewer/pages/iframe/iframe.ts | 2 +- src/directives/format-text.ts | 21 +++++++++++++++++++-- src/providers/utils/iframe.ts | 8 +++++++- 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/classes/site.ts b/src/classes/site.ts index 565493518..503a6b279 100644 --- a/src/classes/site.ts +++ b/src/classes/site.ts @@ -1434,24 +1434,31 @@ export class CoreSite { * Given a URL, convert it to a URL that will auto-login if supported. * * @param {string} url The URL to convert. + * @param {boolean} [showModal=true] Whether to show a loading modal. * @return {Promise} Promise resolved with the converted URL. */ - getAutoLoginUrl(url: string): Promise { + getAutoLoginUrl(url: string, showModal: boolean = true): Promise { if (!this.privateToken || !this.wsAvailable('tool_mobile_get_autologin_key') || (this.lastAutoLogin && this.timeUtils.timestamp() - this.lastAutoLogin < CoreConstants.SECONDS_MINUTE * 6)) { // No private token, WS not available or last auto-login was less than 6 minutes ago. Don't change the URL. + return Promise.resolve(url); } const userId = this.getUserId(), params = { privatetoken: this.privateToken - }, + }; + let modal; + + if (showModal) { modal = this.domUtils.showModalLoading(); + } // Use write to not use cache. return this.write('tool_mobile_get_autologin_key', params).then((data) => { + if (!data.autologinurl || !data.key) { // Not valid data, return the same URL. return url; @@ -1461,10 +1468,11 @@ export class CoreSite { return data.autologinurl + '?userid=' + userId + '&key=' + data.key + '&urltogo=' + url; }).catch(() => { + // Couldn't get autologin key, return the same URL. return url; }).finally(() => { - modal.dismiss(); + modal && modal.dismiss(); }); } diff --git a/src/core/viewer/pages/iframe/iframe.ts b/src/core/viewer/pages/iframe/iframe.ts index c8df18a24..8bcec245b 100644 --- a/src/core/viewer/pages/iframe/iframe.ts +++ b/src/core/viewer/pages/iframe/iframe.ts @@ -42,7 +42,7 @@ export class CoreViewerIframePage { if (currentSite && (this.autoLogin == 'yes' || (this.autoLogin == 'check' && currentSite.containsUrl(url)))) { // Format the URL to add auto-login. - currentSite.getAutoLoginUrl(url).then((url) => { + currentSite.getAutoLoginUrl(url, false).then((url) => { this.url = url; }); } else { diff --git a/src/directives/format-text.ts b/src/directives/format-text.ts index d5a620daa..da3ba1d6e 100644 --- a/src/directives/format-text.ts +++ b/src/directives/format-text.ts @@ -329,7 +329,7 @@ export class CoreFormatTextDirective implements OnChanges { buttons = Array.from(div.querySelectorAll('.button')); elementsWithInlineStyles = Array.from(div.querySelectorAll('*[style]')); stopClicksElements = Array.from(div.querySelectorAll('button,input,select,textarea')); - frames = Array.from(div.querySelectorAll(CoreIframeUtilsProvider.FRAME_TAGS.join(','))); + frames = Array.from(div.querySelectorAll(CoreIframeUtilsProvider.FRAME_TAGS.join(',').replace(/iframe,?/, ''))); // Walk through the content to find the links and add our directive to it. // Important: We need to look for links first because in 'img' we add new links without core-link. @@ -528,9 +528,22 @@ export class CoreFormatTextDirective implements OnChanges { * @param {Boolean} canTreatVimeo Whether Vimeo videos can be treated in the site. */ protected treatIframe(iframe: HTMLIFrameElement, site: CoreSite, canTreatVimeo: boolean): void { + const src = iframe.src, + currentSite = this.sitesProvider.getCurrentSite(); + this.addMediaAdaptClass(iframe); - if (iframe.src && canTreatVimeo) { + if (currentSite && currentSite.containsUrl(src)) { + // URL points to current site, try to use auto-login. + currentSite.getAutoLoginUrl(src, false).then((finalUrl) => { + iframe.src = finalUrl; + + this.iframeUtils.treatFrame(iframe); + }); + + return; + + } else if (src && canTreatVimeo) { // Check if it's a Vimeo video. If it is, use the wsplayer script instead to make restricted videos work. const matches = iframe.src.match(/https?:\/\/player\.vimeo\.com\/video\/([0-9]+)/); if (matches && matches[1]) { @@ -578,8 +591,12 @@ export class CoreFormatTextDirective implements OnChanges { } }); } + + return; } } + + this.iframeUtils.treatFrame(iframe); } /** diff --git a/src/providers/utils/iframe.ts b/src/providers/utils/iframe.ts index 1d2287266..7bcbf00b2 100644 --- a/src/providers/utils/iframe.ts +++ b/src/providers/utils/iframe.ts @@ -46,7 +46,13 @@ export class CoreIframeUtilsProvider { */ getContentWindowAndDocument(element: any): { window: Window, document: Document } { let contentWindow: Window = element.contentWindow, - contentDocument: Document = element.contentDocument || (contentWindow && contentWindow.document); + contentDocument: Document; + + try { + contentDocument = element.contentDocument || (contentWindow && contentWindow.document); + } catch (ex) { + // Ignore errors. + } if (!contentWindow && contentDocument) { // It's probably an . Try to get the window.