2017-09-15 12:51:29 +02:00
|
|
|
var ManageAccountDialog = {};
|
2018-03-04 11:55:47 +01:00
|
|
|
|
|
|
|
ManageAccountDialog.firstLogin = true;
|
2018-01-02 18:32:44 +01:00
|
|
|
|
2020-06-19 04:56:07 +02:00
|
|
|
/**
|
|
|
|
* Displays the logged in page if the user is logged in,
|
|
|
|
* displays the login page otherwise.
|
|
|
|
*/
|
|
|
|
ManageAccountDialog.display = function (logged_in_username, onSuccess) {
|
2020-06-16 04:08:36 +02:00
|
|
|
if (logged_in_username == null) {
|
2020-06-19 04:56:07 +02:00
|
|
|
if (ManageAccountDialog.firstLogin) {
|
|
|
|
ManageAccountDialog.firstLogin = false;
|
|
|
|
ManageAccountDialog.tryLoginWithCookies(onSuccess);
|
|
|
|
} else {
|
|
|
|
ManageAccountDialog.displayPasswordLogin(onSuccess);
|
|
|
|
}
|
2020-01-06 11:40:02 +01:00
|
|
|
} else {
|
2020-06-19 04:56:07 +02:00
|
|
|
ManageAccountDialog.displayLoggedIn(logged_in_username);
|
2020-01-06 11:40:02 +01:00
|
|
|
}
|
2020-06-16 04:08:36 +02:00
|
|
|
};
|
|
|
|
|
2020-06-19 04:56:07 +02:00
|
|
|
|
|
|
|
ManageAccountDialog.tryLoginWithCookies = function (onSuccess) {
|
|
|
|
// In the first login, we try logging in with cookies,
|
|
|
|
// the backend may take a while to tell if the cookies are valid or not.
|
|
|
|
// So we need to call DialogSystem.showBusy to inform the user that
|
|
|
|
// OpenRefine is trying connecting to Wikidata.
|
|
|
|
const discardWaiter = DialogSystem.showBusy($.i18n('wikidata-account/connecting-to-wikidata'));
|
2020-06-16 04:08:36 +02:00
|
|
|
Refine.postCSRF(
|
|
|
|
"command/wikidata/login",
|
|
|
|
{},
|
|
|
|
function (data) {
|
2020-06-19 04:56:07 +02:00
|
|
|
discardWaiter();
|
2020-06-16 04:08:36 +02:00
|
|
|
if (data.logged_in) {
|
2020-06-19 04:56:07 +02:00
|
|
|
onSuccess(data.username);
|
|
|
|
ManageAccountDialog.displayLoggedIn(data.username);
|
2020-06-16 04:08:36 +02:00
|
|
|
} else {
|
2020-06-19 04:56:07 +02:00
|
|
|
ManageAccountDialog.displayPasswordLogin(onSuccess);
|
2020-06-16 04:08:36 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2018-02-23 15:57:16 +01:00
|
|
|
|
2020-06-16 04:08:36 +02:00
|
|
|
ManageAccountDialog.initCommon = function (elmts) {
|
|
|
|
elmts.dialogHeader.text($.i18n('wikidata-account/dialog-header'));
|
|
|
|
elmts.explainLogIn.html($.i18n('wikidata-account/explain-log-in'));
|
|
|
|
elmts.cancelButton.text($.i18n('wikidata-account/close'));
|
|
|
|
};
|
2017-09-15 12:51:29 +02:00
|
|
|
|
2020-06-19 04:56:07 +02:00
|
|
|
ManageAccountDialog.displayLoggedIn = function (logged_in_username) {
|
2020-06-16 04:08:36 +02:00
|
|
|
var frame = $(DOM.loadHTML("wikidata", "scripts/dialogs/logged-in-dialog.html"));
|
|
|
|
var elmts = DOM.bind(frame);
|
|
|
|
ManageAccountDialog.initCommon(elmts);
|
|
|
|
elmts.loggedInAs.text($.i18n('wikidata-account/logged-in-as'));
|
|
|
|
elmts.logoutButton.text($.i18n('wikidata-account/log-out'));
|
|
|
|
|
|
|
|
var level = DialogSystem.showDialog(frame);
|
|
|
|
var dismiss = function () {
|
|
|
|
DialogSystem.dismissUntil(level - 1);
|
2017-09-15 12:51:29 +02:00
|
|
|
};
|
|
|
|
|
2018-03-04 11:55:47 +01:00
|
|
|
elmts.loggedInUsername
|
2020-06-16 04:08:36 +02:00
|
|
|
.text(logged_in_username)
|
|
|
|
.attr('href', 'https://www.wikidata.org/wiki/User:' + logged_in_username);
|
|
|
|
|
|
|
|
elmts.cancelButton.click(function (e) {
|
|
|
|
dismiss();
|
|
|
|
});
|
2019-12-21 00:51:43 +01:00
|
|
|
|
2020-06-16 04:08:36 +02:00
|
|
|
elmts.logoutButton.click(function () {
|
|
|
|
frame.hide();
|
|
|
|
Refine.postCSRF(
|
|
|
|
"command/wikidata/login",
|
|
|
|
"logout=true",
|
|
|
|
function (data) {
|
|
|
|
frame.show();
|
|
|
|
if (!data.logged_in) {
|
|
|
|
dismiss();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-06-19 04:56:07 +02:00
|
|
|
ManageAccountDialog.displayPasswordLogin = function (onSuccess) {
|
2020-06-16 04:08:36 +02:00
|
|
|
const frame = $(DOM.loadHTML("wikidata", "scripts/dialogs/password-login-dialog.html"));
|
|
|
|
const elmts = DOM.bind(frame);
|
|
|
|
ManageAccountDialog.initCommon(elmts);
|
|
|
|
elmts.explainOwnerOnlyConsumerLogin.html($.i18n('wikidata-account/explain-owner-only-consumer-login'));
|
|
|
|
elmts.invalidCredentials.text($.i18n('wikidata-account/invalid-credentials'));
|
|
|
|
elmts.invalidCredentials.hide();
|
|
|
|
elmts.usernameLabel.text($.i18n('wikidata-account/username-label'));
|
|
|
|
elmts.usernameInput.attr("placeholder", $.i18n('wikidata-account/username-placeholder'));
|
|
|
|
elmts.passwordLabel.text($.i18n('wikidata-account/password-label'));
|
|
|
|
elmts.passwordInput.attr("placeholder", $.i18n('wikidata-account/password-placeholder'));
|
|
|
|
elmts.rememberMe.text($.i18n('wikidata-account/remember-me'));
|
|
|
|
elmts.passwordRememberMeTitle.attr("title", $.i18n('wikidata-account/password-remember-me-title'));
|
|
|
|
elmts.loginButton.text($.i18n('wikidata-account/log-in'));
|
|
|
|
elmts.usernameInput.focus();
|
|
|
|
|
|
|
|
var level = DialogSystem.showDialog(frame);
|
|
|
|
var dismiss = function () {
|
|
|
|
DialogSystem.dismissUntil(level - 1);
|
|
|
|
};
|
|
|
|
|
|
|
|
elmts.cancelButton.click(function (e) {
|
|
|
|
dismiss();
|
2020-01-06 11:40:02 +01:00
|
|
|
});
|
2020-06-16 04:08:36 +02:00
|
|
|
|
|
|
|
elmts.explainOwnerOnlyConsumerLogin.click(function (e) {
|
|
|
|
dismiss();
|
2020-06-19 04:56:07 +02:00
|
|
|
ManageAccountDialog.displayOwnerOnlyConsumerLogin(onSuccess);
|
2017-09-15 12:51:29 +02:00
|
|
|
});
|
|
|
|
|
2020-06-16 04:08:36 +02:00
|
|
|
elmts.loginForm.submit(function (e) {
|
|
|
|
frame.hide();
|
|
|
|
Refine.postCSRF(
|
2019-12-22 15:26:39 +01:00
|
|
|
"command/wikidata/login",
|
|
|
|
elmts.loginForm.serialize(),
|
2020-06-16 04:08:36 +02:00
|
|
|
function (data) {
|
2019-12-22 15:26:39 +01:00
|
|
|
if (data.logged_in) {
|
|
|
|
dismiss();
|
2020-06-19 04:56:07 +02:00
|
|
|
onSuccess(data.username);
|
2020-06-16 04:08:36 +02:00
|
|
|
} else {
|
|
|
|
frame.show();
|
|
|
|
elmts.invalidCredentials.show();
|
2019-12-22 15:26:39 +01:00
|
|
|
}
|
2020-06-16 04:08:36 +02:00
|
|
|
});
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-06-19 04:56:07 +02:00
|
|
|
ManageAccountDialog.displayOwnerOnlyConsumerLogin = function (onSuccess) {
|
2020-06-16 04:08:36 +02:00
|
|
|
var frame = $(DOM.loadHTML("wikidata", "scripts/dialogs/owner-only-consumer-login-dialog.html"));
|
|
|
|
var elmts = DOM.bind(frame);
|
|
|
|
ManageAccountDialog.initCommon(elmts);
|
|
|
|
elmts.explainOwnerOnlyConsumerWiki.html($.i18n('wikidata-account/explain-owner-only-consumer-wiki'));
|
|
|
|
elmts.explainPasswordLogin.html($.i18n('wikidata-account/explain-password-login'));
|
|
|
|
elmts.invalidCredentials.text($.i18n('wikidata-account/invalid-credentials'));
|
|
|
|
elmts.invalidCredentials.hide();
|
|
|
|
elmts.consumerTokenLabel.text($.i18n('wikidata-account/consumer-token-label'));
|
|
|
|
elmts.consumerTokenInput.attr("placeholder", $.i18n('wikidata-account/consumer-token-placeholder'));
|
|
|
|
elmts.consumerSecretLabel.text($.i18n('wikidata-account/consumer-secret-label'));
|
|
|
|
elmts.consumerSecretInput.attr("placeholder", $.i18n('wikidata-account/consumer-secret-placeholder'));
|
|
|
|
elmts.accessTokenLabel.text($.i18n('wikidata-account/access-token-label'));
|
|
|
|
elmts.accessTokenInput.attr("placeholder", $.i18n('wikidata-account/access-token-placeholder'));
|
|
|
|
elmts.accessSecretLabel.text($.i18n('wikidata-account/access-secret-label'));
|
|
|
|
elmts.accessSecretInput.attr("placeholder", $.i18n('wikidata-account/access-secret-placeholder'));
|
|
|
|
elmts.rememberMe.text($.i18n('wikidata-account/remember-me'));
|
|
|
|
elmts.ownerOnlyConsumerRememberMeTitle.attr("title", $.i18n('wikidata-account/owner-only-consumer-remember-me-title'));
|
|
|
|
elmts.loginButton.text($.i18n('wikidata-account/log-in'));
|
|
|
|
elmts.consumerTokenInput.focus();
|
|
|
|
|
|
|
|
var level = DialogSystem.showDialog(frame);
|
|
|
|
var dismiss = function () {
|
|
|
|
DialogSystem.dismissUntil(level - 1);
|
|
|
|
};
|
|
|
|
|
|
|
|
elmts.cancelButton.click(function (e) {
|
|
|
|
dismiss();
|
|
|
|
});
|
|
|
|
|
|
|
|
elmts.explainPasswordLogin.click(function (e) {
|
|
|
|
dismiss();
|
2020-06-19 04:56:07 +02:00
|
|
|
ManageAccountDialog.displayPasswordLogin(onSuccess);
|
2020-06-16 04:08:36 +02:00
|
|
|
});
|
2019-12-21 00:51:43 +01:00
|
|
|
|
2020-06-16 04:08:36 +02:00
|
|
|
elmts.loginForm.submit(function (e) {
|
|
|
|
frame.hide();
|
2019-10-17 11:42:05 +02:00
|
|
|
Refine.postCSRF(
|
2020-06-16 04:08:36 +02:00
|
|
|
"command/wikidata/login",
|
|
|
|
elmts.loginForm.serialize(),
|
|
|
|
function (data) {
|
|
|
|
if (data.logged_in) {
|
|
|
|
dismiss();
|
2020-06-19 04:56:07 +02:00
|
|
|
onSuccess(data.username);
|
2020-06-16 04:08:36 +02:00
|
|
|
} else {
|
|
|
|
frame.show();
|
|
|
|
elmts.invalidCredentials.show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
e.preventDefault();
|
2017-09-15 12:51:29 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-06-19 04:56:07 +02:00
|
|
|
/**
|
|
|
|
* Checks if the user is logged in or not.
|
|
|
|
*
|
|
|
|
* The callback needs to react to both cases.
|
|
|
|
*/
|
2020-06-16 04:08:36 +02:00
|
|
|
ManageAccountDialog.isLoggedIn = function (callback) {
|
|
|
|
$.get(
|
2017-09-15 12:51:29 +02:00
|
|
|
"command/wikidata/login",
|
2020-06-16 04:08:36 +02:00
|
|
|
function (data) {
|
|
|
|
callback(data.username);
|
|
|
|
});
|
2019-12-21 00:51:43 +01:00
|
|
|
};
|
2017-09-15 12:51:29 +02:00
|
|
|
|
2020-06-19 04:56:07 +02:00
|
|
|
/**
|
|
|
|
* The onSuccess callback is called if and only if the user is logged in.
|
|
|
|
*/
|
|
|
|
ManageAccountDialog.ensureLoggedIn = function (onSuccess) {
|
2020-06-16 04:08:36 +02:00
|
|
|
ManageAccountDialog.isLoggedIn(function (logged_in_username) {
|
|
|
|
if (logged_in_username == null) {
|
2020-06-19 04:56:07 +02:00
|
|
|
ManageAccountDialog.display(null, onSuccess);
|
2020-06-16 04:08:36 +02:00
|
|
|
} else {
|
2020-06-19 04:56:07 +02:00
|
|
|
onSuccess(logged_in_username);
|
2020-06-16 04:08:36 +02:00
|
|
|
}
|
|
|
|
});
|
2017-09-15 12:51:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ManageAccountDialog.checkAndLaunch = function () {
|
2020-06-16 04:08:36 +02:00
|
|
|
ManageAccountDialog.isLoggedIn(function (logged_in_username) {
|
|
|
|
ManageAccountDialog.display(logged_in_username, function (success) {
|
|
|
|
});
|
|
|
|
});
|
2017-09-15 12:51:29 +02:00
|
|
|
};
|