def _get_user_permission_view_menus(
        self, user: object, permission_name: str, view_menus_name: List[str]
    ) -> Set[str]:
        """
        Return a set of view menu names with a certain permission name
        that a user has access to. Mainly used to fetch all menu permissions
        on a single db call, will also check public permissions and builtin roles
        """
        # Determine user roles (use public role if user is None)
        roles = [self.get_public_role()] if user is None else self.get_user_roles(user)

        # First, check built-in roles (avoiding unnecessary DB queries)
        result = {
            view_menu_name
            for role in roles
            if role.name in self.builtin_roles
            for view_menu_name in view_menus_name
            if self._has_access_builtin_roles(role, permission_name, view_menu_name)
        }

        # Collect database role IDs for further checking
        db_role_ids = [role.id for role in roles if role.name not in self.builtin_roles]

        # Check database-stored roles if needed
        if db_role_ids:
            result.update(
                pvm.view_menu.name
                for pvm in self.find_roles_permission_view_menus(
                    permission_name, db_role_ids
                )
            )

        return result

        Return a set of view menu names with a certain permission name
        that a user has access to. Mainly used to fetch all menu permissions
        on a single db call, will also check public permissions and builtin roles
        
flask_appbuilder/security/manager.py