From 69fa7468923c423882edb65639b8493264129acc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20BEAU?= <sebastien.beau@akretion.com>
Date: Mon, 11 Jan 2021 15:13:16 +0100
Subject: [PATCH 1/6] [IMP] add module to manage ir.rule only in code

---
 security_rule_not_editable/__init__.py        |  1 +
 security_rule_not_editable/__manifest__.py    | 25 +++++++++++++++++++
 security_rule_not_editable/models/__init__.py |  2 ++
 .../models/ir_model_data.py                   | 17 +++++++++++++
 security_rule_not_editable/models/ir_rule.py  | 23 +++++++++++++++++
 5 files changed, 68 insertions(+)
 create mode 100644 security_rule_not_editable/__init__.py
 create mode 100644 security_rule_not_editable/__manifest__.py
 create mode 100644 security_rule_not_editable/models/__init__.py
 create mode 100644 security_rule_not_editable/models/ir_model_data.py
 create mode 100644 security_rule_not_editable/models/ir_rule.py

diff --git a/security_rule_not_editable/__init__.py b/security_rule_not_editable/__init__.py
new file mode 100644
index 000000000..0650744f6
--- /dev/null
+++ b/security_rule_not_editable/__init__.py
@@ -0,0 +1 @@
+from . import models
diff --git a/security_rule_not_editable/__manifest__.py b/security_rule_not_editable/__manifest__.py
new file mode 100644
index 000000000..52f8d462a
--- /dev/null
+++ b/security_rule_not_editable/__manifest__.py
@@ -0,0 +1,25 @@
+# Copyright 2021 Akretion (https://www.akretion.com).
+# @author Sébastien BEAU <sebastien.beau@akretion.com>
+# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
+
+{
+    "name": "Security Rule not editable",
+    "summary": "Forbid editing rule form UI force using code",
+    "version": "14.0.1.0.0",
+    "category": "Base",
+    "website": "https://github.com/akretion/ak-odoo-incubator",
+    "author": " Akretion",
+    "license": "AGPL-3",
+    "application": False,
+    "installable": True,
+    "external_dependencies": {
+        "python": [],
+        "bin": [],
+    },
+    "depends": [
+        "base",
+    ],
+    "data": [],
+    "demo": [],
+    "qweb": [],
+}
diff --git a/security_rule_not_editable/models/__init__.py b/security_rule_not_editable/models/__init__.py
new file mode 100644
index 000000000..f37d348a3
--- /dev/null
+++ b/security_rule_not_editable/models/__init__.py
@@ -0,0 +1,2 @@
+from . import ir_model_data
+from . import ir_rule
diff --git a/security_rule_not_editable/models/ir_model_data.py b/security_rule_not_editable/models/ir_model_data.py
new file mode 100644
index 000000000..a28227330
--- /dev/null
+++ b/security_rule_not_editable/models/ir_model_data.py
@@ -0,0 +1,17 @@
+# Copyright 2021 Akretion (https://www.akretion.com).
+# @author Sébastien BEAU <sebastien.beau@akretion.com>
+# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
+
+from odoo import models
+
+
+class IrModelData(models.Model):
+    _inherit = "ir.model.data"
+
+    def _lookup_xmlids(self, xml_ids, model):
+        res = super()._lookup_xmlids(xml_ids, model)
+        if model._name == "ir.rule":
+            # Make xml updatable
+            return [item[0:5] + (False,) + item[6:] for item in res]
+        else:
+            return res
diff --git a/security_rule_not_editable/models/ir_rule.py b/security_rule_not_editable/models/ir_rule.py
new file mode 100644
index 000000000..623925cc5
--- /dev/null
+++ b/security_rule_not_editable/models/ir_rule.py
@@ -0,0 +1,23 @@
+# Copyright 2021 Akretion (https://www.akretion.com).
+# @author Sébastien BEAU <sebastien.beau@akretion.com>
+# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
+
+from odoo import _, api, models
+from odoo.exceptions import UserError
+
+
+class IrRule(models.Model):
+    _inherit = "ir.rule"
+
+    def _ensure_install_mode(self):
+        if not self._context.get("install_mode"):
+            raise UserError(_("Rule are not editable, put your rule in your code"))
+
+    @api.model_create_multi
+    def create(self, vals_list):
+        self._ensure_install_mode()
+        return super().create(vals_list)
+
+    def write(self, vals):
+        self._ensure_install_mode()
+        return super().write(vals)

From 0dbed1ccddc85dc2289db883b3f37affb04b10bb Mon Sep 17 00:00:00 2001
From: beau sebastien <sebastien.beau@akretion.com>
Date: Wed, 13 Jan 2021 21:49:17 +0100
Subject: [PATCH 2/6] Apply suggestions from code review

