是否可以从方法write()
中访问提供给vals
参数(check_access_rights
)的数据?
我继承自res.partner
并覆盖方法check_access_rights
,意图允许res.partner
没有写权限的用户更新child_ids
(该合作伙伴)由该用户创建(create_uid = user.id)。我希望能够在某个地方(方法write
或check_access_rights
)实现这个伪代码:
if `the user belongs to a group "GroupX"` and `user tries to only update field "child_ids" with records that are created by that user`
then `allow this write operation on res.partner`
else `raise AccessError`
答案 0 :(得分:1)
让用户(来自" Group X")修改由他创建的res.partner对象,并在任何人创建的res.partner对象上修改child_ids:
首先创建一个组"组X"有权利:a)r,w,c,u on res.partner; b)关于ir.property的r,w,c。
然后创建一个继承自res.partner并覆盖方法write
的类。
# -*- coding: utf-8 -*-
class InheritedResPartner(models.Model):
"""Description"""
_inherit = 'res.partner'
@api.multi
def write(self, vals):
is_in_group = 'Group X' in map(lambda x: x.name, self.env.user.groups_id)
if is_in_group:
operation = 'write'
owns_record = self.create_uid == self.env.user
if owns_record:
True
else:
allowed = True
# Do all checks further and set `allowed` to either True or False
...<omitted intentionaly> put your logic here
#
if not allowed:
raise AccessError(_('The requested operation cannot be completed due to security restrictions. Please contact your system administrator.\n\n(Document type: %s, Operation: %s)') % (self._description, operation))
return super(InheritedResPartner, self).write(vals)
注意:我们提供res.partner的完全权限,但是如果检测到不需要的操作,我们将覆盖write方法并引发AccessError。