88class AuditlogRule (models .Model ):
99 _inherit = "auditlog.rule"
1010
11- field_ids = fields .Many2many (
12- "ir.model.fields"
13- )
14- group_ids = fields .Many2many (
15- "res.groups" ,
16- help = """Groups that will be allowed to see the logged fields, if left empty
17- default will be all users with a login""" ,
11+ auditlog_line_access_rule_ids = fields .One2many (
12+ 'auditlog.line.access.rule' , 'auditlog_rule_id' ,
13+ ondelete = 'cascade'
1814 )
1915
2016 @api .onchange ("model_id" )
2117 def onchange_model_id (self ):
2218 # if model changes we must wipe out all field ids
23- self .field_ids = False
24-
25- def needs_rule (self ):
26- self .ensure_one ()
27- return bool (self .group_ids )
28-
29- def get_linked_rules (self ):
30- # return with context key so that deletion will not be forbidden
31- return self .with_context (auditlog_write = True ).env ["ir.rule" ].search (
32- [("auditlog_id" , "in" , self .ids )])
33-
34- def get_field_ids_domain (self ):
35- """note this solution will work only with a hardcoded design of models,
36- because on initialization , self.model_id.id still is not defined.
37- for now, to keep generality we put the filtering in the view."""
38- return [
39- ("model_id" , "=" , self .env .ref ("base.model_res_partner" ).id ),
40- ("name" , "not in" , FIELDS_BLACKLIST ),
41- ]
19+ self .auditlog_line_access_rule_ids .unlink ()
4220
21+ @api .multi
4322 def unlink (self ):
44- # if we delete auditlog rule, corresponding ir.rules are removed
45- # TODO PROPOSAL: a warning here with detailed information?
46- to_delete = self .get_linked_rules ()
23+ lines = self .mapped ('auditlog_line_access_rule_ids' )
4724 res = super (AuditlogRule , self ).unlink ()
4825 if res :
49- res = res and to_delete .unlink ()
50- return res
51-
52- def add_default_group_if_needed (self ):
53- self .ensure_one ()
54- res = False
55- if not self .group_ids and self .field_ids :
56- # if group has been removed and no group specified ad base user default
57- res = self .with_context (no_iter = True ).write (
58- {"group_ids" : [(6 , 0 , [self .env .ref ("base.group_user" ).id ])]})
59- return res
60-
61- @api .model
62- def create (self , vals ):
63- res = super (AuditlogRule , self ).create (vals )
64- res .add_default_group_if_needed ()
65- if res .needs_rule ():
66- res .generate_rules ()
26+ lines .unlink ()
6727 return res
6828
69- @api .multi
70- def write (self , vals ):
71- res = super (AuditlogRule , self ).write (vals )
72- for this in self :
73- added = this .add_default_group_if_needed ()
74- if any ([x in vals for x in ("group_ids" , "field_ids" , "model_id" , "all_fields" )]) or added :
75- if this .needs_rule ():
76- this .generate_rules ()
77- else :
78- this .get_linked_rules ().unlink ()
79- return res
80-
81- def generate_rules (self ):
82- old_rule = self .env ["ir.rule" ].search ([("auditlog_id" , "=" , self .id )], limit = 1 )
83- values = self ._prepare_rule_values ()
84- if old_rule :
85- old_rule .with_context (auditlog_write = True ).write (values )
86- else :
87- self .with_context (auditlog_write = True ).env ["ir.rule" ].create (values )
88-
89- def _prepare_rule_values (self ):
90- domain_force = "[" + " ('log_id.model_id' , '=', %s)," % (self .model_id .id )
91- if self .field_ids :
92- domain_force += "('field_id', 'in', %s)" % (self .field_ids .ids )
93- domain_force += "]"
94- return {
95- "name" : "auditlog_extended_%s" % self .id ,
96- "model_id" : self .env .ref ("auditlog.model_auditlog_log_line" ).id ,
97- "groups" : [(6 , 0 , self .group_ids .ids )],
98- "perm_read" : True ,
99- "domain_force" : domain_force ,
100- "auditlog_id" : self .id ,
101- }
102-
10329 @api .multi
10430 def subscribe (self ):
31+ super (AuditlogRule , self ).subscribe ()
10532 act_window_model = self .env ['ir.actions.act_window' ]
10633 for rule in self :
10734 domain = "[('log_id.model_id', '=', %s), ('log_id.res_id', '=', active_id)]" % (
@@ -116,3 +43,4 @@ def subscribe(self):
11643 act_window = act_window_model .sudo ().create (vals )
11744 rule .write ({'state' : 'subscribed' , 'action_id' : act_window .id })
11845 return True
46+
0 commit comments