Merge pull request #1470 from dpalou/MOBILE-2161

MOBILE-2161 menu: Support auto-login in embedded menu items
main
Juan Leyva 2018-08-22 15:13:38 +01:00 committed by GitHub
commit ca20c78136
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 86 additions and 52 deletions

View File

@ -1221,59 +1221,30 @@ export class CoreSite {
* @return {Promise<InAppBrowserObject|void>} Promise resolved when done. Resolve param is returned only if inApp=true. * @return {Promise<InAppBrowserObject|void>} Promise resolved when done. Resolve param is returned only if inApp=true.
*/ */
openWithAutoLogin(inApp: boolean, url: string, options?: any, alertMessage?: string): Promise<InAppBrowserObject | void> { openWithAutoLogin(inApp: boolean, url: string, options?: any, alertMessage?: string): Promise<InAppBrowserObject | void> {
// Convenience function to open the URL. // Get the URL to open.
const open = (url): Promise<any> => { return this.getAutoLoginUrl(url).then((url) => {
return new Promise<InAppBrowserObject | void>((resolve, reject): void => { if (!alertMessage) {
if (modal) { // Just open the URL.
modal.dismiss(); if (inApp) {
} return this.utils.openInApp(url, options);
if (alertMessage) {
this.domUtils.showAlert(this.translate.instant('core.notice'), alertMessage, undefined, 3000).then((alert) => {
alert.onDidDismiss(() => {
if (inApp) {
resolve(this.utils.openInApp(url, options));
} else {
resolve(this.utils.openInBrowser(url));
}
});
});
} else { } else {
if (inApp) { return this.utils.openInBrowser(url);
resolve(this.utils.openInApp(url, options));
} else {
resolve(this.utils.openInBrowser(url));
}
} }
});
};
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.
// Open the final URL without auto-login.
return Promise.resolve(open(url));
}
const userId = this.getUserId(),
params = {
privatetoken: this.privateToken
},
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, open the final URL without auto-login.
return open(url);
} }
this.lastAutoLogin = this.timeUtils.timestamp(); // Show an alert first.
return this.domUtils.showAlert(this.translate.instant('core.notice'), alertMessage, undefined, 3000).then((alert) => {
return open(data.autologinurl + '?userid=' + userId + '&key=' + data.key + '&urltogo=' + url); return new Promise<InAppBrowserObject | void>((resolve, reject): void => {
}).catch(() => { alert.onDidDismiss(() => {
// Couldn't get autologin key, open the final URL without auto-login. if (inApp) {
return open(url); resolve(this.utils.openInApp(url, options));
} else {
resolve(this.utils.openInBrowser(url));
}
});
});
});
}); });
} }
@ -1459,6 +1430,44 @@ export class CoreSite {
return false; return false;
} }
/**
* Given a URL, convert it to a URL that will auto-login if supported.
*
* @param {string} url The URL to convert.
* @return {Promise<string>} Promise resolved with the converted URL.
*/
getAutoLoginUrl(url: string): 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
},
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;
}
this.lastAutoLogin = this.timeUtils.timestamp();
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();
});
}
/** /**
* Get a version number from a release version. * Get a version number from a release version.
* If release version is valid but not found in the list of Moodle releases, it will use the last released major version. * If release version is valid but not found in the list of Moodle releases, it will use the last released major version.

View File

@ -4,7 +4,7 @@
<p class="core-loading-message" *ngIf="message">{{message}}</p> <p class="core-loading-message" *ngIf="message">{{message}}</p>
</span> </span>
</div> </div>
<div #content> <div #content class="core-loading-content">
<ng-content [@coreShowHideAnimation] *ngIf="hideUntil"> <ng-content [@coreShowHideAnimation] *ngIf="hideUntil">
</ng-content> </ng-content>
</div> </div>

View File

@ -4,5 +4,7 @@
</ion-navbar> </ion-navbar>
</ion-header> </ion-header>
<ion-content> <ion-content>
<core-iframe [src]="url"></core-iframe> <core-loading [hideUntil]="url">
<core-iframe *ngIf="url" [src]="url"></core-iframe>
</core-loading>
</ion-content> </ion-content>

View File

@ -0,0 +1,5 @@
page-core-viewer-iframe {
core-loading .core-loading-content {
height: 100%;
}
}

View File

@ -14,6 +14,7 @@
import { Component } from '@angular/core'; import { Component } from '@angular/core';
import { IonicPage, NavParams } from 'ionic-angular'; import { IonicPage, NavParams } from 'ionic-angular';
import { CoreSitesProvider } from '@providers/sites';
/** /**
* Page to display a URL in an iframe. * Page to display a URL in an iframe.
@ -27,8 +28,25 @@ export class CoreViewerIframePage {
title: string; // Page title. title: string; // Page title.
url: string; // Iframe URL. url: string; // Iframe URL.
constructor(params: NavParams) { protected autoLogin; // 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.
constructor(params: NavParams, sitesProvider: CoreSitesProvider) {
this.title = params.get('title'); this.title = params.get('title');
this.url = params.get('url'); this.autoLogin = params.get('autoLogin') || 'check';
const url = params.get('url'),
currentSite = sitesProvider.getCurrentSite();
if (currentSite && (this.autoLogin == 'yes' || (this.autoLogin == 'check' && currentSite.containsUrl(url)))) {
// Format the URL to add auto-login.
currentSite.getAutoLoginUrl(url).then((url) => {
this.url = url;
});
} else {
this.url = url;
}
} }
} }