[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) <rde@odoo.com>
This commit is contained in:
Serge Bayet (seba)
2024-03-08 12:49:01 +00:00
parent 1a0e3ee5c1
commit 9079b70387
3 changed files with 31 additions and 1 deletions
+9
View File
@@ -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"
+14 -1
View File
@@ -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
+8
View File
@@ -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()