Skip to content
This repository was archived by the owner on Feb 16, 2026. It is now read-only.

Commit 9f500af

Browse files
committed
[ADD] review fixes
1 parent dee4f2d commit 9f500af

5 files changed

Lines changed: 87 additions & 60 deletions

File tree

auditlog_security/__manifest__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
{
55
"name": "Audit Log Permissions",
6-
"version": "11.0.1.0.1",
6+
"version": "11.0.1.0.0",
77
"author": "Therp B.V.,Odoo Community Association (OCA)",
88
"license": "AGPL-3",
99
"website": "https://github.com/OCA/server-tools/",
@@ -13,7 +13,6 @@
1313
"contacts",
1414
],
1515
"data": [
16-
"demo/auditlog_rule.xml",
1716
"views/auditlog_view.xml",
1817
],
1918
"application": True,

auditlog_security/demo/auditlog_rule.xml

Lines changed: 0 additions & 10 deletions
This file was deleted.

auditlog_security/models/auditlog_rule.py

Lines changed: 66 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright 2021 Therp B.V.
22
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
33

4-
from odoo import models, fields, api, modules, _
4+
from odoo import exceptions, models, fields, api, modules, _
55
from odoo.addons.auditlog.models.rule import FIELDS_BLACKLIST
66

77

@@ -10,20 +10,36 @@ class AuditlogRule(models.Model):
1010

1111
field_ids = fields.Many2many(
1212
"ir.model.fields",
13-
required=True,
1413
)
1514
group_ids = fields.Many2many(
1615
"res.groups",
17-
string="Groups",
16+
default= lambda x: x.env.ref('base.group_user'),
1817
help="""Groups that will be allowed to see the logged fields, if left empty
19-
all groups will be allowed (global rule creation)""",
18+
default will be all users with a login""",
2019
)
20+
all_fields = fields.Boolean(
21+
string="Publicly visible log lines for all fields",
22+
readonly=True,
23+
compute="compute_all_fields",
24+
store=False,
25+
help="Check this field if you want this model to be visible by everyone")
26+
27+
@api.depends('field_ids')
28+
def compute_all_fields(self):
29+
for this in self.sudo():
30+
this.all_fields = not bool(len(this.field_ids))
31+
32+
@api.onchange('model_id')
33+
def onchange_model_id(self):
34+
# if model changes we must wipe out all field ids
35+
self.field_ids = False
36+
2137

22-
""" note this solution will work only with a hardcoded design of models,
23-
because on initialization , self.model_id.id still is not defined.
24-
for now, to keep generality we put the filtering in the view."""
2538

2639
def get_field_ids_domain(self):
40+
""" note this solution will work only with a hardcoded design of models,
41+
because on initialization , self.model_id.id still is not defined.
42+
for now, to keep generality we put the filtering in the view."""
2743
return [
2844
("model_id", "=", self.env.ref("base.model_res_partner").id),
2945
("name", "not in", FIELDS_BLACKLIST),
@@ -32,40 +48,63 @@ def get_field_ids_domain(self):
3248
def unlink(self):
3349
# if we delete auditlog rule, corresponding ir.rules are removed
3450
# TODO PROPOSAL: a warning here with detailed information?
51+
to_delete = self.env["ir.rule"].with_context(auditlog_write=True).search(
52+
[("auditlog_id", "=", self.ids)])
3553
res = super(AuditlogRule, self).unlink()
36-
return (
37-
res
38-
and self.env["ir.rule"]
39-
.with_context(auditlog_write=True)
40-
.search([("auditlog_id", "in", self.ids)])
41-
.unlink()
42-
)
54+
if res:
55+
res = res and to_delete.unlink()
56+
return res
4357

58+
@api.model
59+
def create(self, vals):
60+
if 'group_ids' not in vals:
61+
# if group has been removed and no group specified ad base user default
62+
vals['group_ids'] = [(6, 0, [self.env.ref('base.group_user').id])]
63+
if 'field_ids' not in vals and not vals.get('all_fields'):
64+
# this was done because of a
65+
# bug in attrs={'required': [('all_fields', '!=', True}
66+
raise exceptions.ValidationError(
67+
_("please specify fields to log or make all_fields true"))
68+
res = super(AuditlogRule, self).create(vals)
69+
res.generate_rules()
70+
return res
71+
4472
@api.multi
4573
def write(self, vals):
4674
res = super(AuditlogRule, self).write(vals)
4775
for this in self:
48-
if any([x in vals for x in ("group_ids", "field_ids", "model_id")]):
76+
if not this.group_ids:
77+
# if no group is specified, we will not create a global, we
78+
# always put at least base.group_user
79+
this.write(
80+
{'group_ids': [(6, 0, [
81+
self.env.ref('base.group_user').id])]})
82+
if any([x in vals for x in (
83+
"group_ids", "field_ids", "model_id", "all_fields")]):
4984
this.generate_rules()
5085
return res
5186

5287
def generate_rules(self):
53-
old_rule = self.env["ir.rule"].search([("auditlog_id", "=", self.id)], limit=1)
54-
domain_force = (
55-
"[ "
56-
+ " ('log_id.model_id' , '=', %s)," % (self.model_id.id)
57-
+ "('field_id', 'in', %s)" % (self.field_ids.ids)
58-
+ "]"
59-
)
60-
values = {
88+
old_rule = self.env["ir.rule"].search(
89+
[("auditlog_id", "=", self.id)], limit=1)
90+
values = self._prepare_rule_values()
91+
if old_rule:
92+
old_rule.with_context(auditlog_write=True).write(values)
93+
else:
94+
self.with_context(auditlog_write=True).env["ir.rule"].create(
95+
values)
96+
97+
def _prepare_rule_values(self):
98+
domain_force = "[" + " ('log_id.model_id' , '=', %s)," % (
99+
self.model_id.id)
100+
if self.field_ids:
101+
domain_force += "('field_id', 'in', %s)" % (self.field_ids.ids)
102+
domain_force += "]"
103+
return {
61104
"name": "auditlog_extended_%s" % self.id,
62105
"model_id": self.env.ref("auditlog.model_auditlog_log_line").id,
63106
"groups": [(6, 0, self.group_ids.ids)],
64107
"perm_read": True,
65108
"domain_force": domain_force,
66109
"auditlog_id": self.id,
67110
}
68-
if old_rule:
69-
old_rule.with_context(auditlog_write=True).write(values)
70-
else:
71-
self.with_context(auditlog_write=True).env["ir.rule"].create(values)

auditlog_security/models/ir_rule.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,6 @@ class IrRule(models.Model):
1313
help="Auditlog Rule that generated this ir.rule",
1414
)
1515

16-
def prevent_rule_mod(self, vals=None):
17-
auditlog_write = self.env.context.get("auditlog_write")
18-
if "auditlog_id" in vals and not auditlog_write:
19-
raise exceptions.validationerror(_("""Cannot change auditlog_id"""))
20-
for this in self:
21-
if this.auditlog_id and not auditlog_write:
22-
raise exceptions.validationerror(
23-
_(
24-
"""
25-
auditlog line rules are automatically generated from the auditlog
26-
interface, please use that to edit/delete/"""
27-
)
28-
)
2916

3017
@api.model
3118
def create(self, values):
@@ -35,18 +22,30 @@ def create(self, values):
3522
raise exceptions.ValidationError(
3623
_(
3724
"""
38-
Auditlog line rules are automatically generated from the auditlog
39-
interface, please use that to create"""
25+
Auditlog line rules are automatically generated from the
26+
auditlog interface, please use that to create"""
4027
)
4128
)
4229
return super(IrRule, self).create(values)
4330

