Skip to content

Commit c91cf1a

Browse files
luisg123vmart-e
authored andcommitted
[FIX] base: don't lose sudo when searching partners
Currently, when searching a partner through a related record using `sudo()`, the superuser privileges are used when searching the record, but not when searching the related partner. That's because the su flag is lost during the call to with_user, even if it was to keep the same user. For instance, a code like the following wouldn't work if the current user has no enough rights (e.g. the public user): self.sudo().search([('partner_id', 'ilike', 'John Doe')]) To solve the above, the with_user is called only when a specific name_get_uid is given, as done in the _search implementation. closes odoo#83156 Signed-off-by: Raphael Collet <[email protected]>
1 parent 9f202e0 commit c91cf1a

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

odoo/addons/base/models/res_partner.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ def _get_name_search_order_by_fields(self):
759759

760760
@api.model
761761
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None):
762-
self = self.with_user(name_get_uid or self.env.uid)
762+
self = self.with_user(name_get_uid) if name_get_uid else self
763763
# as the implementation is in SQL, we force the recompute of fields if necessary
764764
self.recompute(['display_name'])
765765
self.flush()

odoo/addons/base/tests/test_res_partner.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from odoo.tests import Form
55
from odoo.tests.common import TransactionCase
6-
from odoo.exceptions import UserError
6+
from odoo.exceptions import AccessError, UserError
77

88

99
class TestPartner(TransactionCase):
@@ -20,6 +20,13 @@ def test_name_search(self):
2020
ns_res = self.env['res.partner'].name_search('Vlad', args=[('user_ids.email', 'ilike', 'vlad')])
2121
self.assertEqual(set(i[0] for i in ns_res), set(test_user.partner_id.ids))
2222

23+
# Check a partner may be searched when current user has no access but sudo is used
24+
public_user = self.env.ref('base.public_user')
25+
with self.assertRaises(AccessError):
26+
test_partner.with_user(public_user).check_access_rule('read')
27+
ns_res = self.env['res.partner'].with_user(public_user).sudo().name_search('Vlad', args=[('user_ids.email', 'ilike', 'vlad')])
28+
self.assertEqual(set(i[0] for i in ns_res), set(test_user.partner_id.ids))
29+
2330
def test_name_get(self):
2431
""" Check name_get on partner, especially with different context
2532
Check name_get correctly return name with context. """

0 commit comments

Comments
 (0)