Skip to content

Commit

Permalink
[IMP] product_assortment: Add security group to only show assortments…
Browse files Browse the repository at this point in the history
… to managers

TT51064
  • Loading branch information
sergio-teruel committed Oct 7, 2024
1 parent 6dc0923 commit 287849a
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions product_assortment/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"depends": ["base", "product"],
"data": [
"data/ir_cron.xml",
"security/product_assortment_security.xml",
"views/product_assortment.xml",
"views/res_partner_view.xml",
],
Expand Down
1 change: 1 addition & 0 deletions product_assortment/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from . import ir_filters
from . import ir_rule
from . import res_partner
36 changes: 36 additions & 0 deletions product_assortment/models/ir_rule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2024 Tecnativa - Sergio Teruel
# License AGPL-3 - See https://www.gnu.org/licenses/agpl-3.0.html

from odoo import api, models, tools
from odoo.osv import expression
from odoo.tools import config


class IrRule(models.Model):
_inherit = "ir.rule"

@api.model
@tools.conditional(
"xml" not in config["dev_mode"],
tools.ormcache(
"self.env.uid",
"self.env.su",
"model_name",
"mode",
"tuple(self._compute_domain_context_values())",
),
)
def _compute_domain(self, model_name, mode="read"):
"""Inject extra domain for restricting filter (Assortments) when the user
has not the group 'Product Assortment Manager'.
"""
res = super()._compute_domain(model_name, mode=mode)
user = self.env.user
if model_name == "ir.filters" and not self.env.su:
if not user.has_group(
"product_assortment.group_product_assortment_manager"
):
extra_domain = [("is_assortment", "=", False)]
extra_domain = expression.normalize_domain(extra_domain)
res = expression.AND([extra_domain] + [res])

Check warning on line 35 in product_assortment/models/ir_rule.py

View check run for this annotation

Codecov / codecov/patch

product_assortment/models/ir_rule.py#L33-L35

Added lines #L33 - L35 were not covered by tests
return res
18 changes: 18 additions & 0 deletions product_assortment/security/product_assortment_security.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo noupdate="1">
<record id="group_product_assortment_manager" model="res.groups">
<field name="name">Product Assortment Manager</field>
<field name="comment">Access to all product assortment</field>
</record>

<record id="ir_filters_product_assortment_rule" model="ir.rule">
<field name="name">ir.filters.product.assortment.manager.rule</field>
<field name="model_id" ref="model_ir_filters" />
<field name="domain_force">[('is_assortment','=', True)]</field>
<field
name="groups"
eval="[Command.link(ref('product_assortment.group_product_assortment_manager'))]"
/>
<field name="perm_unlink" eval="False" />
</record>
</odoo>
1 change: 1 addition & 0 deletions product_assortment/views/product_assortment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
name="Product Assortment"
id="menu_product_assortments"
action="actions_product_assortment_view"
groups="product_assortment.group_product_assortment_manager"
sequence="5"
web_icon="product_assortment,static/description/icon.png"
/>
Expand Down
1 change: 1 addition & 0 deletions product_assortment/views/res_partner_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
name="action_define_product_assortment"
icon="fa-pencil"
type="object"
groups="product_assortment.group_product_assortment_manager"
>
<span class="o_stat_text">Product Assortments</span>
</button>
Expand Down

0 comments on commit 287849a

Please sign in to comment.