diff --git a/src/addons/block/recentlyaccesseditems/components/recentlyaccesseditems/recentlyaccesseditems.ts b/src/addons/block/recentlyaccesseditems/components/recentlyaccesseditems/recentlyaccesseditems.ts index ddcc27cee..68b766359 100644 --- a/src/addons/block/recentlyaccesseditems/components/recentlyaccesseditems/recentlyaccesseditems.ts +++ b/src/addons/block/recentlyaccesseditems/components/recentlyaccesseditems/recentlyaccesseditems.ts @@ -89,7 +89,7 @@ export class AddonBlockRecentlyAccessedItemsComponent extends CoreBlockBaseCompo try { const treated = await CoreContentLinksHelper.handleLink(url); if (!treated) { - return CoreSites.getCurrentSite()?.openInBrowserWithAutoLoginIfSameSite(url); + return CoreSites.getCurrentSite()?.openInBrowserWithAutoLogin(url); } } finally { modal.dismiss(); diff --git a/src/addons/block/timeline/components/events/events.ts b/src/addons/block/timeline/components/events/events.ts index bdebff829..8f6306349 100644 --- a/src/addons/block/timeline/components/events/events.ts +++ b/src/addons/block/timeline/components/events/events.ts @@ -149,7 +149,7 @@ export class AddonBlockTimelineEventsComponent implements OnChanges { try { const treated = await CoreContentLinksHelper.handleLink(url); if (!treated) { - return CoreSites.getRequiredCurrentSite().openInBrowserWithAutoLoginIfSameSite(url); + return CoreSites.getRequiredCurrentSite().openInBrowserWithAutoLogin(url); } } finally { modal.dismiss(); diff --git a/src/addons/mod/feedback/pages/form/form.ts b/src/addons/mod/feedback/pages/form/form.ts index 6226940cc..c3d5a08c5 100644 --- a/src/addons/mod/feedback/pages/form/form.ts +++ b/src/addons/mod/feedback/pages/form/form.ts @@ -418,7 +418,7 @@ export class AddonModFeedbackFormPage implements OnInit, OnDestroy, CanLeave { const treated = await CoreContentLinksHelper.handleLink(this.siteAfterSubmit); if (!treated) { - await this.currentSite.openInBrowserWithAutoLoginIfSameSite(this.siteAfterSubmit); + await this.currentSite.openInBrowserWithAutoLogin(this.siteAfterSubmit); } } finally { modal.dismiss(); diff --git a/src/addons/mod/resource/components/index/index.ts b/src/addons/mod/resource/components/index/index.ts index c13acb8fd..6b4f51679 100644 --- a/src/addons/mod/resource/components/index/index.ts +++ b/src/addons/mod/resource/components/index/index.ts @@ -221,7 +221,7 @@ export class AddonModResourceIndexComponent extends CoreCourseModuleMainResource } // The resource cannot be downloaded, open the activity in browser. - await CoreSites.getCurrentSite()?.openInBrowserWithAutoLoginIfSameSite(this.module.url || ''); + await CoreSites.getCurrentSite()?.openInBrowserWithAutoLogin(this.module.url || ''); } /** diff --git a/src/addons/mod/url/components/index/index.ts b/src/addons/mod/url/components/index/index.ts index 9c253a334..6bd96a863 100644 --- a/src/addons/mod/url/components/index/index.ts +++ b/src/addons/mod/url/components/index/index.ts @@ -151,9 +151,9 @@ export class AddonModUrlIndexComponent extends CoreCourseModuleMainResourceCompo // Will be displayed in an iframe. Check if we need to auto-login. const currentSite = CoreSites.getCurrentSite(); - if (currentSite?.containsUrl(this.url)) { - // Format the URL to add auto-login. - this.url = await currentSite.getAutoLoginUrl(this.url!, false); + 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/addons/mod/url/services/url-helper.ts b/src/addons/mod/url/services/url-helper.ts index 076569e31..c59e77076 100644 --- a/src/addons/mod/url/services/url-helper.ts +++ b/src/addons/mod/url/services/url-helper.ts @@ -36,7 +36,7 @@ export class AddonModUrlHelperProvider { const treated = await CoreContentLinksHelper.handleLink(url, undefined, true, true); if (!treated) { - await CoreSites.getCurrentSite()?.openInBrowserWithAutoLoginIfSameSite(url); + await CoreSites.getCurrentSite()?.openInBrowserWithAutoLogin(url); } } finally { modal.dismiss(); diff --git a/src/addons/report/insights/services/handlers/action-link.ts b/src/addons/report/insights/services/handlers/action-link.ts index aad2a84fc..75dfc371e 100644 --- a/src/addons/report/insights/services/handlers/action-link.ts +++ b/src/addons/report/insights/services/handlers/action-link.ts @@ -80,7 +80,7 @@ export class AddonReportInsightsActionLinkHandlerService extends CoreContentLink // Cannot be opened in the app, open in browser. const site = await CoreSites.getSite(siteId); - await site.openInBrowserWithAutoLoginIfSameSite(forwardUrl); + await site.openInBrowserWithAutoLogin(forwardUrl); } } }, diff --git a/src/core/classes/site.ts b/src/core/classes/site.ts index 8afc08b21..20dc8f448 100644 --- a/src/core/classes/site.ts +++ b/src/core/classes/site.ts @@ -1533,13 +1533,14 @@ export class CoreSite { * @param alertMessage If defined, an alert will be shown before opening the browser. * @param options Other options. * @return Promise resolved when done, rejected otherwise. + * @deprecated since 4.1. Use openInBrowserWithAutoLogin instead, now it always checks that URL belongs to same site. */ async openInBrowserWithAutoLoginIfSameSite( url: string, alertMessage?: string, options: CoreUtilsOpenInBrowserOptions = {}, ): Promise { - await this.openWithAutoLoginIfSameSite(false, url, options, alertMessage); + return this.openInBrowserWithAutoLogin(url, alertMessage, options); } /** @@ -1563,15 +1564,14 @@ export class CoreSite { * @param options Override default options passed to inappbrowser. * @param alertMessage If defined, an alert will be shown before opening the inappbrowser. * @return Promise resolved when done. + * @deprecated since 4.1. Use openInAppWithAutoLogin instead, now it always checks that URL belongs to same site. */ async openInAppWithAutoLoginIfSameSite( url: string, options?: InAppBrowserOptions, alertMessage?: string, ): Promise { - const iabInstance = await this.openWithAutoLoginIfSameSite(true, url, options, alertMessage); - - return iabInstance; + return this.openInAppWithAutoLogin(url, options, alertMessage); } /** @@ -1623,6 +1623,7 @@ export class CoreSite { * @param options Override default options passed to inappbrowser. * @param alertMessage If defined, an alert will be shown before opening the browser/inappbrowser. * @return Promise resolved when done. Resolve param is returned only if inApp=true. + * @deprecated since 4.1. Use openWithAutoLogin instead, now it always checks that URL belongs to same site. */ async openWithAutoLoginIfSameSite( inApp: boolean, @@ -1630,15 +1631,7 @@ export class CoreSite { options: InAppBrowserOptions & CoreUtilsOpenInBrowserOptions = {}, alertMessage?: string, ): Promise { - if (this.containsUrl(url)) { - return this.openWithAutoLogin(inApp, url, options, alertMessage); - } else { - if (inApp) { - return Promise.resolve(CoreUtils.openInApp(url, options)); - } else { - CoreUtils.openInBrowser(url, options); - } - } + return this.openWithAutoLogin(inApp, url, options, alertMessage); } /** @@ -1822,6 +1815,11 @@ export class CoreSite { return url; } + if (!this.containsUrl(url)) { + // URL doesn't belong to the site, don't auto login. + return url; + } + if (this.lastAutoLogin > 0) { const timeBetweenRequests = await CoreUtils.ignoreErrors( this.getConfig('tool_mobile_autologinmintimebetweenreq'), diff --git a/src/core/components/context-menu/context-menu-item.ts b/src/core/components/context-menu/context-menu-item.ts index 922332a07..2c4a209b1 100644 --- a/src/core/components/context-menu/context-menu-item.ts +++ b/src/core/components/context-menu/context-menu-item.ts @@ -45,7 +45,7 @@ export class CoreContextMenuItemComponent implements OnInit, OnDestroy, OnChange @Input() ariaAction?: string; // Aria label to add to iconAction. If not set, it will be equal to content. @Input() href?: string; // Link to go if no action provided. @Input() captureLink?: boolean | string; // Whether the link needs to be captured by the app. - @Input() autoLogin?: string; // Whether the link needs to be opened using auto-login. + @Input() autoLogin: boolean | string = true; // Whether the link needs to be opened using auto-login. @Input() closeOnClick = true; // Whether to close the popover when the item is clicked. @Input() priority?: number; // Used to sort items. The highest priority, the highest position. @Input() badge?: string; // A badge to show in the item. @@ -88,7 +88,6 @@ export class CoreContextMenuItemComponent implements OnInit, OnDestroy, OnChange // Navigation help if href provided. this.captureLink = this.href && this.captureLink ? this.captureLink : false; - this.autoLogin = this.autoLogin || 'check'; if (!this.destroyed) { this.ctxtMenu.addItem(this); diff --git a/src/core/directives/format-text.ts b/src/core/directives/format-text.ts index 74eef5ace..13edae04e 100644 --- a/src/core/directives/format-text.ts +++ b/src/core/directives/format-text.ts @@ -601,7 +601,7 @@ export class CoreFormatTextDirective implements OnChanges, OnDestroy, AsyncCompo } if (openInApp) { - site.openInAppWithAutoLoginIfSameSite(url); + site.openInAppWithAutoLogin(url); if (refreshOnResume && this.refreshContext) { // Refresh the context when the IAB is closed. @@ -610,7 +610,7 @@ export class CoreFormatTextDirective implements OnChanges, OnDestroy, AsyncCompo }); } } else { - site.openInBrowserWithAutoLoginIfSameSite(url, undefined, { + site.openInBrowserWithAutoLogin(url, undefined, { showBrowserWarning: !confirmMessage, }); diff --git a/src/core/directives/link.ts b/src/core/directives/link.ts index f2fa04d86..cabc76f25 100644 --- a/src/core/directives/link.ts +++ b/src/core/directives/link.ts @@ -41,11 +41,7 @@ export class CoreLinkDirective implements OnInit { @Input() href?: string | SafeUrl; // Link URL. @Input() capture?: boolean | string; // If the link needs to be captured by the app. @Input() inApp?: boolean | string; // True to open in embedded browser, false to open in system browser. - /* Whether the link should be opened with auto-login. Accepts the following values: - "yes" -> Always auto-login. - "no" -> Never auto-login. - "check" -> Auto-login only if it points to the current site. Default value. */ - @Input() autoLogin = 'check'; + @Input() autoLogin: boolean | string = true; // Whether to try to use auto-login. Values yes/no/check are deprecated. @Input() showBrowserWarning = true; // Whether to show a warning before opening browser. Defaults to true. protected element: HTMLElement; @@ -61,8 +57,6 @@ export class CoreLinkDirective implements OnInit { * Function executed when the component is initialized. */ ngOnInit(): void { - this.inApp = this.inApp === undefined ? this.inApp : CoreUtils.isTrueOrOne(this.inApp); - if (this.element.tagName != 'BUTTON' && this.element.tagName != 'A') { this.element.setAttribute('tabindex', '0'); this.element.setAttribute('role', 'button'); @@ -184,10 +178,16 @@ export class CoreLinkDirective implements OnInit { * @return Promise resolved when done. */ protected async openExternalLink(href: string, openIn?: string | null): Promise { - // It's an external link, we will open with browser. Check if we need to auto-login. + // Priority order is: core-link inApp attribute > forceOpenLinksIn setting > data-open-in HTML attribute. + const openInApp = this.inApp !== undefined ? + CoreUtils.isTrueOrOne(this.inApp) : + (CoreConstants.CONFIG.forceOpenLinksIn !== 'browser' && + (CoreConstants.CONFIG.forceOpenLinksIn === 'app' || openIn === 'app')); + + // Check if we need to auto-login. if (!CoreSites.isLoggedIn()) { // Not logged in, cannot auto-login. - if (this.inApp) { + if (openInApp) { CoreUtils.openInApp(href); } else { CoreUtils.openInBrowser(href, { showBrowserWarning: this.showBrowserWarning }); @@ -221,38 +221,22 @@ export class CoreLinkDirective implements OnInit { } } - if (this.autoLogin == 'yes') { - if (this.inApp) { + const autoLogin = typeof this.autoLogin === 'boolean' ? + this.autoLogin : + !CoreUtils.isFalseOrZero(this.autoLogin) && this.autoLogin !== 'no'; // Support deprecated values yes/no/check. + + if (autoLogin) { + if (openInApp) { await currentSite.openInAppWithAutoLogin(href); } else { await currentSite.openInBrowserWithAutoLogin(href, undefined, { showBrowserWarning: this.showBrowserWarning }); } - } else if (this.autoLogin == 'no') { - if (this.inApp) { + } else { + if (openInApp) { CoreUtils.openInApp(href); } else { CoreUtils.openInBrowser(href, { showBrowserWarning: this.showBrowserWarning }); } - } else { - // Priority order is: core-link inApp attribute > forceOpenLinksIn setting > data-open-in HTML attribute. - let openInApp = this.inApp; - if (this.inApp === undefined) { - if (CoreConstants.CONFIG.forceOpenLinksIn == 'browser') { - openInApp = false; - } else if (CoreConstants.CONFIG.forceOpenLinksIn == 'app' || openIn == 'app') { - openInApp = true; - } - } - - if (openInApp) { - await currentSite.openInAppWithAutoLoginIfSameSite(href); - } else { - await currentSite.openInBrowserWithAutoLoginIfSameSite( - href, - undefined, - { showBrowserWarning: this.showBrowserWarning }, - ); - } } } diff --git a/src/core/features/course/services/handlers/default-module.ts b/src/core/features/course/services/handlers/default-module.ts index 13f40a844..01d113c1e 100644 --- a/src/core/features/course/services/handlers/default-module.ts +++ b/src/core/features/course/services/handlers/default-module.ts @@ -67,7 +67,7 @@ export class CoreCourseModuleDefaultHandler implements CoreCourseModuleHandler { e.preventDefault(); e.stopPropagation(); - CoreSites.getRequiredCurrentSite().openInBrowserWithAutoLoginIfSameSite(url); + CoreSites.getRequiredCurrentSite().openInBrowserWithAutoLogin(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 9c5d39396..f3e5ea549 100644 --- a/src/core/features/h5p/components/h5p-iframe/h5p-iframe.ts +++ b/src/core/features/h5p/components/h5p-iframe/h5p-iframe.ts @@ -129,7 +129,7 @@ export class CoreH5PIframeComponent implements OnChanges, OnDestroy { CoreH5PCore.DISPLAY_OPTION_DOWNLOAD + '=0', ); - // Get auto-login URL so the user is automatically authenticated. + // 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. diff --git a/src/core/features/mainmenu/pages/more/more.ts b/src/core/features/mainmenu/pages/more/more.ts index 275686ba2..f56521f7d 100644 --- a/src/core/features/mainmenu/pages/more/more.ts +++ b/src/core/features/mainmenu/pages/more/more.ts @@ -166,7 +166,7 @@ export class CoreMainMenuMorePage implements OnInit, OnDestroy { if (!treated) { // Can't handle it, open it in browser. - CoreSites.getCurrentSite()?.openInBrowserWithAutoLoginIfSameSite(text); + CoreSites.getCurrentSite()?.openInBrowserWithAutoLogin(text); } } else { // It's not a URL, open it in a modal so the user can see it and copy it. diff --git a/src/core/features/viewer/pages/iframe/iframe.ts b/src/core/features/viewer/pages/iframe/iframe.ts index fca24169a..7383a50d2 100644 --- a/src/core/features/viewer/pages/iframe/iframe.ts +++ b/src/core/features/viewer/pages/iframe/iframe.ts @@ -28,17 +28,16 @@ export class CoreViewerIframePage implements OnInit { title?: string; // Page title. url?: string; // Iframe URL. - /* Whether the URL should be open with auto-login. Accepts the following values: - "yes" -> Always auto-login. - "no" -> Never auto-login. - "check" -> Auto-login only if it points to the current site. Default value. */ - autoLogin?: string; + autoLogin?: boolean; // Whether to try to use auto-login. finalUrl?: string; async ngOnInit(): Promise { this.title = CoreNavigator.getRouteParam('title'); this.url = CoreNavigator.getRouteParam('url'); - this.autoLogin = CoreNavigator.getRouteParam('autoLogin') || 'check'; + const autoLoginParam = CoreNavigator.getRouteParam('autoLogin') ?? true; + this.autoLogin = typeof autoLoginParam === 'boolean' ? + autoLoginParam : + autoLoginParam !== 'no'; // Support deprecated values yes/no/check. if (!this.url) { return; @@ -46,7 +45,7 @@ export class CoreViewerIframePage implements OnInit { const currentSite = CoreSites.getCurrentSite(); - if (currentSite && (this.autoLogin == 'yes' || (this.autoLogin == 'check' && currentSite.containsUrl(this.url)))) { + if (currentSite && this.autoLogin) { // Format the URL to add auto-login. this.finalUrl = await currentSite.getAutoLoginUrl(this.url, false); } else { diff --git a/src/core/services/utils/iframe.ts b/src/core/services/utils/iframe.ts index f9a5ccf11..56c7d07de 100644 --- a/src/core/services/utils/iframe.ts +++ b/src/core/services/utils/iframe.ts @@ -510,7 +510,7 @@ export class CoreIframeUtilsProvider { if (!CoreSites.isLoggedIn()) { CoreUtils.openInBrowser(link.href); } else { - await CoreSites.getCurrentSite()!.openInBrowserWithAutoLoginIfSameSite(link.href); + await CoreSites.getCurrentSite()?.openInBrowserWithAutoLogin(link.href); } } else if (link.target == '_parent' || link.target == '_top' || link.target == '_blank') { // Opening links with _parent, _top or _blank can break the app. We'll open it in InAppBrowser. diff --git a/src/core/singletons/window.ts b/src/core/singletons/window.ts index 4e5d6891c..d4033f95f 100644 --- a/src/core/singletons/window.ts +++ b/src/core/singletons/window.ts @@ -112,7 +112,7 @@ export class CoreWindow { // Not logged in, cannot auto-login. CoreUtils.openInBrowser(url); } else { - await CoreSites.getRequiredCurrentSite().openInBrowserWithAutoLoginIfSameSite(url); + await CoreSites.getRequiredCurrentSite().openInBrowserWithAutoLogin(url); } } }