forked from CIT/Vmeda.Online
		
	Merge pull request #1483 from dpalou/MOBILE-2533
MOBILE-2533 core: Apply auto-login to embedded iframes
This commit is contained in:
		
						commit
						5e4e951352
					
				| @ -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<string>} Promise resolved with the converted URL. | ||||
|      */ | ||||
|     getAutoLoginUrl(url: string): Promise<string> { | ||||
|     getAutoLoginUrl(url: string, showModal: boolean = true): Promise<string> { | ||||
| 
 | ||||
|         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(); | ||||
|         }); | ||||
|     } | ||||
| 
 | ||||
|  | ||||
| @ -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 { | ||||
|  | ||||
| @ -334,7 +334,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.
 | ||||
| @ -531,9 +531,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]) { | ||||
| @ -581,8 +594,12 @@ export class CoreFormatTextDirective implements OnChanges { | ||||
|                         } | ||||
|                     }); | ||||
|                 } | ||||
| 
 | ||||
|                 return; | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         this.iframeUtils.treatFrame(iframe); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|  | ||||
| @ -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 <object>. Try to get the window.
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user