4431
@api.multi
4532
def write(self, vals):
46-
self.prevent_rule_mod(vals)
33+
if "auditlog_id" in vals and not self.env.context.get(
34+
'auditlog_write'):
35+
raise exceptions.ValidationError(_("""Cannot change auditlog_id"""))
4736
return super(IrRule, self).write(vals)
4837

4938
@api.multi
5039
def unlink(self):
51-
self.prevent_rule_mod()
40+
auditlog_write = self.env.context.get("auditlog_write")
41+
for this in self:
42+
if this.auditlog_id and not auditlog_write:
43+
raise exceptions.ValidationError(
44+
_(
45+
46+
"""
47+
Auditlog line rules are automatically generated from the
48+
auditlog interface, please use that to delete"""
49+
)
50+
)
5251
return super(IrRule, self).unlink()

auditlog_security/views/auditlog_view.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@
5050
would be skipped anyway, so we repeat here to avoid incongruencies
5151
-->
5252
<field name="field_ids"
53-
domain="[('model_id', '=' , model_id),('name', 'not in', ['id', 'create_uid', 'create_date', 'write_uid', 'write_date','display_name', '__last_update']) ]"
54-
/>
53+
domain="[('model_id', '=' , model_id),('name', 'not in', ['id', 'create_uid', 'create_date', 'write_uid', 'write_date','display_name', '__last_update']) ]"/>
5554
</group>
5655
<group>
57-
<field name="group_ids"/>
56+
<field name="group_ids" />
57+
<field name="all_fields"/>
5858
</group>
5959
</xpath>
6060

@@ -81,7 +81,7 @@
8181
<search string="Log Lines">
8282
<filter name="has_extensions"
8383
domain="['|', ('field_ids','!=', False), ('group_ids', '!=', False)]"
84-
string="Has security Extensions"/>
84+
string="Has Security Extensions"/>
8585
<filter name="global_security"
8686
domain="[('field_ids','!=', False)]"
8787
string="Accessible to all - globally accessible"/>

0 commit comments

Comments
 (0)