【问题标题】:Advanced access for `write` on res.partnerres.partner 上的“write”高级访问权限
【发布时间】:2018-05-10 06:56:04
【问题描述】:

是否可以从方法 check_access_rights 中访问带有参数 (vals) 提供给 write() 的数据?

我继承自 res.partner 并重写了方法 check_access_rights,目的是允许在 res.partner 上没有写入权限的用户更新 child_ids(该合作伙伴的),如果该孩子是由该用户创建的(create_uid =用户身份) 。我希望能够在某个地方(在方法writecheck_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`

【问题讨论】:

  • 如果写入用户无法写入所提供的 child_ids 条目,直接覆盖 res.partnerwrite() 并发出警告怎么办?
  • 你好@CZoellner 这有效!请添加为答案:)。
  • 只需发布您的最终代码作为答案。我只是给了一个建议;-)
  • @CZoellner 发布:)。无论如何,非常感谢您的建议:)。

标签: python security acl odoo odoo-10


【解决方案1】:

让用户(来自“X组”)修改由他创建的 res.partner 对象,并让修改由任何人创建的 res.partner 对象上的 child_ids:

首先在 res.partner 上创建一个具有以下权限的组“Group X”:a) r,w,c,u ; 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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    相关资源
    最近更新 更多