From f9176184630e5c70ec33ad16a39738b864fcc513 Mon Sep 17 00:00:00 2001 From: tslai Date: Fri, 5 Feb 2021 11:28:16 +0800 Subject: [PATCH] [12.0][ADD] base_model_restrict_update --- base_model_restrict_update/README.rst | 80 ++++ base_model_restrict_update/__init__.py | 1 + base_model_restrict_update/__manifest__.py | 13 + .../i18n/base_model_restrict_update.po | 56 +++ base_model_restrict_update/i18n/ja_JP.po | 58 +++ base_model_restrict_update/models/__init__.py | 3 + base_model_restrict_update/models/ir_model.py | 14 + .../models/ir_model_access.py | 35 ++ .../models/res_users.py | 18 + .../readme/CONFIGURE.rst | 2 + base_model_restrict_update/readme/CREDITS.rst | 1 + .../readme/DESCRIPTION.rst | 2 + .../static/description/index.html | 426 ++++++++++++++++++ base_model_restrict_update/tests/__init__.py | 1 + .../tests/test_base_model_restrict_update.py | 56 +++ .../views/ir_model_views.xml | 15 + .../views/res_users_views.xml | 18 + 17 files changed, 799 insertions(+) create mode 100644 base_model_restrict_update/README.rst create mode 100644 base_model_restrict_update/__init__.py create mode 100644 base_model_restrict_update/__manifest__.py create mode 100644 base_model_restrict_update/i18n/base_model_restrict_update.po create mode 100644 base_model_restrict_update/i18n/ja_JP.po create mode 100644 base_model_restrict_update/models/__init__.py create mode 100644 base_model_restrict_update/models/ir_model.py create mode 100644 base_model_restrict_update/models/ir_model_access.py create mode 100644 base_model_restrict_update/models/res_users.py create mode 100644 base_model_restrict_update/readme/CONFIGURE.rst create mode 100644 base_model_restrict_update/readme/CREDITS.rst create mode 100644 base_model_restrict_update/readme/DESCRIPTION.rst create mode 100644 base_model_restrict_update/static/description/index.html create mode 100644 base_model_restrict_update/tests/__init__.py create mode 100644 base_model_restrict_update/tests/test_base_model_restrict_update.py create mode 100644 base_model_restrict_update/views/ir_model_views.xml create mode 100644 base_model_restrict_update/views/res_users_views.xml diff --git a/base_model_restrict_update/README.rst b/base_model_restrict_update/README.rst new file mode 100644 index 000000000..fb86df2fe --- /dev/null +++ b/base_model_restrict_update/README.rst @@ -0,0 +1,80 @@ +===================== +Update Restrict Model +===================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png + :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html + :alt: License: LGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github + :target: https://github.com/OCA/server-tools/tree/12.0/base_model_restrict_update + :alt: OCA/server-tools +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/server-tools-12-0/server-tools-12-0-base_model_restrict_update + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/149/12.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module adds a config to apply a global update restriction to specific model, +while only certain users can update the records if the config is enabled. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +Enable the "Update Restrict Model" of specific model to restrict update from unpermitted users. +To set an user as a permitted user to update restricted model(s), click on "Grant Update Permit" in user form. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Quartile Limited + +Other credits +~~~~~~~~~~~~~ + +* This module borrows the idea from 'Moises Lopez ' + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/server-tools `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/base_model_restrict_update/__init__.py b/base_model_restrict_update/__init__.py new file mode 100644 index 000000000..0650744f6 --- /dev/null +++ b/base_model_restrict_update/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/base_model_restrict_update/__manifest__.py b/base_model_restrict_update/__manifest__.py new file mode 100644 index 000000000..6e52e666e --- /dev/null +++ b/base_model_restrict_update/__manifest__.py @@ -0,0 +1,13 @@ +# Copyright 2021 Quartile Limited +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). +{ + "name": "Update Restrict Model", + "version": "12.0.1.0.0", + "depends": ["base"], + "website": "https://github.com/OCA/server-tools", + "author": "Odoo Community Association (OCA), Quartile Limited", + "category": "Others", + "license": "LGPL-3", + "data": ["views/ir_model_views.xml", "views/res_users_views.xml"], + "installable": True, +} diff --git a/base_model_restrict_update/i18n/base_model_restrict_update.po b/base_model_restrict_update/i18n/base_model_restrict_update.po new file mode 100644 index 000000000..5e34a514e --- /dev/null +++ b/base_model_restrict_update/i18n/base_model_restrict_update.po @@ -0,0 +1,56 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * base_model_restrict_update +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 12.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: <>\n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: base_model_restrict_update +#: model:ir.model,name:base_model_restrict_update.model_ir_model_access +msgid "Model Access" +msgstr "" + +#. module: base_model_restrict_update +#: model:ir.model,name:base_model_restrict_update.model_ir_model +msgid "Models" +msgstr "" + +#. module: base_model_restrict_update +#: model:ir.model.fields,help:base_model_restrict_update.field_res_users__unrestrict_model_update +msgid "Set to true and the user can update restricted model." +msgstr "" + +#. module: base_model_restrict_update +#: model:ir.model.fields,field_description:base_model_restrict_update.field_res_users__unrestrict_model_update +msgid "Unrestrict Model Update" +msgstr "" + +#. module: base_model_restrict_update +#: model:ir.model.fields,field_description:base_model_restrict_update.field_ir_model__restrict_update +msgid "Update Restrict Model" +msgstr "" + +#. module: base_model_restrict_update +#: model:ir.model,name:base_model_restrict_update.model_res_users +msgid "Users" +msgstr "" + +#. module: base_model_restrict_update +#: model:ir.model.fields,help:base_model_restrict_update.field_ir_model__restrict_update +msgid "When selected, the model is restricted to read-only unless the user has the special permission." +msgstr "" + +#. module: base_model_restrict_update +#: code:addons/base_model_restrict_update/models/ir_model_access.py:31 +#, python-format +msgid "You are only allowed to read this record. (%s - %s)" +msgstr "" + diff --git a/base_model_restrict_update/i18n/ja_JP.po b/base_model_restrict_update/i18n/ja_JP.po new file mode 100644 index 000000000..b707e6afa --- /dev/null +++ b/base_model_restrict_update/i18n/ja_JP.po @@ -0,0 +1,58 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * base_model_restrict_update +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 12.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2021-02-04 09:47+0000\n" +"PO-Revision-Date: 2021-02-04 09:47+0000\n" +"Last-Translator: <>\n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: base_model_restrict_update +#: model:ir.model,name:base_model_restrict_update.model_ir_model_access +msgid "Model Access" +msgstr "モデルアクセス" + +#. module: base_model_restrict_update +#: model:ir.model,name:base_model_restrict_update.model_ir_model +msgid "Models" +msgstr "モデル" + +#. module: base_model_restrict_update +#: model:ir.model.fields,help:base_model_restrict_update.field_res_users__unrestrict_model_update +msgid "Set to true and the user can update restricted model." +msgstr "有効化にすると制限されたモデルを更新することができます。" + +#. module: base_model_restrict_update +#: model:ir.model.fields,field_description:base_model_restrict_update.field_res_users__unrestrict_model_update +msgid "Unrestrict Model Update" +msgstr "制限モデル更新許可" + +#. module: base_model_restrict_update +#: model:ir.model.fields,field_description:base_model_restrict_update.field_ir_model__restrict_update +msgid "Update Restrict Model" +msgstr "更新制限モデル" + +#. module: base_model_restrict_update +#: model:ir.model,name:base_model_restrict_update.model_res_users +msgid "Users" +msgstr "ユーザ" + +#. module: base_model_restrict_update +#: model:ir.model.fields,help:base_model_restrict_update.field_ir_model__restrict_update +msgid "When selected, the model is restricted to read-only unless the user has the special permission." +msgstr "有効化にすると、「制限モデル更新許可」を持ちユーザのみモデルレコードを更新できます。" + +#. module: base_model_restrict_update +#: code:addons/base_model_restrict_update/models/ir_model_access.py:31 +#, python-format +msgid "You are only allowed to read this record. (%s - %s)" +msgstr "レコードを更新することはできません。(%s - %s)" + diff --git a/base_model_restrict_update/models/__init__.py b/base_model_restrict_update/models/__init__.py new file mode 100644 index 000000000..4f02d73ba --- /dev/null +++ b/base_model_restrict_update/models/__init__.py @@ -0,0 +1,3 @@ +from . import ir_model_access +from . import ir_model +from . import res_users diff --git a/base_model_restrict_update/models/ir_model.py b/base_model_restrict_update/models/ir_model.py new file mode 100644 index 000000000..a60b6b875 --- /dev/null +++ b/base_model_restrict_update/models/ir_model.py @@ -0,0 +1,14 @@ +# Copyright 2021 Quartile Limited +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +from odoo import fields, models + + +class IrModel(models.Model): + _inherit = "ir.model" + + restrict_update = fields.Boolean( + "Update Restrict Model", + help="When selected, the model is restricted to read-only unless the " + "user has the special permission.", + ) diff --git a/base_model_restrict_update/models/ir_model_access.py b/base_model_restrict_update/models/ir_model_access.py new file mode 100644 index 000000000..f6d7e0e89 --- /dev/null +++ b/base_model_restrict_update/models/ir_model_access.py @@ -0,0 +1,35 @@ +# Copyright 2021 Quartile Limited +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +from odoo import api, models, tools +from odoo.exceptions import AccessError +from odoo.tools.translate import _ + + +class IrModelAccess(models.Model): + _inherit = "ir.model.access" + + @api.model + @tools.ormcache_context( + "self._uid", "model", "mode", "raise_exception", keys=("lang",) + ) + def check(self, model, mode="read", raise_exception=True): + res = super(IrModelAccess, self).check(model, mode, raise_exception) + if self._uid == 1: + return True + self._cr.execute( + "SELECT restrict_update FROM ir_model WHERE model = %s", (model,) + ) + query_res = self._cr.dictfetchall()[0] + if ( + query_res["restrict_update"] + and mode != "read" + and not self.env.user.unrestrict_model_update + ): + if raise_exception: + raise AccessError( + _("You are only allowed to read this record. (%s - %s)") + % (model, mode) + ) + return False + return res diff --git a/base_model_restrict_update/models/res_users.py b/base_model_restrict_update/models/res_users.py new file mode 100644 index 000000000..053ed6e59 --- /dev/null +++ b/base_model_restrict_update/models/res_users.py @@ -0,0 +1,18 @@ +# Copyright 2021 Quartile Limited +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +from odoo import api, fields, models + + +class ResUsers(models.Model): + _inherit = "res.users" + + unrestrict_model_update = fields.Boolean( + "Unrestrict Model Update", + help="Set to true and the user can update restricted model.", + ) + + @api.multi + def toggle_unrestrict_model_update(self): + for record in self: + record.unrestrict_model_update = not record.unrestrict_model_update diff --git a/base_model_restrict_update/readme/CONFIGURE.rst b/base_model_restrict_update/readme/CONFIGURE.rst new file mode 100644 index 000000000..5ac08602f --- /dev/null +++ b/base_model_restrict_update/readme/CONFIGURE.rst @@ -0,0 +1,2 @@ +Enable the "Update Restrict Model" of specific model to restrict update from unpermitted users. +To set an user as a permitted user to update restricted model(s), click on "Grant Update Permit" in user form. diff --git a/base_model_restrict_update/readme/CREDITS.rst b/base_model_restrict_update/readme/CREDITS.rst new file mode 100644 index 000000000..ac1be061e --- /dev/null +++ b/base_model_restrict_update/readme/CREDITS.rst @@ -0,0 +1 @@ +* This module borrows the idea from 'Moises Lopez ' diff --git a/base_model_restrict_update/readme/DESCRIPTION.rst b/base_model_restrict_update/readme/DESCRIPTION.rst new file mode 100644 index 000000000..0a8c8c551 --- /dev/null +++ b/base_model_restrict_update/readme/DESCRIPTION.rst @@ -0,0 +1,2 @@ +This module adds a config to apply a global update restriction to specific model, +while only certain users can update the records if the config is enabled. diff --git a/base_model_restrict_update/static/description/index.html b/base_model_restrict_update/static/description/index.html new file mode 100644 index 000000000..3d9f11d17 --- /dev/null +++ b/base_model_restrict_update/static/description/index.html @@ -0,0 +1,426 @@ + + + + + + +Update Restrict Model + + + +
+

