/* This file is part of Tryton. The COPYRIGHT file at the top level of this repository contains the full copyright notices and license terms. */ (function() { 'use strict'; // https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/btoa#Unicode_strings function utoa(str) { return window.btoa(unescape(encodeURIComponent(str))); } Sao.Session = Sao.class_(Object, { init: function(database, login) { this.login_service = null; this.user_id = null; this.session = null; this.cache = new Cache(); this.prm = jQuery.when(); // renew promise this.database = database; this.login = login; this.restore(); this.context = {}; this.restore_context(); if (!Sao.Session.current_session) { Sao.Session.current_session = this; } }, get_auth: function() { return utoa(this.login + ':' + this.user_id + ':' + this.session); }, do_login: function(parameters) { var dfd = jQuery.Deferred(); var login = this.login; var device_cookies = JSON.parse( localStorage.getItem('sao_device_cookies')); var device_cookie = null; if (device_cookies && device_cookies[this.database]) { device_cookie = device_cookies[this.database][this.login]; } var func = function(parameters) { parameters.device_cookie = device_cookie; return { 'method': 'common.db.login', 'params': [login, parameters, Sao.i18n.getlang()] }; }; new Sao.Login(func, this).run().then(result => { this.login = login; this.user_id = result[0]; this.session = result[1]; this.store(); this.renew_device_cookie(); dfd.resolve(); }, () => { this.user_id = null; this.session = null; this.store(); dfd.reject(); }); return dfd.promise(); }, do_logout: function() { if (!(this.user_id && this.session)) { return jQuery.when(); } var args = { 'id': 0, 'method': 'common.db.logout', 'params': [] }; var prm = jQuery.ajax({ 'headers': { 'Authorization': 'Session ' + this.get_auth() }, 'contentType': 'application/json', 'data': JSON.stringify(args), 'dataType': 'json', 'url': '/' + this.database + '/', 'type': 'post', }); this.unstore(); this.database = null; this.login = null; this.user_id = null; this.session = null; if (Sao.Session.current_session === this) { Sao.Session.current_session = null; } return prm; }, reload_context: function() { var args = { 'method': 'model.res.user.get_preferences', 'params': [true, this.context] }; this.reset_context(); var prm = Sao.rpc(args, this); return prm.then(context => { jQuery.extend(this.context, context); this.store_context(); }); }, reset_context: function() { this.context = { client: Sao.Bus.id, }; }, restore_context: function() { this.reset_context(); var context = sessionStorage.getItem('sao_context_' + this.database); if (context !== null) { jQuery.extend( this.context, Sao.rpc.convertJSONObject(JSON.parse(context))); } }, store_context: function() { var context = jQuery.extend({}, this.context); delete context.client; context = JSON.stringify(Sao.rpc.prepareObject(context)); sessionStorage.setItem('sao_context_' + this.database, context); }, restore: function() { if (this.database && !this.session) { var session_data = localStorage.getItem( 'sao_session_' + this.database); if (session_data !== null) { session_data = JSON.parse(session_data); if (!this.login || this.login == session_data.login) { this.login_service = session_data.login_service; this.login = session_data.login; this.user_id = session_data.user_id; this.session = session_data.session; } } } }, store: function() { var session = { 'login': this.login, 'user_id': this.user_id, 'session': this.session, }; session = JSON.stringify(session); localStorage.setItem('sao_session_' + this.database, session); }, unstore: function() { localStorage.removeItem('sao_session_' + this.database); }, renew_device_cookie: function() { var device_cookie; var device_cookies = JSON.parse( localStorage.getItem('sao_device_cookies')); if (!device_cookies || !(this.database in device_cookies)) { device_cookie = null; } else { device_cookie = device_cookies[this.database][this.login]; } var renew_prm = Sao.rpc({ method: 'model.res.user.device.renew', params: [device_cookie, {}], }, this); renew_prm.done(result => { device_cookies = JSON.parse( localStorage.getItem('sao_device_cookies')); if (!device_cookies) { device_cookies = {}; } if (!(this.database in device_cookies)) { device_cookies[this.database] = {}; } device_cookies[this.database][this.login] = result; localStorage.setItem( 'sao_device_cookies', JSON.stringify(device_cookies)); }); renew_prm.fail(() => { Sao.error("Cannot renew device cookie"); }); } }); Sao.Session.server_version = function() { var timeoutID = Sao.common.processing.show(); return jQuery.ajax({ 'contentType': 'application/json', 'data': JSON.stringify({ 'id': 0, 'method': 'common.server.version', 'params': [] }), 'dataType': 'json', 'url': '/', 'type': 'post', 'complete': [function() { Sao.common.processing.hide(timeoutID); }] }).then(function(data) { return data.result; }); }; Sao.Session.login_dialog = function() { var dialog = new Sao.Dialog( Sao.i18n.gettext("Login"), 'login-dialog', 'md', true, Sao.__version__); dialog.database_select = jQuery('', { 'class': 'form-control', 'id': 'database', 'name': 'database', }).hide(); dialog.login_input = jQuery('', { 'class': 'form-control', 'id': 'login', 'name': 'login', }); dialog.button = jQuery('