Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion product_cost_security/models/product_cost_security_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from odoo import api, fields, models
from odoo.exceptions import AccessError
from odoo.tools import config


class ProductCostSecurityMixin(models.AbstractModel):
Expand Down Expand Up @@ -60,7 +61,25 @@ def check_field_access_rights(self, operation, fields):
extra protection to prevent only editing if the user is not in the
`product_cost_security.group_product_edit_cost` group.
"""
valid_fields = super().check_field_access_rights(operation, fields)
valid_fields = False
if (
config["test_enable"]
and not self.env.context.get("testing_product_cost_security")
and "standard_price" in fields
):
# In unrelated tests, users usually do not have this security group.
# Apply this bypass only when explicitly testing this module.
try:
valid_fields = super().check_field_access_rights(operation, fields)
except AccessError as e:
if (
"You do not have enough rights to access the "
'fields "standard_price" on' in str(e)
):
return fields
valid_fields = valid_fields or super().check_field_access_rights(
operation, fields
)
if self.env.su:
return valid_fields
product_cost_fields = self._product_cost_security_fields().intersection(
Expand Down
8 changes: 7 additions & 1 deletion product_cost_security/tests/test_product_cost_security.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ def setUpClass(cls):
# and attempting to change standard_price
# Without this flag in the context,
# the system tries to access stock.move, which the user may not have permission.
cls.env = cls.env(context=dict(cls.env.context, disable_auto_svl=True))
cls.env = cls.env(
context=dict(
cls.env.context,
disable_auto_svl=True,
testing_product_cost_security=True,
)
)
cls.base_group_user_id = cls.env.ref("base.group_user").id
cls.product_edit_cost_group_id = cls.env.ref(
"product_cost_security.group_product_edit_cost"
Expand Down
Loading