Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion spp_user_roles/models/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import logging

from odoo import fields, models
from odoo import api, fields, models

_logger = logging.getLogger(__name__)

Expand All @@ -12,6 +12,29 @@ class ResUsersRoleCustomSPP(models.Model):

role_type = fields.Selection([("local", "Local"), ("global", "Global")], default="global")

@api.model_create_multi
def create(self, vals_list):
# Workaround: same Odoo cache-clearing bug as in base_user_role's write()
# override. When res.groups fields are set via _inherits on create(),
# implied_ids gets dropped. Extract group fields and write them to
# group_id directly after creation, mirroring the write() fix.
groups_vals_list = []
group_fields = set(self.env["res.groups"]._fields) - {"name"}
for vals in vals_list:
group_vals = {}
for field in group_fields:
if field in vals:
group_vals[field] = vals.pop(field)
groups_vals_list.append(group_vals)

new_records = super().create(vals_list)

for record, group_vals in zip(new_records, groups_vals_list, strict=True):
if group_vals:
record.group_id.write(group_vals)
Comment on lines +21 to +34
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation of the create override is broader than necessary. By popping all fields belonging to res.groups (except name), you risk interfering with the standard Odoo _inherits mechanism for fields that don't suffer from the cache-clearing bug. It also makes the creation fragile if other modules add required fields to res.groups. It is recommended to target only the problematic fields, specifically implied_ids, as mentioned in the PR description and mirroring the write workaround in base_user_role.

Suggested change
groups_vals_list = []
group_fields = set(self.env["res.groups"]._fields) - {"name"}
for vals in vals_list:
group_vals = {}
for field in group_fields:
if field in vals:
group_vals[field] = vals.pop(field)
groups_vals_list.append(group_vals)
new_records = super().create(vals_list)
for record, group_vals in zip(new_records, groups_vals_list):
if group_vals:
record.group_id.write(group_vals)
implied_ids_list = [vals.pop("implied_ids", None) for vals in vals_list]
new_records = super().create(vals_list)
for record, implied_ids in zip(new_records, implied_ids_list):
if implied_ids:
record.group_id.write({"implied_ids": implied_ids})


return new_records

def action_update_users(self):
"""
Call the update_users function to force the update of associated users in the role.
Expand Down
12 changes: 11 additions & 1 deletion spp_user_roles/views/role.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,19 @@
<xpath expr="//field[@name='name']" position="after">
<field name="role_type" />
</xpath>
<!-- Restrict the Groups m2m to access groups not already
owned by another role, and embed a list view that
disables inline creation (avoids OP#979's "Add a line
creates a new empty record" trap). Done via attributes
+ inside instead of replace so the OCA xml-view-
dangerous-replace check stays happy. -->
<xpath expr="//field[@name='implied_ids']" position="attributes">
<attribute name="domain">[('role_id', '=', False)]</attribute>
<attribute name="widget">many2many</attribute>
</xpath>
<xpath expr="//field[@name='implied_ids']" position="inside">
<list create="0">
<field name="full_name" string="Group" />
</list>
</xpath>
<xpath
expr="//field[@name='line_ids']/list/field[@name='user_id']"
Expand Down
Loading