Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
2 Replies
5341 Tampilan

Since Odoo 9 or 11, we use the feature "visible groups" for our website menus. Now in Odoo 16 the field is removed.


How to we restrict a menu item visibility to a certain user group in Odoo 16? Note, it's also about mege menu entries, page visibility *does not help*.

Avatar
Buang

Did you figure this out?

Penulis

@Matthew Harrison Yes, we did, see my answer below.

Penulis Jawaban Terbai

We developed a solution as part of https://apps.odoo.com/apps/modules/16.0/website_user_types/

The module makes the website menu tree visible outside debug mode. In the menus' form you can restrict a menu to specific website user groups, e.g. B2C and B2B. The important part is to consider the menu caching, too! In Odoo 16 the menu is cached and group visibility is not going to be recalculated without extending the t-cache. This can look like this:

< template
id="layout"
inherit_id="website.layout">
< xpath expr="//header/t[@t-cache]" position="attributes">
< attribute name="t-cache" add="website.get_website_user_group_cache_key()"/>
< /xpath>
< /template>

Depending on your use case you might add more groups. For us, the user groups fulfill the requirements.

Avatar
Buang

Hi Michael
What is the minimum things we need to change to show a menu based on group in website ?
I saw your module but it seems a lot of things to configure.
Can you provide a step by step guide to where to change ?
Thanks in Advance

Penulis

@Gouranga Kala

You need to add the groups field, add the security filter for portal and public users (see website_user_types/security/website_security.xml or implement another solution), and extend the cache key in the header according to the group combinations.

Jawaban Terbai

You can achieve it by inheriting the _compute_visible method of website.menu model.

Consider the below given example:-

class WebsiteMenu(models.Model):
_inherit = 'website.menu'


def _compute_visible(self):
"""Compute menu invisible"""
super()._compute_visible()
for menu in self:
if not menu.is_visible:
return
if menu.name == 'Shop' and not self.env.user.user_has_groups(
'base.group_user'):
menu.is_visible = False

Regards

Avatar
Buang
Penulis

Thanks four your post. Please note, that this solution will not work seamlessly in Odoo 16 because the menu is cached. Thus, every odoo worker might cache a different version of the menu and the menu shown to the user will be depending on the worker who answers the request, but not his user group. To extend t-cache, see my answer above.

Post Terkait Replies Tampilan Aktivitas
1
Mei 25
9741
1
Mei 25
1498
1
Apr 24
1996
0
Jul 24
256
1
Agu 23
2419