From 9079b7038754ca4fe547086d3fe8947a4bb6ddce Mon Sep 17 00:00:00 2001 From: "Serge Bayet (seba)" Date: Tue, 5 Mar 2024 11:49:40 +0000 Subject: [PATCH] [FIX] website: Ensure no public users when archiving company This commit prohibits the archival of a company if it is associated with a website. Otherwise, the website wouldn't be accessible anymore by public users (and so by all users if they need to login). Step to reproduce: - On website 2, change the company from SF to Chicago - In the website list view (in debug mode), reorder the websites so website 2 is the first one in the tree view - Archive the Chicago company (from debug > companies and do it from the list view) - Try to access the website/runbot from an incognito tab - It will show a raw 403 error Note that the companies can only be archives since Odoo 16, thanks to this commit: https://github.com/odoo/odoo/commit/7d0996bb151f68647b22719eee5ed4a4c35574bb opw-3749772 closes odoo/odoo#156988 X-original-commit: d7456bf5376cf53670dbe8eaf1a2281aa254d521 Signed-off-by: Romain Derie (rde) --- addons/website/i18n/website.pot | 9 +++++++++ addons/website/models/res_company.py | 15 ++++++++++++++- addons/website/tests/test_res_users.py | 8 ++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/addons/website/i18n/website.pot b/addons/website/i18n/website.pot index 738129a137a..0bedec3d28c 100644 --- a/addons/website/i18n/website.pot +++ b/addons/website/i18n/website.pot @@ -10756,6 +10756,15 @@ msgid "" " interruption." msgstr "" +#. module: website +#. odoo-python +#: code:addons/website/models/res_company.py:0 +#, python-format +msgid "" +"The company %(company_name)r cannot be archived because it has a linked website %(website_name)r.\n" +"Change that website's company first." +msgstr "" + #. module: website #: model_terms:ir.ui.view,arch_db:website.res_config_settings_view_form msgid "The company this website belongs to" diff --git a/addons/website/models/res_company.py b/addons/website/models/res_company.py index 9311e885662..3b8d1c77cf7 100644 --- a/addons/website/models/res_company.py +++ b/addons/website/models/res_company.py @@ -1,7 +1,8 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from odoo import api, fields, models +from odoo import api, fields, models, _ +from odoo.exceptions import ValidationError class Company(models.Model): @@ -19,6 +20,18 @@ class Company(models.Model): action['target'] = 'new' return action + @api.constrains('active') + def _check_active(self): + super()._check_active() + for company in self: + if not company.active and company.website_id: + raise ValidationError(_( + 'The company %(company_name)r cannot be archived because it has a linked website %(website_name)r.' + '\nChange that website\'s company first.', + company_name=company.name, + website_name=company.website_id.name + )) + def google_map_img(self, zoom=8, width=298, height=298): partner = self.sudo().partner_id return partner and partner.google_map_img(zoom, width, height) or None diff --git a/addons/website/tests/test_res_users.py b/addons/website/tests/test_res_users.py index 87814cbda83..1658ea25d5e 100644 --- a/addons/website/tests/test_res_users.py +++ b/addons/website/tests/test_res_users.py @@ -102,3 +102,11 @@ class TestWebsiteResUsers(TransactionCase): self._create_and_check_portal_user(False, company_1, company_2, website_1, website_2) self._create_and_check_portal_user(True, company_1, company_2, website_1, website_2) + + def test_archive_company_linked_to_website(self): + company = self.env['res.company'].create({'name': 'Company'}) + self.env['website'].create({'name': "Website 1", 'company_id': company.id}) + + # The company cannot be archived because it has a website linked to it + with self.assertRaises(ValidationError): + company.action_archive()