diff --git a/setup/stock_warehouse_security/odoo/addons/stock_warehouse_security b/setup/stock_warehouse_security/odoo/addons/stock_warehouse_security new file mode 120000 index 000000000000..2300f9548600 --- /dev/null +++ b/setup/stock_warehouse_security/odoo/addons/stock_warehouse_security @@ -0,0 +1 @@ +../../../../stock_warehouse_security \ No newline at end of file diff --git a/setup/stock_warehouse_security/setup.py b/setup/stock_warehouse_security/setup.py new file mode 100644 index 000000000000..28c57bb64031 --- /dev/null +++ b/setup/stock_warehouse_security/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/stock_warehouse_security/README.rst b/stock_warehouse_security/README.rst new file mode 100644 index 000000000000..aeff87dc565b --- /dev/null +++ b/stock_warehouse_security/README.rst @@ -0,0 +1,125 @@ +======================== +Stock Warehouse Security +======================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:ff7a5a003ab575c5293d08a54256d9e54601ba8b1d11a05fd7f28bf5d4b287c5 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Alpha-red.png + :target: https://odoo-community.org/page/development-status + :alt: Alpha +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fstock--logistics--warehouse-lightgray.png?logo=github + :target: https://github.com/OCA/stock-logistics-warehouse/tree/16.0/stock_warehouse_security + :alt: OCA/stock-logistics-warehouse +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/stock-logistics-warehouse-16-0/stock-logistics-warehouse-16-0-stock_warehouse_security + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/stock-logistics-warehouse&target_branch=16.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +With this module you are able to set a restricted list of allowed +warehouses that user can see and operate with. + +This module is inspired from the experiences of +`stock_warehouse_security `__ +on version 12.0 but has some key differences on user experience: + +- In this module there is no new groups, user is able to see allowed + warehouses only or all if not set. +- So in this module there is no "current warehouse" concept on user ( in + v12.0 that module was based on + `base_multi_warehouse `__ + which allowed users to switch between warehouses). + +.. IMPORTANT:: + This is an alpha version, the data model and design can change at any time without warning. + Only for development or testing purpose, do not use in production. + `More details on development status `_ + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +Each user administrators is able to defined allowed warehouse. + +No warehouse define in such list means no restrictions. + +Known issues / Roadmap +====================== + +- test make sure default warehouse can still be set if user also sales + goods but processus is not unit-tested +- Add unitest test to ensure transit goods between warehouses still + working + +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 to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* Akretion +* Pierre Verkest + +Contributors +------------ + +- `Foodles `__ + + - Pierre Verkest + +- Florian da Costa + +- `Tecnativa `__: + + - Christian Ramos + +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. + +.. |maintainer-petrus-v| image:: https://github.com/petrus-v.png?size=40px + :target: https://github.com/petrus-v + :alt: petrus-v + +Current `maintainer `__: + +|maintainer-petrus-v| + +This module is part of the `OCA/stock-logistics-warehouse `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/stock_warehouse_security/__init__.py b/stock_warehouse_security/__init__.py new file mode 100644 index 000000000000..0650744f6bc6 --- /dev/null +++ b/stock_warehouse_security/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/stock_warehouse_security/__manifest__.py b/stock_warehouse_security/__manifest__.py new file mode 100644 index 000000000000..5774af0d604c --- /dev/null +++ b/stock_warehouse_security/__manifest__.py @@ -0,0 +1,23 @@ +# Copyright (C) 2019 Akretion +# Copyright 2022 Foodles (http://www.foodles.co). +# @author Pierre Verkest +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +{ + "name": "Stock Warehouse Security", + "version": "16.0.1.0.0", + "category": "Warehouse Management", + "website": "https://github.com/OCA/stock-logistics-warehouse", + "author": "Akretion, Pierre Verkest, Odoo Community Association (OCA)", + "maintainers": ["petrus-v"], + "license": "AGPL-3", + "installable": True, + "summary": "Restrict user access in multi-warehouse environment", + "depends": [ + "stock", + ], + "data": [ + "security/stock_security.xml", + "views/res_users.xml", + ], + "development_status": "Alpha", +} diff --git a/stock_warehouse_security/i18n/fr.po b/stock_warehouse_security/i18n/fr.po new file mode 100644 index 000000000000..2e901b304ee5 --- /dev/null +++ b/stock_warehouse_security/i18n/fr.po @@ -0,0 +1,45 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * stock_warehouse_security +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: \n" +"Language-Team: \n" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: stock_warehouse_security +#: model:ir.model.fields,field_description:stock_warehouse_security.field_res_users__warehouse_ids +msgid "Allowed Warehouses" +msgstr "Entrepôts autorisés" + +#. module: stock_warehouse_security +#: model_terms:ir.ui.view,arch_db:stock_warehouse_security.view_users_form +msgid "Multi Warehouse" +msgstr "Multi entrepôt" + +#. module: stock_warehouse_security +#: model:ir.model,name:stock_warehouse_security.model_product_product +msgid "Product Variant" +msgstr "" + +#. module: stock_warehouse_security +#: model:ir.model,name:stock_warehouse_security.model_res_users +#, fuzzy +msgid "User" +msgstr "Utilisateurs" + +#~ msgid "Display Name" +#~ msgstr "Nom affiché" + +#~ msgid "ID" +#~ msgstr "ID" + +#~ msgid "Last Modified on" +#~ msgstr "Dernière modification le" diff --git a/stock_warehouse_security/i18n/it.po b/stock_warehouse_security/i18n/it.po new file mode 100644 index 000000000000..9f946a206021 --- /dev/null +++ b/stock_warehouse_security/i18n/it.po @@ -0,0 +1,37 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * stock_warehouse_security +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 18.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2025-11-10 10:04+0000\n" +"Last-Translator: mymage \n" +"Language-Team: none\n" +"Language: it\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" +"X-Generator: Weblate 5.10.4\n" + +#. module: stock_warehouse_security +#: model:ir.model.fields,field_description:stock_warehouse_security.field_res_users__warehouse_ids +msgid "Allowed Warehouses" +msgstr "Magazzini consentiti" + +#. module: stock_warehouse_security +#: model_terms:ir.ui.view,arch_db:stock_warehouse_security.view_users_form +msgid "Multi Warehouse" +msgstr "Magazzini multipli" + +#. module: stock_warehouse_security +#: model:ir.model,name:stock_warehouse_security.model_product_product +msgid "Product Variant" +msgstr "Variante prodotto" + +#. module: stock_warehouse_security +#: model:ir.model,name:stock_warehouse_security.model_res_users +msgid "User" +msgstr "Utente" diff --git a/stock_warehouse_security/i18n/stock_multi_warehouse_security.pot b/stock_warehouse_security/i18n/stock_multi_warehouse_security.pot new file mode 100644 index 000000000000..7363cd8c1ccb --- /dev/null +++ b/stock_warehouse_security/i18n/stock_multi_warehouse_security.pot @@ -0,0 +1,44 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * stock_warehouse_security +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.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: stock_warehouse_security +#: model:ir.model.fields,field_description:stock_warehouse_security.field_res_users__warehouse_ids +msgid "Allowed Warehouses" +msgstr "" + +#. module: stock_warehouse_security +#: model:ir.model.fields,field_description:stock_warehouse_security.field_res_users__display_name +msgid "Display Name" +msgstr "" + +#. module: stock_warehouse_security +#: model:ir.model.fields,field_description:stock_warehouse_security.field_res_users__id +msgid "ID" +msgstr "" + +#. module: stock_warehouse_security +#: model:ir.model.fields,field_description:stock_warehouse_security.field_res_users____last_update +msgid "Last Modified on" +msgstr "" + +#. module: stock_warehouse_security +#: model_terms:ir.ui.view,arch_db:stock_warehouse_security.view_users_form +msgid "Multi Warehouse" +msgstr "" + +#. module: stock_warehouse_security +#: model:ir.model,name:stock_warehouse_security.model_res_users +msgid "Users" +msgstr "" diff --git a/stock_warehouse_security/i18n/stock_warehouse_security.pot b/stock_warehouse_security/i18n/stock_warehouse_security.pot new file mode 100644 index 000000000000..aacc47aa12bc --- /dev/null +++ b/stock_warehouse_security/i18n/stock_warehouse_security.pot @@ -0,0 +1,34 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * stock_warehouse_security +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 18.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: stock_warehouse_security +#: model:ir.model.fields,field_description:stock_warehouse_security.field_res_users__warehouse_ids +msgid "Allowed Warehouses" +msgstr "" + +#. module: stock_warehouse_security +#: model_terms:ir.ui.view,arch_db:stock_warehouse_security.view_users_form +msgid "Multi Warehouse" +msgstr "" + +#. module: stock_warehouse_security +#: model:ir.model,name:stock_warehouse_security.model_product_product +msgid "Product Variant" +msgstr "" + +#. module: stock_warehouse_security +#: model:ir.model,name:stock_warehouse_security.model_res_users +msgid "User" +msgstr "" diff --git a/stock_warehouse_security/models/__init__.py b/stock_warehouse_security/models/__init__.py new file mode 100644 index 000000000000..ffa7e63f6ca7 --- /dev/null +++ b/stock_warehouse_security/models/__init__.py @@ -0,0 +1,2 @@ +from . import res_users +from . import product diff --git a/stock_warehouse_security/models/product.py b/stock_warehouse_security/models/product.py new file mode 100644 index 000000000000..0bb5163918cf --- /dev/null +++ b/stock_warehouse_security/models/product.py @@ -0,0 +1,21 @@ +# Copyright 2025 Tecnativa - Christian Ramos +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo import models + + +class Product(models.Model): + _inherit = "product.product" + + def _get_domain_locations_new(self, location_ids): + if self.env.user.warehouse_ids: + location_ids = set( + self.env["stock.location"] + .search( + [ + ("warehouse_id", "in", self.env.user.warehouse_ids.ids), + ("id", "in", list(location_ids)), + ] + ) + .ids + ) + return super()._get_domain_locations_new(location_ids) diff --git a/stock_warehouse_security/models/res_users.py b/stock_warehouse_security/models/res_users.py new file mode 100644 index 000000000000..65329b6a3575 --- /dev/null +++ b/stock_warehouse_security/models/res_users.py @@ -0,0 +1,21 @@ +# Copyright (C) 2019 Akretion +# Copyright 2022 Foodles (http://www.foodles.co). +# @author Pierre Verkest +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import api, fields, models + + +class ResUsers(models.Model): + _inherit = "res.users" + + warehouse_ids = fields.Many2many( + "stock.warehouse", + string="Allowed Warehouses", + ) + + @api.model + def _get_invalidation_fields(self): + res = super()._get_invalidation_fields() + res.add("warehouse_ids") + return res diff --git a/stock_warehouse_security/pyproject.toml b/stock_warehouse_security/pyproject.toml new file mode 100644 index 000000000000..4231d0cccb3d --- /dev/null +++ b/stock_warehouse_security/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/stock_warehouse_security/readme/CONTRIBUTORS.md b/stock_warehouse_security/readme/CONTRIBUTORS.md new file mode 100644 index 000000000000..eb04424f4bfd --- /dev/null +++ b/stock_warehouse_security/readme/CONTRIBUTORS.md @@ -0,0 +1,8 @@ +- [Foodles](https://www.foodles.co) + + > - Pierre Verkest \ + +- Florian da Costa \ +- [Tecnativa](https://www.tecnativa.com): + + - Christian Ramos diff --git a/stock_warehouse_security/readme/DESCRIPTION.md b/stock_warehouse_security/readme/DESCRIPTION.md new file mode 100644 index 000000000000..fbd522cdeabb --- /dev/null +++ b/stock_warehouse_security/readme/DESCRIPTION.md @@ -0,0 +1,13 @@ +With this module you are able to set a restricted list of allowed +warehouses that user can see and operate with. + +This module is inspired from the experiences of +[stock_warehouse_security](https://github.com/akretion/stock-logistics-warehouse/tree/12-muli-wh-security/stock_warehouse_security/) +on version 12.0 but has some key differences on user experience: + +- In this module there is no new groups, user is able to see allowed + warehouses only or all if not set. +- So in this module there is no "current warehouse" concept on user ( in + v12.0 that module was based on + [base_multi_warehouse](https://github.com/akretion/stock-logistics-warehouse/tree/12-base-multi_warehouse/base_multi_warehouse) + which allowed users to switch between warehouses). diff --git a/stock_warehouse_security/readme/ROADMAP.md b/stock_warehouse_security/readme/ROADMAP.md new file mode 100644 index 000000000000..43d75ecb5925 --- /dev/null +++ b/stock_warehouse_security/readme/ROADMAP.md @@ -0,0 +1,4 @@ +- test make sure default warehouse can still be set if user also sales + goods but processus is not unit-tested +- Add unitest test to ensure transit goods between warehouses still + working diff --git a/stock_warehouse_security/readme/USAGE.md b/stock_warehouse_security/readme/USAGE.md new file mode 100644 index 000000000000..44cd2dbd7128 --- /dev/null +++ b/stock_warehouse_security/readme/USAGE.md @@ -0,0 +1,3 @@ +Each user administrators is able to defined allowed warehouse. + +No warehouse define in such list means no restrictions. diff --git a/stock_warehouse_security/security/stock_security.xml b/stock_warehouse_security/security/stock_security.xml new file mode 100644 index 000000000000..d48c4f0f63b6 --- /dev/null +++ b/stock_warehouse_security/security/stock_security.xml @@ -0,0 +1,97 @@ + + + + Stock locations from allowed Warehouse + + ['|',(1 if user.warehouse_ids.ids == [] else 0, "=", 1), '|', ('warehouse_id', '=', False), ('warehouse_id', 'in', user.warehouse_ids.ids)] + + + + + + + + Stock pickings type from allowed Warehouse + + ['|', (1 if user.warehouse_ids.ids == [] else 0, "=", 1), '|', ('warehouse_id', '=', False), ('warehouse_id', 'in', user.warehouse_ids.ids)] + + + + + + + + Stock pickings from allowed Warehouse + + ['|', (1 if user.warehouse_ids.ids == [] else 0, "=", 1), '|', ('picking_type_id.warehouse_id', '=', False), ('picking_type_id.warehouse_id', 'in', user.warehouse_ids.ids)] + + + + + + + + Stock moves from allowed Warehouse + + ['|', (1 if user.warehouse_ids.ids == [] else 0, "=", 1), '|', ('picking_type_id.warehouse_id', '=', False), ('picking_type_id.warehouse_id', 'in', user.warehouse_ids.ids)] + + + + + + + Stock moves lines from allowed Warehouse + + ['|', (1 if user.warehouse_ids.ids == [] else 0, "=", 1), '|', ('picking_type_id.warehouse_id', '=', False), ('picking_type_id.warehouse_id', 'in', user.warehouse_ids.ids)] + + + + + + + + Stock Quants from allowed Warehouse + + ['|', (1 if user.warehouse_ids.ids == [] else 0, "=", 1), '|', ('location_id.warehouse_id', '=', False), ('location_id.warehouse_id', 'in', user.warehouse_ids.ids)] + + + + + + + + Packages from allowed Warehouse + + ['|', (1 if user.warehouse_ids.ids == [] else 0, "=", 1), '|', ('location_id.warehouse_id', '=', False), ('location_id.warehouse_id', 'in', user.warehouse_ids.ids)] + + + + + + + + Orderpoints from allowed Warehouse + + ['|', (1 if user.warehouse_ids.ids == [] else 0, "=", 1), '|', ('warehouse_id', '=', False), ('warehouse_id', 'in', user.warehouse_ids.ids)] + + + + + + diff --git a/stock_warehouse_security/static/description/icon.png b/stock_warehouse_security/static/description/icon.png new file mode 100644 index 000000000000..3a0328b516c4 Binary files /dev/null and b/stock_warehouse_security/static/description/icon.png differ diff --git a/stock_warehouse_security/static/description/index.html b/stock_warehouse_security/static/description/index.html new file mode 100644 index 000000000000..d45aea6f2d31 --- /dev/null +++ b/stock_warehouse_security/static/description/index.html @@ -0,0 +1,473 @@ + + + + + +Stock Warehouse Security + + + +
+

