Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
5775 Lượt xem

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*.

Ảnh đại diện
Huỷ bỏ

Did you figure this out?

Tác giả

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

Tác giả Câu trả lời hay nhất

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.

Ảnh đại diện
Huỷ bỏ

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

Tác giả

@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.

Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Tác giả

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.

Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 5 25
10241
1
thg 5 25
1884
1
thg 4 24
2527
0
thg 7 24
256
1
thg 8 23
2989