如何使用Odoo中的One2many字段管理安全性?

时间:2016-01-04 12:16:39

标签: xml python-2.7 security openerp odoo-8

我对Odoo 8中的安全组有疑问,我将展示一个简单的例子,使其更容易理解:

我创建了两个新模型,母亲儿童。他们之间的关系是,一个母亲可以有几个孩子,但一个孩子只有一个母亲。因此,我们在母亲中有一个One2many字段指向儿童(名为child_ids),并且指向Many2one >母亲(名为mother_id)在儿童

现在,我有一个用户可以创建一些孩子,但不是母亲。当他创建一个孩子时,他只能在已经由其他用户创建的母亲之间进行选择。

问题

当此用户创建子项时,会出现安全性错误,告知用户属于无法修改 Mother 模型的组。我认为这是因为当孩子被创建时,母亲的字段child_ids会自动修改。

时间解决方案

目前我已经解决了这个问题,将母亲模式中的写入权限授予上述用户的群组。因此,当用户打开表单以创建子项并在mother_id中选择一位母亲时,旁边会显示打开和编辑图标。如果用户点击它,他可以自由地修改母亲,这是我不想要的。我隐藏了那个图标,但由于这个原因,没有人可以从这里修改母亲,我也不想要。除此之外,如果字段mother_id出现在我不知道的其他部分,则打开和编辑图标甚至不会被隐藏。

注意

在实际案例中,我甚至没有在母亲中创建字段child_ids,我的意思是,我只在 Child <中声明了Many2one mother_id / strong>,但似乎该程序知道母亲中应该存在One2many字段,因此如果我不修改写入权限,我会收到上述错误。

我简化了我的代码,问题仍然存在。下面你可以阅读整个代码,我已经没有了。如果我从 Child 的字段related='mother_id.is_a_good_mother',中删除行has_a_good_mother,则不会调用母亲的ORM方法写入(这是我需要的)。

任何人都可以帮助我吗?你知道如何实现我的目的吗?

Python代码

class mother(models.Model):
    _name = 'mother'

    name = fields.Char(string='Name', size=64, required=True)
    is_a_good_mother = fields.Boolean(string='Is a good mother?')

    @api.multi
    def write(self, vals):
        _logger.info('I DO NOT KNOW WHY WHEN CREATING A CHILD THIS ORM '
                     'METHOD IS BEING EXECUTED, RECEIVING THE KEY '
                     'is_a_good_mother')
        return super(mother, self).write(vals)


class child(models.Model):
    _name = 'child'

    mother_id = fields.Many2one(comodel_name='mother',
                                string='Mother', ondelete='cascade')
    has_a_good_mother = fields.Boolean(
        string='Does the child have a good mother?',
        related='mother_id.is_a_good_mother',
        related_sudo=True)

    @api.one
    def create_child(self):
        return {
            'type': 'ir.actions.act_window_close',
        }

XML代码

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
        <record id="child_create_form_view" model="ir.ui.view">
            <field name="name">child.create.form</field>
            <field name="model">child</field>
            <field name="type">form</field>
            <field name="arch" type="xml">
                <form string="A child">
                    <group>
                        <group col="2">
                            <field name="id" invisible="1" />
                            <field name="mother_id" required="1" options="{'no_open': True}" />
                        </group>
                        <group col="2">
                            <field name="has_a_good_mother" invisible="1"/>
                        </group>
                    </group>
                    <footer attrs="{'invisible': [('id','!=',False)]}">
                        <button name="create_child" string="Create" type="object"
                            class="oe_highlight" />
                        or
                        <button string="Discard" class="oe_link" special="cancel" />
                    </footer>
                </form>
            </field>
        </record>
        <record id="action_child_create_form" model="ir.actions.act_window">
            <field name="name">Create child</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">child</field>
            <field name="view_type">form</field>
            <field name="view_mode">form</field>
            <field name="view_id" ref="child_create_form_view" />
            <field name="target">new</field>
        </record>
        <record id="res_partner_test_view" model="ir.ui.view">
            <field name="name">res.partner.test.form</field>
            <field name="model">res.partner</field>
            <field name="inherit_id" ref="base_location.view_partner_form" />
            <field name="type">form</field>
            <field name="arch" type="xml">
                <xpath expr="//field[@name='website']" position="after">
                    <button name="%(test.action_child_create_form)d"
                        string="Create child" type="action"
                        class="oe_highlight oe_edit_only" />
                </xpath>
            </field>
        </record>
    </data>
</openerp>

0 个答案:

没有答案