[FIX] core: default multi-company domain

of check_company=True fields.

Suppose two models

class A:

    company_id = fields.M2O() # not required

class B:
    _check_company_auto = True

    company_id = fields.M2O() # not required
    a_id = fields.M20(check_company=True)

and the following code:

a = A.create({'company_id': 1})
b = B.create({'a_id': a.id, 'company_id': False})

The creation of B will fail because of the multi-company
checks, which is expected.

Nevertheless, since 0d30cc2bc9, the domain
of the field a_id would be:

(company_id and ['|', ('company_id', '=', False), ('company_id', 'in', [company_id])] or []) + ([])

which means that through the interface, if you create a record b following
the example above (no company_id on b), the evaluated domain would be empty,
allowing to select records of class A, even if they belong to another company.
Of course, this would lead to a multi-company error when trying to save the
record.

This commit makes sure that the right domain is applied on
check_company=True fields, even if the current record has no value
in its `company_id` field.

opw-3629374

closes odoo/odoo#151341

X-original-commit: aaddedc3bab4f16747fb0f71ae626d94f3975ee3
Signed-off-by: Victor Feyens (vfe) <vfe@odoo.com>
This commit is contained in:
Victor Feyens
2024-01-27 00:01:24 +00:00
parent 11358e95ba
commit 9ea17019f6
3 changed files with 8 additions and 3 deletions
+4 -2
View File
@@ -280,8 +280,10 @@ class Users(models.Model):
_order = 'name, login'
_allow_sudo_commands = False
def _check_company_domain(self, companies=None):
return [('company_ids', 'in', models.to_company_ids(companies))] if companies else []
def _check_company_domain(self, companies):
if not companies:
return []
return [('company_ids', 'in', models.to_company_ids(companies))]
@property
def SELF_READABLE_FIELDS(self):
+2 -1
View File
@@ -2925,7 +2925,8 @@ class _Relational(Field):
else:
cid = "id" if self.model_name == "res.company" else "company_id"
company_domain = env[self.comodel_name]._check_company_domain(companies=unquote(cid))
return f"({cid} and {company_domain} or []) + ({domain or []})"
no_company_domain = env[self.comodel_name]._check_company_domain(companies='')
return f"({cid} and {company_domain} or {no_company_domain}) + ({domain or []})"
return domain
+2
View File
@@ -4000,6 +4000,8 @@ class BaseModel(metaclass=MetaModel):
:param companies: the allowed companies for the related record
:type companies: BaseModel or list or tuple or int or unquote
"""
if not companies:
return [('company_id', '=', False)]
return ['|', ('company_id', '=', False), ('company_id', 'in', to_company_ids(companies))]
def _check_company(self, fnames=None):