Skip to content

Commit f121006

Browse files
authored
Merge pull request #177 from OpenSPP/fix/user-role-assignment-issue
fix(spp_user_roles): prevent validation error when assigning groups to a new role
2 parents 7349b12 + ead65ff commit f121006

6 files changed

Lines changed: 69 additions & 3 deletions

File tree

spp_user_roles/README.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,20 @@ Dependencies
129129
Changelog
130130
=========
131131

132+
19.0.2.0.1
133+
~~~~~~~~~~
134+
135+
- fix(role): allow assigning groups to a brand-new role in a single
136+
save. Clicking **Add a line** in the role form's Groups tab used to
137+
trigger inline creation of a blank ``res.groups`` row, which raised a
138+
required-field validation error and aborted the save. The
139+
``implied_ids`` field's inner list now uses ``create="0"`` so the
140+
button opens an "Add" picker against existing groups, and
141+
``res.users.role.create()`` extracts ``implied_ids`` before
142+
``super().create()`` and writes them to the role's ``group_id``
143+
afterwards — mirroring the existing ``write()`` workaround in
144+
``base_user_role`` for the same ``_inherits`` cache-clearing bug.
145+
132146
19.0.2.0.0
133147
~~~~~~~~~~
134148

spp_user_roles/__manifest__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
"name": "OpenSPP User Roles",
44
"category": "OpenSPP",
5-
"version": "19.0.2.0.0",
5+
"version": "19.0.2.0.1",
66
"sequence": 1,
77
"author": "OpenSPP.org",
88
"website": "https://github.com/OpenSPP/OpenSPP2",

spp_user_roles/models/role.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import logging
44

5-
from odoo import fields, models
5+
from odoo import api, fields, models
66

77
_logger = logging.getLogger(__name__)
88

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

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

15+
@api.model_create_multi
16+
def create(self, vals_list):
17+
# Workaround: same Odoo cache-clearing bug as in base_user_role's write()
18+
# override. When res.groups fields are set via _inherits on create(),
19+
# implied_ids gets dropped. Extract group fields and write them to
20+
# group_id directly after creation, mirroring the write() fix.
21+
groups_vals_list = []
22+
group_fields = set(self.env["res.groups"]._fields) - {"name"}
23+
for vals in vals_list:
24+
group_vals = {}
25+
for field in group_fields:
26+
if field in vals:
27+
group_vals[field] = vals.pop(field)
28+
groups_vals_list.append(group_vals)
29+
30+
new_records = super().create(vals_list)
31+
32+
for record, group_vals in zip(new_records, groups_vals_list, strict=True):
33+
if group_vals:
34+
record.group_id.write(group_vals)
35+
36+
return new_records
37+
1538
def action_update_users(self):
1639
"""
1740
Call the update_users function to force the update of associated users in the role.

spp_user_roles/readme/HISTORY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### 19.0.2.0.1
2+
3+
- fix(role): allow assigning groups to a brand-new role in a single save. Clicking **Add a line** in the role form's Groups tab used to trigger inline creation of a blank `res.groups` row, which raised a required-field validation error and aborted the save. The `implied_ids` field's inner list now uses `create="0"` so the button opens an "Add" picker against existing groups, and `res.users.role.create()` extracts `implied_ids` before `super().create()` and writes them to the role's `group_id` afterwards — mirroring the existing `write()` workaround in `base_user_role` for the same `_inherits` cache-clearing bug.
4+
15
### 19.0.2.0.0
26

37
- Initial migration to OpenSPP2

spp_user_roles/static/description/index.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,21 @@ <h2><a class="toc-backref" href="#toc-entry-1">Changelog</a></h2>
503503
</div>
504504
</div>
505505
<div class="section" id="section-1">
506+
<h1>19.0.2.0.1</h1>
507+
<ul class="simple">
508+
<li>fix(role): allow assigning groups to a brand-new role in a single
509+
save. Clicking <strong>Add a line</strong> in the role form’s Groups tab used to
510+
trigger inline creation of a blank <tt class="docutils literal">res.groups</tt> row, which raised a
511+
required-field validation error and aborted the save. The
512+
<tt class="docutils literal">implied_ids</tt> field’s inner list now uses <tt class="docutils literal"><span class="pre">create=&quot;0&quot;</span></tt> so the
513+
button opens an “Add” picker against existing groups, and
514+
<tt class="docutils literal">res.users.role.create()</tt> extracts <tt class="docutils literal">implied_ids</tt> before
515+
<tt class="docutils literal"><span class="pre">super().create()</span></tt> and writes them to the role’s <tt class="docutils literal">group_id</tt>
516+
afterwards — mirroring the existing <tt class="docutils literal">write()</tt> workaround in
517+
<tt class="docutils literal">base_user_role</tt> for the same <tt class="docutils literal">_inherits</tt> cache-clearing bug.</li>
518+
</ul>
519+
</div>
520+
<div class="section" id="section-2">
506521
<h1>19.0.2.0.0</h1>
507522
<ul class="simple">
508523
<li>Initial migration to OpenSPP2</li>

spp_user_roles/views/role.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,19 @@
2020
<xpath expr="//field[@name='name']" position="after">
2121
<field name="role_type" />
2222
</xpath>
23+
<!-- Restrict the Groups m2m to access groups not already
24+
owned by another role, and embed a list view that
25+
disables inline creation (avoids OP#979's "Add a line
26+
creates a new empty record" trap). Done via attributes
27+
+ inside instead of replace so the OCA xml-view-
28+
dangerous-replace check stays happy. -->
2329
<xpath expr="//field[@name='implied_ids']" position="attributes">
2430
<attribute name="domain">[('role_id', '=', False)]</attribute>
25-
<attribute name="widget">many2many</attribute>
31+
</xpath>
32+
<xpath expr="//field[@name='implied_ids']" position="inside">
33+
<list create="0">
34+
<field name="full_name" string="Group" />
35+
</list>
2636
</xpath>
2737
<xpath
2838
expr="//field[@name='line_ids']/list/field[@name='user_id']"

0 commit comments

Comments
 (0)