Co-authored-by: David Beal <david.beal@akretion.com>
---
 security_rule_not_editable/__manifest__.py   | 1 -
 security_rule_not_editable/models/ir_rule.py | 2 +-
 2 files changed, 1 insertion(+), 2 deletions(-)

diff --git a/security_rule_not_editable/__manifest__.py b/security_rule_not_editable/__manifest__.py
index 52f8d462a..0ae40c406 100644
--- a/security_rule_not_editable/__manifest__.py
+++ b/security_rule_not_editable/__manifest__.py
@@ -21,5 +21,4 @@
     ],
     "data": [],
     "demo": [],
-    "qweb": [],
 }
diff --git a/security_rule_not_editable/models/ir_rule.py b/security_rule_not_editable/models/ir_rule.py
index 623925cc5..de033255f 100644
--- a/security_rule_not_editable/models/ir_rule.py
+++ b/security_rule_not_editable/models/ir_rule.py
@@ -10,7 +10,7 @@ class IrRule(models.Model):
     _inherit = "ir.rule"
 
     def _ensure_install_mode(self):
-        if not self._context.get("install_mode"):
+        if not self.env.context.get("install_mode"):
             raise UserError(_("Rule are not editable, put your rule in your code"))
 
     @api.model_create_multi

From c5b0aaec9d79b9e73c82d26ca617b2454968a45a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20BEAU?= <sebastien.beau@akretion.com>
Date: Fri, 19 Feb 2021 01:38:26 +0100
Subject: [PATCH 3/6] [FIX] fix reloading existing no update rule

---
 security_rule_not_editable/__init__.py       |  1 +
 security_rule_not_editable/tools/__init__.py |  1 +
 security_rule_not_editable/tools/convert.py  | 20 ++++++++++++++++++++
 3 files changed, 22 insertions(+)
 create mode 100644 security_rule_not_editable/tools/__init__.py
 create mode 100644 security_rule_not_editable/tools/convert.py

diff --git a/security_rule_not_editable/__init__.py b/security_rule_not_editable/__init__.py
index 0650744f6..738a2eec0 100644
--- a/security_rule_not_editable/__init__.py
+++ b/security_rule_not_editable/__init__.py
@@ -1 +1,2 @@
 from . import models
+from . import tools
diff --git a/security_rule_not_editable/tools/__init__.py b/security_rule_not_editable/tools/__init__.py
new file mode 100644
index 000000000..99a9527ec
--- /dev/null
+++ b/security_rule_not_editable/tools/__init__.py
@@ -0,0 +1 @@
+from . import convert
diff --git a/security_rule_not_editable/tools/convert.py b/security_rule_not_editable/tools/convert.py
new file mode 100644
index 000000000..7f57f53a2
--- /dev/null
+++ b/security_rule_not_editable/tools/convert.py
@@ -0,0 +1,20 @@
+# Copyright 2021 Akretion (https://www.akretion.com).
+# @author Sébastien BEAU <sebastien.beau@akretion.com>
+# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
+
+
+from odoo.tools.convert import xml_import
+
+ori_tag_record = xml_import._tag_record
+
+
+def _tag_record(self, rec):
+    noupdate = self._noupdate
+    if rec.get("model") == "ir.rule":
+        self._noupdate = [False]
+    ori_tag_record(self, rec)
+    if rec.get("model") == "ir.rule":
+        self._noupdate = noupdate
+
+
+xml_import._tag_record = _tag_record

From 9258d718eaef8726a71a647f17d33c9d70b9a62d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20BEAU?= <sebastien.beau@akretion.com>
Date: Mon, 22 Mar 2021 00:42:27 +0100
Subject: [PATCH 4/6] [FIX] fix sharing intercompany contact

---
 security_rule_not_editable/models/ir_rule.py | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/security_rule_not_editable/models/ir_rule.py b/security_rule_not_editable/models/ir_rule.py
index de033255f..3252f5b87 100644
--- a/security_rule_not_editable/models/ir_rule.py
+++ b/security_rule_not_editable/models/ir_rule.py
@@ -18,6 +18,15 @@ def create(self, vals_list):
         self._ensure_install_mode()
         return super().create(vals_list)
 
+    def _is_useless_write_on_active(self, vals):
+        """Return True if we try to only write the field 'active' and the
+        records already have the same value"""
+        return set(vals.keys()) == {"active"} and set(self.mapped("active")) == {
+            vals["active"]
+        }
+
     def write(self, vals):
+        if self._is_useless_write_on_active(vals):
+            return True
         self._ensure_install_mode()
         return super().write(vals)

From eafb6a0aaa0d5a4a58beb897f515f91fe06b42e2 Mon Sep 17 00:00:00 2001
From: Kev-Roche <kevin.roche@akretion.com>
Date: Tue, 23 Jul 2024 15:26:43 +0200
Subject: [PATCH 5/6] [IMP] security_rule_not_editable: black, isort, prettier

