access_restriction_by_user/models/res_users.py

87 lines
3.1 KiB
Python

from odoo import models, fields
class ResUsers(models.Model):
_inherit = 'res.users'
allowed_warehouse_ids = fields.Many2many(
'stock.warehouse',
'res_users_stock_warehouse_rel',
'user_id',
'warehouse_id',
string="Allowed Warehouses",
help="Warehouses this user is allowed to access. Leave empty to restrict access to none."
)
allowed_picking_type_ids = fields.Many2many(
'stock.picking.type',
'res_users_stock_picking_type_rel',
'user_id',
'picking_type_id',
string="Allowed Picking Types",
help="Picking Types this user is allowed to access. Leave empty to restrict access to none."
)
allowed_location_ids = fields.Many2many(
'stock.location',
'res_users_stock_location_rel',
'user_id',
'location_id',
string="Allowed Locations",
help="Locations this user is allowed to access. Leave empty to restrict access to none."
)
allowed_workcenter_ids = fields.Many2many(
'mrp.workcenter',
'res_users_mrp_workcenter_rel',
'user_id',
'workcenter_id',
string="Allowed Work Centers",
help="Work Centers this user is allowed to access. Leave empty to restrict access to none."
)
allowed_approval_category_ids = fields.Many2many(
'approval.category',
'res_users_approval_category_rel',
'user_id',
'category_id',
string="Allowed Approvals",
help="Approval Categories this user is allowed to access. Leave empty to restrict access to none."
)
allowed_quality_checks = fields.Boolean(
string="Is Allowed todo Quality Checks?",
default=False,
help="If checked, this user can see the Quality Checks button on Manufacturing Orders.",
prefetch=False,
)
def _get_allowed_ids_sql(self, table_name, column_name):
self.ensure_one()
self.env.cr.execute(f"SELECT {column_name} FROM {table_name} WHERE user_id = %s", (self.id,))
return [r[0] for r in self.env.cr.fetchall()]
def sql_allowed_warehouse_ids(self):
return self._get_allowed_ids_sql('res_users_stock_warehouse_rel', 'warehouse_id')
def sql_allowed_picking_type_ids(self):
return self._get_allowed_ids_sql('res_users_stock_picking_type_rel', 'picking_type_id')
def sql_allowed_location_ids(self):
return self._get_allowed_ids_sql('res_users_stock_location_rel', 'location_id')
def sql_allowed_location_warehouse_ids(self):
self.ensure_one()
self.env.cr.execute("""
SELECT l.warehouse_id
FROM res_users_stock_location_rel r
JOIN stock_location l ON r.location_id = l.id
WHERE r.user_id = %s AND l.warehouse_id IS NOT NULL
""", (self.id,))
return [r[0] for r in self.env.cr.fetchall()]
def sql_allowed_workcenter_ids(self):
return self._get_allowed_ids_sql('res_users_mrp_workcenter_rel', 'workcenter_id')
def sql_allowed_approval_category_ids(self):
return self._get_allowed_ids_sql('res_users_approval_category_rel', 'category_id')