Skip to content

Commit 23fa82a

Browse files
committed
MOBILE-1806 autologin: Implement autologin functions
1 parent 816266e commit 23fa82a

File tree

5 files changed

+184
-43
lines changed

5 files changed

+184
-43
lines changed

www/core/components/login/controllers/credentials.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ angular.module('mm.core.login')
121121

122122
// Start the authentication process.
123123
return $mmSitesManager.getUserToken(siteurl, username, password).then(function(data) {
124-
return $mmSitesManager.newSite(data.siteurl, data.token).then(function() {
124+
return $mmSitesManager.newSite(data.siteurl, data.token, data.privatetoken).then(function() {
125125
delete $scope.credentials; // Delete username and password from the scope.
126126
$ionicHistory.nextViewOptions({disableBack: true});
127127

www/core/components/login/controllers/reconnect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ angular.module('mm.core.login')
7070

7171
// Start the authentication process.
7272
$mmSitesManager.getUserToken(siteurl, username, password).then(function(data) {
73-
$mmSitesManager.updateSiteToken(infositeurl, username, data.token).then(function() {
73+
$mmSitesManager.updateSiteToken(infositeurl, username, data.token, data.privatetoken).then(function() {
7474
// Update site info too because functions might have changed (e.g. unisntall local_mobile).
7575
$mmSitesManager.updateSiteInfoByUrl(infositeurl, username).finally(function() {
7676
delete $scope.credentials; // Delete password from the scope.

www/core/components/login/controllers/site.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ angular.module('mm.core.login')
4141
if (sitedata) {
4242
// It's a demo site.
4343
$mmSitesManager.getUserToken(sitedata.url, sitedata.username, sitedata.password).then(function(data) {
44-
$mmSitesManager.newSite(data.siteurl, data.token).then(function() {
44+
$mmSitesManager.newSite(data.siteurl, data.token, data.privatetoken).then(function() {
4545
$ionicHistory.nextViewOptions({disableBack: true});
4646
return $mmLoginHelper.goToSiteInitialPage();
4747
}, function(error) {

www/core/lib/sitesfactory.js

Lines changed: 141 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,19 @@ angular.module('mm.core')
162162
/**
163163
* Site object to store site data.
164164
*
165-
* @param {String} id Site ID.
166-
* @param {String} siteurl Site URL.
167-
* @param {String} token User's token in the site.
168-
* @param {Object} infos Site's info.
165+
* @param {String} id Site ID.
166+
* @param {String} siteurl Site URL.
167+
* @param {String} token User's token in the site.
168+
* @param {Object} infos Site's info.
169+
* @param {String} [privateToken] User's private token.
170+
* @return {Void}
169171
*/
170-
function Site(id, siteurl, token, infos) {
172+
function Site(id, siteurl, token, infos, privateToken) {
171173
this.id = id;
172174
this.siteurl = siteurl;
173175
this.token = token;
174176
this.infos = infos;
177+
this.privateToken = privateToken;
175178

176179
if (this.id) {
177180
this.db = $mmDB.getDB('Site-' + this.id, siteSchema, dboptions);
@@ -214,6 +217,15 @@ angular.module('mm.core')
214217
return this.infos;
215218
};
216219

220+
/**
221+
* Get site private token.
222+
*
223+
* @return {String} Current site private token.
224+
*/
225+
Site.prototype.getPrivateToken = function() {
226+
return this.privateToken;
227+
};
228+
217229
/**
218230
* Get site DB.
219231
*
@@ -255,6 +267,15 @@ angular.module('mm.core')
255267
this.token = token;
256268
};
257269

270+
/**
271+
* Set site private token.
272+
*
273+
* @param {String} privateToken New private token.
274+
*/
275+
Site.prototype.setPrivateToken = function(privateToken) {
276+
this.privateToken = privateToken;
277+
};
278+
258279
/**
259280
* Check if token is already expired using local data.
260281
*
@@ -835,6 +856,113 @@ angular.module('mm.core')
835856
});
836857
};
837858

859+
/**
860+
* Open a URL in browser using auto-login in the Moodle site if available.
861+
*
862+
* @param {String} url The URL to open.
863+
* @return {Promise} Promise resolved when done, rejected otherwise.
864+
*/
865+
Site.prototype.openInBrowserWithAutoLogin = function(url) {
866+
return this.openWithAutoLogin(false, url);
867+
};
868+
869+
/**
870+
* Open a URL in browser using auto-login in the Moodle site if available and the URL belongs to the site.
871+
*
872+
* @param {String} url The URL to open.
873+
* @return {Promise} Promise resolved when done, rejected otherwise.
874+
*/
875+
Site.prototype.openInBrowserWithAutoLoginIfSameSite = function(url) {
876+
return this.openWithAutoLoginIfSameSite(false, url);
877+
};
878+
879+
/**
880+
* Open a URL in inappbrowser using auto-login in the Moodle site if available.
881+
*
882+
* @param {String} url The URL to open.
883+
* @param {Object} options Override default options passed to $cordovaInAppBrowser#open
884+
* @return {Promise} Promise resolved when done, rejected otherwise.
885+
*/
886+
Site.prototype.openInAppWithAutoLogin = function(url, options) {
887+
return this.openWithAutoLogin(true, url, options);
888+
};
889+
890+
/**
891+
* Open a URL in inappbrowser using auto-login in the Moodle site if available and the URL belongs to the site.
892+
*
893+
* @param {String} url The URL to open.
894+
* @param {Object} options Override default options passed to $cordovaInAppBrowser#open
895+
* @return {Promise} Promise resolved when done, rejected otherwise.
896+
*/
897+
Site.prototype.openInAppWithAutoLoginIfSameSite = function(url, options) {
898+
return this.openWithAutoLoginIfSameSite(true, url, options);
899+
};
900+
901+
/**
902+
* Open a URL in browser or InAppBrowser using auto-login in the Moodle site if available.
903+
*
904+
* @param {Boolean} inApp True to open it in InAppBrowser, false to open in browser.
905+
* @param {String} url The URL to open.
906+
* @param {Object} options Override default options passed to $cordovaInAppBrowser#open.
907+
* @return {Promise} Promise resolved when done, rejected otherwise.
908+
*/
909+
Site.prototype.openWithAutoLogin = function(inApp, url, options) {
910+
if (!this.privateToken || !this.wsAvailable('tool_mobile_get_autologin_key')) {
911+
// No private token or WS not available, open the final URL without auto-login.
912+
open(url);
913+
return $q.when();
914+
}
915+
916+
var userId = this.getUserId(),
917+
params = {
918+
privatetoken: this.privateToken
919+
},
920+
modal = $mmUtil.showModalLoading();
921+
922+
// Use write to not use cache.
923+
return this.write('tool_mobile_get_autologin_key', params).then(function(data) {
924+
if (!data.autologinurl || !data.key) {
925+
// Not valid data, open the final URL without auto-login.
926+
open(url);
927+
return;
928+
}
929+
930+
open(data.autologinurl + '?userid=' + userId + '&key=' + data.key + '&urltogo=' + url);
931+
}).catch(function() {
932+
// Couldn't get autologin key, open the final URL without auto-login.
933+
open(url);
934+
}).finally(function() {
935+
modal.dismiss();
936+
});
937+
938+
function open(url) {
939+
if (inApp) {
940+
$mmUtil.openInApp(url, options);
941+
} else {
942+
$mmUtil.openInBrowser(url);
943+
}
944+
}
945+
};
946+
947+
/**
948+
* Open a URL in browser or InAppBrowser using auto-login in the Moodle site if available and the URL belongs to the site.
949+
*
950+
* @param {String} url The URL to open.
951+
* @return {Promise} Promise resolved when done, rejected otherwise.
952+
*/
953+
Site.prototype.openWithAutoLoginIfSameSite = function(inApp, url, options) {
954+
if (this.containsUrl(url)) {
955+
return this.openWithAutoLogin(inApp, url, options);
956+
} else {
957+
if (inApp) {
958+
$mmUtil.openInApp(url, options);
959+
} else {
960+
$mmUtil.openInBrowser(url);
961+
}
962+
return $q.when();
963+
}
964+
};
965+
838966
/**
839967
* Invalidate entries from the cache.
840968
*
@@ -994,16 +1122,17 @@ angular.module('mm.core')
9941122
* @module mm.core
9951123
* @ngdoc method
9961124
* @name $mmSitesFactory#makeSite
997-
* @param {String} id Site ID.
998-
* @param {String} siteurl Site URL.
999-
* @param {String} token User's token in the site.
1000-
* @param {Object} infos Site's info.
1001-
* @return {Object} The current site object.
1125+
* @param {String} id Site ID.
1126+
* @param {String} siteurl Site URL.
1127+
* @param {String} token User's token in the site.
1128+
* @param {Object} infos Site's info.
1129+
* @param {String} [privateToken] User's private token.
1130+
* @return {Object} The current site object.
10021131
* @description
10031132
* This returns a site object.
10041133
*/
1005-
self.makeSite = function(id, siteurl, token, infos) {
1006-
return new Site(id, siteurl, token, infos);
1134+
self.makeSite = function(id, siteurl, token, infos, privateToken) {
1135+
return new Site(id, siteurl, token, infos, privateToken);
10071136
};
10081137

10091138
/**

www/core/lib/sitesmanager.js

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ angular.module('mm.core')
251251
* @param {String} [service] Service to use. If not defined, it will be searched in memory.
252252
* @param {Boolean} retry We are retrying with a prefixed URL.
253253
* @return {Promise} A promise to be resolved when the token is retrieved. If success, returns an object
254-
* with the token and the siteurl to use.
254+
* with the token, private token and the siteurl to use.
255255
*/
256256
self.getUserToken = function(siteurl, username, password, service, retry) {
257257
retry = retry || false;
@@ -278,7 +278,7 @@ angular.module('mm.core')
278278
return $mmLang.translateAndReject('mm.core.cannotconnect');
279279
} else {
280280
if (typeof data.token != 'undefined') {
281-
return {token: data.token, siteurl: siteurl};
281+
return {token: data.token, siteurl: siteurl, privatetoken: data.privatetoken};
282282
} else {
283283
if (typeof data.error != 'undefined') {
284284
// We only allow one retry (to avoid loops).
@@ -306,21 +306,23 @@ angular.module('mm.core')
306306
* @module mm.core
307307
* @ngdoc method
308308
* @name $mmSitesManager#newSite
309-
* @param {String} siteurl The site url.
310-
* @param {String} token User's token.
311-
* @return {Promise} A promise to be resolved when the site is added and the user is authenticated.
309+
* @param {String} siteurl The site url.
310+
* @param {String} token User's token.
311+
* @param {String} [privateToken] User's private token.
312+
* @return {Promise} A promise to be resolved when the site is added and the user is authenticated.
312313
*/
313-
self.newSite = function(siteurl, token) {
314+
self.newSite = function(siteurl, token, privateToken) {
315+
privateToken = privateToken || '';
314316

315-
var candidateSite = $mmSitesFactory.makeSite(undefined, siteurl, token);
317+
var candidateSite = $mmSitesFactory.makeSite(undefined, siteurl, token, undefined, privateToken);
316318

317319
return candidateSite.fetchSiteInfo().then(function(infos) {
318320
if (isValidMoodleVersion(infos)) {
319321
var validation = validateSiteInfo(infos);
320322
if (validation === true) {
321323
var siteid = self.createSiteID(infos.siteurl, infos.username);
322324
// Add site to sites list.
323-
self.addSite(siteid, siteurl, token, infos);
325+
self.addSite(siteid, siteurl, token, infos, privateToken);
324326
// Turn candidate site into current site.
325327
candidateSite.setId(siteid);
326328
candidateSite.setInfo(infos);
@@ -439,17 +441,21 @@ angular.module('mm.core')
439441
* @module mm.core
440442
* @ngdoc method
441443
* @name $mmSitesManager#addSite
442-
* @param {String} id Site ID.
443-
* @param {String} siteurl Site URL.
444-
* @param {String} token User's token in the site.
445-
* @param {Object} infos Site's info.
444+
* @param {String} id Site ID.
445+
* @param {String} siteurl Site URL.
446+
* @param {String} token User's token in the site.
447+
* @param {Object} infos Site's info.
448+
* @param {String} [privateToken] User's private token.
449+
* @return {Promise} Promise resolved when done.
446450
*/
447-
self.addSite = function(id, siteurl, token, infos) {
451+
self.addSite = function(id, siteurl, token, infos, privateToken) {
452+
privateToken = privateToken || '';
448453
return $mmApp.getDB().insert(mmCoreSitesStore, {
449454
id: id,
450455
siteurl: siteurl,
451456
token: token,
452-
infos: infos
457+
infos: infos,
458+
privatetoken: privateToken
453459
});
454460
};
455461

@@ -590,7 +596,7 @@ angular.module('mm.core')
590596
return $q.when(sites[siteId]);
591597
} else {
592598
return $mmApp.getDB().get(mmCoreSitesStore, siteId).then(function(data) {
593-
var site = $mmSitesFactory.makeSite(siteId, data.siteurl, data.token, data.infos);
599+
var site = $mmSitesFactory.makeSite(siteId, data.siteurl, data.token, data.infos, data.privatetoken);
594600
sites[siteId] = site;
595601
return site;
596602
});
@@ -740,14 +746,15 @@ angular.module('mm.core')
740746
* @module mm.core
741747
* @ngdoc method
742748
* @name $mmSitesManager#updateSiteToken
743-
* @param {String} siteUrl Site's URL.
744-
* @param {String} username Username.
745-
* @param {String} token User's new token.
746-
* @return {Promise} A promise to be resolved when the site is updated.
749+
* @param {String} siteUrl Site's URL.
750+
* @param {String} username Username.
751+
* @param {String} token User's new token.
752+
* @param {String} [privateToken] User's private token.
753+
* @return {Promise} A promise to be resolved when the site is updated.
747754
*/
748-
self.updateSiteToken = function(siteUrl, username, token) {
755+
self.updateSiteToken = function(siteUrl, username, token, privateToken) {
749756
var siteId = self.createSiteID(siteUrl, username);
750-
return self.updateSiteTokenBySiteId(siteId, token);
757+
return self.updateSiteTokenBySiteId(siteId, token, privateToken);
751758
};
752759

753760
/**
@@ -756,19 +763,23 @@ angular.module('mm.core')
756763
* @module mm.core
757764
* @ngdoc method
758765
* @name $mmSitesManager#updateSiteTokenBySiteId
759-
* @param {String} siteId Site Id.
760-
* @param {String} token User's new token.
761-
* @return {Promise} A promise to be resolved when the site is updated.
766+
* @param {String} siteId Site Id.
767+
* @param {String} token User's new token.
768+
* @param {String} [privateToken] User's private token.
769+
* @return {Promise} A promise to be resolved when the site is updated.
762770
*/
763-
self.updateSiteTokenBySiteId = function(siteId, token) {
771+
self.updateSiteTokenBySiteId = function(siteId, token, privateToken) {
772+
privateToken = privateToken || '';
764773
return self.getSite(siteId).then(function(site) {
765774
site.token = token;
775+
site.privatetoken = privateToken;
766776

767777
return $mmApp.getDB().insert(mmCoreSitesStore, {
768778
id: siteId,
769779
siteurl: site.getURL(),
770780
token: token,
771-
infos: site.getInfo()
781+
infos: site.getInfo(),
782+
privatetoken: privateToken
772783
});
773784
});
774785
};
@@ -790,7 +801,8 @@ angular.module('mm.core')
790801
id: siteid,
791802
siteurl: site.getURL(),
792803
token: site.getToken(),
793-
infos: infos
804+
infos: infos,
805+
privatetoken: site.getPrivateToken()
794806
}).finally(function() {
795807
$mmEvents.trigger(mmCoreEventSiteUpdated, siteid);
796808
});
@@ -854,7 +866,7 @@ angular.module('mm.core')
854866
var ids = [];
855867
angular.forEach(sites, function(site) {
856868
if (!sites[site.id]) {
857-
sites[site.id] = $mmSitesFactory.makeSite(site.id, site.siteurl, site.token, site.infos);
869+
sites[site.id] = $mmSitesFactory.makeSite(site.id, site.siteurl, site.token, site.infos, site.privatetoken);
858870
}
859871
if (sites[site.id].containsUrl(url)) {
860872
if (!username || sites[site.id].getInfo().username == username) {

0 commit comments

Comments
 (0)