Files
tryton/src/session.js
T

260 lines
8.7 KiB
JavaScript
Raw Normal View History

2012-11-29 11:32:58 +01:00
/* This file is part of Tryton. The COPYRIGHT file at the top level of
this repository contains the full copyright notices and license terms. */
2013-01-04 21:29:48 +01:00
(function() {
'use strict';
2012-11-29 11:32:58 +01:00
2013-01-04 21:29:48 +01:00
Sao.Session = Sao.class_(Object, {
2015-03-06 09:33:03 +01:00
init: function(database, login) {
this.user_id = null;
this.session = null;
this.database = database;
this.login = login;
2013-05-10 10:35:42 +02:00
this.context = {};
2013-01-04 21:29:48 +01:00
if (!Sao.Session.current_session) {
Sao.Session.current_session = this;
}
},
do_login: function(login, password) {
var dfd = jQuery.Deferred();
var args = {
'method': 'common.db.login',
'params': [login, password]
};
var ajax_prm = jQuery.ajax({
'contentType': 'application/json',
'data': JSON.stringify(args),
'dataType': 'json',
'url': '/' + this.database,
'type': 'post'
});
var ajax_success = function(data) {
if (data === null) {
2015-05-29 10:44:10 +02:00
Sao.common.warning.run('',
Sao.i18n.gettext('Unable to reach the server.'));
2013-01-04 21:29:48 +01:00
dfd.reject();
} else if (data.error) {
2015-03-06 14:24:55 +01:00
Sao.common.error.run(data.error[0], data.error[1]);
2013-01-04 21:29:48 +01:00
dfd.reject();
} else {
if (!data.result) {
this.user_id = null;
this.session = null;
2015-03-06 14:24:55 +01:00
dfd.reject();
2013-01-04 21:29:48 +01:00
} else {
this.user_id = data.result[0];
this.session = data.result[1];
2015-03-06 14:24:55 +01:00
dfd.resolve();
2013-01-04 21:29:48 +01:00
}
}
};
ajax_prm.success(ajax_success.bind(this));
ajax_prm.error(dfd.reject);
return dfd.promise();
},
do_logout: function() {
if (!(this.user_id && this.session)) {
2015-03-06 14:24:55 +01:00
return jQuery.when();
2013-01-04 21:29:48 +01:00
}
var args = {
'method': 'common.db.logout',
'params': []
};
2013-05-06 16:20:01 +02:00
var prm = Sao.rpc(args, this);
2013-01-04 21:29:48 +01:00
this.database = null;
this.login = null;
this.user_id = null;
this.session = null;
return prm;
2013-05-10 10:35:42 +02:00
},
reload_context: function() {
var args = {
'method': 'model.res.user.get_preferences',
'params': [true, {}]
};
2014-04-12 23:41:39 +02:00
// Call with custom session to not send context
2014-05-19 21:08:13 +02:00
var session = jQuery.extend({}, this);
2014-04-12 23:41:39 +02:00
session.context = {};
var prm = Sao.rpc(args, session);
2013-05-10 10:35:42 +02:00
return prm.then(function(context) {
this.context = context;
}.bind(this));
2012-11-29 15:58:22 +01:00
}
2013-01-04 21:29:48 +01:00
});
2015-05-29 10:44:10 +02:00
Sao.Session.login_dialog = function() {
var dialog = new Sao.Dialog(Sao.i18n.gettext('Login'), 'lg');
dialog.database_select = jQuery('<select/>', {
'class': 'form-control',
'id': 'login-database'
});
dialog.login_input = jQuery('<input/>', {
'class': 'form-control',
'id': 'login-login',
'placeholder': Sao.i18n.gettext('Login')
});
dialog.password_input = jQuery('<input/>', {
'class': 'form-control',
'type': 'password',
'id': 'login-password',
'placeholder': Sao.i18n.gettext('Password')
});
dialog.body.append(jQuery('<div/>', {
'class': 'form-group'
}).append(jQuery('<label/>', {
'for': 'login-database'
}).append(Sao.i18n.gettext('Database')))
.append(dialog.database_select)
).append(jQuery('<div/>', {
'class': 'form-group'
}).append(jQuery('<label/>', {
'for': 'login-login'
}).append(Sao.i18n.gettext('Login')))
.append(dialog.login_input)
).append(jQuery('<div/>', {
'class': 'form-group'
}).append(jQuery('<label/>', {
'for': 'login-password'
}).append(Sao.i18n.gettext('Password')))
.append(dialog.password_input));
jQuery('<button/>', {
'class': 'btn btn-primary',
'type': 'submit'
}).append(Sao.i18n.gettext('Login')).appendTo(dialog.footer);
dialog.modal.on('hidden.bs.modal', function(event) {
jQuery(this).remove();
});
return dialog;
};
2015-03-06 14:24:55 +01:00
Sao.Session.get_credentials = function() {
var dfd = jQuery.Deferred();
2013-01-04 21:29:48 +01:00
var database = window.location.hash.replace(
/^(#(!|))/, '') || null;
2015-05-29 10:44:10 +02:00
var dialog = Sao.Session.login_dialog();
2013-01-04 21:29:48 +01:00
var ok_func = function() {
2015-05-29 10:44:10 +02:00
var login = dialog.login_input.val();
var password = dialog.password_input.val();
2015-03-06 14:24:55 +01:00
// clear the password as the input will stay in the DOM
2015-05-29 10:44:10 +02:00
dialog.password_input.val('');
var database = database || dialog.database_select.val();
2015-02-06 00:32:51 +01:00
if (!(login && password)) {
2013-01-04 21:29:48 +01:00
return;
}
2015-02-06 00:32:51 +01:00
var session = new Sao.Session(database, login);
2015-03-06 14:24:55 +01:00
session.do_login(login, password)
.then(function() {
dfd.resolve(session);
}, function() {
2015-05-29 10:44:10 +02:00
dialog.modal.modal('show');
2015-03-06 14:24:55 +01:00
});
2015-05-29 10:44:10 +02:00
dialog.modal.modal('hide');
2012-11-29 15:58:22 +01:00
};
2013-01-04 21:29:48 +01:00
2015-05-29 10:44:10 +02:00
dialog.modal.modal({
2015-03-06 14:24:55 +01:00
backdrop: false,
keyboard: false
});
2015-05-29 10:44:10 +02:00
dialog.modal.on('shown.bs.modal', function() {
2015-03-06 14:24:55 +01:00
if (database) {
2015-05-29 10:44:10 +02:00
if (!dialog.login_input.val()) {
dialog.login_input.focus();
2015-03-06 14:24:55 +01:00
} else {
2015-05-29 10:44:10 +02:00
dialog.password_input.focus();
2015-03-06 14:24:55 +01:00
}
} else {
2015-05-29 10:44:10 +02:00
dialog.database_select.focus();
2015-03-06 14:24:55 +01:00
}
});
2015-05-29 10:44:10 +02:00
dialog.modal.find('form').unbind().submit(function(e) {
2015-03-06 14:24:55 +01:00
ok_func();
e.preventDefault();
});
2015-03-06 09:33:03 +01:00
jQuery.when(Sao.DB.list()).then(function(databases) {
databases.forEach(function(database) {
2015-05-29 10:44:10 +02:00
dialog.database_select.append(jQuery('<option/>', {
2015-03-06 09:33:03 +01:00
'value': database,
'text': database
}));
2013-01-04 21:29:48 +01:00
});
2015-02-06 00:32:51 +01:00
if (database) {
2015-05-29 10:44:10 +02:00
dialog.database_select.val(database);
2013-01-04 21:29:48 +01:00
}
2015-05-29 10:44:10 +02:00
dialog.modal.modal('show');
});
2015-03-06 14:24:55 +01:00
return dfd.promise();
2013-01-04 21:29:48 +01:00
};
2015-05-29 10:44:10 +02:00
Sao.Session.password_dialog = function() {
var dialog = new Sao.Dialog(Sao.i18n.gettext('Password'), 'lg');
dialog.password_input = jQuery('<input/>', {
'class': 'form-control',
2015-07-31 22:25:40 +02:00
'type': 'password',
2015-05-29 10:44:10 +02:00
'id': 'password-password',
'placeholder': Sao.i18n.gettext('Password')
});
dialog.body.append(jQuery('<div/>', {
'class': 'form-group'
}).append(jQuery('<label/>', {
'for': 'password-password'
}).append(Sao.i18n.gettext('Password')))
.append(dialog.password_input));
jQuery('<button/>', {
'class': 'btn btn-primary',
'type': 'submit'
}).append(Sao.i18n.gettext('OK')).appendTo(dialog.footer);
dialog.modal.on('hidden.bs.modal', function(event) {
jQuery(this).remove();
});
return dialog;
};
2013-09-06 15:01:29 +02:00
Sao.Session.renew = function(session) {
var dfd = jQuery.Deferred();
2015-05-29 10:44:10 +02:00
var dialog = Sao.Session.password_dialog();
2013-11-25 12:18:32 +01:00
if (!session.login) {
return dfd.reject();
}
2013-01-04 21:29:48 +01:00
var ok_func = function() {
2015-05-29 10:44:10 +02:00
var password = dialog.password_input.val();
2015-03-06 14:24:55 +01:00
// clear the password as the input will stay in the DOM
2015-05-29 10:44:10 +02:00
dialog.password_input.val('');
2015-03-06 14:24:55 +01:00
session.do_login(session.login, password)
.then(function() {
dfd.resolve();
}, function() {
2015-05-29 10:44:10 +02:00
dialog.modal.modal('show');
2015-03-06 14:24:55 +01:00
});
2015-05-29 10:44:10 +02:00
dialog.modal.modal('hide');
2012-11-29 15:58:22 +01:00
};
2013-01-04 21:29:48 +01:00
2015-05-29 10:44:10 +02:00
dialog.modal.modal({
2015-02-06 00:32:51 +01:00
backdrop: false,
keyboard: false
2013-01-04 21:29:48 +01:00
});
2015-05-29 10:44:10 +02:00
dialog.modal.on('shown.bs.modal', function() {
dialog.password_input.focus();
2013-01-04 21:29:48 +01:00
});
2015-05-29 10:44:10 +02:00
dialog.modal.find('form').unbind().submit(function(e) {
ok_func();
e.preventDefault();
});
2015-05-29 10:44:10 +02:00
dialog.modal.modal('show');
2013-09-06 15:01:29 +02:00
return dfd.promise();
2013-01-04 21:29:48 +01:00
};
Sao.Session.current_session = null;
Sao.DB = {};
Sao.DB.list = function() {
2012-11-29 15:58:22 +01:00
var args = {
2013-01-04 21:29:48 +01:00
'method': 'common.db.list',
2012-11-29 15:58:22 +01:00
'params': []
};
2013-01-04 21:29:48 +01:00
return Sao.rpc(args);
2012-11-29 11:32:58 +01:00
};
2013-01-04 21:29:48 +01:00
}());