diff --git a/support_branding/models/__init__.py b/support_branding/models/__init__.py index 4124bd743..c873322e0 100644 --- a/support_branding/models/__init__.py +++ b/support_branding/models/__init__.py @@ -2,3 +2,4 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). from . import res_config_settings +from . import res_company diff --git a/support_branding/models/res_company.py b/support_branding/models/res_company.py new file mode 100644 index 000000000..875fc5b1e --- /dev/null +++ b/support_branding/models/res_company.py @@ -0,0 +1,27 @@ +# Copyright 2023 Sunflower IT +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import _, api, models +from odoo.exceptions import AccessError + + +class ResCompany(models.Model): + _inherit = "res.company" + + @api.model + def get_support_branding_config_param_data(self): + if not self.env.user.has_group("base.group_user"): + raise AccessError( + _( + "You are not allowed to access this " + "functionality, please contact Admin for " + "more support" + ) + ) + self.env.cr.execute( + "select key, value from ir_config_parameter where key ilike " "'support_%';" + ) + res = self.env.cr.dictfetchall() + if any(res): + support_vals = {x["key"]: x["value"] for x in res} + return support_vals diff --git a/support_branding/static/src/js/res_config_edition.js b/support_branding/static/src/js/res_config_edition.js index 48ac99cc3..3bf72448d 100644 --- a/support_branding/static/src/js/res_config_edition.js +++ b/support_branding/static/src/js/res_config_edition.js @@ -7,27 +7,23 @@ odoo.define("support_branding.ResConfigEdition", function (require) { willStart: function () { var self = this; var def_1 = this._rpc({ - model: "ir.config_parameter", - method: "get_param", - args: ["support_company"], - }).then(function (name) { - self.support_cp_name = name; + model: "res.company", + method: "get_support_branding_config_param_data", + args: [], + }).then(function (result) { + if (result && "support_company" in result) + self.support_cp_name = result.support_company; + if (result && "support_company_url" in result) + self.support_cp_url = result.support_company_url; + if (result && "support_email" in result) + self.support_cp_email = result.support_email; + if (result && "support_release" in result) + self.support_cp_release = result.support_release; + if (result && "support_branding_color" in result) + self.support_cp_color = result.support_branding_color; }); - var def_2 = this._rpc({ - model: "ir.config_parameter", - method: "get_param", - args: ["support_company_url"], - }).then(function (url) { - self.support_cp_url = url; - }); - var def_3 = this._rpc({ - model: "ir.config_parameter", - method: "get_param", - args: ["support_email"], - }).then(function (email) { - self.support_cp_email = email; - }); - return $.when(this._super.apply(this, arguments), def_1, def_2, def_3); + + return $.when(this._super.apply(this, arguments), def_1); }, }); }); diff --git a/support_branding/static/src/js/support_branding.js b/support_branding/static/src/js/support_branding.js index 95db62a55..7f8ea9a80 100644 --- a/support_branding/static/src/js/support_branding.js +++ b/support_branding/static/src/js/support_branding.js @@ -19,43 +19,20 @@ odoo.define("support_branding.CrashManager", function (require) { var self = this; $.when(this._super.apply(this, arguments)).then(function () { self._rpc({ - model: "ir.config_parameter", - method: "get_param", - args: ["support_company"], - }).then(function (name) { - self.support_cp_name = name; - }); - - self._rpc({ - model: "ir.config_parameter", - method: "get_param", - args: ["support_company_url"], - }).then(function (url) { - self.support_cp_url = url; - }); - - self._rpc({ - model: "ir.config_parameter", - method: "get_param", - args: ["support_email"], - }).then(function (email) { - self.support_cp_email = email; - }); - - self._rpc({ - model: "ir.config_parameter", - method: "get_param", - args: ["support_release"], - }).then(function (release) { - self.support_cp_release = release; - }); - - self._rpc({ - model: "ir.config_parameter", - method: "get_param", - args: ["support_branding_color"], - }).then(function (color) { - self.support_cp_color = color; + model: "res.company", + method: "get_support_branding_config_param_data", + args: [], + }).then(function (result) { + if (result && "support_company" in result) + self.support_cp_name = result.support_company; + if (result && "support_company_url" in result) + self.support_cp_url = result.support_company_url; + if (result && "support_email" in result) + self.support_cp_email = result.support_email; + if (result && "support_release" in result) + self.support_cp_release = result.support_release; + if (result && "support_branding_color" in result) + self.support_cp_color = result.support_branding_color; }); }); }, diff --git a/support_branding/static/src/js/user_menu.js b/support_branding/static/src/js/user_menu.js index f0c76ba45..2ee54a4f8 100644 --- a/support_branding/static/src/js/user_menu.js +++ b/support_branding/static/src/js/user_menu.js @@ -12,13 +12,17 @@ odoo.define("support_branding.UserMenu", function (require) { var self = this; var def = self ._rpc({ - model: "ir.config_parameter", - method: "get_param", - args: ["support_company_url"], + model: "res.company", + method: "get_support_branding_config_param_data", + args: [], }) - .then(function (site) { - if (site && site !== "") { - self.support_url = site; + .then(function (result) { + if ( + result && + "support_company_url" in result && + result.support_company_url !== "" + ) { + self.support_url = result.support_company_url; } }); return $.when(this._super.apply(this, arguments), def); diff --git a/support_branding/tests/__init__.py b/support_branding/tests/__init__.py new file mode 100644 index 000000000..1a36f0c42 --- /dev/null +++ b/support_branding/tests/__init__.py @@ -0,0 +1,4 @@ +# Copyright 2023 Sunflower IT +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import test_support_branding diff --git a/support_branding/tests/test_support_branding.py b/support_branding/tests/test_support_branding.py new file mode 100644 index 000000000..d6a3b8f48 --- /dev/null +++ b/support_branding/tests/test_support_branding.py @@ -0,0 +1,66 @@ +# Copyright 2023 Sunflower IT +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo.exceptions import AccessError +from odoo.tests.common import TransactionCase + + +class TestSupportBranding(TransactionCase): + def setUp(self): + super(TestSupportBranding, self).setUp() + self.company_obj = self.env["res.company"] + self.ir_config_obj = self.env["ir.config_parameter"].sudo() + self.demo_user = self.env.ref("base.user_demo") + self.admin_user = self.env.ref("base.user_admin") + self.portal_user = self.env.ref("base.demo_user0") + self.demo_support_branding_company_name = self.env.ref( + "support_branding.demo_config_parameter_company_name" + ) + self.demo_support_company_branding_url = self.env.ref( + "support_branding.demo_config_parameter_company_url" + ) + + def test_fetch_support_branding_vals_from_res_company(self): + # Check if user has the right access rights e.g. portal user not allowed + with self.assertRaises(AccessError): + self.ir_config_obj.with_user(self.portal_user).get_param( + self.demo_support_branding_company_name.key + ) + + # Check if demo user is able to access. + # NB: ir.config_parameter model requires admin access rights. + with self.assertRaises(AccessError): + self.ir_config_obj.with_user(self.demo_user).get_param( + self.demo_support_branding_company_name.key + ) + + # Check if user has the right access rights e.g. portal user not allowed + # by calling 'get_support_branding_config_param_data' + with self.assertRaises(AccessError): + self.company_obj.with_user( + self.portal_user + ).get_support_branding_config_param_data() + + vals = self.company_obj.with_user( + self.demo_user + ).get_support_branding_config_param_data() + + self.assertEquals( + vals["support_company"], self.demo_support_branding_company_name.value + ) + + # Check if admin user is able to access + # admin has access all through + vals_1 = self.company_obj.with_user( + self.admin_user + ).get_support_branding_config_param_data() + vals_2 = self.company_obj.with_user( + self.admin_user + ).get_support_branding_config_param_data() + self.assertEquals(vals_1, vals_2) + self.assertEquals( + vals_1["support_company_url"], self.demo_support_company_branding_url.value + ) + self.assertEquals( + vals_2["support_company_url"], self.demo_support_company_branding_url.value + )