Stock Warehouse Security

+ + +

Alpha License: AGPL-3 OCA/stock-logistics-warehouse Translate me on Weblate Try me on Runboat

+

With this module you are able to set a restricted list of allowed +warehouses that user can see and operate with.

+

This module is inspired from the experiences of +stock_warehouse_security +on version 12.0 but has some key differences on user experience:

+
    +
  • In this module there is no new groups, user is able to see allowed +warehouses only or all if not set.
  • +
  • So in this module there is no “current warehouse” concept on user ( in +v12.0 that module was based on +base_multi_warehouse +which allowed users to switch between warehouses).
  • +
+
+

Important

+

This is an alpha version, the data model and design can change at any time without warning. +Only for development or testing purpose, do not use in production. +More details on development status

+
+

Table of contents

+ +
+

Usage

+

Each user administrators is able to defined allowed warehouse.

+

No warehouse define in such list means no restrictions.

+
+
+

Known issues / Roadmap

+
    +
  • test make sure default warehouse can still be set if user also sales +goods but processus is not unit-tested
  • +
  • Add unitest test to ensure transit goods between warehouses still +working
  • +
+
+
+

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 to smash it by providing a detailed and welcomed +feedback.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • Akretion
  • +
  • Pierre Verkest
  • +
+
+
+

