odoo / odoo

Odoo. Open Source Apps To Grow Your Business.
https://www.odoo.com
Other
37.54k stars 24.42k forks source link

Should use superuser to recompute function field? #3178

Closed auyeung112002 closed 9 years ago

auyeung112002 commented 9 years ago

Impacted versions: 8.0 stable, Oct 15

Steps to reproduce:

  1. Use new API to define a field with compute method
  2. Use api.depends to define dependency on any field of other model. And user don't have read right(ir.model.access) on the model being depended.

Current behavior: Fail to save because of access error in recompute method in model.py

Expected behavior: Recompute method compute the new value as superuser.

auyeung112002 commented 9 years ago

@rco-odoo @xmo-odoo If I add this line in recompute method of BaseModel in model.py, it does not work as expected. Somehow odoo will read the field as normal user and raise access error when it runs line 133.

I need some urgent advice since this issue disallows saving the object, after adding the function field.Would you please shed some light on this issue? Thanks!

image

auyeung112002 commented 9 years ago

Finally, I changed the code like below. I am not sure about the side effects but it does not raise access error now.

@rco-odoo @xmo-odoo I guess another reason to use superuser for recomputation of function field relates to record rule. Please correct me if my guess is incorrect. If function field depends on columns in One2many field, and different user may see different rows of that One2many field(record rule effect), resulting in different value for function field depending on who triggers the recomputation.

image

lmignon commented 9 years ago

@rco-odoo @xmo-odoo I don't know if it's relevant here but I've observed some problems with the use of sudo in terms of performance. In fact each time the sudo is used, a new env is created with an empty cache and each access to a record in this new env generate a new query to the database even if the record has already been read in the same transaction in an other env. If you set the log_level to sql and refresh a tree view with a one2many field (for ex), you all see one sql query by line to get the name of the related field even if each row reference the same id.... This is just one example among many others. I've also observed that a 'read' of a specific field on a record, generate one sql to select the column, and others sql to select all the columns if the field is a 'related' .. (it's also true when you use 'write' ).

rco-odoo commented 9 years ago

@auyeung112002 @lmignon please check the alternative fix in #5087: instead of always recomputing fields as user admin, we introduce an explicit flag on the field to do it.

This should provide you a solution without breaking the behavior of other modules.

auyeung112002 commented 9 years ago

Hi @rco-odoo What if there are two compute fields using the same compute function, but these two fields have different compute_sudo setting?