---
 security_rule_not_editable/__manifest__.py    |  8 ++++---
 .../security/ir_model_access.xml              | 22 +++++++++++++++++++
 2 files changed, 27 insertions(+), 3 deletions(-)
 create mode 100644 security_rule_not_editable/security/ir_model_access.xml

diff --git a/security_rule_not_editable/__manifest__.py b/security_rule_not_editable/__manifest__.py
index 0ae40c406..998b95219 100644
--- a/security_rule_not_editable/__manifest__.py
+++ b/security_rule_not_editable/__manifest__.py
@@ -1,11 +1,11 @@
-# Copyright 2021 Akretion (https://www.akretion.com).
+# Copyright 2024 Akretion (https://www.akretion.com).
 # @author Sébastien BEAU <sebastien.beau@akretion.com>
 # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
 
 {
     "name": "Security Rule not editable",
     "summary": "Forbid editing rule form UI force using code",
-    "version": "14.0.1.0.0",
+    "version": "16.0.1.0.0",
     "category": "Base",
     "website": "https://github.com/akretion/ak-odoo-incubator",
     "author": " Akretion",
@@ -19,6 +19,8 @@
     "depends": [
         "base",
     ],
-    "data": [],
+    "data": [
+        "security/ir_model_access.xml",
+    ],
     "demo": [],
 }
diff --git a/security_rule_not_editable/security/ir_model_access.xml b/security_rule_not_editable/security/ir_model_access.xml
new file mode 100644
index 000000000..5072edd94
--- /dev/null
+++ b/security_rule_not_editable/security/ir_model_access.xml
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<odoo>
+
+    <record id="base.access_ir_rule_group_erp_manager" model="ir.model.access">
+        <field name="perm_write">False</field>
+        <field name="perm_create">False</field>
+        <field name="perm_unlink">False</field>
+    </record>
+
+    <record id="base.access_ir_model_access_group_erp_manager" model="ir.model.access">
+        <field name="perm_write">False</field>
+        <field name="perm_create">False</field>
+        <field name="perm_unlink">False</field>
+    </record>
+
+    <record id="base.access_res_groups_group_erp_manager" model="ir.model.access">
+        <field name="perm_write">False</field>
+        <field name="perm_create">False</field>
+        <field name="perm_unlink">False</field>
+    </record>
+
+</odoo>

From 465f0cb7daf008bf5d2e0af49a1a9c540d96bf51 Mon Sep 17 00:00:00 2001
From: Kev-Roche <kevin.roche@akretion.com>
Date: Tue, 23 Jul 2024 16:28:41 +0200
Subject: [PATCH 6/6] [MIG] security_rule_not_editable: Migration to 16.0

---
 security_rule_not_editable/tools/convert.py                 | 4 ++--
 .../odoo/addons/security_rule_not_editable                  | 1 +
 setup/security_rule_not_editable/setup.py                   | 6 ++++++
 3 files changed, 9 insertions(+), 2 deletions(-)
 create mode 120000 setup/security_rule_not_editable/odoo/addons/security_rule_not_editable
 create mode 100644 setup/security_rule_not_editable/setup.py

diff --git a/security_rule_not_editable/tools/convert.py b/security_rule_not_editable/tools/convert.py
index 7f57f53a2..1a0a3b19e 100644
--- a/security_rule_not_editable/tools/convert.py
+++ b/security_rule_not_editable/tools/convert.py
@@ -8,11 +8,11 @@
 ori_tag_record = xml_import._tag_record
 
 
-def _tag_record(self, rec):
+def _tag_record(self, rec, extra_vals=None):
     noupdate = self._noupdate
     if rec.get("model") == "ir.rule":
         self._noupdate = [False]
-    ori_tag_record(self, rec)
+    ori_tag_record(self, rec, extra_vals)
     if rec.get("model") == "ir.rule":
         self._noupdate = noupdate
 
diff --git a/setup/security_rule_not_editable/odoo/addons/security_rule_not_editable b/setup/security_rule_not_editable/odoo/addons/security_rule_not_editable
new file mode 120000
index 000000000..20b2a413c
--- /dev/null
+++ b/setup/security_rule_not_editable/odoo/addons/security_rule_not_editable
@@ -0,0 +1 @@
+../../../../security_rule_not_editable
\ No newline at end of file
diff --git a/setup/security_rule_not_editable/setup.py b/setup/security_rule_not_editable/setup.py
new file mode 100644
index 000000000..28c57bb64
--- /dev/null
+++ b/setup/security_rule_not_editable/setup.py
@@ -0,0 +1,6 @@
+import setuptools
+
+setuptools.setup(
+    setup_requires=['setuptools-odoo'],
+    odoo_addon=True,
+)