Contributors

+ +
+
+

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.

+

Current maintainer:

+

petrus-v

+

This module is part of the OCA/stock-logistics-warehouse project on GitHub.

+

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

+
+
+
+ + diff --git a/stock_warehouse_security/tests/__init__.py b/stock_warehouse_security/tests/__init__.py new file mode 100644 index 000000000000..e5314a9aee73 --- /dev/null +++ b/stock_warehouse_security/tests/__init__.py @@ -0,0 +1,2 @@ +from . import test_stock_warehouse_security_rules +from . import test_stock_warehouse diff --git a/stock_warehouse_security/tests/common.py b/stock_warehouse_security/tests/common.py new file mode 100644 index 000000000000..603c0e3faeca --- /dev/null +++ b/stock_warehouse_security/tests/common.py @@ -0,0 +1,190 @@ +from decorator import decorator + +from odoo.tests.common import TransactionCase + + +def allowed_companies(): + """Decorate a method to change allowed companies in current + context from current user info. + + This is to mimic user interface as if current user has choosen + all allowed companies from the list of available companies he can + currently see. + + you should probably associate it with the @users decorator. + Ordering your decorator is important otherwise you won't get proper list + of companies. + + @users( + "user1", + ) + @allowed_companies() + def test_something(self): + # return data will be filtered by allowed user1 companies + self.env["some.model"].search([]) + """ + + @decorator + def wrapper(func, *args, **kwargs): + self = args[0] + + previous_allowed_company_ids = self.env.context.get("allowed_company_ids") + try: + self.env = self.env( + context=dict( + self.env.context, + # company_id=self.env.user.company_ids[0].id, + allowed_company_ids=self.env.user.company_ids.ids, + ) + ) + func(*args, **kwargs) + finally: + self.env = self.env( + context=dict( + self.env.context, + allowed_company_ids=previous_allowed_company_ids, + ) + ) + + return wrapper + + +class TestStockCommon(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + + cls.company_1 = cls.env.ref("base.main_company") + cls.company_2 = cls.env["res.company"].create({"name": "company 2"}) + cls.warehouse_0 = cls.env.ref("stock.warehouse0") + cls.warehouse_1 = cls.env["stock.warehouse"].create( + { + "name": "Warehouse 1", + "company_id": cls.company_1.id, + "reception_steps": "one_step", + "delivery_steps": "pick_ship", + "code": "WH1", + } + ) + cls.warehouse_2 = cls.env["stock.warehouse"].create( + { + "name": "Warehouse 2", + "company_id": cls.company_1.id, + "reception_steps": "one_step", + "delivery_steps": "pick_ship", + "code": "WH2", + } + ) + cls.warehouse_3 = cls.env["stock.warehouse"].create( + { + "name": "Warehouse 3 - company 2", + "company_id": cls.company_2.id, + "reception_steps": "one_step", + "delivery_steps": "pick_ship", + "code": "WH3", + } + ) + cls.warehouses = cls.warehouse_1 | cls.warehouse_2 | cls.warehouse_3 + cls.suppliers_location = cls.env.ref("stock.stock_location_suppliers") + cls.customers_location = cls.env.ref("stock.stock_location_customers") + + cls.stock_user_c1_wh12 = cls.env["res.users"].create( + { + "name": "unlimited multi warehouse user", + "login": "stock_user_c1_wh12", + "groups_id": [(6, 0, [cls.env.ref("stock.group_stock_user").id])], + "warehouse_ids": [], + "company_ids": [(6, 0, cls.company_1.ids)], + } + ) + cls.stock_user_c12_wh2 = cls.env["res.users"].create( + { + "name": "Limited warehouse user", + "login": "stock_user_c12_wh2", + "groups_id": [(6, 0, [cls.env.ref("stock.group_stock_user").id])], + "warehouse_ids": [(6, 0, cls.warehouse_2.ids)], + "company_ids": [(6, 0, (cls.company_1 | cls.company_2).ids)], + } + ) + cls.stock_user_c12_wh23 = cls.env["res.users"].create( + { + "name": "Limited warehouse user", + "login": "stock_user_c12_wh23", + "groups_id": [(6, 0, [cls.env.ref("stock.group_stock_user").id])], + "warehouse_ids": [(6, 0, (cls.warehouse_2 | cls.warehouse_3).ids)], + "company_ids": [(6, 0, (cls.company_1 | cls.company_2).ids)], + } + ) + cls.product = cls.env["product.product"].create( + { + "name": "Product for test", + "type": "product", + } + ) + cls.stock_picking_wh_1 = cls._create_picking( + cls.warehouse_1, location_src=cls.suppliers_location + ) + cls.stock_picking_wh_2 = cls._create_picking( + cls.warehouse_2, location_src=cls.suppliers_location + ) + cls.stock_picking_wh_3 = cls._create_picking( + cls.warehouse_3, location_src=cls.suppliers_location + ) + for wh in cls.warehouses: + cls.env["stock.warehouse.orderpoint"].with_company(wh.company_id).create( + { + "name": f"RR for {wh.name}", + "warehouse_id": wh.id, + "location_id": wh.lot_stock_id.id, + "product_id": cls.product.id, + "product_min_qty": 1, + "product_max_qty": 5, + } + ) + + @classmethod + def _create_picking( + cls, + warehouse, + picking_type=None, + location_src=None, + location_dest=None, + env=None, + ): + if not picking_type: + picking_type = warehouse.in_type_id + + if not location_src: + location_src = picking_type.default_location_src_id + + if not location_dest: + location_dest = picking_type.default_location_dest_id + + if not env: + env = cls.env + + location_src.ensure_one() + location_dest.ensure_one() + picking = ( + env["stock.picking"] + .with_company(warehouse.company_id) + .create( + { + "picking_type_id": picking_type.id, + "location_id": location_src.id, + "location_dest_id": location_dest.id, + } + ) + ) + env["stock.move"].with_company(warehouse.company_id).create( + { + "name": "a move", + "product_id": cls.product.id, + "product_uom_qty": 5.0, + "product_uom": cls.product.uom_id.id, + "picking_id": picking.id, + "location_id": location_src.id, + "location_dest_id": location_dest.id, + } + ) + return picking diff --git a/stock_warehouse_security/tests/test_stock_warehouse.py b/stock_warehouse_security/tests/test_stock_warehouse.py new file mode 100644 index 000000000000..8767191aa2bf --- /dev/null +++ b/stock_warehouse_security/tests/test_stock_warehouse.py @@ -0,0 +1,43 @@ +from odoo.tests.common import users + +from odoo.addons.stock_warehouse_security.tests.common import TestStockCommon + + +class TestStockWarehouseAccess(TestStockCommon): + @users("stock_user_c12_wh2", "stock_user_c1_wh12", "stock_user_c12_wh23") + def test_setting_preferred_default_warehouse_allowed(self): + """Even user is not allowed to manage all warehouse + if the user also sales goods (require sales_stock module) he/she + must be able to set its default warehouse""" + if ( + self.env["ir.module.module"] + .sudo() + .search( + [("name", "=", "sale_stock"), ("state", "!=", "installed")], + limit=1, + ) + ): + self.skipTest("skipped because sale_stock is not installed.") + + self.env.user.property_warehouse_id = self.warehouse_1 + + @users("stock_user_c12_wh2", "stock_user_c12_wh23") + def test_reading_all_warehouse(self): + """I must be able to read other warehouse to set my preferred warehouse""" + warehouses = self.env["stock.warehouse"].search([]) + self.assertTrue( + all( + [ + wh in warehouses + for wh in (self.warehouse_1 | self.warehouse_2 | self.warehouse_3) + ] + ) + ) + + @users("stock_user_c1_wh12") + def test_reading_all_warehouse_company_restriction(self): + """I must be able to read other warehouse to set my preferred warehouse""" + warehouses = self.env["stock.warehouse"].search([]) + self.assertTrue( + all([wh in warehouses for wh in (self.warehouse_1 | self.warehouse_2)]) + ) diff --git a/stock_warehouse_security/tests/test_stock_warehouse_security_rules.py b/stock_warehouse_security/tests/test_stock_warehouse_security_rules.py new file mode 100644 index 000000000000..a4813a65d56e --- /dev/null +++ b/stock_warehouse_security/tests/test_stock_warehouse_security_rules.py @@ -0,0 +1,326 @@ +from odoo.exceptions import AccessError, UserError +from odoo.tests.common import users + +from odoo.addons.stock_warehouse_security.tests.common import ( + TestStockCommon, + allowed_companies, +) + + +class TestStockWarehouseAccess(TestStockCommon): + @users("stock_user_c12_wh2") + def test_read_stock_picking_limited_user(self): + self.assertEqual( + self.env["stock.picking"].search( + [("picking_type_id.warehouse_id", "in", self.warehouses.ids)] + ), + (self.stock_picking_wh_2), + ) + + @users("stock_user_c1_wh12") + def test_read_stock_picking_unlimited_user(self): + self.assertEqual( + self.env["stock.picking"].search( + [("picking_type_id.warehouse_id", "in", self.warehouses.ids)] + ), + (self.stock_picking_wh_1 | self.stock_picking_wh_2), + ) + + @users("stock_user_c12_wh23") + def test_read_stock_picking_multi_company(self): + self.assertEqual( + self.env["stock.picking"].search( + [("picking_type_id.warehouse_id", "in", self.warehouses.ids)] + ), + (self.stock_picking_wh_2 | self.stock_picking_wh_3), + ) + + @users("stock_user_c12_wh2") + def test_read_stock_picking_type_limited_user(self): + self.assertEqual( + self.env["stock.picking.type"] + .search([("warehouse_id", "in", self.warehouses.ids)]) + .mapped("warehouse_id"), + (self.warehouse_2), + ) + + @users("stock_user_c1_wh12") + def test_read_stock_picking_type_unlimited_user(self): + self.assertEqual( + self.env["stock.picking.type"] + .search([("warehouse_id", "in", self.warehouses.ids)]) + .mapped("warehouse_id"), + (self.warehouse_1 | self.warehouse_2), + ) + + @users("stock_user_c12_wh23") + def test_read_stock_picking_type_multi_company(self): + self.assertEqual( + self.env["stock.picking.type"] + .search([("warehouse_id", "in", self.warehouses.ids)]) + .mapped("warehouse_id"), + (self.warehouse_2 | self.warehouse_3), + ) + + @users("stock_user_c12_wh2") + def test_stock_user_wont_be_granted_by_ir_rule_to_create_stock_picking_type(self): + with self.assertRaisesRegex( + UserError, + r"You are not allowed to create 'Picking Type' \(stock.picking.type\) records.*", # noqa: B950 + ): + self.env["stock.picking.type"].create( + { + "name": "Test internal", + "sequence_code": "TEST-INT", + "code": "internal", + "warehouse_id": self.warehouse_2.id, + } + ) + + @users( + "stock_user_c12_wh2", + "stock_user_c12_wh23", + "stock_user_c1_wh12", + ) + def test_create_and_validate_picking(self): + picking = self._create_picking( + self.warehouse_2, location_src=self.suppliers_location + ) + picking.action_assign() + self.assertEqual(picking.state, "assigned") + picking.move_ids.write({"quantity_done": 5}) + picking.button_validate() + self.assertEqual(picking.state, "done") + + @users( + "stock_user_c12_wh2", + "stock_user_c12_wh23", + ) + def test_forbid_create_picking_other_warehouse(self): + with self.assertRaisesRegex(AccessError, ".*you are not allowed to create.*"): + self._create_picking( + self.warehouse_1, + location_src=self.suppliers_location, + env=self.env, + ) + + @users( + "stock_user_c1_wh12", + ) + @allowed_companies() + def test_read_stock_move_wh12(self): + self.assertEqual( + self.env["stock.move"] + .search([("picking_type_id.warehouse_id", "!=", self.warehouse_0.id)]) + .mapped("picking_type_id.warehouse_id"), + (self.warehouse_1 | self.warehouse_2), + ) + + @users( + "stock_user_c12_wh2", + ) + def test_read_stock_move_wh2_only(self): + self.assertEqual( + self.env["stock.move"].search([]).mapped("picking_type_id.warehouse_id"), + (self.warehouse_2), + ) + + @users( + "stock_user_c12_wh23", + ) + def test_read_stock_move_wh23(self): + self.assertEqual( + self.env["stock.move"].search([]).mapped("picking_type_id.warehouse_id"), + (self.warehouse_2 | self.warehouse_3), + ) + + @users( + "stock_user_c12_wh2", + ) + def test_read_stock_location_wh2_only(self): + self.assertEqual( + self.env["stock.location"].search([]).mapped("warehouse_id"), + (self.warehouse_2), + ) + + @users( + "stock_user_c12_wh23", + ) + def test_read_stock_location_wh23(self): + self.assertEqual( + self.env["stock.location"].search([]).mapped("warehouse_id"), + (self.warehouse_2 | self.warehouse_3), + ) + + @users( + "stock_user_c1_wh12", + ) + @allowed_companies() + def test_read_stock_location_wh12(self): + self.assertEqual( + self.env["stock.location"] + .search([("warehouse_id", "!=", self.warehouse_0.id)]) + .mapped("warehouse_id"), + (self.warehouse_1 | self.warehouse_2), + ) + + @users( + "stock_user_c12_wh2", + ) + def test_read_stock_warehouse_orderpoint_wh2_only(self): + self.assertEqual( + self.env["stock.warehouse.orderpoint"].search([]).mapped("warehouse_id"), + (self.warehouse_2), + ) + + @users( + "stock_user_c12_wh23", + ) + def test_read_stock_warehouse_orderpoint_wh23(self): + self.assertEqual( + self.env["stock.warehouse.orderpoint"].search([]).mapped("warehouse_id"), + (self.warehouse_2 | self.warehouse_3), + ) + + @users( + "stock_user_c1_wh12", + ) + @allowed_companies() + def test_read_stock_warehouse_orderpoint_wh12(self): + self.assertEqual( + self.env["stock.warehouse.orderpoint"] + .search([("warehouse_id", "!=", self.warehouse_0.id)]) + .mapped("warehouse_id"), + (self.warehouse_1 | self.warehouse_2), + ) + + +class TestStockWarehouseAccessWithReceivedGoods(TestStockCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + pickings = ( + cls.stock_picking_wh_1 | cls.stock_picking_wh_2 | cls.stock_picking_wh_3 + ) + pickings.action_assign() + pickings.move_ids.write({"quantity_done": 5}) + pickings.button_validate() + + @users( + "stock_user_c12_wh2", + ) + def test_read_stock_move_line_wh2_only(self): + self.assertEqual( + self.env["stock.move.line"] + .search([]) + .mapped("picking_type_id.warehouse_id"), + (self.warehouse_2), + ) + + @users( + "stock_user_c12_wh23", + ) + def test_read_stock_move_line_wh23(self): + self.assertEqual( + self.env["stock.move.line"] + .search([]) + .mapped("picking_type_id.warehouse_id"), + (self.warehouse_2 | self.warehouse_3), + ) + + @users( + "stock_user_c1_wh12", + ) + @allowed_companies() + def test_read_stock_move_line_wh12(self): + self.assertEqual( + self.env["stock.move.line"] + .search([("picking_type_id.warehouse_id", "!=", self.warehouse_0.id)]) + .mapped("picking_type_id.warehouse_id"), + (self.warehouse_1 | self.warehouse_2), + ) + + @users( + "stock_user_c12_wh2", + ) + def test_read_stock_quant_wh2_only(self): + self.assertEqual( + self.env["stock.quant"].search([]).mapped("warehouse_id"), + (self.warehouse_2), + ) + + @users( + "stock_user_c12_wh23", + ) + def test_read_stock_quant_wh23(self): + self.assertEqual( + self.env["stock.quant"].search([]).mapped("warehouse_id"), + (self.warehouse_2 | self.warehouse_3), + ) + + @users( + "stock_user_c1_wh12", + ) + @allowed_companies() + def test_read_stock_quant_wh12(self): + self.assertEqual( + self.env["stock.quant"] + .search([("warehouse_id", "!=", self.warehouse_0.id)]) + .mapped("warehouse_id"), + (self.warehouse_1 | self.warehouse_2), + ) + + +class TestStockWarehouseAccessWithReceivedPackedGoods(TestStockCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + pickings = ( + cls.stock_picking_wh_1 | cls.stock_picking_wh_2 | cls.stock_picking_wh_3 + ) + pickings.action_assign() + for picking in pickings: + warehouse = picking.picking_type_id.warehouse_id.name + picking.move_line_ids.write( + { + "result_package_id": cls.env["stock.quant.package"] + .create({"name": f"Dest Pack {warehouse}"}) + .id, + "qty_done": 5, + } + ) + pickings.button_validate() + + @users( + "stock_user_c12_wh2", + ) + def test_read_stock_quant_wh2_only(self): + self.assertEqual( + self.env["stock.quant.package"] + .search([]) + .mapped("location_id.warehouse_id"), + (self.warehouse_2), + ) + + @users( + "stock_user_c12_wh23", + ) + def test_read_stock_quant_package_wh23(self): + self.assertEqual( + self.env["stock.quant.package"] + .search([]) + .mapped("location_id.warehouse_id"), + (self.warehouse_2 | self.warehouse_3), + ) + + @users( + "stock_user_c1_wh12", + ) + @allowed_companies() + def test_read_stock_quant_package_wh12(self): + self.assertEqual( + self.env["stock.quant.package"] + .search([("location_id.warehouse_id", "!=", self.warehouse_0.id)]) + .mapped("location_id.warehouse_id"), + (self.warehouse_1 | self.warehouse_2), + ) diff --git a/stock_warehouse_security/views/res_users.xml b/stock_warehouse_security/views/res_users.xml new file mode 100644 index 000000000000..2e19714dd24e --- /dev/null +++ b/stock_warehouse_security/views/res_users.xml @@ -0,0 +1,19 @@ + + + + res.users + + + + + + + + + + +