Update Restrict Model

+ + +

Beta License: LGPL-3 OCA/server-tools Translate me on Weblate Try me on Runbot

+

This module adds a config to apply a global update restriction to specific model, +while only certain users can update the records if the config is enabled.

+

Table of contents

+ +
+

Configuration

+

Enable the “Update Restrict Model” of specific model to restrict update from unpermitted users. +To set an user as a permitted user to update restricted model(s), click on “Grant Update Permit” in user form.

+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Quartile Limited
  • +
+
+
+

Other credits

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/server-tools project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/base_model_restrict_update/tests/__init__.py b/base_model_restrict_update/tests/__init__.py new file mode 100644 index 000000000..6b23d93c7 --- /dev/null +++ b/base_model_restrict_update/tests/__init__.py @@ -0,0 +1 @@ +from . import test_base_model_restrict_update diff --git a/base_model_restrict_update/tests/test_base_model_restrict_update.py b/base_model_restrict_update/tests/test_base_model_restrict_update.py new file mode 100644 index 000000000..fa3bfd86c --- /dev/null +++ b/base_model_restrict_update/tests/test_base_model_restrict_update.py @@ -0,0 +1,56 @@ +# Copyright 2021 Quartile Limited +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +from odoo.exceptions import AccessError +from odoo.tests import SavepointCase, tagged + + +@tagged("post_install", "-at_install") +class TestBaseModelRestrictUpdate(SavepointCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.partner_model = cls.env["ir.model"].search([("model", "=", "res.partner")])[ + 0 + ] + cls.partner_model.restrict_update = True + cls.test_partner = cls.env["res.partner"].create({"name": "Test Partner"}) + cls.restrict_test_user = cls.env["res.users"].create( + { + "name": "Resticted user", + "login": "resticted@example.com", + "unrestrict_model_update": False, + } + ) + cls.permit_test_user = cls.env["res.users"].create( + { + "name": "Permit user", + "login": "permit@example.com", + "email": "permit@example.com", + "unrestrict_model_update": True, + } + ) + + def test_01_create_partner(self): + with self.assertRaises(AccessError): + self.env["res.partner"].sudo(self.restrict_test_user.id).create( + {"name": "Test Partner"} + ) + self.env["res.partner"].sudo(self.permit_test_user.id).create( + {"name": "Test Partner"} + ) + + def test_02_update_partner(self): + with self.assertRaises(AccessError): + self.test_partner.sudo(self.restrict_test_user.id).update( + {"name": "Test Partner 2"} + ) + self.test_partner.sudo(self.permit_test_user.id).update( + {"name": "Test Partner 2"} + ) + + def test_03_unlink_partner(self): + test_partner = self.test_partner.sudo().copy() + with self.assertRaises(AccessError): + test_partner.sudo(self.restrict_test_user.id).unlink() + test_partner.sudo(self.permit_test_user.id).unlink() diff --git a/base_model_restrict_update/views/ir_model_views.xml b/base_model_restrict_update/views/ir_model_views.xml new file mode 100644 index 000000000..1c17e162b --- /dev/null +++ b/base_model_restrict_update/views/ir_model_views.xml @@ -0,0 +1,15 @@ + + + + + ir.model form + ir.model + + + + + + + + + diff --git a/base_model_restrict_update/views/res_users_views.xml b/base_model_restrict_update/views/res_users_views.xml new file mode 100644 index 000000000..1ad2b1295 --- /dev/null +++ b/base_model_restrict_update/views/res_users_views.xml @@ -0,0 +1,18 @@ + + + + + res.users.form + res.users + + +
+ +
+
+
+ +