[IMP] add user switch detection and rename module

This commit is contained in:
Adrià Gil Sorribes
2019-06-04 13:17:44 +02:00
parent 48ccea132f
commit 99cdb6069f
16 changed files with 39 additions and 35 deletions

View File

@@ -0,0 +1,61 @@
odoo.define('web_switch_context_warning.widget', function (require) {
'use strict';
var Widget = require('web.Widget');
var UserMenu = require('web.UserMenu');
var session = require('web.session');
// Show a big banner in the top of the page if the company has been
// changed in another tab or window (in the same browser)
if (!window.SharedWorker) {
// Not supported
return;
}
var SwitchCompanyWarningWidget = Widget.extend({
template:'web_switch_context_warning.warningWidget',
init: function () {
this._super();
var self = this;
var w = new SharedWorker('/web_switch_context_warning/static/src/js/switch_company_warning_worker.js');
w.port.addEventListener('message', function (msg) {
if (msg.data.type !== 'newCtx') {
return;
}
if (msg.data.newCtx === self.generateSignature()) {
self.$el.hide();
} else {
self.$el.show();
}
});
w.port.start();
w.port.postMessage(this.generateSignature());
},
generateSignature: function () {
console.log(session)
return [session.uid, session.company_id, session.db].join();
},
});
UserMenu.include({
init: function (parent) {
this._super(parent);
var switchCompanyWarning = new SwitchCompanyWarningWidget();
// Check if Odoo version is Enterprise
var isEnterprise = odoo.session_info.server_version_info[5] === 'e';
if (isEnterprise) {
switchCompanyWarning.insertAfter('.o_main_navbar');
} else {
// Choose where to append depending on whether web_responsive is installed or not
if (document.getElementById('oe_main_menu_navbar')) {
switchCompanyWarning.appendTo('#oe_main_menu_navbar');
} else {
switchCompanyWarning.insertAfter('.main-nav');
}
}
},
});
return SwitchCompanyWarningWidget;
});

View File

@@ -0,0 +1,23 @@
// Show a big banner in the top of the page if the company has been
// Changed in another tab or window (in the same browser)
var con = [];
var lastCtx = null;
addEventListener("connect", function (ee) {
"use strict";
var port = ee.ports[0];
con.push(port);
port.onmessage = function (e) {
var newCtx = e.data;
if (lastCtx && newCtx !== lastCtx) {
con.forEach(function (eport) {
eport.postMessage({type: "newCtx", "newCtx": newCtx, "lastCtx": lastCtx});
});
}
lastCtx = newCtx;
};
}, false);

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<template>
<t t-name="web_switch_context_warning.warningWidget">
<div class="container-fluid bg-warning" style="text-align: center; display:none;">
<h3>You switched to a different user, company or database with another tab or window</h3>
<p><button onclick="location.reload(true);" class="btn">Reload</button> to refresh your session</p>
</div>
</t>
</template>