From 42a29ee499d488f3d8dea57addfb13e3ba5f2dd8 Mon Sep 17 00:00:00 2001 From: David Date: Mon, 8 Jul 2024 16:58:02 +0200 Subject: [PATCH] [ADD] website_conditional_visibility_user_group: New module TT50008 --- .../website_conditional_visibility_user_group | 1 + .../setup.py | 6 + .../README.rst | 94 ++++ .../__init__.py | 1 + .../__manifest__.py | 28 ++ .../i18n/es.po | 43 ++ ...site_conditional_visibility_user_group.pot | 41 ++ .../models/__init__.py | 1 + .../models/ir_http.py | 20 + .../readme/CONFIGURE.md | 9 + .../readme/CONTRIBUTORS.md | 2 + .../readme/DESCRIPTION.md | 2 + .../static/description/index.html | 441 ++++++++++++++++++ .../static/src/js/inject_dom.esm.js | 9 + .../snippet_visibility_portal_tests.esm.js | 28 ++ .../src/tests/snippet_visibility_tests.esm.js | 67 +++ .../tests/__init__.py | 1 + .../test_conditional_visibility_user_group.py | 19 + .../views/snippet_options_template.xml | 48 ++ 19 files changed, 861 insertions(+) create mode 120000 setup/website_conditional_visibility_user_group/odoo/addons/website_conditional_visibility_user_group create mode 100644 setup/website_conditional_visibility_user_group/setup.py create mode 100644 website_conditional_visibility_user_group/README.rst create mode 100644 website_conditional_visibility_user_group/__init__.py create mode 100644 website_conditional_visibility_user_group/__manifest__.py create mode 100644 website_conditional_visibility_user_group/i18n/es.po create mode 100644 website_conditional_visibility_user_group/i18n/website_conditional_visibility_user_group.pot create mode 100644 website_conditional_visibility_user_group/models/__init__.py create mode 100644 website_conditional_visibility_user_group/models/ir_http.py create mode 100644 website_conditional_visibility_user_group/readme/CONFIGURE.md create mode 100644 website_conditional_visibility_user_group/readme/CONTRIBUTORS.md create mode 100644 website_conditional_visibility_user_group/readme/DESCRIPTION.md create mode 100644 website_conditional_visibility_user_group/static/description/index.html create mode 100644 website_conditional_visibility_user_group/static/src/js/inject_dom.esm.js create mode 100644 website_conditional_visibility_user_group/static/src/tests/snippet_visibility_portal_tests.esm.js create mode 100644 website_conditional_visibility_user_group/static/src/tests/snippet_visibility_tests.esm.js create mode 100644 website_conditional_visibility_user_group/tests/__init__.py create mode 100644 website_conditional_visibility_user_group/tests/test_conditional_visibility_user_group.py create mode 100644 website_conditional_visibility_user_group/views/snippet_options_template.xml diff --git a/setup/website_conditional_visibility_user_group/odoo/addons/website_conditional_visibility_user_group b/setup/website_conditional_visibility_user_group/odoo/addons/website_conditional_visibility_user_group new file mode 120000 index 0000000000..71de1c5d0c --- /dev/null +++ b/setup/website_conditional_visibility_user_group/odoo/addons/website_conditional_visibility_user_group @@ -0,0 +1 @@ +../../../../website_conditional_visibility_user_group \ No newline at end of file diff --git a/setup/website_conditional_visibility_user_group/setup.py b/setup/website_conditional_visibility_user_group/setup.py new file mode 100644 index 0000000000..28c57bb640 --- /dev/null +++ b/setup/website_conditional_visibility_user_group/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/website_conditional_visibility_user_group/README.rst b/website_conditional_visibility_user_group/README.rst new file mode 100644 index 0000000000..db42063980 --- /dev/null +++ b/website_conditional_visibility_user_group/README.rst @@ -0,0 +1,94 @@ +==================================================== +Conditional visibility for internal users in Website +==================================================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:748c0a8174d9f9a0b4aee5b58aeaced2309a29997f74d53c86a129c7b0b021a0 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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-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%2Fwebsite-lightgray.png?logo=github + :target: https://github.com/OCA/website/tree/15.0/website_conditional_visibility_user_group + :alt: OCA/website +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/website-15-0/website-15-0-website_conditional_visibility_user_group + :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/website&target_branch=15.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module adds extra granularity in the visibility of the website +snippets allowing to set if the users that can see a block are internal +or portal ones. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +To configure the user group visibility: + +- With the frontend editor opened select the block you want to set the + visibility for. +- In *Style* tab of the editor panel set the *Visibility* option to + *Conditional* and unfold it. +- Set the *Users* option to *Visible for logged in*. +- Now you'll see the new *Groups* option. +- You can choose between *Only internal users*, *Only portal users* or + *No restriction*, which is the default value. + +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 +------- + +* Tecnativa + +Contributors +------------ + +- `Tecnativa `__ + + - David Vidal + +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/website `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/website_conditional_visibility_user_group/__init__.py b/website_conditional_visibility_user_group/__init__.py new file mode 100644 index 0000000000..0650744f6b --- /dev/null +++ b/website_conditional_visibility_user_group/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/website_conditional_visibility_user_group/__manifest__.py b/website_conditional_visibility_user_group/__manifest__.py new file mode 100644 index 0000000000..d7fe90cb76 --- /dev/null +++ b/website_conditional_visibility_user_group/__manifest__.py @@ -0,0 +1,28 @@ +# Copyright 2024 Tecnativa - David Vidal +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Conditional visibility for internal users in Website", + "summary": "Only internal users will see the blocks you add this condition to", + "version": "15.0.1.0.0", + "category": "Product", + "website": "https://github.com/OCA/website", + "author": "Tecnativa, Odoo Community Association (OCA)", + "license": "AGPL-3", + "depends": ["website"], + "data": ["views/snippet_options_template.xml"], + "assets": { + "web.assets_frontend_minimal": [ + "website_conditional_visibility_user_group/static/src/js/*.js", + ], + "web.assets_frontend_lazy": [ + # Remove assets_frontend_minimal + ( + "remove", + "website_conditional_visibility_user_group/static/src/js/*.js", + ), + ], + "web.assets_tests": [ + "website_conditional_visibility_user_group/static/src/tests/*.js" + ], + }, +} diff --git a/website_conditional_visibility_user_group/i18n/es.po b/website_conditional_visibility_user_group/i18n/es.po new file mode 100644 index 0000000000..b0c0c3eeeb --- /dev/null +++ b/website_conditional_visibility_user_group/i18n/es.po @@ -0,0 +1,43 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * website_conditional_visibility_user_group +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 15.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2024-07-12 05:20+0000\n" +"PO-Revision-Date: 2024-07-12 07:21+0200\n" +"Last-Translator: \n" +"Language-Team: \n" +"Language: es\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" +"X-Generator: Poedit 3.4.4\n" + +#. module: website_conditional_visibility_user_group +#: model:ir.model,name:website_conditional_visibility_user_group.model_ir_http +msgid "HTTP Routing" +msgstr "Ruta HTTP" + +#. module: website_conditional_visibility_user_group +#: model_terms:ir.ui.view,arch_db:website_conditional_visibility_user_group.snippet_options +msgid "No restriction" +msgstr "Sin restricciones" + +#. module: website_conditional_visibility_user_group +#: model_terms:ir.ui.view,arch_db:website_conditional_visibility_user_group.snippet_options +msgid "Only internal users" +msgstr "Solo usuarios internos" + +#. module: website_conditional_visibility_user_group +#: model_terms:ir.ui.view,arch_db:website_conditional_visibility_user_group.snippet_options +msgid "Only portal users" +msgstr "Solo usuarios de portal" + +#. module: website_conditional_visibility_user_group +#: model_terms:ir.ui.view,arch_db:website_conditional_visibility_user_group.snippet_options +msgid "⌙ Groups" +msgstr "⌙ Grupos" diff --git a/website_conditional_visibility_user_group/i18n/website_conditional_visibility_user_group.pot b/website_conditional_visibility_user_group/i18n/website_conditional_visibility_user_group.pot new file mode 100644 index 0000000000..eb2f3ac984 --- /dev/null +++ b/website_conditional_visibility_user_group/i18n/website_conditional_visibility_user_group.pot @@ -0,0 +1,41 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * website_conditional_visibility_user_group +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 15.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2024-07-12 05:19+0000\n" +"PO-Revision-Date: 2024-07-12 05:19+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: website_conditional_visibility_user_group +#: model:ir.model,name:website_conditional_visibility_user_group.model_ir_http +msgid "HTTP Routing" +msgstr "" + +#. module: website_conditional_visibility_user_group +#: model_terms:ir.ui.view,arch_db:website_conditional_visibility_user_group.snippet_options +msgid "No restriction" +msgstr "" + +#. module: website_conditional_visibility_user_group +#: model_terms:ir.ui.view,arch_db:website_conditional_visibility_user_group.snippet_options +msgid "Only internal users" +msgstr "" + +#. module: website_conditional_visibility_user_group +#: model_terms:ir.ui.view,arch_db:website_conditional_visibility_user_group.snippet_options +msgid "Only portal users" +msgstr "" + +#. module: website_conditional_visibility_user_group +#: model_terms:ir.ui.view,arch_db:website_conditional_visibility_user_group.snippet_options +msgid "⌙ Groups" +msgstr "" diff --git a/website_conditional_visibility_user_group/models/__init__.py b/website_conditional_visibility_user_group/models/__init__.py new file mode 100644 index 0000000000..9a5eb71871 --- /dev/null +++ b/website_conditional_visibility_user_group/models/__init__.py @@ -0,0 +1 @@ +from . import ir_http diff --git a/website_conditional_visibility_user_group/models/ir_http.py b/website_conditional_visibility_user_group/models/ir_http.py new file mode 100644 index 0000000000..5bbd10afbc --- /dev/null +++ b/website_conditional_visibility_user_group/models/ir_http.py @@ -0,0 +1,20 @@ +# Copyright 2024 Tecnativa - David Vidal +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo import api, models +from odoo.http import request + + +class IrHttp(models.AbstractModel): + _inherit = "ir.http" + + @api.model + def get_frontend_session_info(self): + # Used for the visibility of the website snippets + session_info = super().get_frontend_session_info() + if request.env.user.has_group("base.group_user"): + session_info["user_group"] = "internal" + elif request.env.user.has_group("base.group_portal"): + session_info["user_group"] = "portal" + else: + session_info["user_group"] = "public" + return session_info diff --git a/website_conditional_visibility_user_group/readme/CONFIGURE.md b/website_conditional_visibility_user_group/readme/CONFIGURE.md new file mode 100644 index 0000000000..b518af04f9 --- /dev/null +++ b/website_conditional_visibility_user_group/readme/CONFIGURE.md @@ -0,0 +1,9 @@ +To configure the user group visibility: + +- With the frontend editor opened select the block you want to set the visibility for. +- In *Style* tab of the editor panel set the *Visibility* option to *Conditional* and + unfold it. +- Set the *Users* option to *Visible for logged in*. +- Now you'll see the new *Groups* option. +- You can choose between *Only internal users*, *Only portal users* or *No restriction*, + which is the default value. diff --git a/website_conditional_visibility_user_group/readme/CONTRIBUTORS.md b/website_conditional_visibility_user_group/readme/CONTRIBUTORS.md new file mode 100644 index 0000000000..96d0c4c7ff --- /dev/null +++ b/website_conditional_visibility_user_group/readme/CONTRIBUTORS.md @@ -0,0 +1,2 @@ +- [Tecnativa](https://tecnativa.com) + - David Vidal diff --git a/website_conditional_visibility_user_group/readme/DESCRIPTION.md b/website_conditional_visibility_user_group/readme/DESCRIPTION.md new file mode 100644 index 0000000000..d97bd2b265 --- /dev/null +++ b/website_conditional_visibility_user_group/readme/DESCRIPTION.md @@ -0,0 +1,2 @@ +This module adds extra granularity in the visibility of the website snippets allowing +to set if the users that can see a block are internal or portal ones. diff --git a/website_conditional_visibility_user_group/static/description/index.html b/website_conditional_visibility_user_group/static/description/index.html new file mode 100644 index 0000000000..b8faeddd20 --- /dev/null +++ b/website_conditional_visibility_user_group/static/description/index.html @@ -0,0 +1,441 @@ + + + + + + +Conditional visibility for internal users in Website + + + +
+

