/* 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.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(function(result) { this.login = login; this.user_id = result[0]; this.session = result[1]; this.store(); this.renew_device_cookie(); dfd.resolve(); }.bind(this), function() { this.user_id = null; this.session = null; this.store(); dfd.reject(); }.bind(this)); 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(function(context) { jQuery.extend(this.context, context); this.store_context(); }.bind(this)); }, 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 = 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(function(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)); }.bind(this)); } }); Sao.Session.login_dialog = function() { var dialog = new Sao.Dialog(Sao.i18n.gettext('Login'), 'lg'); dialog.database_select = jQuery('', { 'class': 'form-control', 'id': 'database' }).hide(); dialog.login_input = jQuery('', { 'class': 'form-control', 'id': 'login', 'placeholder': Sao.i18n.gettext('User name') }); dialog.body.append(jQuery('
', { 'class': 'form-group' }).append(jQuery('