Merge pull request #1350 from dpalou/MOBILE-2434

MOBILE-2434 login: Handle old Moodle sites
main
Juan Leyva 2018-06-19 16:35:45 +02:00 committed by GitHub
commit e19cd8a9ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 50 additions and 6 deletions

View File

@ -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",

View File

@ -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();
});
});

View File

@ -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'));
});

View File

@ -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;
}
}

View File

@ -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<any>} Promise resolved when done.
*/
protected logoutLegacySites(): Promise<any> {
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);
});
}
}