[FIX] simplify_access_management

This commit is contained in:
Samir Ladoui 2025-08-18 05:04:15 +01:00
parent b16a2cf054
commit 50b62adf84
1 changed files with 34 additions and 12 deletions

View File

@ -6,16 +6,38 @@ class ir_ui_menu(models.Model):
@api.model
def search(self, args, offset=0, limit=None, order=None, count=False):
ids = super(ir_ui_menu, self).search(args, offset=0, limit=None, order=order, count=False)
user = self.env.user
# user.clear_caches()
cids = request.httprequest.cookies.get('cids') and request.httprequest.cookies.get('cids').split(',')[0] or self.env.company.id
for menu_id in user.access_management_ids.filtered(lambda line: int(cids) in line.company_ids.ids).mapped('hide_menu_ids'):
if menu_id in ids:
ids = ids - menu_id
if offset:
ids = ids[offset:]
if limit:
ids = ids[:limit]
return len(ids) if count else ids
# Step 1: Safely determine the current company ID from the request or fallback to env.
cids = None
try:
# This block only runs safely in an HTTP context.
if request:
cookie_cids = request.httprequest.cookies.get('cids')
if cookie_cids:
# Ensure conversion to integer is also safe
cids = int(cookie_cids.split(',')[0])
except (RuntimeError, ValueError):
# This will catch the 'object unbound' error during startup
# and any errors if the cookie value is not a valid integer.
cids = None
if not cids:
cids = self.env.company.id
# Step 2: Get the list of menu record IDs that should be hidden.
user = self.env.user
# The .ids at the end gets a list of integers, which is required for the 'not in' operator.
menu_ids_to_hide = user.access_management_ids.filtered(
lambda line: cids in line.company_ids.ids
).mapped('hide_menu_ids').ids
# Step 3: If there are menus to hide, add a new condition to the search domain.
if menu_ids_to_hide:
# This modifies the search query to exclude the hidden menus at the database level.
# It's the most efficient way to perform this filtering.
# We ensure 'args' is a list before appending.
args = (args or []) + [('id', 'not in', menu_ids_to_hide)]
# Step 4: Call super() ONCE with the modified domain and original parameters.
# This lets the database correctly handle all filtering, ordering, offset, limit, and counting.
return super(ir_ui_menu, self).search(args, offset=offset, limit=limit, order=order, count=count)