refactor: use sudo() in product archive and write methods to bypass multi-company restrictions

This commit is contained in:
Suherdy Yacob 2026-05-30 19:11:33 +07:00
parent 48fa2d0dd7
commit 971f004a26

View File

@ -5,40 +5,24 @@ class ProductTemplate(models.Model):
_inherit = 'product.template' _inherit = 'product.template'
def action_archive(self): def action_archive(self):
if not self.env.su: # Use sudo() to bypass multi-company checks during archival cascade
if hasattr(self.env.user, '_get_company_ids'): return super(ProductTemplate, self.sudo()).action_archive()
allowed_companies = list(self.env.user._get_company_ids())
else:
allowed_companies = self.env.user.company_ids.filtered('active').ids
self = self.with_context(allowed_company_ids=allowed_companies)
return super().action_archive()
def write(self, vals): def write(self, vals):
if not self.env.su and 'active' in vals and not vals['active']: # Use sudo() during archival write to bypass multi-company checks
if hasattr(self.env.user, '_get_company_ids'): if 'active' in vals and not vals['active']:
allowed_companies = list(self.env.user._get_company_ids()) return super(ProductTemplate, self.sudo()).write(vals)
else:
allowed_companies = self.env.user.company_ids.filtered('active').ids
self = self.with_context(allowed_company_ids=allowed_companies)
return super().write(vals) return super().write(vals)
class ProductProduct(models.Model): class ProductProduct(models.Model):
_inherit = 'product.product' _inherit = 'product.product'
def action_archive(self): def action_archive(self):
if not self.env.su: # Use sudo() to bypass multi-company checks during archival cascade
if hasattr(self.env.user, '_get_company_ids'): return super(ProductProduct, self.sudo()).action_archive()
allowed_companies = list(self.env.user._get_company_ids())
else:
allowed_companies = self.env.user.company_ids.filtered('active').ids
self = self.with_context(allowed_company_ids=allowed_companies)
return super().action_archive()
def write(self, vals): def write(self, vals):
if not self.env.su and 'active' in vals and not vals['active']: # Use sudo() during archival write to bypass multi-company checks
if hasattr(self.env.user, '_get_company_ids'): if 'active' in vals and not vals['active']:
allowed_companies = list(self.env.user._get_company_ids()) return super(ProductProduct, self.sudo()).write(vals)
else:
allowed_companies = self.env.user.company_ids.filtered('active').ids
self = self.with_context(allowed_company_ids=allowed_companies)
return super().write(vals) return super().write(vals)