diff --git a/odex25_base/simplify_access_management/models/ir_ui_menu.py b/odex25_base/simplify_access_management/models/ir_ui_menu.py index eddcba995..4f7d74cf1 100644 --- a/odex25_base/simplify_access_management/models/ir_ui_menu.py +++ b/odex25_base/simplify_access_management/models/ir_ui_menu.py @@ -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)