Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][MIG] security_rule_not_editable #313

Open
wants to merge 6 commits into
base: 16.0
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions security_rule_not_editable/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import models
from . import tools
26 changes: 26 additions & 0 deletions security_rule_not_editable/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2024 Akretion (https://www.akretion.com).
# @author Sébastien BEAU <[email protected]>
# 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": "16.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": [
"security/ir_model_access.xml",
],
"demo": [],
}
2 changes: 2 additions & 0 deletions security_rule_not_editable/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import ir_model_data
from . import ir_rule
17 changes: 17 additions & 0 deletions security_rule_not_editable/models/ir_model_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2021 Akretion (https://www.akretion.com).
# @author Sébastien BEAU <[email protected]>
# 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
32 changes: 32 additions & 0 deletions security_rule_not_editable/models/ir_rule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2021 Akretion (https://www.akretion.com).
# @author Sébastien BEAU <[email protected]>
# 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.env.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 _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)
22 changes: 22 additions & 0 deletions security_rule_not_editable/security/ir_model_access.xml
Original file line number Diff line number Diff line change
@@ -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>
1 change: 1 addition & 0 deletions security_rule_not_editable/tools/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import convert
20 changes: 20 additions & 0 deletions security_rule_not_editable/tools/convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2021 Akretion (https://www.akretion.com).
# @author Sébastien BEAU <[email protected]>
# 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, extra_vals=None):
noupdate = self._noupdate
if rec.get("model") == "ir.rule":
self._noupdate = [False]
ori_tag_record(self, rec, extra_vals)
if rec.get("model") == "ir.rule":
self._noupdate = noupdate


xml_import._tag_record = _tag_record
6 changes: 6 additions & 0 deletions setup/security_rule_not_editable/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)
Loading