0

enter image description hereI am using Odoo 9 and I want to deny the creation of products for some users, for example, for the persons who do sales, I want them to only have access to products which are already created. They must not have the right to create new products. How can I do it? Any idea for this, please?

product.py

class product_product(models.Model):
_inherit = "product.product"

@api.model
def create(self, vals):
if self.env.user.has_group('yor_module.your_group'):
    raise Warning(
        _('Sorry, you are not allowed to create new products.'),
    )
else:
    return super(product_product, self).create(vals)

security.xml

 <?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
    <record model="res.groups" id="your_group">
        <field name="name">Group of users who cannot create new products</field>
    </record>
</data>

Dhouha
  • 661
  • 1
  • 8
  • 27

1 Answers1

0

This functionality should be done with rules, but in my opinion, they are unpredictable, and in this case they do not give you flexibility to restrict more your conditions if you wanted. What I would do is the following:

First create a new security group in an XML file in your module (do not forget to add the file in the __manifest__.py / __openerp__.py):

<record model="res.groups" id="your_group">
    <field name="name">Group of users who cannot create new products</field>
</record>

Then overwrite product.product ORM create method and add your conditions inside:

@api.model
def create(self, vals):
    if self.env.user.has_group('your_module.your_group'):
        raise Warning(
            _('Sorry, you are not allowed to create new products.'),
        )
    else:
        return super(ProductProduct, self).create(vals)

Or do it in product.template ORM create method, it does not matter as products inherit from templates by delegation.

If you wanted the users of that group not to write or delete the products, inherit from those ORM methods too and add a similar condition to them.

You should add users who cannot create products to your new group, by hand through the interface, or adding them (or even a whole group of users) through the XML in which you have created your group (through the parameters users -to restrict specific users- or implied_ids -to restrict whole groups-).

forvas
  • 9,801
  • 7
  • 62
  • 158
  • Why do you think that the rules are unpredictable? The rules feature actually is really nice but really complex too. – CZoellner Jan 08 '19 at 12:11
  • @CZoellner Rules have unexpected consequences for me. Here you have two questions I did about them in two different situations with weird behaviours, in fact you had commented one of them: https://stackoverflow.com/questions/48083001/are-odoo-rules-working-ok-actually and https://stackoverflow.com/questions/49013437/can-anyone-explain-me-the-rules-behaviour-in-odoo. I still do not understand them, and I have never found a decent explanation of their behaviour (because the explanation written everywhere taken from the official documentation is not enough). – forvas Jan 08 '19 at 13:04
  • 1
    Yes the documentation about the topic is really short. Look into Rule #8 in Olivier Dony's safer code slides (https://www.odoo.com/de_DE/slides/slide/10-rules-for-safer-code-398), It's a nice visualization. – CZoellner Jan 08 '19 at 13:55
  • Those rules' slides are very clear @CZoellner. However, I still think that those pictures in the slides are not always true. One of the links I wrote you before is an example of that: https://stackoverflow.com/questions/49013437/can-anyone-explain-me-the-rules-behaviour-in-odoo. What happens there? The slides are not right in that case, may be the `domain_force` is breaking anything? But making a rule without a `domain_force` is a nosense, isn't it? – forvas Jan 08 '19 at 14:55
  • I have installed my custom module that i have developed then i added the user to the group as you tell (it is mention in the photo of the question) me but i notice when i connect with that user he still have the right to create a new product – Dhouha Jan 17 '19 at 15:53
  • That user will keep seeing the *Create* button in the interface, but when he saves the new product the warning should be raised with that code. Where did you add the code, ¿`product.product` model? – forvas Jan 18 '19 at 08:20
  • yes in the model **"product.product"** as i mention in the code of my edited question – Dhouha Jan 21 '19 at 14:07