71 lines
2.4 KiB
Python
71 lines
2.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
Tour Mixin (Simplified)
|
|
=======================
|
|
Mixin for module access detection.
|
|
Uses standard Odoo ir.module.module instead of registry.
|
|
"""
|
|
|
|
from odoo import models, api
|
|
import logging
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
class GeniusModuleAccessMixin(models.AbstractModel):
|
|
"""Mixin for detecting which modules a user has access to."""
|
|
_name = 'genius.module.access.mixin'
|
|
_description = 'Module Access Detection Mixin'
|
|
|
|
@api.model
|
|
def _get_user_accessible_modules(self, user=None):
|
|
"""
|
|
Detect which modules a user has access to.
|
|
Returns list of module technical names.
|
|
"""
|
|
user = user or self.env.user
|
|
|
|
# Admin sees everything
|
|
if user.has_group('tour_genius.group_genius_admin'):
|
|
return self.env['ir.module.module'].search([
|
|
('state', '=', 'installed')
|
|
]).mapped('name')
|
|
|
|
accessible_modules = set()
|
|
|
|
try:
|
|
# Check model access rights
|
|
model_accesses = self.env['ir.model.access'].sudo().search([
|
|
('group_id', 'in', user.groups_id.ids),
|
|
('perm_read', '=', True),
|
|
])
|
|
|
|
for access in model_accesses:
|
|
if access.model_id and access.model_id.modules:
|
|
module_names = [m.strip() for m in access.model_id.modules.split(',')]
|
|
accessible_modules.update(module_names)
|
|
|
|
except Exception as e:
|
|
_logger.warning("Error detecting accessible modules: %s", str(e))
|
|
# Fallback: return all installed modules
|
|
return self.env['ir.module.module'].sudo().search([
|
|
('state', '=', 'installed')
|
|
]).mapped('name')
|
|
|
|
return list(accessible_modules)
|
|
|
|
def filter_by_user_access(self, records=None, user=None):
|
|
"""Filter records to only those the user can access based on module."""
|
|
records = records if records is not None else self
|
|
user = user or self.env.user
|
|
|
|
# Admin sees everything
|
|
if user.has_group('tour_genius.group_genius_admin'):
|
|
return records
|
|
|
|
accessible_modules = self._get_user_accessible_modules(user)
|
|
|
|
return records.filtered(
|
|
lambda r: not r.module_name or r.module_name in accessible_modules
|
|
)
|