Conditional visibility for internal users in Website

+ + +

Beta License: AGPL-3 OCA/website Translate me on Weblate Try me on Runboat

+

This module adds extra granularity in the visibility of the website +snippets allowing to set if the users that can see a block are internal +or portal ones.

+

Table of contents

+ +
+

Configuration

+

To configure the user group visibility:

+
    +
  • With the frontend editor opened select the block you want to set the +visibility for.
  • +
  • In Style tab of the editor panel set the Visibility option to +Conditional and unfold it.
  • +
  • Set the Users option to Visible for logged in.
  • +
  • Now you’ll see the new Groups option.
  • +
  • You can choose between Only internal users, Only portal users or +No restriction, which is the default value.
  • +
+
+
+

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

+
    +
  • Tecnativa
  • +
+
+
+

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.

+

This module is part of the OCA/website project on GitHub.

+

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

+
+
+
+ + diff --git a/website_conditional_visibility_user_group/static/src/js/inject_dom.esm.js b/website_conditional_visibility_user_group/static/src/js/inject_dom.esm.js new file mode 100644 index 0000000000..0190631516 --- /dev/null +++ b/website_conditional_visibility_user_group/static/src/js/inject_dom.esm.js @@ -0,0 +1,9 @@ +/** @odoo-module */ +/* Copyright 2024 Tecnativa - David Vidal + * License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */ +import {session} from "@web/session"; + +document.addEventListener("DOMContentLoaded", () => { + const htmlEl = document.documentElement; + htmlEl.dataset.user_group = session.user_group; +}); diff --git a/website_conditional_visibility_user_group/static/src/tests/snippet_visibility_portal_tests.esm.js b/website_conditional_visibility_user_group/static/src/tests/snippet_visibility_portal_tests.esm.js new file mode 100644 index 0000000000..867ea65984 --- /dev/null +++ b/website_conditional_visibility_user_group/static/src/tests/snippet_visibility_portal_tests.esm.js @@ -0,0 +1,28 @@ +/** @odoo-module */ +/* Copyright 2024 Tecnativa - David Vidal + * License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */ +import tour from "web_tour.tour"; + +tour.register( + "conditional_visibility_portal", + { + test: true, + url: "/", + }, + [ + { + content: "The content should be hidden for portal users", + trigger: "#wrap", + run: function () { + const style = window.getComputedStyle( + this.$anchor[0].getElementsByClassName("s_text_image")[0] + ); + if (style.display !== "none") { + console.error( + "Error: This item should be invisible for portal users" + ); + } + }, + }, + ] +); diff --git a/website_conditional_visibility_user_group/static/src/tests/snippet_visibility_tests.esm.js b/website_conditional_visibility_user_group/static/src/tests/snippet_visibility_tests.esm.js new file mode 100644 index 0000000000..a192b485bd --- /dev/null +++ b/website_conditional_visibility_user_group/static/src/tests/snippet_visibility_tests.esm.js @@ -0,0 +1,67 @@ +/** @odoo-module */ +/* Copyright 2024 Tecnativa - David Vidal + * License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */ +import tour from "web_tour.tour"; +import wTourUtils from "website.tour_utils"; + +const snippet = { + id: "s_text_image", + name: "Text - Image", +}; + +tour.register( + "conditional_visibility_only_internal_user", + { + test: true, + url: "/", + }, + [ + { + content: "enter edit mode", + trigger: "a[data-action=edit]", + }, + wTourUtils.dragNDrop(snippet), + wTourUtils.clickOnSnippet(snippet), + wTourUtils.changeOption("ConditionalVisibility", "we-toggler"), + { + content: "Set on conditional visibility", + trigger: '[data-name="visibility_conditional"]', + run: "click", + }, + { + content: "Click in User visibility", + trigger: '[data-save-attribute="visibilityValueLogged"]', + run: "click", + }, + { + content: "Set visibility to logged in users", + trigger: '[data-name="visibility_logged_in"]', + run: "click", + }, + { + content: "Click in group visibility", + trigger: '[data-save-attribute="visibilityUserGroup"]', + run: "click", + }, + { + content: "Set visibility to logged internal users only", + trigger: '[data-name="user_group_internal"]', + run: "click", + }, + ...wTourUtils.clickOnSave(), + { + content: "Check if the rule was applied", + trigger: "body:not(.editor_enable) #wrap", + run: function () { + const style = window.getComputedStyle( + this.$anchor[0].getElementsByClassName("s_text_image")[0] + ); + if (style.display === "none") { + console.error( + "Error: this item should be visible for internal users" + ); + } + }, + }, + ] +); diff --git a/website_conditional_visibility_user_group/tests/__init__.py b/website_conditional_visibility_user_group/tests/__init__.py new file mode 100644 index 0000000000..d1c4bc70cb --- /dev/null +++ b/website_conditional_visibility_user_group/tests/__init__.py @@ -0,0 +1 @@ +from . import test_conditional_visibility_user_group diff --git a/website_conditional_visibility_user_group/tests/test_conditional_visibility_user_group.py b/website_conditional_visibility_user_group/tests/test_conditional_visibility_user_group.py new file mode 100644 index 0000000000..d21c0546d3 --- /dev/null +++ b/website_conditional_visibility_user_group/tests/test_conditional_visibility_user_group.py @@ -0,0 +1,19 @@ +# Copyright 2024 Tecnativa - David Vidal +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo.tests import HttpCase, new_test_user, tagged + + +@tagged("-at_install", "post_install") +class TestConditionalVisibilityUserGroup(HttpCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + new_test_user(cls.env, login="portal_user", groups="base.group_portal") + + def test_snippet_for_internal_users(self): + """Only internal users can see the snippet""" + # We drag a new block, set its visibility to internal users and check that + # we can see it as internal users + self.start_tour("/", "conditional_visibility_only_internal_user", login="admin") + # The block is hidden for portal users + self.start_tour("/", "conditional_visibility_portal", login="portal_user") diff --git a/website_conditional_visibility_user_group/views/snippet_options_template.xml b/website_conditional_visibility_user_group/views/snippet_options_template.xml new file mode 100644 index 0000000000..9b387a8265 --- /dev/null +++ b/website_conditional_visibility_user_group/views/snippet_options_template.xml @@ -0,0 +1,48 @@ + + + + +