diff --git a/res_partner_operating_unit/__manifest__.py b/res_partner_operating_unit/__manifest__.py index 85692a1232..d2df41f89f 100644 --- a/res_partner_operating_unit/__manifest__.py +++ b/res_partner_operating_unit/__manifest__.py @@ -13,7 +13,7 @@ "category": "Generic", "depends": ["operating_unit"], "license": "LGPL-3", - "data": ["security/res_partner_security.xml", "views/res_partner_view.xml"], + "data": ["views/res_partner_view.xml"], "installable": True, "pre_init_hook": "pre_init_hook", } diff --git a/res_partner_operating_unit/models/res_partner.py b/res_partner_operating_unit/models/res_partner.py index b63570a680..32375d1f6b 100644 --- a/res_partner_operating_unit/models/res_partner.py +++ b/res_partner_operating_unit/models/res_partner.py @@ -8,17 +8,34 @@ class ResPartner(models.Model): _inherit = "res.partner" _check_company_auto = True - @api.model - def _default_operating_unit(self): - user = self.env["res.users"].browse(self.env.user.id) - return user.default_operating_unit_id - operating_unit_ids = fields.Many2many( - "operating.unit", - "operating_unit_partner_rel", - "partner_id", - "operating_unit_id", - "Operating Units", - required=True, - default=lambda self: self._default_operating_unit(), + comodel_name="operating.unit", + relation="operating_unit_partner_rel", + column1="partner_id", + column2="operating_unit_id", + string="Operating Units", ) + + @api.model + def _user_ous_domain(self): + ou_ids = self.env.user.operating_unit_ids.ids + domain = [ + "|", + ("operating_unit_ids", "in", ou_ids), + ("operating_unit_ids", "=", False), + ] + return domain + + # Extending methods to replace a record rule. + # Ref: https://github.com/OCA/operating-unit/issues/258 + @api.model + def search(self, args, offset=0, limit=None, order=None): + # Get the OUs of the user + domain = self._user_ous_domain() + return super().search(domain + args, offset=offset, limit=limit, order=order) + + @api.model + def search_count(self, args, limit=None): + # Get the OUs of the user + domain = self._user_ous_domain() + return super().search_count(domain + args, limit=limit) diff --git a/res_partner_operating_unit/models/res_users.py b/res_partner_operating_unit/models/res_users.py index 6810b7e253..31c3e18978 100644 --- a/res_partner_operating_unit/models/res_users.py +++ b/res_partner_operating_unit/models/res_users.py @@ -9,42 +9,39 @@ class ResUsers(models.Model): _inherit = "res.users" + def _sync_partner_default_operating_unit(self): + for user in self: + if user.default_operating_unit_id: + user.partner_id.operating_unit_ids = [ + Command.link(user.default_operating_unit_id.id) + ] + user.check_partner_operating_unit() + @api.model_create_multi def create(self, vals_list): users = super().create(vals_list) for user in users: - user_ou = user.default_operating_unit_id or user._default_operating_unit() - if not user_ou: - user.check_partner_operating_unit() - continue - user.partner_id.operating_unit_ids = [Command.link(user_ou.id)] - user.check_partner_operating_unit() + user._sync_partner_default_operating_unit() return users def write(self, vals): - for user in self: - res = super().write(vals) - if vals.get("default_operating_unit_id"): - # Add the new OU - user.partner_id.operating_unit_ids = [ - Command.link(user.default_operating_unit_id.id) - ] - user.check_partner_operating_unit() - return res + res = super().write(vals) + if vals.get("default_operating_unit_id"): + for user in self: + user._sync_partner_default_operating_unit() + return res def check_partner_operating_unit(self): - self.ensure_one() - if ( - self.partner_id.operating_unit_ids - and self.default_operating_unit_id - and ( - self.default_operating_unit_id.id - not in self.partner_id.operating_unit_ids.ids - ) - ): - raise UserError( - _( - "The operating units of the partner must include the default " - "one of the user." + for user in self: + if ( + user.partner_id.operating_unit_ids + and user.default_operating_unit_id + and user.default_operating_unit_id.id + not in user.partner_id.operating_unit_ids.ids + ): + raise UserError( + _( + "The operating units of the partner must include the default " + "one of the user." + ) ) - ) diff --git a/res_partner_operating_unit/security/res_partner_security.xml b/res_partner_operating_unit/security/res_partner_security.xml deleted file mode 100644 index 9f0441417f..0000000000 --- a/res_partner_operating_unit/security/res_partner_security.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - ['|', ('operating_unit_ids', '=', False), ('operating_unit_ids', 'in', operating_unit_ids)] - Partner from allowed operating units - - - - - - - -