Skip to content

Commit d05c11e

Browse files
authored
Merge pull request #4292 from dpalou/MOBILE-4741
Mobile 4741
2 parents 472730e + c8c0edf commit d05c11e

File tree

9 files changed

+99
-63
lines changed

9 files changed

+99
-63
lines changed

src/core/classes/sites/unauthenticated-site.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ export class CoreUnauthenticatedSite {
219219
* @returns URL with params.
220220
*/
221221
createSiteUrl(path: string, params?: Record<string, unknown>, anchor?: string): string {
222-
return CoreUrl.addParamsToUrl(CorePath.concatenatePaths(this.siteUrl, path), params, anchor);
222+
return CoreUrl.addParamsToUrl(CorePath.concatenatePaths(this.siteUrl, path), params, { anchor });
223223
}
224224

225225
/**

src/core/components/iframe/iframe.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { CoreSites } from '@services/sites';
3030
import { toBoolean } from '@/core/transforms/boolean';
3131
import { CoreDom } from '@singletons/dom';
3232
import { CoreAlerts } from '@services/overlays/alerts';
33+
import { CoreLang, CoreLangFormat } from '@services/lang';
3334

3435
@Component({
3536
selector: 'core-iframe',
@@ -234,9 +235,16 @@ export class CoreIframeComponent implements OnChanges, OnDestroy {
234235
this.displayHelp = CoreIframeUtils.shouldDisplayHelpForUrl(url);
235236

236237
const currentSite = CoreSites.getCurrentSite();
237-
if (this.allowAutoLogin && currentSite) {
238-
// Format the URL to add auto-login if needed.
239-
url = await currentSite.getAutoLoginUrl(url, false);
238+
if (currentSite?.containsUrl(url)) {
239+
// Format the URL to add auto-login if needed and add the lang parameter.
240+
const autoLoginUrl = this.allowAutoLogin ?
241+
await currentSite.getAutoLoginUrl(url, false) :
242+
url;
243+
244+
const lang = await CoreLang.getCurrentLanguage(CoreLangFormat.LMS);
245+
url = CoreUrl.addParamsToUrl(autoLoginUrl, { lang }, {
246+
checkAutoLoginUrl: autoLoginUrl !== url,
247+
});
240248
}
241249

242250
if (currentSite?.isVersionGreaterEqualThan('3.7') && CoreUrl.isVimeoVideoUrl(url)) {

src/core/directives/format-text.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import { toBoolean } from '../transforms/boolean';
6060
import { CoreViewer } from '@features/viewer/services/viewer';
6161
import { CorePromiseUtils } from '@singletons/promise-utils';
6262
import { CoreAlerts } from '@services/overlays/alerts';
63+
import { CoreLang, CoreLangFormat } from '@services/lang';
6364

6465
/**
6566
* Directive to format text rendered. It renders the HTML and treats all links and media, using CoreLinkDirective
@@ -833,7 +834,13 @@ export class CoreFormatTextDirective implements OnChanges, OnDestroy, AsyncDirec
833834
// Remove iframe src, otherwise it can cause auto-login issues if there are several iframes with auto-login.
834835
iframe.src = '';
835836

836-
const finalUrl = await CoreIframeUtils.getAutoLoginUrlForIframe(iframe, src);
837+
let finalUrl = await CoreIframeUtils.getAutoLoginUrlForIframe(iframe, src);
838+
839+
const lang = await CoreLang.getCurrentLanguage(CoreLangFormat.LMS);
840+
finalUrl = CoreUrl.addParamsToUrl(finalUrl, { lang }, {
841+
checkAutoLoginUrl: src !== finalUrl,
842+
});
843+
837844
await CoreIframeUtils.fixIframeCookies(finalUrl);
838845

839846
iframe.src = finalUrl;

src/core/features/user/services/support.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import { CoreSubscriptions } from '@singletons/subscriptions';
2424
import { AlertButton } from '@ionic/angular';
2525
import { CoreLang } from '@services/lang';
2626
import { CoreUserNullSupportConfig } from '@features/user/classes/support/null-support-config';
27-
import { CoreOpener } from '@singletons/opener';
2827
import { CoreAlerts } from '@services/overlays/alerts';
2928

3029
/**
@@ -41,10 +40,9 @@ export class CoreUserSupportService {
4140
async contact(options: CoreUserSupportContactOptions = {}): Promise<void> {
4241
const supportConfig = options.supportConfig ?? CoreUserAuthenticatedSupportConfig.forCurrentSite();
4342
const supportPageUrl = supportConfig.getSupportPageUrl();
44-
const autoLoginUrl = await CoreSites.getCurrentSite()?.getAutoLoginUrl(supportPageUrl, false);
45-
const browser = CoreOpener.openInApp(autoLoginUrl ?? supportPageUrl);
43+
const browser = await CoreSites.getCurrentSite()?.openInAppWithAutoLogin(supportPageUrl);
4644

47-
if (supportPageUrl.endsWith('/user/contactsitesupport.php')) {
45+
if (browser && supportPageUrl.endsWith('/user/contactsitesupport.php')) {
4846
this.populateSupportForm(browser, options.subject, options.message);
4947
this.listenSupportFormSubmission(browser, supportConfig.getSupportPageLang());
5048
}
@@ -121,6 +119,10 @@ export class CoreUserSupportService {
121119
* @param lang Language used in the support page.
122120
*/
123121
protected async listenSupportFormSubmission(browser: InAppBrowserObject, lang: string | null): Promise<void> {
122+
if (!CorePlatform.isMobile()) {
123+
return;
124+
}
125+
124126
const appSuccessMessage = Translate.instant('core.user.supportmessagesent');
125127
const lmsSuccessMessage = lang && await CoreLang.getMessage('core.user.supportmessagesent', lang);
126128
const subscription = browser.on('loadstop').subscribe(async () => {

src/core/services/utils/url.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class CoreUrlUtilsProvider {
4646
* @deprecated since 4.5. Use CoreUrl.addParamsToUrl instead.
4747
*/
4848
addParamsToUrl(url: string, params?: Record<string, unknown>, anchor?: string, boolToNumber?: boolean): string {
49-
return CoreUrl.addParamsToUrl(url, params, anchor, boolToNumber);
49+
return CoreUrl.addParamsToUrl(url, params, { anchor, boolToNumber });
5050
}
5151

5252
/**

src/core/singletons/opener.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,14 @@ export class CoreOpener {
190190
}
191191
}
192192

193-
const site = CoreSites.getCurrentSite();
193+
if (CoreSites.getCurrentSite()?.containsUrl(url)) {
194+
url = CoreUrl.addParamsToUrl(url, { lang: await CoreLang.getCurrentLanguage(CoreLangFormat.LMS) }, {
195+
checkAutoLoginUrl: options.originalUrl !== url,
196+
});
197+
}
198+
194199
CoreAnalytics.logEvent({ type: CoreAnalyticsEventType.OPEN_LINK, link: originaUrl });
195-
window.open(
196-
site?.containsUrl(url)
197-
? CoreUrl.addParamsToUrl(url, { lang: await CoreLang.getCurrentLanguage(CoreLangFormat.LMS) })
198-
: url,
199-
'_system',
200-
);
200+
window.open(url, '_system');
201201
}
202202

203203
/**
@@ -309,6 +309,12 @@ export class CoreOpener {
309309

310310
CoreOpener.setInAppBrowserToolbarColors(options);
311311

312+
if (CoreSites.getCurrentSite()?.containsUrl(url)) {
313+
url = CoreUrl.addParamsToUrl(url, { lang: CoreLang.getCurrentLanguageSync(CoreLangFormat.LMS) }, {
314+
checkAutoLoginUrl: options.originalUrl !== url,
315+
});
316+
}
317+
312318
CoreOpener.iabInstance = InAppBrowser.create(url, '_blank', options);
313319

314320
if (CorePlatform.isMobile()) {

src/core/singletons/tests/url.test.ts

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -83,55 +83,50 @@ describe('CoreUrl singleton', () => {
8383
expect(url).toEqual('https://moodle.org');
8484
});
8585

86-
it('adds params to URL without params', () => {
87-
const originalUrl = 'https://moodle.org';
88-
const params = {
86+
it('adds params and anchors to URLs', () => {
87+
// Add params to a URL without params.
88+
expect(CoreUrl.addParamsToUrl('https://moodle.org', {
8989
first: '1',
9090
second: '2',
91-
};
92-
const url = CoreUrl.addParamsToUrl(originalUrl, params);
93-
94-
expect(url).toEqual('https://moodle.org?first=1&second=2');
95-
});
91+
})).toEqual('https://moodle.org?first=1&second=2');
9692

97-
it('adds params to URL with existing params', () => {
98-
const originalUrl = 'https://moodle.org?existing=1';
99-
const params = {
93+
// Add params to a URL with existing params.
94+
expect(CoreUrl.addParamsToUrl('https://moodle.org?existing=1', {
10095
first: '1',
10196
second: '2',
102-
};
103-
const url = CoreUrl.addParamsToUrl(originalUrl, params);
104-
105-
expect(url).toEqual('https://moodle.org?existing=1&first=1&second=2');
106-
});
107-
108-
it('doesn\'t change URL if no params supplied', () => {
109-
const originalUrl = 'https://moodle.org';
110-
const url = CoreUrl.addParamsToUrl(originalUrl);
97+
})).toEqual('https://moodle.org?existing=1&first=1&second=2');
11198

112-
expect(url).toEqual(originalUrl);
113-
});
99+
// No params supplied.
100+
expect(CoreUrl.addParamsToUrl('https://moodle.org')).toEqual('https://moodle.org');
114101

115-
it('doesn\'t add undefined or null params', () => {
116-
const originalUrl = 'https://moodle.org';
117-
const url = CoreUrl.addParamsToUrl(originalUrl, {
102+
// Undefined or null params aren't added.
103+
expect(CoreUrl.addParamsToUrl('https://moodle.org', {
118104
foo: undefined,
119105
bar: null,
120106
baz: 1,
121-
});
107+
})).toEqual('https://moodle.org?baz=1');
122108

123-
expect(url).toEqual('https://moodle.org?baz=1');
124-
});
109+
// Adds anchor to URL.
110+
expect(CoreUrl.addParamsToUrl('https://moodle.org', {
111+
first: '1',
112+
second: '2',
113+
}, {
114+
anchor: 'myanchor',
115+
})).toEqual('https://moodle.org?first=1&second=2#myanchor');
125116

126-
it('adds anchor to URL', () => {
127-
const originalUrl = 'https://moodle.org';
128-
const params = {
117+
// Adds params to the urltogo in case it's an auto-login URL.
118+
expect(CoreUrl.addParamsToUrl('https://mysite.com/autologin.php?urltogo=https%3A%2F%2Fmoodle.org', {
129119
first: '1',
130120
second: '2',
131-
};
132-
const url = CoreUrl.addParamsToUrl(originalUrl, params, 'myanchor');
121+
}, {
122+
checkAutoLoginUrl: true,
123+
})).toEqual('https://mysite.com/autologin.php?urltogo=https%3A%2F%2Fmoodle.org%3Ffirst%3D1%26second%3D2');
133124

134-
expect(url).toEqual('https://moodle.org?first=1&second=2#myanchor');
125+
// Adds params to the base URL even if it has urltogo if checkAutoLoginUrl is not set.
126+
expect(CoreUrl.addParamsToUrl('https://mysite.com/autologin.php?urltogo=https%3A%2F%2Fmoodle.org', {
127+
first: '1',
128+
second: '2',
129+
})).toEqual('https://mysite.com/autologin.php?urltogo=https%3A%2F%2Fmoodle.org&first=1&second=2');
135130
});
136131

137132
it('parses standard urls', () => {

src/core/singletons/url.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -403,28 +403,31 @@ export class CoreUrl {
403403
*
404404
* @param url URL to add the params to.
405405
* @param params Object with the params to add.
406-
* @param anchor Anchor text if needed.
407-
* @param boolToNumber Whether to convert bools to 1 or 0.
406+
* @param options Other options.
408407
* @returns URL with params.
409408
*/
410-
static addParamsToUrl(url: string, params?: Record<string, unknown>, anchor?: string, boolToNumber?: boolean): string {
409+
static addParamsToUrl(url: string, params?: Record<string, unknown>, options: CoreUrlAddParamsOptions = {}): string {
410+
// If it's an auto-login URL, add the params to the urltogo. extractUrlParams returns the urltogo already decoded.
411+
const urlParams = options.checkAutoLoginUrl ? CoreUrl.extractUrlParams(url) : undefined;
412+
let urlToTreat = urlParams?.urltogo ?? url;
413+
411414
// Remove any existing anchor to add the params before it.
412-
const urlAndAnchor = url.split('#');
413-
url = urlAndAnchor[0];
415+
const urlAndAnchor = urlToTreat.split('#');
416+
urlToTreat = urlAndAnchor[0];
414417

415-
let separator = url.indexOf('?') !== -1 ? '&' : '?';
418+
let separator = urlToTreat.indexOf('?') !== -1 ? '&' : '?';
416419

417420
for (const key in params) {
418421
let value = params[key];
419422

420-
if (boolToNumber && typeof value === 'boolean') {
423+
if (options.boolToNumber && typeof value === 'boolean') {
421424
// Convert booleans to 1 or 0.
422425
value = value ? '1' : '0';
423426
}
424427

425428
// Ignore objects and undefined.
426429
if (typeof value !== 'object' && value !== undefined) {
427-
url += separator + key + '=' + value;
430+
urlToTreat += separator + key + '=' + value;
428431
separator = '&';
429432
}
430433
}
@@ -435,14 +438,19 @@ export class CoreUrl {
435438
urlAndAnchor.shift();
436439

437440
// Use a join in case there is more than one #.
438-
url += '#' + urlAndAnchor.join('#');
441+
urlToTreat += '#' + urlAndAnchor.join('#');
439442
}
440443

441-
if (anchor) {
442-
url += '#' + anchor;
444+
if (options.anchor) {
445+
urlToTreat += '#' + options.anchor;
443446
}
444447

445-
return url;
448+
if (!urlParams?.urltogo) {
449+
return urlToTreat;
450+
}
451+
452+
// Replace the urltogo with the treated one.
453+
return url.replace(encodeURIComponent(urlParams.urltogo), encodeURIComponent(urlToTreat));
446454
}
447455

448456
/**
@@ -997,3 +1005,12 @@ export class CoreUrl {
9971005
}
9981006

9991007
export type CoreUrlParams = {[key: string]: string};
1008+
1009+
/**
1010+
* Options for addParamsToUrl.
1011+
*/
1012+
export type CoreUrlAddParamsOptions = {
1013+
anchor?: string; // Anchor text if needed.
1014+
boolToNumber?: boolean; // Whether to convert bools to 1 / 0.
1015+
checkAutoLoginUrl?: boolean; // Whether the URL could be an auto-login URL. If so, any param will be added to the urltogo.
1016+
};

upgrade.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ For more information about upgrading, read the official documentation: https://m
77
- The logout process has been refactored, now it uses a logout page to trigger Angular guards. CoreSites.logout now uses this process, and CoreSites.logoutForRedirect is deprecated and shouldn't be used anymore.
88
- The parameters of treatDownloadedFile of plugin file handlers have changed. Now the third parameter is an object with all the optional parameters.
99
- Some CoreColors functions have been refactored to handle alpha and to validate colors.
10+
- The parameters of CoreUrl.addParamsToUrl have changed. Now the third parameter is an object with all the optional parameters.
1011

1112
=== 4.5.0 ===
1213

0 commit comments

Comments
 (0)