diff --git a/src/core/login/lang/en.json b/src/core/login/lang/en.json index faf05a97a..c9031b28d 100644 --- a/src/core/login/lang/en.json +++ b/src/core/login/lang/en.json @@ -32,6 +32,7 @@ "invalidurl": "Invalid URL specified", "invalidvaluemax": "The maximum value is {{$a}}", "invalidvaluemin": "The minimum value is {{$a}}", + "legacymoodleversion": "You are trying to connect to an unsupported Moodle version. Please, download the Moodle Classic app to access this Moodle site.", "localmobileunexpectedresponse": "Moodle Mobile Additional Features check returned an unexpected response, you will be authenticated using the standard Mobile service.", "login": "Log in", "loginbutton": "Log in", diff --git a/src/core/login/pages/reconnect/reconnect.ts b/src/core/login/pages/reconnect/reconnect.ts index 99e1ad423..27c853129 100644 --- a/src/core/login/pages/reconnect/reconnect.ts +++ b/src/core/login/pages/reconnect/reconnect.ts @@ -137,7 +137,7 @@ export class CoreLoginReconnectPage { this.sitesProvider.getUserToken(siteUrl, username, password).then((data) => { return this.sitesProvider.updateSiteToken(this.infoSiteUrl, username, data.token, data.privateToken).then(() => { // Update site info too because functions might have changed (e.g. unisntall local_mobile). - return this.sitesProvider.updateSiteInfoByUrl(this.infoSiteUrl, username).finally(() => { + return this.sitesProvider.updateSiteInfoByUrl(this.infoSiteUrl, username).then(() => { // Reset fields so the data is not in the view anymore. this.credForm.controls['password'].reset(); @@ -148,8 +148,8 @@ export class CoreLoginReconnectPage { return this.loginHelper.goToSiteInitialPage(); } }).catch((error) => { - // Site deleted? Go back to login page. - this.domUtils.showErrorModal('core.login.errorupdatesite', true); + // Error, go back to login page. + this.domUtils.showErrorModalDefault(error, 'core.login.errorupdatesite', true); this.cancel(); }); }); diff --git a/src/core/login/providers/helper.ts b/src/core/login/providers/helper.ts index 942fb37da..ef2460cb0 100644 --- a/src/core/login/providers/helper.ts +++ b/src/core/login/providers/helper.ts @@ -178,8 +178,10 @@ export class CoreLoginHelperProvider { this.goToSiteInitialPage(); } }).catch((errorMessage) => { - if (typeof errorMessage == 'string' && errorMessage != '') { + if (errorMessage) { + // An error occurred, display the error and logout the user. this.domUtils.showErrorModal(errorMessage); + this.sitesProvider.logout(); } }).finally(() => { modal.dismiss(); @@ -436,8 +438,8 @@ export class CoreLoginHelperProvider { const info = this.sitesProvider.getCurrentSite().getInfo(); if (typeof info != 'undefined' && typeof info.username != 'undefined') { return this.sitesProvider.updateSiteToken(info.siteurl, info.username, token, privateToken).then(() => { - this.sitesProvider.updateSiteInfoByUrl(info.siteurl, info.username); - }).catch(() => { + return this.sitesProvider.updateSiteInfoByUrl(info.siteurl, info.username); + }, () => { // Error updating token, return proper error message. return Promise.reject(this.translate.instant('core.login.errorupdatesite')); }); diff --git a/src/providers/sites.ts b/src/providers/sites.ts index 2c087c955..c3e8678bb 100644 --- a/src/providers/sites.ts +++ b/src/providers/sites.ts @@ -1050,6 +1050,11 @@ export class CoreSitesProvider { return site.fetchSiteInfo().then((info) => { site.setInfo(info); + if (this.isLegacyMoodleByInfo(info)) { + // The Moodle version is not supported, reject. + return Promise.reject(this.translate.instant('core.login.legacymoodleversion')); + } + // Try to get the site config. return this.getSiteConfig(site).catch(() => { // Error getting config, keep the current one. @@ -1227,4 +1232,14 @@ export class CoreSitesProvider { return site && site.wsAvailable(method, checkPrefix); } + + /** + * Check if a site is a legacy site by its info. + * + * @param {any} info The site info. + * @return {boolean} Whether it's a legacy Moodle. + */ + isLegacyMoodleByInfo(info: any): boolean { + return this.isValidMoodleVersion(info) == this.LEGACY_APP_VERSION; + } } diff --git a/src/providers/update-manager.ts b/src/providers/update-manager.ts index 744adcd5b..fb451a9f9 100644 --- a/src/providers/update-manager.ts +++ b/src/providers/update-manager.ts @@ -367,6 +367,10 @@ export class CoreUpdateManagerProvider implements CoreInitHandler { // In version 2018 we adapted the forum offline stores to match a new schema. // However, due to the migration of data to SQLite we can no longer do that. + if (versionCode >= 3500 && versionApplied < 3500 && versionApplied > 0) { + promises.push(this.logoutLegacySites()); + } + return Promise.all(promises).then(() => { return this.configProvider.set(this.VERSION_APPLIED, versionCode); }).catch((error) => { @@ -684,4 +688,26 @@ export class CoreUpdateManagerProvider implements CoreInitHandler { }); }); } + + /** + * Logout from legacy sites. + * + * @return {Promise} Promise resolved when done. + */ + protected logoutLegacySites(): Promise { + return this.sitesProvider.getSitesIds().then((siteIds) => { + const promises = []; + + siteIds.forEach((siteId) => { + promises.push(this.sitesProvider.getSite(siteId).then((site) => { + // If the site is a legacy site, mark it as logged out so the user has to authenticate again. + if (this.sitesProvider.isLegacyMoodleByInfo(site.getInfo())) { + return this.sitesProvider.setSiteLoggedOut(site.getId(), true); + } + })); + }); + + return this.utils.allPromises(promises); + }); + } }