feat: add res.users override to enforce active company assignment during creation and write operations
This commit is contained in:
parent
96d86809a6
commit
5da11587d2
@ -1,3 +1,4 @@
|
||||
from . import res_partner
|
||||
from . import pos_order
|
||||
from . import loyalty_card
|
||||
from . import res_users
|
||||
|
||||
54
models/res_users.py
Normal file
54
models/res_users.py
Normal file
@ -0,0 +1,54 @@
|
||||
from odoo import models, api
|
||||
|
||||
class ResUsers(models.Model):
|
||||
_inherit = 'res.users'
|
||||
|
||||
def copy(self, default=None):
|
||||
portal_template = self.env.ref('base.template_portal_user_id', raise_if_not_found=False)
|
||||
if portal_template and self.id == portal_template.id:
|
||||
default = default or {}
|
||||
# Find first active company
|
||||
active_company = self.env['res.company'].search([('active', '=', True)], limit=1)
|
||||
if active_company:
|
||||
default['company_id'] = active_company.id
|
||||
default['company_ids'] = [(6, 0, [active_company.id])]
|
||||
return super().copy(default=default)
|
||||
|
||||
@api.model_create_multi
|
||||
def create(self, vals_list):
|
||||
portal_group = self.env.ref('base.group_portal', raise_if_not_found=False)
|
||||
active_company = self.env['res.company'].search([('active', '=', True)], limit=1)
|
||||
|
||||
for vals in vals_list:
|
||||
is_portal = False
|
||||
if portal_group:
|
||||
group_commands = vals.get('group_ids', [])
|
||||
for cmd in group_commands:
|
||||
if cmd[0] == 4 and cmd[1] == portal_group.id:
|
||||
is_portal = True
|
||||
elif cmd[0] == 6 and portal_group.id in cmd[2]:
|
||||
is_portal = True
|
||||
|
||||
company_id = vals.get('company_id')
|
||||
if company_id:
|
||||
company = self.env['res.company'].browse(company_id)
|
||||
if not company.active:
|
||||
if active_company:
|
||||
vals['company_id'] = active_company.id
|
||||
vals['company_ids'] = [(6, 0, [active_company.id])]
|
||||
elif is_portal and active_company:
|
||||
vals['company_id'] = active_company.id
|
||||
vals['company_ids'] = [(6, 0, [active_company.id])]
|
||||
|
||||
return super().create(vals_list)
|
||||
|
||||
def write(self, vals):
|
||||
if 'company_id' in vals:
|
||||
company = self.env['res.company'].browse(vals['company_id'])
|
||||
if not company.active:
|
||||
active_company = self.env['res.company'].search([('active', '=', True)], limit=1)
|
||||
if active_company:
|
||||
vals['company_id'] = active_company.id
|
||||
if 'company_ids' not in vals:
|
||||
vals['company_ids'] = [(6, 0, [active_company.id])]
|
||||
return super().write(vals)
|
||||
Loading…
Reference in New Issue
Block a user